Skip to content

Commit

Permalink
[Plugin API] - Simplify plugin transaction selector interface to retu…
Browse files Browse the repository at this point in the history
…rn object instead of list (hyperledger#5995)

Signed-off-by: Gabriel-Trintinalia <gabriel.trintinalia@consensys.net>
  • Loading branch information
Gabriel-Trintinalia committed Oct 20, 2023
1 parent 6717b57 commit 64f9dc4
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.ethereum.GasLimitCalculator;
import org.hyperledger.besu.ethereum.blockcreation.txselection.selectors.AbstractTransactionSelector;
import org.hyperledger.besu.ethereum.blockcreation.txselection.selectors.AllAcceptingTransactionSelector;
import org.hyperledger.besu.ethereum.blockcreation.txselection.selectors.BlobPriceTransactionSelector;
import org.hyperledger.besu.ethereum.blockcreation.txselection.selectors.BlockSizeTransactionSelector;
import org.hyperledger.besu.ethereum.blockcreation.txselection.selectors.PriceTransactionSelector;
Expand Down Expand Up @@ -73,6 +74,7 @@
*/
public class BlockTransactionSelector {
private static final Logger LOG = LoggerFactory.getLogger(BlockTransactionSelector.class);

private final Supplier<Boolean> isCancelled;
private final MainnetTransactionProcessor transactionProcessor;
private final Blockchain blockchain;
Expand All @@ -82,7 +84,7 @@ public class BlockTransactionSelector {
private final TransactionSelectionResults transactionSelectionResults =
new TransactionSelectionResults();
private final List<AbstractTransactionSelector> transactionSelectors;
private final List<TransactionSelector> externalTransactionSelectors;
private final TransactionSelector externalTransactionSelector;

public BlockTransactionSelector(
final MainnetTransactionProcessor transactionProcessor,
Expand Down Expand Up @@ -117,8 +119,10 @@ public BlockTransactionSelector(
miningBeneficiary,
transactionPool);
transactionSelectors = createTransactionSelectors(blockSelectionContext);
externalTransactionSelectors =
transactionSelectorFactory.map(TransactionSelectorFactory::create).orElse(List.of());
externalTransactionSelector =
transactionSelectorFactory
.map(TransactionSelectorFactory::create)
.orElse(AllAcceptingTransactionSelector.INSTANCE);
}

/**
Expand Down Expand Up @@ -263,18 +267,7 @@ private TransactionSelectionResult evaluateTransactionPreProcessing(
return result;
}
}

// Process the transaction through external selectors
for (var selector : externalTransactionSelectors) {
TransactionSelectionResult result =
selector.evaluateTransactionPreProcessing(pendingTransaction);
// If the transaction is not selected by any external selector, return the result
if (!result.equals(TransactionSelectionResult.SELECTED)) {
return result;
}
}
// If the transaction is selected by all selectors, return SELECTED
return TransactionSelectionResult.SELECTED;
return externalTransactionSelector.evaluateTransactionPreProcessing(pendingTransaction);
}

/**
Expand All @@ -301,19 +294,8 @@ private TransactionSelectionResult evaluateTransactionPostProcessing(
return result;
}
}

// Process the transaction through external selectors
for (var selector : externalTransactionSelectors) {
TransactionSelectionResult result =
selector.evaluateTransactionPostProcessing(pendingTransaction, processingResult);
// If the transaction is not selected by any external selector, return the result
if (!result.equals(TransactionSelectionResult.SELECTED)) {
return result;
}
}

// If the transaction is selected by all selectors, return SELECTED
return TransactionSelectionResult.SELECTED;
return externalTransactionSelector.evaluateTransactionPostProcessing(
pendingTransaction, processingResult);
}

private List<AbstractTransactionSelector> createTransactionSelectors(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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.PendingTransaction;
import org.hyperledger.besu.plugin.data.TransactionProcessingResult;
import org.hyperledger.besu.plugin.data.TransactionSelectionResult;
import org.hyperledger.besu.plugin.services.txselection.TransactionSelector;

/** A TransactionSelector that unconditionally selects all transactions. */
public class AllAcceptingTransactionSelector implements TransactionSelector {
public static final AllAcceptingTransactionSelector INSTANCE =
new AllAcceptingTransactionSelector();

private AllAcceptingTransactionSelector() {}

/**
* Always selects the transaction in the pre-processing stage.
*
* @param pendingTransaction The transaction to be evaluated.
* @return Always SELECTED.
*/
@Override
public TransactionSelectionResult evaluateTransactionPreProcessing(
final PendingTransaction pendingTransaction) {
return TransactionSelectionResult.SELECTED;
}

/**
* Always selects the transaction in the post-processing stage.
*
* @param pendingTransaction The transaction to be evaluated.
* @param processingResult The result of the transaction processing.
* @return Always SELECTED.
*/
@Override
public TransactionSelectionResult evaluateTransactionPostProcessing(
final PendingTransaction pendingTransaction,
final TransactionProcessingResult processingResult) {
return TransactionSelectionResult.SELECTED;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -554,26 +554,25 @@ public void transactionSelectionPluginShouldWork_PreProcessing() {

final TransactionSelectorFactory transactionSelectorFactory =
() ->
List.of(
new TransactionSelector() {
@Override
public TransactionSelectionResult evaluateTransactionPreProcessing(
final PendingTransaction pendingTransaction) {
if (pendingTransaction.getTransaction().equals(notSelectedTransient))
return TransactionSelectionResult.invalidTransient("transient");
if (pendingTransaction.getTransaction().equals(notSelectedInvalid))
return TransactionSelectionResult.invalid("invalid");
return TransactionSelectionResult.SELECTED;
}

@Override
public TransactionSelectionResult evaluateTransactionPostProcessing(
final PendingTransaction pendingTransaction,
final org.hyperledger.besu.plugin.data.TransactionProcessingResult
processingResult) {
return TransactionSelectionResult.SELECTED;
}
});
new TransactionSelector() {
@Override
public TransactionSelectionResult evaluateTransactionPreProcessing(
final PendingTransaction pendingTransaction) {
if (pendingTransaction.getTransaction().equals(notSelectedTransient))
return TransactionSelectionResult.invalidTransient("transient");
if (pendingTransaction.getTransaction().equals(notSelectedInvalid))
return TransactionSelectionResult.invalid("invalid");
return TransactionSelectionResult.SELECTED;
}

@Override
public TransactionSelectionResult evaluateTransactionPostProcessing(
final PendingTransaction pendingTransaction,
final org.hyperledger.besu.plugin.data.TransactionProcessingResult
processingResult) {
return TransactionSelectionResult.SELECTED;
}
};

final Address miningBeneficiary = AddressHelpers.ofValue(1);
final BlockTransactionSelector selector =
Expand Down Expand Up @@ -619,27 +618,25 @@ public void transactionSelectionPluginShouldWork_PostProcessing() {

final TransactionSelectorFactory transactionSelectorFactory =
() ->
List.of(
new TransactionSelector() {
@Override
public TransactionSelectionResult evaluateTransactionPreProcessing(
final PendingTransaction pendingTransaction) {
return TransactionSelectionResult.SELECTED;
}

@Override
public TransactionSelectionResult evaluateTransactionPostProcessing(
final PendingTransaction pendingTransaction,
final org.hyperledger.besu.plugin.data.TransactionProcessingResult
processingResult) {
// the transaction with max gas +1 should fail
if (processingResult.getEstimateGasUsedByTransaction()
> maxGasUsedByTransaction) {
return TransactionSelectionResult.invalidTransient("Invalid");
}
return TransactionSelectionResult.SELECTED;
}
});
new TransactionSelector() {
@Override
public TransactionSelectionResult evaluateTransactionPreProcessing(
final PendingTransaction pendingTransaction) {
return TransactionSelectionResult.SELECTED;
}

@Override
public TransactionSelectionResult evaluateTransactionPostProcessing(
final PendingTransaction pendingTransaction,
final org.hyperledger.besu.plugin.data.TransactionProcessingResult
processingResult) {
// the transaction with max gas +1 should fail
if (processingResult.getEstimateGasUsedByTransaction() > maxGasUsedByTransaction) {
return TransactionSelectionResult.invalidTransient("Invalid");
}
return TransactionSelectionResult.SELECTED;
}
};

final Address miningBeneficiary = AddressHelpers.ofValue(1);
final BlockTransactionSelector selector =
Expand Down
2 changes: 1 addition & 1 deletion plugin-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Calculated : ${currentHash}
tasks.register('checkAPIChanges', FileStateChecker) {
description = "Checks that the API for the Plugin-API project does not change without deliberate thought"
files = sourceSets.main.allJava.files
knownHash = 'IPqcFdM1uy+ZDbcvzsKxMIrzhP9VoaSeanhBOLtbhfE='
knownHash = '8NVdDoCnMQiibEZUsZuemZU+wm3W1LPklcQkiKsriKs='
}
check.dependsOn('checkAPIChanges')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@

import org.hyperledger.besu.plugin.Unstable;

import java.util.List;

/** Interface for a factory that creates transaction selectors */
@Unstable
public interface TransactionSelectorFactory {

/**
* Create a list of transaction selectors
* Create a transaction selector
*
* @return the transaction selector list
* @return the transaction selector
*/
List<TransactionSelector> create();
TransactionSelector create();
}

0 comments on commit 64f9dc4

Please sign in to comment.