-
Notifications
You must be signed in to change notification settings - Fork 414
/
GapNonceFilter.cs
59 lines (52 loc) · 2.28 KB
/
GapNonceFilter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only
using Nethermind.Core;
using Nethermind.Int256;
using Nethermind.Logging;
using Nethermind.TxPool.Collections;
namespace Nethermind.TxPool.Filters
{
/// <summary>
/// Filters out transactions with nonces set too far in the future.
/// Without this filter it would be possible to fill in TX pool with transactions that have low chance of being executed soon.
/// </summary>
internal sealed class GapNonceFilter : IIncomingTxFilter
{
private readonly TxDistinctSortedPool _txs;
private readonly TxDistinctSortedPool _blobTxs;
private readonly ILogger _logger;
public GapNonceFilter(TxDistinctSortedPool txs, TxDistinctSortedPool blobTxs, ILogger logger)
{
_txs = txs;
_blobTxs = blobTxs;
_logger = logger;
}
public AcceptTxResult Accept(Transaction tx, ref TxFilteringState state, TxHandlingOptions handlingOptions)
{
bool isLocal = (handlingOptions & TxHandlingOptions.PersistentBroadcast) != 0;
bool nonceGapsAllowed = isLocal || !_txs.IsFull();
if (nonceGapsAllowed && !tx.SupportsBlobs)
{
return AcceptTxResult.Accepted;
}
int numberOfSenderTxsInPending = tx.SupportsBlobs
? _blobTxs.GetBucketCount(tx.SenderAddress!)
: _txs.GetBucketCount(tx.SenderAddress!); // since unknownSenderFilter will run before this one
UInt256 currentNonce = state.SenderAccount.Nonce;
long nextNonceInOrder = (long)currentNonce + numberOfSenderTxsInPending;
bool isTxNonceNextInOrder = tx.Nonce <= nextNonceInOrder;
if (!isTxNonceNextInOrder)
{
Metrics.PendingTransactionsNonceGap++;
if (_logger.IsTrace)
{
_logger.Trace($"Skipped adding transaction {tx.ToString(" ")}, nonce in future.");
}
return !isLocal ?
AcceptTxResult.NonceGap :
AcceptTxResult.NonceGap.WithMessage($"Future nonce. Expected nonce: {nextNonceInOrder}");
}
return AcceptTxResult.Accepted;
}
}
}