Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New filter restricting delegations in txpool #8022

Merged
merged 22 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
// SPDX-FileCopyrightText: 2024 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using Nethermind.Core;
using Nethermind.Core.Specs;
using Nethermind.Core.Test.Builders;
using Nethermind.Crypto;
using Nethermind.Db;
using Nethermind.Evm;
using Nethermind.Logging;
using Nethermind.Specs.Forks;
using Nethermind.State;
using Nethermind.Trie.Pruning;
using Nethermind.TxPool.Collections;
using Nethermind.TxPool.Filters;
using NSubstitute;
using NUnit.Framework;
using Org.BouncyCastle.Pqc.Crypto.Lms;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Nethermind.TxPool.Test;
internal class OnlyOneTxPerDelegatedAccountFilterTest
{
[Test]
public void Accept_SenderIsNotDelegated_ReturnsAccepted()
{
IChainHeadSpecProvider headInfoProvider = Substitute.For<IChainHeadSpecProvider>();
headInfoProvider.GetCurrentHeadSpec().Returns(Prague.Instance);
TxDistinctSortedPool standardPool = new TxDistinctSortedPool(MemoryAllowance.MemPoolSize, Substitute.For<IComparer<Transaction>>(), NullLogManager.Instance);
TxDistinctSortedPool blobPool = new BlobTxDistinctSortedPool(10, Substitute.For<IComparer<Transaction>>(), NullLogManager.Instance);
OnlyOneTxPerDelegatedAccountFilter filter = new(headInfoProvider, standardPool, blobPool, Substitute.For<IReadOnlyStateProvider>(), new CodeInfoRepository(), new DelegationCache());
Transaction transaction = Build.A.Transaction.SignedAndResolved(new EthereumEcdsa(0), TestItem.PrivateKeyA).TestObject;
TxFilteringState state = new();

AcceptTxResult result = filter.Accept(transaction, ref state, TxHandlingOptions.None);

Assert.That(result, Is.EqualTo(AcceptTxResult.Accepted));
}

[Test]
public void Accept_SenderIsDelegatedWithNoTransactionsInPool_ReturnsAccepted()
{
IChainHeadSpecProvider headInfoProvider = Substitute.For<IChainHeadSpecProvider>();
headInfoProvider.GetCurrentHeadSpec().Returns(Prague.Instance);
TxDistinctSortedPool standardPool = new TxDistinctSortedPool(MemoryAllowance.MemPoolSize, Substitute.For<IComparer<Transaction>>(), NullLogManager.Instance);
TxDistinctSortedPool blobPool = new BlobTxDistinctSortedPool(10, Substitute.For<IComparer<Transaction>>(), NullLogManager.Instance);
OnlyOneTxPerDelegatedAccountFilter filter = new(headInfoProvider, standardPool, blobPool, Substitute.For<IReadOnlyStateProvider>(), new CodeInfoRepository(), new DelegationCache());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are setting up a filter in every test, repeating the same lines of code. Maybe worth to move it to method e.g. CreateFilter(stateProvider), which is returning OnlyOneTxPerDelegatedAccountFilter filter?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or even move state provider setup into it and return (filter, stateProvider?)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But it's just details about code repeating in test class, feel free to ignore and focus on more important things

Transaction transaction = Build.A.Transaction.SignedAndResolved(new EthereumEcdsa(0), TestItem.PrivateKeyA).TestObject;
TxFilteringState state = new();

AcceptTxResult result = filter.Accept(transaction, ref state, TxHandlingOptions.None);

Assert.That(result, Is.EqualTo(AcceptTxResult.Accepted));
ak88 marked this conversation as resolved.
Show resolved Hide resolved
}

[Test]
public void Accept_SenderIsDelegatedWithOneTransactionInPoolWithSameNonce_ReturnsAccepted()
{
IChainHeadSpecProvider headInfoProvider = Substitute.For<IChainHeadSpecProvider>();
headInfoProvider.GetCurrentHeadSpec().Returns(Prague.Instance);
TxDistinctSortedPool standardPool = new TxDistinctSortedPool(MemoryAllowance.MemPoolSize, Substitute.For<IComparer<Transaction>>(), NullLogManager.Instance);
TxDistinctSortedPool blobPool = new BlobTxDistinctSortedPool(10, Substitute.For<IComparer<Transaction>>(), NullLogManager.Instance);
Transaction inPool = Build.A.Transaction.SignedAndResolved(new EthereumEcdsa(0), TestItem.PrivateKeyA).TestObject;
standardPool.TryInsert(inPool.Hash, inPool);
IDb stateDb = new MemDb();
IDb codeDb = new MemDb();
TrieStore trieStore = new(stateDb, LimboLogs.Instance);
IWorldState stateProvider = new WorldState(trieStore, codeDb, LimboLogs.Instance);
stateProvider.CreateAccount(TestItem.AddressA, 0);
CodeInfoRepository codeInfoRepository = new();
byte[] code = [.. Eip7702Constants.DelegationHeader, .. TestItem.PrivateKeyA.Address.Bytes];
codeInfoRepository.InsertCode(stateProvider, code, TestItem.AddressA, Prague.Instance);
OnlyOneTxPerDelegatedAccountFilter filter = new(headInfoProvider, standardPool, blobPool, stateProvider, codeInfoRepository, new DelegationCache());
Transaction transaction = Build.A.Transaction.SignedAndResolved(new EthereumEcdsa(0), TestItem.PrivateKeyA).TestObject;
TxFilteringState state = new();

AcceptTxResult result = filter.Accept(transaction, ref state, TxHandlingOptions.None);

Assert.That(result, Is.EqualTo(AcceptTxResult.Accepted));
}

[Test]
public void Accept_SenderIsDelegatedWithOneTransactionInPoolWithDifferentNonce_ReturnsOnlyOneTxPerDelegatedAccount()
{
IChainHeadSpecProvider headInfoProvider = Substitute.For<IChainHeadSpecProvider>();
headInfoProvider.GetCurrentHeadSpec().Returns(Prague.Instance);
TxDistinctSortedPool standardPool = new TxDistinctSortedPool(MemoryAllowance.MemPoolSize, Substitute.For<IComparer<Transaction>>(), NullLogManager.Instance);
TxDistinctSortedPool blobPool = new BlobTxDistinctSortedPool(10, Substitute.For<IComparer<Transaction>>(), NullLogManager.Instance);
Transaction inPool = Build.A.Transaction.SignedAndResolved(new EthereumEcdsa(0), TestItem.PrivateKeyA).TestObject;
standardPool.TryInsert(inPool.Hash, inPool);
IDb stateDb = new MemDb();
IDb codeDb = new MemDb();
TrieStore trieStore = new(stateDb, LimboLogs.Instance);
IWorldState stateProvider = new WorldState(trieStore, codeDb, LimboLogs.Instance);
stateProvider.CreateAccount(TestItem.AddressA, 0);
CodeInfoRepository codeInfoRepository = new();
byte[] code = [.. Eip7702Constants.DelegationHeader, .. TestItem.PrivateKeyA.Address.Bytes];
codeInfoRepository.InsertCode(stateProvider, code, TestItem.AddressA, Prague.Instance);
OnlyOneTxPerDelegatedAccountFilter filter = new(headInfoProvider, standardPool, blobPool, stateProvider, codeInfoRepository, new DelegationCache());
Transaction transaction = Build.A.Transaction.WithNonce(1).SignedAndResolved(new EthereumEcdsa(0), TestItem.PrivateKeyA).TestObject;
TxFilteringState state = new();

AcceptTxResult result = filter.Accept(transaction, ref state, TxHandlingOptions.None);

Assert.That(result, Is.EqualTo(AcceptTxResult.OnlyOneTxPerDelegatedAccount));
}

private static object[] EipActiveCases =
{
new object[]{ true, AcceptTxResult.OnlyOneTxPerDelegatedAccount },
new object[]{ false, AcceptTxResult.Accepted},
};
[TestCaseSource(nameof(EipActiveCases))]
public void Accept_Eip7702IsNotActivated_ReturnsExpected(bool isActive, AcceptTxResult expected)
{
IChainHeadSpecProvider headInfoProvider = Substitute.For<IChainHeadSpecProvider>();
headInfoProvider.GetCurrentHeadSpec().Returns(isActive ? Prague.Instance : Cancun.Instance);
TxDistinctSortedPool standardPool = new TxDistinctSortedPool(MemoryAllowance.MemPoolSize, Substitute.For<IComparer<Transaction>>(), NullLogManager.Instance);
TxDistinctSortedPool blobPool = new BlobTxDistinctSortedPool(10, Substitute.For<IComparer<Transaction>>(), NullLogManager.Instance);
Transaction inPool = Build.A.Transaction.SignedAndResolved(new EthereumEcdsa(0), TestItem.PrivateKeyA).TestObject;
standardPool.TryInsert(inPool.Hash, inPool);
IDb stateDb = new MemDb();
IDb codeDb = new MemDb();
TrieStore trieStore = new(stateDb, LimboLogs.Instance);
IWorldState stateProvider = new WorldState(trieStore, codeDb, LimboLogs.Instance);
stateProvider.CreateAccount(TestItem.AddressA, 0);
CodeInfoRepository codeInfoRepository = new();
byte[] code = [.. Eip7702Constants.DelegationHeader, .. TestItem.PrivateKeyA.Address.Bytes];
codeInfoRepository.InsertCode(stateProvider, code, TestItem.AddressA, Prague.Instance);
OnlyOneTxPerDelegatedAccountFilter filter = new(headInfoProvider, standardPool, blobPool, stateProvider, codeInfoRepository, new DelegationCache());
Transaction transaction = Build.A.Transaction.WithNonce(1).SignedAndResolved(new EthereumEcdsa(0), TestItem.PrivateKeyA).TestObject;
TxFilteringState state = new();

AcceptTxResult result = filter.Accept(transaction, ref state, TxHandlingOptions.None);

Assert.That(result, Is.EqualTo(expected));
}

[Test]
public void Accept_SenderHasPendingDelegation_ReturnsPendingDelegation()
{
IChainHeadSpecProvider headInfoProvider = Substitute.For<IChainHeadSpecProvider>();
headInfoProvider.GetCurrentHeadSpec().Returns(Prague.Instance);
TxDistinctSortedPool standardPool = new TxDistinctSortedPool(MemoryAllowance.MemPoolSize, Substitute.For<IComparer<Transaction>>(), NullLogManager.Instance);
TxDistinctSortedPool blobPool = new BlobTxDistinctSortedPool(10, Substitute.For<IComparer<Transaction>>(), NullLogManager.Instance);
DelegationCache pendingDelegations = new();
pendingDelegations.IncrementDelegationCount(TestItem.AddressA, 0, true);
OnlyOneTxPerDelegatedAccountFilter filter = new(headInfoProvider, standardPool, blobPool, Substitute.For<IReadOnlyStateProvider>(), new CodeInfoRepository(), pendingDelegations);
Transaction transaction = Build.A.Transaction.WithNonce(0).SignedAndResolved(new EthereumEcdsa(0), TestItem.PrivateKeyA).TestObject;
TxFilteringState state = new();

AcceptTxResult result = filter.Accept(transaction, ref state, TxHandlingOptions.None);

Assert.That(result, Is.EqualTo(AcceptTxResult.PendingDelegation));
}
}
129 changes: 129 additions & 0 deletions src/Nethermind/Nethermind.TxPool.Test/TxPoolTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1756,6 +1756,135 @@ public void SubmitTx_CodeIsNotDelegationAndDelegation_DelegationIsAccepted((byte
result.Should().Be(testCase.expected);
}

[Test]
public void Delegated_account_can_only_have_one_tx()
{
ISpecProvider specProvider = GetPragueSpecProvider();
TxPoolConfig txPoolConfig = new TxPoolConfig { Size = 30, PersistentBlobStorageSize = 0 };
_txPool = CreatePool(txPoolConfig, specProvider);

PrivateKey signer = TestItem.PrivateKeyA;
_stateProvider.CreateAccount(signer.Address, UInt256.MaxValue);
byte[] delegation = [.. Eip7702Constants.DelegationHeader, .. TestItem.AddressC.Bytes];
_stateProvider.InsertCode(signer.Address, delegation.AsMemory(), Prague.Instance);

Transaction firstTx = Build.A.Transaction
.WithNonce(0)
.WithType(TxType.EIP1559)
.WithMaxFeePerGas(9.GWei())
.WithMaxPriorityFeePerGas(9.GWei())
.WithGasLimit(GasCostOf.Transaction)
.WithTo(TestItem.AddressB)
.SignedAndResolved(_ethereumEcdsa, signer).TestObject;

AcceptTxResult result = _txPool.SubmitTx(firstTx, TxHandlingOptions.PersistentBroadcast);
result.Should().Be(AcceptTxResult.Accepted);

Transaction secondTx = Build.A.Transaction
.WithNonce(1)
.WithType(TxType.EIP1559)
.WithMaxFeePerGas(9.GWei())
.WithMaxPriorityFeePerGas(9.GWei())
.WithGasLimit(GasCostOf.Transaction)
.WithTo(TestItem.AddressB)
.SignedAndResolved(_ethereumEcdsa, signer).TestObject;

result = _txPool.SubmitTx(secondTx, TxHandlingOptions.PersistentBroadcast);

result.Should().Be(AcceptTxResult.OnlyOneTxPerDelegatedAccount);
}

[Test]
public void Tx_with_pending_delegation_is_rejected_then_is_accepted_after_delegation_removal()
{
ISpecProvider specProvider = GetPragueSpecProvider();
TxPoolConfig txPoolConfig = new TxPoolConfig { Size = 30, PersistentBlobStorageSize = 0 };
_txPool = CreatePool(txPoolConfig, specProvider);

PrivateKey signer = TestItem.PrivateKeyA;
_stateProvider.CreateAccount(signer.Address, UInt256.MaxValue);

EthereumEcdsa ecdsa = new EthereumEcdsa(_specProvider.ChainId);

Transaction firstTx = Build.A.Transaction
.WithNonce(0)
.WithType(TxType.SetCode)
.WithMaxFeePerGas(9.GWei())
.WithMaxPriorityFeePerGas(9.GWei())
.WithGasLimit(100_000)
.WithAuthorizationCode(ecdsa.Sign(signer, specProvider.ChainId, TestItem.AddressC, 0))
.WithTo(TestItem.AddressB)
.SignedAndResolved(_ethereumEcdsa, signer).TestObject;

AcceptTxResult result = _txPool.SubmitTx(firstTx, TxHandlingOptions.PersistentBroadcast);
result.Should().Be(AcceptTxResult.Accepted);

Transaction secondTx = Build.A.Transaction
.WithNonce(1)
.WithType(TxType.EIP1559)
.WithMaxFeePerGas(9.GWei())
.WithMaxPriorityFeePerGas(9.GWei())
.WithGasLimit(GasCostOf.Transaction)
.WithTo(TestItem.AddressB)
.SignedAndResolved(_ethereumEcdsa, signer).TestObject;

result = _txPool.SubmitTx(secondTx, TxHandlingOptions.PersistentBroadcast);

result.Should().Be(AcceptTxResult.PendingDelegation);

_txPool.RemoveTransaction(firstTx.Hash);

result = _txPool.SubmitTx(secondTx, TxHandlingOptions.PersistentBroadcast);

result.Should().Be(AcceptTxResult.AlreadyKnown);
}


[Test]
public void Test()
{
ISpecProvider specProvider = GetPragueSpecProvider();
TxPoolConfig txPoolConfig = new TxPoolConfig { Size = 30, PersistentBlobStorageSize = 0 };
_txPool = CreatePool(txPoolConfig, specProvider);

PrivateKey signer = TestItem.PrivateKeyA;
_stateProvider.CreateAccount(signer.Address, UInt256.MaxValue);

EthereumEcdsa ecdsa = new EthereumEcdsa(_specProvider.ChainId);

Transaction firstTx = Build.A.Transaction
.WithNonce(0)
.WithType(TxType.SetCode)
.WithMaxFeePerGas(9.GWei())
.WithMaxPriorityFeePerGas(9.GWei())
.WithGasLimit(100_000)
.WithAuthorizationCode(ecdsa.Sign(signer, specProvider.ChainId, TestItem.AddressC, 0))
.WithTo(TestItem.AddressB)
.SignedAndResolved(_ethereumEcdsa, signer).TestObject;

AcceptTxResult result = _txPool.SubmitTx(firstTx, TxHandlingOptions.PersistentBroadcast);
result.Should().Be(AcceptTxResult.Accepted);

Transaction secondTx = Build.A.Transaction
.WithNonce(1)
.WithType(TxType.EIP1559)
.WithMaxFeePerGas(9.GWei())
.WithMaxPriorityFeePerGas(9.GWei())
.WithGasLimit(GasCostOf.Transaction)
.WithTo(TestItem.AddressB)
.SignedAndResolved(_ethereumEcdsa, signer).TestObject;

result = _txPool.SubmitTx(secondTx, TxHandlingOptions.PersistentBroadcast);

result.Should().Be(AcceptTxResult.PendingDelegation);

_txPool.RemoveTransaction(firstTx.Hash);

result = _txPool.SubmitTx(secondTx, TxHandlingOptions.PersistentBroadcast);

result.Should().Be(AcceptTxResult.AlreadyKnown);
ak88 marked this conversation as resolved.
Show resolved Hide resolved
}

private IDictionary<ITxPoolPeer, PrivateKey> GetPeers(int limit = 100)
{
var peers = new Dictionary<ITxPoolPeer, PrivateKey>();
Expand Down
9 changes: 9 additions & 0 deletions src/Nethermind/Nethermind.TxPool/AcceptTxResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ namespace Nethermind.TxPool
/// Ignores transactions if tx type is not supported
/// </summary>
public static readonly AcceptTxResult NotSupportedTxType = new(15, nameof(NotSupportedTxType));
/// <summary>
/// Only one tx is allowed per delegated account.
/// </summary>
public static readonly AcceptTxResult OnlyOneTxPerDelegatedAccount = new(16, nameof(OnlyOneTxPerDelegatedAccount));
ak88 marked this conversation as resolved.
Show resolved Hide resolved
/// <summary>
/// There is a pending delegation in the tx pool already
/// </summary>
public static readonly AcceptTxResult PendingDelegation = new(17, nameof(PendingDelegation));


/// <summary>
/// The node is syncing and cannot accept transactions at this time.
Expand Down
53 changes: 53 additions & 0 deletions src/Nethermind/Nethermind.TxPool/DelegationCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// SPDX-FileCopyrightText: 2024 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using Nethermind.Core;
using Nethermind.Int256;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Nethermind.TxPool;
internal sealed class DelegationCache
{
private readonly ConcurrentDictionary<UInt256, int> _pendingDelegations = new();

public bool HasPending(AddressAsKey key, UInt256 nonce)
{
return _pendingDelegations.ContainsKey(KeyMask(key, nonce));
}

public void IncrementDelegationCount(AddressAsKey key, UInt256 nonce, bool increment)
ak88 marked this conversation as resolved.
Show resolved Hide resolved
{
UInt256 addressPlusNonce = KeyMask(key, nonce);

int value = increment ? 1 : -1;
var lastCount = _pendingDelegations.AddOrUpdate(addressPlusNonce,
(k) =>
{
if (increment)
return 1;
return 0;
},
(k, c) => c + value);

if (lastCount == 0)
{
//Remove() is threadsafe and only removes if the count is the same as the updated one
((ICollection<KeyValuePair<AddressAsKey, int>>)_pendingDelegations).Remove(
new KeyValuePair<AddressAsKey, int>(key, lastCount));
}
}

private static UInt256 KeyMask(AddressAsKey key, UInt256 nonce)
{
//A nonce cannot exceed 2^64-1 and an address is 20 bytes, so we can pack them together in one u256
UInt256 addressPlusNonce = new(key.Value.Bytes);
nonce <<= 64 * 3;
addressPlusNonce += nonce;
return addressPlusNonce;
ak88 marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ internal sealed class MalformedTxFilter(
public AcceptTxResult Accept(Transaction tx, ref TxFilteringState state, TxHandlingOptions txHandlingOptions)
{
IReleaseSpec spec = specProvider.GetCurrentHeadSpec();
if (!txValidator.IsWellFormed(tx, spec))
ValidationResult result = txValidator.IsWellFormed(tx, spec);
if (!result)
{
Metrics.PendingTransactionsMalformed++;
// It may happen that other nodes send us transactions that were signed for another chain or don't have enough gas.
Expand Down
Loading
Loading