Skip to content

Commit

Permalink
Refactor(transaction-selection): Implement TransactionSelector new me…
Browse files Browse the repository at this point in the history
…thods (#218)

Signed-off-by: Gabriel-Trintinalia <[email protected]>
  • Loading branch information
Gabriel-Trintinalia authored Oct 11, 2023
1 parent df4daf2 commit a01f302
Show file tree
Hide file tree
Showing 11 changed files with 537 additions and 86 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
version=0.0.1-SNAPSHOT
besuVersion=23.7.4-SNAPSHOT
besuVersion=23.10.1-SNAPSHOT
besuArtifactGroup=io.consensys.linea-besu
distributionIdentifier=linea-besu-plugin
distributionBaseUrl=https://artifacts.consensys.net/public/linea-besu/raw/names/linea-besu.tar.gz/versions/
18 changes: 10 additions & 8 deletions src/main/java/net/consensys/linea/sequencer/LineaCliOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class LineaCliOptions {
"Maximum size for the calldata of a Transaction (default: "
+ DEFAULT_MAX_TX_CALLDATA_SIZE
+ ")")
private int maxTxCalldataSize = DEFAULT_MAX_TX_CALLDATA_SIZE;
private int maxTxCallDataSize = DEFAULT_MAX_TX_CALLDATA_SIZE;

@CommandLine.Option(
names = {MAX_BLOCK_CALLDATA_SIZE},
Expand All @@ -44,7 +44,7 @@ public class LineaCliOptions {
"Maximum size for the calldata of a Block (default: "
+ DEFAULT_MAX_BLOCK_CALLDATA_SIZE
+ ")")
private int maxBlockCalldataSize = DEFAULT_MAX_BLOCK_CALLDATA_SIZE;
private int maxBlockCallDataSize = DEFAULT_MAX_BLOCK_CALLDATA_SIZE;

private LineaCliOptions() {}

Expand All @@ -65,9 +65,8 @@ public static LineaCliOptions create() {
*/
public static LineaCliOptions fromConfig(final LineaConfiguration config) {
final LineaCliOptions options = create();
options.maxTxCalldataSize = config.maxTxCalldataSize();
options.maxBlockCalldataSize = config.maxBlockCalldataSize();

options.maxTxCallDataSize = config.maxTxCallDataSize();
options.maxBlockCallDataSize = config.maxBlockCallDataSize();
return options;
}

Expand All @@ -77,14 +76,17 @@ public static LineaCliOptions fromConfig(final LineaConfiguration config) {
* @return the Linea factory configuration
*/
public LineaConfiguration toDomainObject() {
return new LineaConfiguration(maxTxCalldataSize, maxBlockCalldataSize);
return new LineaConfiguration.Builder()
.maxTxCallDataSize(maxTxCallDataSize)
.maxBlockCallDataSize(maxBlockCallDataSize)
.build();
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add(MAX_TX_CALLDATA_SIZE, maxTxCalldataSize)
.add(MAX_BLOCK_CALLDATA_SIZE, maxBlockCalldataSize)
.add(MAX_TX_CALLDATA_SIZE, maxTxCallDataSize)
.add(MAX_BLOCK_CALLDATA_SIZE, maxBlockCallDataSize)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,40 @@

package net.consensys.linea.sequencer;

/**
* The Linea configuration.
*
* @param maxTxCalldataSize the maximum calldata size for a transaction.
* @param maxBlockCalldataSize the maximum calldata size for a block.
*/
public record LineaConfiguration(int maxTxCalldataSize, int maxBlockCalldataSize) {}
/** The Linea configuration. */
public final class LineaConfiguration {
private final int maxTxCallDataSize;
private final int maxBlockCallDataSize;

private LineaConfiguration(int maxTxCallDataSize, int maxBlockCallDataSize) {
this.maxTxCallDataSize = maxTxCallDataSize;
this.maxBlockCallDataSize = maxBlockCallDataSize;
}

public int maxTxCallDataSize() {
return maxTxCallDataSize;
}

public int maxBlockCallDataSize() {
return maxBlockCallDataSize;
}

public static class Builder {
private int maxTxCallDataSize;
private int maxBlockCallDataSize;

public Builder maxTxCallDataSize(int maxTxCallDataSize) {
this.maxTxCallDataSize = maxTxCallDataSize;
return this;
}

public Builder maxBlockCallDataSize(int maxBlockCallDataSize) {
this.maxBlockCallDataSize = maxBlockCallDataSize;
return this;
}

public LineaConfiguration build() {
return new LineaConfiguration(maxTxCallDataSize, maxBlockCallDataSize);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
* SPDX-License-Identifier: Apache-2.0
*/

package net.consensys.linea.sequencer;
package net.consensys.linea.sequencer.txselection;

import net.consensys.linea.sequencer.LineaCliOptions;
import net.consensys.linea.sequencer.LineaConfiguration;
import net.consensys.linea.sequencer.txselection.selectors.LineaTransactionSelector;
import org.hyperledger.besu.plugin.services.txselection.TransactionSelector;
import org.hyperledger.besu.plugin.services.txselection.TransactionSelectorFactory;

Expand All @@ -29,7 +32,6 @@ public LineaTransactionSelectorFactory(final LineaCliOptions options) {
@Override
public TransactionSelector create() {
final LineaConfiguration lineaConfiguration = options.toDomainObject();
return new LineaTransactionSelector(
lineaConfiguration.maxTxCalldataSize(), lineaConfiguration.maxBlockCalldataSize());
return new LineaTransactionSelector(lineaConfiguration);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
* SPDX-License-Identifier: Apache-2.0
*/

package net.consensys.linea.sequencer;
package net.consensys.linea.sequencer.txselection;

import java.util.Optional;

import com.google.auto.service.AutoService;
import lombok.extern.slf4j.Slf4j;
import net.consensys.linea.sequencer.LineaCliOptions;
import org.hyperledger.besu.plugin.BesuContext;
import org.hyperledger.besu.plugin.BesuPlugin;
import org.hyperledger.besu.plugin.services.PicoCLIOptions;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Copyright ConsenSys AG.
*
* 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 net.consensys.linea.sequencer.txselection.selectors;

import java.util.List;

import net.consensys.linea.sequencer.LineaConfiguration;
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;

/** Class for transaction selection using a list of selectors. */
public class LineaTransactionSelector implements TransactionSelector {

final LineaConfiguration lineaConfiguration;
List<TransactionSelector> selectors;

public LineaTransactionSelector(LineaConfiguration lineaConfiguration) {
this.lineaConfiguration = lineaConfiguration;
this.selectors = createTransactionSelectors(lineaConfiguration);
}

/**
* Creates a list of selectors based on Linea configuration.
*
* @param lineaConfiguration The configuration to use.
* @return A list of selectors.
*/
private static List<TransactionSelector> createTransactionSelectors(
final LineaConfiguration lineaConfiguration) {
return List.of(
new MaxTransactionCallDataTransactionSelector(lineaConfiguration.maxTxCallDataSize()),
new MaxBlockCallDataTransactionSelector(lineaConfiguration.maxBlockCallDataSize()));
}

/**
* Evaluates a transaction before processing using all selectors. Stops if any selector doesn't
* select the transaction.
*
* @param pendingTransaction The transaction to evaluate.
* @return The first non-SELECTED result or SELECTED if all selectors select the transaction.
*/
@Override
public TransactionSelectionResult evaluateTransactionPreProcessing(
PendingTransaction pendingTransaction) {
for (var selector : selectors) {
TransactionSelectionResult result =
selector.evaluateTransactionPreProcessing(pendingTransaction);
if (!result.equals(TransactionSelectionResult.SELECTED)) {
return result;
}
}
return TransactionSelectionResult.SELECTED;
}

/**
* Evaluates a transaction considering its processing result. Stops if any selector doesn't select
* the transaction.
*
* @param pendingTransaction The processed transaction.
* @param processingResult The result of the transaction processing.
* @return The first non-SELECTED result or SELECTED if all selectors select the transaction.
*/
@Override
public TransactionSelectionResult evaluateTransactionPostProcessing(
PendingTransaction pendingTransaction, TransactionProcessingResult processingResult) {
for (var selector : selectors) {
TransactionSelectionResult result =
selector.evaluateTransactionPostProcessing(pendingTransaction, processingResult);
if (!result.equals(TransactionSelectionResult.SELECTED)) {
return result;
}
}
return TransactionSelectionResult.SELECTED;
}

/**
* Notifies all selectors when a transaction is selected.
*
* @param pendingTransaction The selected transaction.
*/
@Override
public void onTransactionSelected(final PendingTransaction pendingTransaction) {
selectors.forEach(selector -> selector.onTransactionSelected(pendingTransaction));
}

/**
* Notifies all selectors when a transaction is not selected.
*
* @param pendingTransaction The non-selected transaction.
* @param transactionSelectionResult The reason for not selecting the transaction.
*/
@Override
public void onTransactionNotSelected(
final PendingTransaction pendingTransaction,
final TransactionSelectionResult transactionSelectionResult) {
selectors.forEach(
selector ->
selector.onTransactionNotSelected(pendingTransaction, transactionSelectionResult));
}
}
Loading

0 comments on commit a01f302

Please sign in to comment.