From 27319fd42b1f120359a7374cce1e6bded2e89240 Mon Sep 17 00:00:00 2001 From: Fabio Di Fabio Date: Mon, 3 Feb 2025 19:53:57 +0100 Subject: [PATCH] AbstractStatefulPluginTransactionSelector Signed-off-by: Fabio Di Fabio --- .../TransactionEvaluationContext.java | 3 +- .../BlobSizeTransactionSelector.java | 4 +- .../BlockSizeTransactionSelector.java | 4 +- ...ractStatefulPluginTransactionSelector.java | 60 +++++++++++++++++++ .../PluginTransactionSelector.java | 2 +- .../txselection/SelectorsStateManager.java | 6 +- 6 files changed, 70 insertions(+), 9 deletions(-) create mode 100644 plugin-api/src/main/java/org/hyperledger/besu/plugin/services/txselection/AbstractStatefulPluginTransactionSelector.java diff --git a/ethereum/blockcreation/src/main/java/org/hyperledger/besu/ethereum/blockcreation/txselection/TransactionEvaluationContext.java b/ethereum/blockcreation/src/main/java/org/hyperledger/besu/ethereum/blockcreation/txselection/TransactionEvaluationContext.java index e7f07c1a9f7..5738060b8bd 100644 --- a/ethereum/blockcreation/src/main/java/org/hyperledger/besu/ethereum/blockcreation/txselection/TransactionEvaluationContext.java +++ b/ethereum/blockcreation/src/main/java/org/hyperledger/besu/ethereum/blockcreation/txselection/TransactionEvaluationContext.java @@ -22,7 +22,8 @@ import com.google.common.base.Stopwatch; public class TransactionEvaluationContext - implements org.hyperledger.besu.plugin.services.txselection.TransactionEvaluationContext { + implements org.hyperledger.besu.plugin.services.txselection.TransactionEvaluationContext< + PendingTransaction> { private final ProcessableBlockHeader pendingBlockHeader; private final org.hyperledger.besu.datatypes.PendingTransaction pendingTransaction; private final Stopwatch evaluationTimer; diff --git a/ethereum/blockcreation/src/main/java/org/hyperledger/besu/ethereum/blockcreation/txselection/selectors/BlobSizeTransactionSelector.java b/ethereum/blockcreation/src/main/java/org/hyperledger/besu/ethereum/blockcreation/txselection/selectors/BlobSizeTransactionSelector.java index 6ae54b6f4af..e0062b97d40 100644 --- a/ethereum/blockcreation/src/main/java/org/hyperledger/besu/ethereum/blockcreation/txselection/selectors/BlobSizeTransactionSelector.java +++ b/ethereum/blockcreation/src/main/java/org/hyperledger/besu/ethereum/blockcreation/txselection/selectors/BlobSizeTransactionSelector.java @@ -60,7 +60,7 @@ public TransactionSelectionResult evaluateTransactionPreProcessing( if (tx.getType().supportsBlob()) { final var selectorState = getWorkingState(); - final var cumulativeBlobGasUsed = selectorState.getState(); + final var cumulativeBlobGasUsed = selectorState.get(); final var remainingBlobGas = context.gasLimitCalculator().currentBlobGasLimit() - cumulativeBlobGasUsed; @@ -88,7 +88,7 @@ public TransactionSelectionResult evaluateTransactionPreProcessing( return TransactionSelectionResult.TX_TOO_LARGE_FOR_REMAINING_BLOB_GAS; } - selectorState.setState(cumulativeBlobGasUsed + requestedBlobGas); + selectorState.set(cumulativeBlobGasUsed + requestedBlobGas); } return TransactionSelectionResult.SELECTED; } diff --git a/ethereum/blockcreation/src/main/java/org/hyperledger/besu/ethereum/blockcreation/txselection/selectors/BlockSizeTransactionSelector.java b/ethereum/blockcreation/src/main/java/org/hyperledger/besu/ethereum/blockcreation/txselection/selectors/BlockSizeTransactionSelector.java index c5ca6b0eb52..56a7a85bcc8 100644 --- a/ethereum/blockcreation/src/main/java/org/hyperledger/besu/ethereum/blockcreation/txselection/selectors/BlockSizeTransactionSelector.java +++ b/ethereum/blockcreation/src/main/java/org/hyperledger/besu/ethereum/blockcreation/txselection/selectors/BlockSizeTransactionSelector.java @@ -58,7 +58,7 @@ public TransactionSelectionResult evaluateTransactionPreProcessing( final TransactionSelectionResults transactionSelectionResults) { final var selectorState = getWorkingState(); - final long cumulativeGasUsed = selectorState.getState(); + final long cumulativeGasUsed = selectorState.get(); if (transactionTooLargeForBlock(evaluationContext.getTransaction(), cumulativeGasUsed)) { LOG.atTrace() @@ -75,7 +75,7 @@ public TransactionSelectionResult evaluateTransactionPreProcessing( return TransactionSelectionResult.TX_TOO_LARGE_FOR_REMAINING_GAS; } } - selectorState.setState(cumulativeGasUsed + evaluationContext.getTransaction().getGasLimit()); + selectorState.set(cumulativeGasUsed + evaluationContext.getTransaction().getGasLimit()); return TransactionSelectionResult.SELECTED; } diff --git a/plugin-api/src/main/java/org/hyperledger/besu/plugin/services/txselection/AbstractStatefulPluginTransactionSelector.java b/plugin-api/src/main/java/org/hyperledger/besu/plugin/services/txselection/AbstractStatefulPluginTransactionSelector.java new file mode 100644 index 00000000000..8f8b20f1248 --- /dev/null +++ b/plugin-api/src/main/java/org/hyperledger/besu/plugin/services/txselection/AbstractStatefulPluginTransactionSelector.java @@ -0,0 +1,60 @@ +/* + * 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.plugin.services.txselection; + +import org.hyperledger.besu.plugin.services.txselection.SelectorsStateManager.DuplicableState; + +/** + * This class represents an abstract plugin transaction selector which provides manage the selector + * state. + * + * @param The type of the state used by the selector + */ +public abstract class AbstractStatefulPluginTransactionSelector> + implements PluginTransactionSelector { + private final SelectorsStateManager selectorsStateManager; + + /** + * Initialize the plugin state to an initial value + * + * @param selectorsStateManager the selectors state manager + * @param initialState the initial value of the state + */ + public AbstractStatefulPluginTransactionSelector( + final SelectorsStateManager selectorsStateManager, final S initialState) { + this.selectorsStateManager = selectorsStateManager; + selectorsStateManager.createSelectorState(this, initialState); + } + + /** + * Get the working state for this selector. A working state contains changes that have not yet + * commited + * + * @return the working state of this selector + */ + protected S getWorkingState() { + return selectorsStateManager.getSelectorWorkingState(this); + } + + /** + * Get the commited state for this selector. A commited state contains changes that have been + * commited + * + * @return the commited state of this selector + */ + protected S getCommitedState() { + return selectorsStateManager.getSelectorCommittedState(this); + } +} diff --git a/plugin-api/src/main/java/org/hyperledger/besu/plugin/services/txselection/PluginTransactionSelector.java b/plugin-api/src/main/java/org/hyperledger/besu/plugin/services/txselection/PluginTransactionSelector.java index e0bf1cf3130..d0076efd328 100644 --- a/plugin-api/src/main/java/org/hyperledger/besu/plugin/services/txselection/PluginTransactionSelector.java +++ b/plugin-api/src/main/java/org/hyperledger/besu/plugin/services/txselection/PluginTransactionSelector.java @@ -24,7 +24,7 @@ /** Interface for the transaction selector */ @Unstable -public interface PluginTransactionSelector { +public interface PluginTransactionSelector extends TransactionSelector { /** Plugin transaction selector that unconditionally select every transaction */ PluginTransactionSelector ACCEPT_ALL = new PluginTransactionSelector() { diff --git a/plugin-api/src/main/java/org/hyperledger/besu/plugin/services/txselection/SelectorsStateManager.java b/plugin-api/src/main/java/org/hyperledger/besu/plugin/services/txselection/SelectorsStateManager.java index ab9ab2507f1..25426f1070d 100644 --- a/plugin-api/src/main/java/org/hyperledger/besu/plugin/services/txselection/SelectorsStateManager.java +++ b/plugin-api/src/main/java/org/hyperledger/besu/plugin/services/txselection/SelectorsStateManager.java @@ -155,7 +155,7 @@ public DuplicableState(final S state) { * * @return the current state */ - public S getState() { + public S get() { return state; } @@ -164,7 +164,7 @@ public S getState() { * * @param state the new state */ - public void setState(final S state) { + public void set(final S state) { this.state = state; } } @@ -188,7 +188,7 @@ public DuplicableLongState(final Long state) { */ @Override public DuplicableLongState deepCopy() { - return new DuplicableLongState(getState()); + return new DuplicableLongState(get()); } } }