Skip to content

Commit

Permalink
Add a tx selector to skip txs from the same sender after the first no…
Browse files Browse the repository at this point in the history
…t selected

Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
  • Loading branch information
fab-10 committed Feb 3, 2025
1 parent a94f376 commit 820cc51
Show file tree
Hide file tree
Showing 10 changed files with 327 additions and 158 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
### Breaking Changes
### Upcoming Breaking Changes
### Additions and Improvements
- Add a tx selector to skip txs from the same sender after the first not selected [#8216](https://github.com/hyperledger/besu/pull/8216)

#### Prague
- Add timestamps to enable Prague hardfork on Sepolia and Holesky test networks [#8163](https://github.com/hyperledger/besu/pull/8163)
- Update system call addresses to match [devnet-6](https://github.com/ethereum/execution-spec-tests/releases/) values [#8209](https://github.com/hyperledger/besu/issues/8209)

#### Plugins
- Extend simulate transaction on pending block plugin API [#8174](https://github.com/hyperledger/besu/pull/8174)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.hyperledger.besu.ethereum.blockcreation.txselection.selectors.MinPriorityFeePerGasTransactionSelector;
import org.hyperledger.besu.ethereum.blockcreation.txselection.selectors.PriceTransactionSelector;
import org.hyperledger.besu.ethereum.blockcreation.txselection.selectors.ProcessingResultTransactionSelector;
import org.hyperledger.besu.ethereum.blockcreation.txselection.selectors.SkipSenderTransactionSelector;
import org.hyperledger.besu.ethereum.chain.Blockchain;
import org.hyperledger.besu.ethereum.core.MiningConfiguration;
import org.hyperledger.besu.ethereum.core.MutableWorldState;
Expand Down Expand Up @@ -151,6 +152,7 @@ public BlockTransactionSelector(
private List<AbstractTransactionSelector> createTransactionSelectors(
final BlockSelectionContext context) {
return List.of(
new SkipSenderTransactionSelector(context),
new BlockSizeTransactionSelector(context),
new BlobSizeTransactionSelector(context),
new PriceTransactionSelector(context),
Expand Down Expand Up @@ -442,7 +444,8 @@ private TransactionSelectionResult handleTransactionSelected(
evaluationContext, BLOCK_SELECTION_TIMEOUT, txWorldStateUpdater);
}

pluginTransactionSelector.onTransactionSelected(evaluationContext, processingResult);
notifySelected(evaluationContext, processingResult);

blockWorldStateUpdater = worldState.updater();
LOG.atTrace()
.setMessage("Selected {} for block creation, evaluated in {}")
Expand Down Expand Up @@ -475,7 +478,7 @@ private TransactionSelectionResult handleTransactionNotSelected(
: selectionResult;

transactionSelectionResults.updateNotSelected(evaluationContext.getTransaction(), actualResult);
pluginTransactionSelector.onTransactionNotSelected(evaluationContext, actualResult);
notifyNotSelected(evaluationContext, actualResult);
LOG.atTrace()
.setMessage(
"Not selected {} for block creation with result {} (original result {}), evaluated in {}")
Expand Down Expand Up @@ -550,6 +553,26 @@ private TransactionSelectionResult handleTransactionNotSelected(
return handleTransactionNotSelected(evaluationContext, selectionResult);
}

private void notifySelected(
final TransactionEvaluationContext evaluationContext,
final TransactionProcessingResult processingResult) {

for (var selector : transactionSelectors) {
selector.onTransactionSelected(evaluationContext, processingResult);
}
pluginTransactionSelector.onTransactionSelected(evaluationContext, processingResult);
}

private void notifyNotSelected(
final TransactionEvaluationContext evaluationContext,
final TransactionSelectionResult selectionResult) {

for (var selector : transactionSelectors) {
selector.onTransactionNotSelected(evaluationContext, selectionResult);
}
pluginTransactionSelector.onTransactionNotSelected(evaluationContext, selectionResult);
}

private void checkCancellation() {
if (isCancelled.get()) {
throw new CancellationException("Cancelled during transaction selection.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* transactions.
*/
public abstract class AbstractTransactionSelector {
final BlockSelectionContext context;
protected final BlockSelectionContext context;

public AbstractTransactionSelector(final BlockSelectionContext context) {
this.context = context;
Expand Down Expand Up @@ -55,4 +55,24 @@ public abstract TransactionSelectionResult evaluateTransactionPostProcessing(
final TransactionEvaluationContext evaluationContext,
final TransactionSelectionResults blockTransactionResults,
final TransactionProcessingResult processingResult);

/**
* Method called when a transaction is selected to be added to a block.
*
* @param evaluationContext The current selection context
* @param processingResult The result of processing the selected transaction.
*/
public void onTransactionSelected(
final TransactionEvaluationContext evaluationContext,
final TransactionProcessingResult processingResult) {}

/**
* Method called when a transaction is not selected to be added to a block.
*
* @param evaluationContext The current selection context
* @param transactionSelectionResult The transaction selection result
*/
public void onTransactionNotSelected(
final TransactionEvaluationContext evaluationContext,
final TransactionSelectionResult transactionSelectionResult) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright contributors to Besu.
*
* 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.Address;
import org.hyperledger.besu.ethereum.blockcreation.txselection.BlockSelectionContext;
import org.hyperledger.besu.ethereum.blockcreation.txselection.TransactionEvaluationContext;
import org.hyperledger.besu.ethereum.blockcreation.txselection.TransactionSelectionResults;
import org.hyperledger.besu.ethereum.processing.TransactionProcessingResult;
import org.hyperledger.besu.plugin.data.TransactionSelectionResult;

import java.util.HashSet;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SkipSenderTransactionSelector extends AbstractTransactionSelector {
private static final Logger LOG = LoggerFactory.getLogger(SkipSenderTransactionSelector.class);
private final Set<Address> skippedSenders = new HashSet<>();

public SkipSenderTransactionSelector(final BlockSelectionContext context) {
super(context);
}

@Override
public TransactionSelectionResult evaluateTransactionPreProcessing(
final TransactionEvaluationContext evaluationContext,
final TransactionSelectionResults ignored) {
final var sender = evaluationContext.getTransaction().getSender();
if (skippedSenders.contains(sender)) {
LOG.atTrace()
.setMessage("Not selecting tx {} since its sender {} is in the skip list")
.addArgument(() -> evaluationContext.getPendingTransaction().toTraceLog())
.addArgument(sender)
.log();

return TransactionSelectionResult.SENDER_WITH_PREVIOUS_TX_NOT_SELECTED;
}
return TransactionSelectionResult.SELECTED;
}

@Override
public TransactionSelectionResult evaluateTransactionPostProcessing(
final TransactionEvaluationContext evaluationContext,
final TransactionSelectionResults blockTransactionResults,
final TransactionProcessingResult processingResult) {
// All necessary checks were done in the pre-processing method, so nothing to do here.
return TransactionSelectionResult.SELECTED;
}

/**
* When a transaction is not selected we can skip processing any following transaction from the
* same sender, since it will never be selected due to the nonce gap, so we add the sender to the
* skip list.
*
* @param evaluationContext The current selection context
* @param transactionSelectionResult The transaction selection result
*/
@Override
public void onTransactionNotSelected(
final TransactionEvaluationContext evaluationContext,
final TransactionSelectionResult transactionSelectionResult) {
final var sender = evaluationContext.getTransaction().getSender();
skippedSenders.add(sender);
LOG.trace("Sender {} added to the skip list", sender);
}
}
Loading

0 comments on commit 820cc51

Please sign in to comment.