Skip to content

Commit

Permalink
TxFilteringState as stack ref struct (#6972)
Browse files Browse the repository at this point in the history
  • Loading branch information
benaadams authored May 3, 2024
1 parent 89c321e commit dcc33a7
Show file tree
Hide file tree
Showing 20 changed files with 25 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Consensus/TxFilterAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public TxFilterAdapter(IBlockTree blockTree, ITxFilter txFilter, ILogManager log
_blockTree = blockTree ?? throw new ArgumentNullException(nameof(blockTree));
}

public AcceptTxResult Accept(Transaction tx, TxFilteringState state, TxHandlingOptions txHandlingOptions)
public AcceptTxResult Accept(Transaction tx, ref TxFilteringState state, TxHandlingOptions txHandlingOptions)
{
if (tx is not GeneratedTransaction)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public AlreadyKnownTxFilter(
_logger = logger;
}

public AcceptTxResult Accept(Transaction tx, TxFilteringState state, TxHandlingOptions handlingOptions)
public AcceptTxResult Accept(Transaction tx, ref TxFilteringState state, TxHandlingOptions handlingOptions)
{
if (_hashCache.Get(tx.Hash!))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public BalanceTooLowFilter(TxDistinctSortedPool txs, TxDistinctSortedPool blobTx
_logger = logger;
}

public AcceptTxResult Accept(Transaction tx, TxFilteringState state, TxHandlingOptions handlingOptions)
public AcceptTxResult Accept(Transaction tx, ref TxFilteringState state, TxHandlingOptions handlingOptions)
{
if (tx.IsFree())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public BalanceZeroFilter(bool thereIsPriorityContract, ILogger logger)
_logger = logger;
}

public AcceptTxResult Accept(Transaction tx, TxFilteringState state, TxHandlingOptions handlingOptions)
public AcceptTxResult Accept(Transaction tx, ref TxFilteringState state, TxHandlingOptions handlingOptions)
{
AccountStruct account = state.SenderAccount;
UInt256 balance = account.Balance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public DeployedCodeFilter(IChainHeadSpecProvider specProvider)
{
_specProvider = specProvider;
}
public AcceptTxResult Accept(Transaction tx, TxFilteringState state, TxHandlingOptions txHandlingOptions)
public AcceptTxResult Accept(Transaction tx, ref TxFilteringState state, TxHandlingOptions txHandlingOptions)
{
return _specProvider.GetCurrentHeadSpec().IsEip3607Enabled && state.SenderAccount.HasCode
? AcceptTxResult.SenderIsContract
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public FeeTooLowFilter(IChainHeadInfoProvider headInfo, TxDistinctSortedPool txs
_logger = logger;
}

public AcceptTxResult Accept(Transaction tx, TxFilteringState state, TxHandlingOptions handlingOptions)
public AcceptTxResult Accept(Transaction tx, ref TxFilteringState state, TxHandlingOptions handlingOptions)
{
bool isLocal = (handlingOptions & TxHandlingOptions.PersistentBroadcast) != 0;
if (isLocal)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public FutureNonceFilter(ITxPoolConfig txPoolConfig)
_txPoolConfig = txPoolConfig;
}

public AcceptTxResult Accept(Transaction tx, TxFilteringState state, TxHandlingOptions txHandlingOptions)
public AcceptTxResult Accept(Transaction tx, ref TxFilteringState state, TxHandlingOptions txHandlingOptions)
{
int relevantMaxPendingTxsPerSender = (tx.SupportsBlobs
? _txPoolConfig.MaxPendingBlobTxsPerSender
Expand Down
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.TxPool/Filters/GapNonceFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public GapNonceFilter(TxDistinctSortedPool txs, TxDistinctSortedPool blobTxs, IL
_logger = logger;
}

public AcceptTxResult Accept(Transaction tx, TxFilteringState state, TxHandlingOptions handlingOptions)
public AcceptTxResult Accept(Transaction tx, ref TxFilteringState state, TxHandlingOptions handlingOptions)
{
bool isLocal = (handlingOptions & TxHandlingOptions.PersistentBroadcast) != 0;
bool nonceGapsAllowed = isLocal || !_txs.IsFull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public GasLimitTxFilter(IChainHeadInfoProvider chainHeadInfoProvider, ITxPoolCon
_configuredGasLimit = txPoolConfig.GasLimit ?? long.MaxValue;
}

public AcceptTxResult Accept(Transaction tx, TxFilteringState state, TxHandlingOptions handlingOptions)
public AcceptTxResult Accept(Transaction tx, ref TxFilteringState state, TxHandlingOptions handlingOptions)
{
long gasLimit = Math.Min(_chainHeadInfoProvider.BlockGasLimit ?? long.MaxValue, _configuredGasLimit);
if (tx.GasLimit > gasLimit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ namespace Nethermind.TxPool.Filters
/// </summary>
public interface IIncomingTxFilter
{
AcceptTxResult Accept(Transaction tx, TxFilteringState state, TxHandlingOptions txHandlingOptions);
AcceptTxResult Accept(Transaction tx, ref TxFilteringState state, TxHandlingOptions txHandlingOptions);
}
}
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.TxPool/Filters/LowNonceFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public LowNonceFilter(ILogger logger)
_logger = logger;
}

public AcceptTxResult Accept(Transaction tx, TxFilteringState state, TxHandlingOptions handlingOptions)
public AcceptTxResult Accept(Transaction tx, ref TxFilteringState state, TxHandlingOptions handlingOptions)
{
// As we have limited number of transaction that we store in mem pool its fairly easy to fill it up with
// high-priority garbage transactions. We need to filter them as much as possible to use the tx pool space
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public MalformedTxFilter(IChainHeadSpecProvider specProvider, ITxValidator txVal
_logger = logger;
}

public AcceptTxResult Accept(Transaction tx, TxFilteringState state, TxHandlingOptions txHandlingOptions)
public AcceptTxResult Accept(Transaction tx, ref TxFilteringState state, TxHandlingOptions txHandlingOptions)
{
IReleaseSpec spec = _specProvider.GetCurrentHeadSpec();
if (!_txValidator.IsWellFormed(tx, spec, out _))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public NotSupportedTxFilter(ITxPoolConfig txPoolConfig, ILogger logger)
_logger = logger;
}

public AcceptTxResult Accept(Transaction tx, TxFilteringState state, TxHandlingOptions txHandlingOptions)
public AcceptTxResult Accept(Transaction tx, ref TxFilteringState state, TxHandlingOptions txHandlingOptions)
{
if (_txPoolConfig.BlobsSupport.IsDisabled() && tx.SupportsBlobs)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Nethermind.TxPool.Filters
/// </summary>
internal sealed class NullHashTxFilter : IIncomingTxFilter
{
public AcceptTxResult Accept(Transaction tx, TxFilteringState state, TxHandlingOptions handlingOptions)
public AcceptTxResult Accept(Transaction tx, ref TxFilteringState state, TxHandlingOptions handlingOptions)
{
if (tx.Hash is null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ private NullIncomingTxFilter() { }

public static IIncomingTxFilter Instance { get; } = new NullIncomingTxFilter();

public AcceptTxResult Accept(Transaction tx, TxFilteringState state, TxHandlingOptions txHandlingOptions)
public AcceptTxResult Accept(Transaction tx, ref TxFilteringState state, TxHandlingOptions txHandlingOptions)
{
return AcceptTxResult.Accepted;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public PriorityFeeTooLowFilter(ILogger logger)
_logger = logger;
}

public AcceptTxResult Accept(Transaction tx, TxFilteringState state, TxHandlingOptions handlingOptions)
public AcceptTxResult Accept(Transaction tx, ref TxFilteringState state, TxHandlingOptions handlingOptions)
{
if (tx.SupportsBlobs && tx.MaxPriorityFeePerGas < _minBlobsPriorityFee)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.TxPool/Filters/TxTypeTxFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public TxTypeTxFilter(TxDistinctSortedPool txs, TxDistinctSortedPool blobTxs)
_blobTxs = blobTxs;
}

public AcceptTxResult Accept(Transaction tx, TxFilteringState state, TxHandlingOptions txHandlingOptions)
public AcceptTxResult Accept(Transaction tx, ref TxFilteringState state, TxHandlingOptions txHandlingOptions)
{
TxDistinctSortedPool otherTxTypePool = (tx.SupportsBlobs ? _txs : _blobTxs);
if (otherTxTypePool.ContainsBucket(tx.SenderAddress!)) // as unknownSenderFilter will run before this one
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public UnknownSenderFilter(IEthereumEcdsa ecdsa, ILogger logger)
_logger = logger;
}

public AcceptTxResult Accept(Transaction tx, TxFilteringState state, TxHandlingOptions handlingOptions)
public AcceptTxResult Accept(Transaction tx, ref TxFilteringState state, TxHandlingOptions handlingOptions)
{
/* We have encountered multiple transactions that do not resolve sender address properly.
* We need to investigate what these txs are and why the sender address is resolved to null.
Expand Down
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.TxPool/TxFilteringState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Nethermind.TxPool;

public class TxFilteringState(Transaction tx, IAccountStateProvider accounts)
public ref struct TxFilteringState(Transaction tx, IAccountStateProvider accounts)
{
private AccountStruct _senderAccount;

Expand Down
12 changes: 6 additions & 6 deletions src/Nethermind/Nethermind.TxPool/TxPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -379,15 +379,15 @@ public AcceptTxResult SubmitTx(Transaction tx, TxHandlingOptions handlingOptions

TxFilteringState state = new(tx, _accounts);

AcceptTxResult accepted = FilterTransactions(tx, handlingOptions, state);
AcceptTxResult accepted = FilterTransactions(tx, handlingOptions, ref state);

if (!accepted)
{
Metrics.PendingTransactionsDiscarded++;
}
else
{
accepted = AddCore(tx, state, startBroadcast);
accepted = AddCore(tx, ref state, startBroadcast);
if (accepted)
{
// Clear proper snapshot
Expand All @@ -401,12 +401,12 @@ public AcceptTxResult SubmitTx(Transaction tx, TxHandlingOptions handlingOptions
return accepted;
}

private AcceptTxResult FilterTransactions(Transaction tx, TxHandlingOptions handlingOptions, TxFilteringState state)
private AcceptTxResult FilterTransactions(Transaction tx, TxHandlingOptions handlingOptions, ref TxFilteringState state)
{
IIncomingTxFilter[] filters = _preHashFilters;
for (int i = 0; i < filters.Length; i++)
{
AcceptTxResult accepted = filters[i].Accept(tx, state, handlingOptions);
AcceptTxResult accepted = filters[i].Accept(tx, ref state, handlingOptions);

if (!accepted)
{
Expand All @@ -418,15 +418,15 @@ private AcceptTxResult FilterTransactions(Transaction tx, TxHandlingOptions hand
filters = _postHashFilters;
for (int i = 0; i < filters.Length; i++)
{
AcceptTxResult accepted = filters[i].Accept(tx, state, handlingOptions);
AcceptTxResult accepted = filters[i].Accept(tx, ref state, handlingOptions);

if (!accepted) return accepted;
}

return AcceptTxResult.Accepted;
}

private AcceptTxResult AddCore(Transaction tx, TxFilteringState state, bool isPersistentBroadcast)
private AcceptTxResult AddCore(Transaction tx, ref TxFilteringState state, bool isPersistentBroadcast)
{
bool eip1559Enabled = _specProvider.GetCurrentHeadSpec().IsEip1559Enabled;
UInt256 effectiveGasPrice = tx.CalculateEffectiveGasPrice(eip1559Enabled, _headInfo.CurrentBaseFee);
Expand Down

0 comments on commit dcc33a7

Please sign in to comment.