Skip to content

Commit

Permalink
AbstractStatefulPluginTransactionSelector
Browse files Browse the repository at this point in the history
Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
  • Loading branch information
fab-10 committed Feb 3, 2025
1 parent 662e050 commit 27319fd
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
import com.google.common.base.Stopwatch;

public class TransactionEvaluationContext
implements org.hyperledger.besu.plugin.services.txselection.TransactionEvaluationContext<PendingTransaction> {
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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 <S> The type of the state used by the selector
*/
public abstract class AbstractStatefulPluginTransactionSelector<S extends DuplicableState<?>>
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public DuplicableState(final S state) {
*
* @return the current state
*/
public S getState() {
public S get() {
return state;
}

Expand All @@ -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;
}
}
Expand All @@ -188,7 +188,7 @@ public DuplicableLongState(final Long state) {
*/
@Override
public DuplicableLongState deepCopy() {
return new DuplicableLongState(getState());
return new DuplicableLongState(get());
}
}
}

0 comments on commit 27319fd

Please sign in to comment.