-
Notifications
You must be signed in to change notification settings - Fork 847
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add transaction selector based on min priority fee parameter #6083
Merged
Gabriel-Trintinalia
merged 11 commits into
hyperledger:main
from
Gabriel-Trintinalia:tx-selector-min-priority-fee
Nov 2, 2023
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8e5de8d
Add transaction selector based on min priority fee parameter
Gabriel-Trintinalia 50aa983
Change plugin known hash
Gabriel-Trintinalia 76481a6
Merge branch 'main' into tx-selector-min-priority-fee
Gabriel-Trintinalia 54fc006
update changelog
Gabriel-Trintinalia 3c29d8a
Add test
Gabriel-Trintinalia df3155e
Merge main into branch
Gabriel-Trintinalia f5f5d0a
Validate priority senders
Gabriel-Trintinalia 608d9e7
Add unit tests
Gabriel-Trintinalia c262403
Fix known hash
Gabriel-Trintinalia 7f0e8f9
Fix comment
Gabriel-Trintinalia 912e7cb
Rename variable and tests
Gabriel-Trintinalia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
...ethereum/blockcreation/txselection/selectors/MinPriorityFeePerGasTransactionSelector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* Copyright Hyperledger Besu Contributors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.hyperledger.besu.ethereum.blockcreation.txselection.selectors; | ||
|
||
import org.hyperledger.besu.datatypes.Wei; | ||
import org.hyperledger.besu.ethereum.blockcreation.txselection.BlockSelectionContext; | ||
import org.hyperledger.besu.ethereum.blockcreation.txselection.TransactionSelectionResults; | ||
import org.hyperledger.besu.ethereum.eth.transactions.PendingTransaction; | ||
import org.hyperledger.besu.ethereum.processing.TransactionProcessingResult; | ||
import org.hyperledger.besu.plugin.data.TransactionSelectionResult; | ||
|
||
/** This class is responsible for selecting transactions based on the minimum priority fee. */ | ||
public class MinPriorityFeePerGasTransactionSelector extends AbstractTransactionSelector { | ||
|
||
/** | ||
* Constructor for MinPriorityFeeSelector. | ||
* | ||
* @param context The context of block selection. | ||
*/ | ||
public MinPriorityFeePerGasTransactionSelector(final BlockSelectionContext context) { | ||
super(context); | ||
} | ||
|
||
/** | ||
* Evaluates a transaction before processing. | ||
* | ||
* @param pendingTransaction The transaction to be evaluated. | ||
* @param transactionSelectionResults The results of other transaction evaluations in the same | ||
* block. | ||
* @return TransactionSelectionResult. If the priority fee is below the minimum, it returns an | ||
* invalid transient result. Otherwise, it returns a selected result. | ||
*/ | ||
@Override | ||
public TransactionSelectionResult evaluateTransactionPreProcessing( | ||
final PendingTransaction pendingTransaction, | ||
final TransactionSelectionResults transactionSelectionResults) { | ||
if (isPriorityFeePriceBelowMinimum(pendingTransaction)) { | ||
return TransactionSelectionResult.PRIORITY_FEE_PER_GAS_BELOW_CURRENT_MIN; | ||
} | ||
return TransactionSelectionResult.SELECTED; | ||
} | ||
|
||
/** | ||
* Checks if the priority fee price is below the minimum. | ||
* | ||
* @param pendingTransaction The transaction to check. | ||
* @return boolean. Returns true if the minimum priority fee price is below the minimum, false | ||
* otherwise. | ||
*/ | ||
private boolean isPriorityFeePriceBelowMinimum(final PendingTransaction pendingTransaction) { | ||
// Priority txs are exempt from this check | ||
if (pendingTransaction.hasPriority()) { | ||
return false; | ||
} | ||
Wei priorityFeePerGas = | ||
pendingTransaction | ||
.getTransaction() | ||
.getEffectivePriorityFeePerGas(context.processableBlockHeader().getBaseFee()); | ||
return priorityFeePerGas.lessThan(context.miningParameters().getMinPriorityFeePerGas()); | ||
} | ||
|
||
/** | ||
* No evaluation is performed post-processing. | ||
* | ||
* @param pendingTransaction The processed transaction. | ||
* @param processingResult The result of the transaction processing. | ||
* @return Always returns SELECTED. | ||
*/ | ||
@Override | ||
public TransactionSelectionResult evaluateTransactionPostProcessing( | ||
final PendingTransaction pendingTransaction, | ||
final TransactionSelectionResults blockTransactionResults, | ||
final TransactionProcessingResult processingResult) { | ||
return TransactionSelectionResult.SELECTED; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
.../hyperledger/besu/ethereum/blockcreation/MinPriorityFeePerGasTransactionSelectorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* Copyright Hyperledger Besu Contributors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.hyperledger.besu.ethereum.blockcreation; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import org.hyperledger.besu.datatypes.Wei; | ||
import org.hyperledger.besu.ethereum.blockcreation.txselection.BlockSelectionContext; | ||
import org.hyperledger.besu.ethereum.blockcreation.txselection.selectors.AbstractTransactionSelector; | ||
import org.hyperledger.besu.ethereum.blockcreation.txselection.selectors.MinPriorityFeePerGasTransactionSelector; | ||
import org.hyperledger.besu.ethereum.core.MiningParameters; | ||
import org.hyperledger.besu.ethereum.core.ProcessableBlockHeader; | ||
import org.hyperledger.besu.ethereum.core.Transaction; | ||
import org.hyperledger.besu.ethereum.eth.transactions.PendingTransaction; | ||
import org.hyperledger.besu.plugin.data.TransactionSelectionResult; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class MinPriorityFeePerGasTransactionSelectorTest { | ||
private AbstractTransactionSelector transactionSelector; | ||
|
||
private final int minPriorityFeeParameter = 7; | ||
|
||
@BeforeEach | ||
public void initialize() { | ||
MiningParameters miningParameters = | ||
MiningParameters.newDefault().setMinPriorityFeePerGas(Wei.of(minPriorityFeeParameter)); | ||
BlockSelectionContext context = | ||
new BlockSelectionContext( | ||
miningParameters, | ||
null, | ||
null, | ||
mock(ProcessableBlockHeader.class), | ||
null, | ||
null, | ||
null, | ||
null); | ||
transactionSelector = new MinPriorityFeePerGasTransactionSelector(context); | ||
} | ||
|
||
@Test | ||
public void shouldNotSelectWhen_PriorityFeePerGas_IsLessThan_MinPriorityFeePerGas() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. helpful test names! |
||
var transaction = mockTransactionWithPriorityFee(minPriorityFeeParameter - 1); | ||
assertSelection(transaction, TransactionSelectionResult.PRIORITY_FEE_PER_GAS_BELOW_CURRENT_MIN); | ||
} | ||
|
||
@Test | ||
public void shouldSelectWhen_PriorityFeePerGas_IsEqual_MinPriorityFeePerGas() { | ||
var transaction = mockTransactionWithPriorityFee(minPriorityFeeParameter); | ||
assertSelection(transaction, TransactionSelectionResult.SELECTED); | ||
} | ||
|
||
@Test | ||
public void shouldSelectWhen_PriorityFeePerGas_IsGreaterThan_MinPriorityFeePerGas() { | ||
var transaction = mockTransactionWithPriorityFee(minPriorityFeeParameter + 1); | ||
assertSelection(transaction, TransactionSelectionResult.SELECTED); | ||
} | ||
|
||
@Test | ||
public void shouldSelectWhenPrioritySender() { | ||
var prioritySenderTransaction = mockTransactionWithPriorityFee(minPriorityFeeParameter - 1); | ||
assertSelection( | ||
prioritySenderTransaction, | ||
TransactionSelectionResult.PRIORITY_FEE_PER_GAS_BELOW_CURRENT_MIN); | ||
when(prioritySenderTransaction.hasPriority()).thenReturn(true); | ||
assertSelection(prioritySenderTransaction, TransactionSelectionResult.SELECTED); | ||
} | ||
|
||
private void assertSelection( | ||
Gabriel-Trintinalia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
final PendingTransaction transaction, final TransactionSelectionResult expectedResult) { | ||
var actualResult = transactionSelector.evaluateTransactionPreProcessing(transaction, null); | ||
assertThat(actualResult).isEqualTo(expectedResult); | ||
} | ||
|
||
private PendingTransaction mockTransactionWithPriorityFee(final int priorityFeePerGas) { | ||
PendingTransaction mockTransaction = mock(PendingTransaction.class); | ||
Transaction transaction = mock(Transaction.class); | ||
when(mockTransaction.getTransaction()).thenReturn(transaction); | ||
when(transaction.getEffectivePriorityFeePerGas(any())).thenReturn(Wei.of(priorityFeePerGas)); | ||
return mockTransaction; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
are these really related to mining?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
they are related to block creation. I think it is the same case of this: #6027 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the rename from miner to block creation is on the todo list, but it is not trivial and should be done with care, it will introduce a lot of breaking changes and need for external tools to update.
I suggest that for the moment we keep it as is, and we consider miner a synonym of block creation