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

[AA] Only allow max 10 User Operation per sender, Implement op fee replacement #3799

Merged
merged 4 commits into from
Feb 9, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Expand Up @@ -195,7 +195,8 @@ protected override BlockProcessor CreateBlockProcessor()
new UserOperationSortedPool(
_accountAbstractionConfig.UserOperationPoolSize,
new CompareUserOperationsByDecreasingGasPrice(),
LogManager));
LogManager,
_accountAbstractionConfig));

return blockProcessor;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@ public void Evicted_user_operation_has_its_simulated_removed_automatically()
public void should_add_user_operations_concurrently()
{
int capacity = 2048;
_userOperationPool = GenerateUserOperationPool(capacity);
_userOperationPool = GenerateUserOperationPool(capacity, capacity);

Parallel.ForEach(TestItem.PrivateKeys, k =>
{
for (int i = 0; i < 100; i++)
{
UserOperation op = Build.A.UserOperation.WithSender(Address.SystemUser).WithNonce((UInt256)i).SignedAndResolved(k).TestObject;
UserOperation op = Build.A.UserOperation.WithSender(k.Address).WithNonce((UInt256)i).SignedAndResolved(k).TestObject;
_userOperationPool.AddUserOperation(op);
}
});
Expand All @@ -130,7 +130,7 @@ public void should_add_user_operations_concurrently()
public async Task should_remove_user_operations_concurrently()
{
int capacity = 4096;
_userOperationPool = GenerateUserOperationPool(capacity);
_userOperationPool = GenerateUserOperationPool(capacity, capacity);

int maxTryCount = 5;
for (int i = 0; i < maxTryCount; ++i)
Expand All @@ -139,7 +139,7 @@ public async Task should_remove_user_operations_concurrently()
{
for (int j = 0; j < 10; j++)
{
UserOperation op = Build.A.UserOperation.WithSender(Address.SystemUser).WithNonce((UInt256)j).SignedAndResolved(k).TestObject;
UserOperation op = Build.A.UserOperation.WithSender(k.Address).WithNonce((UInt256)j).SignedAndResolved(k).TestObject;
_userOperationPool.AddUserOperation(op);
}
});
Expand All @@ -166,6 +166,116 @@ void DeleteOpsFromPool(UserOperation[] ops)
}
}

[Test]
public void should_not_allow_more_than_max_capacity_per_sender_ops_from_same_sender()
{
_userOperationPool = GenerateUserOperationPool();
for (int j = 0; j < 20; j++)
{
UserOperation op = Build.A.UserOperation.WithSender(Address.SystemUser).WithNonce((UInt256)j).SignedAndResolved(TestItem.PrivateKeyA).TestObject;
_userOperationPool.AddUserOperation(op);
}

_userOperationPool.GetUserOperations().Count().Should().Be(10);
}

[Test]
public void should_not_allow_more_than_max_capacity_per_sender_ops_from_same_sender_()
Copy link
Contributor

Choose a reason for hiding this comment

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

This test is duplicated - same as above

{
_userOperationPool = GenerateUserOperationPool();
for (int j = 0; j < 20; j++)
{
UserOperation op = Build.A.UserOperation.WithSender(Address.SystemUser).WithNonce((UInt256)j).SignedAndResolved(TestItem.PrivateKeyA).TestObject;
_userOperationPool.AddUserOperation(op);
}

_userOperationPool.GetUserOperations().Count().Should().Be(10);
}

[Test]
public void should_replace_op_with_higher_fee()
{
_userOperationPool = GenerateUserOperationPool();
UserOperation op = Build.A.UserOperation.WithSender(Address.SystemUser).WithNonce((UInt256)0).WithMaxFeePerGas(10).WithMaxPriorityFeePerGas(10).SignedAndResolved(TestItem.PrivateKeyA).TestObject;
_userOperationPool.AddUserOperation(op);

_userOperationPool.GetUserOperations().Should().BeEquivalentTo(new[] {op});

UserOperation higherGasPriceOp = Build.A.UserOperation.WithSender(Address.SystemUser).WithNonce((UInt256)0).WithMaxFeePerGas(20).WithMaxPriorityFeePerGas(20).SignedAndResolved(TestItem.PrivateKeyA).TestObject;
_userOperationPool.AddUserOperation(higherGasPriceOp);

_userOperationPool.GetUserOperations().Count().Should().Be(1);
_userOperationPool.GetUserOperations().Should().BeEquivalentTo(new[] {higherGasPriceOp});
}

[Test]
public void should_not_add_op_with_higher_fee_that_does_not_replace_op_if_sender_has_too_many_ops()
{
_userOperationPool = GenerateUserOperationPool();

for (int j = 0; j < 10; j++)
{
UserOperation op = Build.A.UserOperation.WithSender(Address.SystemUser).WithNonce((UInt256)j).SignedAndResolved(TestItem.PrivateKeyA).TestObject;
_userOperationPool.AddUserOperation(op);
}

UserOperation higherGasPriceOp = Build.A.UserOperation.WithSender(Address.SystemUser).WithNonce((UInt256)10).WithMaxFeePerGas(20).WithMaxPriorityFeePerGas(20).SignedAndResolved(TestItem.PrivateKeyA).TestObject;
_userOperationPool.AddUserOperation(higherGasPriceOp);

_userOperationPool.GetUserOperations().Count().Should().Be(10);
_userOperationPool.GetUserOperations().Should().NotContain(new[] {higherGasPriceOp});
}

[Test]
public void should_replace_op_with_higher_fee_even_at_full_capacity()
{
_userOperationPool = GenerateUserOperationPool();

IList<UserOperation> opsIncluded = new List<UserOperation>();

for (int j = 0; j < 10; j++)
{
UserOperation op = Build.A.UserOperation.WithSender(Address.SystemUser).WithNonce((UInt256)j).WithMaxFeePerGas(10).WithMaxPriorityFeePerGas(10).SignedAndResolved(TestItem.PrivateKeyA).TestObject;
opsIncluded.Add(op);
_userOperationPool.AddUserOperation(op);
}

UserOperation higherGasPriceOp = Build.A.UserOperation.WithSender(Address.SystemUser).WithNonce(9).WithMaxFeePerGas(20).WithMaxPriorityFeePerGas(20).SignedAndResolved(TestItem.PrivateKeyA).TestObject;
_userOperationPool.AddUserOperation(higherGasPriceOp);

_userOperationPool.GetUserOperations().Count().Should().Be(10);
_userOperationPool.GetUserOperations().Take(9).Should().BeEquivalentTo(opsIncluded.Take(9));
_userOperationPool.GetUserOperations().Last().Should().BeEquivalentTo(higherGasPriceOp);

UserOperation higherGasPriceOp2 = Build.A.UserOperation.WithSender(Address.SystemUser).WithNonce(3).WithMaxFeePerGas(20).WithMaxPriorityFeePerGas(20).SignedAndResolved(TestItem.PrivateKeyA).TestObject;
_userOperationPool.AddUserOperation(higherGasPriceOp2);

_userOperationPool.GetUserOperations().Count().Should().Be(10);
_userOperationPool.GetUserOperations().ToArray()[3].Should().Be(higherGasPriceOp2);
_userOperationPool.GetUserOperations().Should().NotContain(opsIncluded.ToArray()[3]);
}

public void should_not_replace_op_with_lower_fee_at_full_capacity()
{
_userOperationPool = GenerateUserOperationPool();

IList<UserOperation> opsIncluded = new List<UserOperation>();

for (int j = 0; j < 10; j++)
{
UserOperation op = Build.A.UserOperation.WithSender(Address.SystemUser).WithNonce((UInt256)j).WithMaxFeePerGas(10).WithMaxPriorityFeePerGas(10).SignedAndResolved(TestItem.PrivateKeyA).TestObject;
opsIncluded.Add(op);
_userOperationPool.AddUserOperation(op);
}

UserOperation higherGasPriceOp = Build.A.UserOperation.WithSender(Address.SystemUser).WithNonce(9).WithMaxFeePerGas(5).WithMaxPriorityFeePerGas(5).SignedAndResolved(TestItem.PrivateKeyA).TestObject;
Copy link
Contributor

Choose a reason for hiding this comment

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

minor - lowerGasPriceOp actually

_userOperationPool.AddUserOperation(higherGasPriceOp);

_userOperationPool.GetUserOperations().Count().Should().Be(10);
_userOperationPool.GetUserOperations().Take(10).Should().BeEquivalentTo(opsIncluded.Take(10));
_userOperationPool.GetUserOperations().Should().NotContain(higherGasPriceOp);
}

[Test]
public void should_remove_user_operations_from_pool_when_included_in_block()
{
Expand Down Expand Up @@ -314,10 +424,15 @@ public void Does_not_accept_obviously_bad_user_operations_into_pool(UserOperatio
userOperations.Length.Should().Be(0);
}

private UserOperationPool GenerateUserOperationPool(int capacity = 10)
private UserOperationPool GenerateUserOperationPool(int capacity = 10, int perSenderCapacity = 10)
{
IAccountAbstractionConfig config = Substitute.For<IAccountAbstractionConfig>();
config.EntryPointContractAddress.Returns(_entryPointContractAddress);
config.UserOperationPoolSize.Returns(capacity);
config.MaximumUserOperationPerSender.Returns(perSenderCapacity);

UserOperationSortedPool userOperationSortedPool =
new(capacity, CompareUserOperationsByDecreasingGasPrice.Default, LimboLogs.Instance);
new(capacity, CompareUserOperationsByDecreasingGasPrice.Default, LimboLogs.Instance, config);

_stateProvider.GetBalance(Arg.Any<Address>()).Returns(1.Ether());
_stateProvider.AccountExists(Arg.Any<Address>()).Returns(true);
Expand All @@ -332,10 +447,6 @@ private UserOperationPool GenerateUserOperationPool(int capacity = 10)

_blockTree.Head.Returns(Core.Test.Builders.Build.A.Block.TestObject);

IAccountAbstractionConfig config = Substitute.For<IAccountAbstractionConfig>();
config.EntryPointContractAddress.Returns(_entryPointContractAddress);
config.UserOperationPoolSize.Returns(capacity);

IPaymasterThrottler paymasterThrottler = Substitute.For<PaymasterThrottler>();

return new UserOperationPool(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class AccountAbstractionConfig : IAccountAbstractionConfig
{
public bool Enabled { get; set; }
public int UserOperationPoolSize { get; set; } = 200;
public int MaximumUserOperationPerSender { get; set; } = 10;
public string EntryPointContractAddress { get; set; } = "";
public string Create2FactoryAddress { get; set; } = "";
public UInt256 MinimumGasPrice { get; set; } = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ private UserOperationPool UserOperationPool
UserOperationSortedPool userOperationSortedPool = new(
_accountAbstractionConfig.UserOperationPoolSize,
CompareUserOperationsByDecreasingGasPrice.Default,
getFromApi.LogManager);
getFromApi.LogManager,
_accountAbstractionConfig);

_userOperationPool = new UserOperationPool(
_accountAbstractionConfig,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public interface IAccountAbstractionConfig : IConfig
Description = "Defines the maximum number of UserOperations that can be kept in memory by clients",
DefaultValue = "200")]
int UserOperationPoolSize { get; set; }

[ConfigItem(
Description = "Defines the maximum number of UserOperations that can be kept for each sender",
DefaultValue = "10")]
int MaximumUserOperationPerSender { get; set; }

[ConfigItem(
Description =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) 2021 Demerzel Solutions Limited
// This file is part of the Nethermind library.
//
// The Nethermind library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The Nethermind library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the Nethermind. If not, see <http://www.gnu.org/licenses/>.
//

using System.Collections.Generic;
using Nethermind.AccountAbstraction.Data;
using Nethermind.Int256;

namespace Nethermind.AccountAbstraction.Source
{
public class CompareReplacedUserOperationByFee : IComparer<UserOperation?>
{
public static readonly CompareReplacedUserOperationByFee Instance = new();

// To replace old user operation, new user operation needs to have fee higher by at least 10% (1/10) of current fee.
// It is required to avoid acceptance and propagation of user operation with almost the same fee as replaced one.
private const int PartOfFeeRequiredToIncrease = 10;

private CompareReplacedUserOperationByFee() { }

public int Compare(UserOperation? x, UserOperation? y)
{
if (ReferenceEquals(x, y)) return 0;
if (ReferenceEquals(null, y)) return 1;
if (ReferenceEquals(null, x)) return -1;

y.MaxFeePerGas.Divide(PartOfFeeRequiredToIncrease, out UInt256 bumpMaxFeePerGas);
if (y.MaxFeePerGas + bumpMaxFeePerGas > x.MaxFeePerGas) return 1;

y.MaxPriorityFeePerGas.Divide(PartOfFeeRequiredToIncrease, out UInt256 bumpMaxPriorityFeePerGas);
return (y.MaxPriorityFeePerGas + bumpMaxPriorityFeePerGas).CompareTo(x.MaxPriorityFeePerGas);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) 2021 Demerzel Solutions Limited
// This file is part of the Nethermind library.
//
// The Nethermind library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The Nethermind library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the Nethermind. If not, see <http://www.gnu.org/licenses/>.
//

using System.Collections.Generic;
using Nethermind.AccountAbstraction.Data;

namespace Nethermind.AccountAbstraction.Source
{
public class CompareUserOperationByNonce : IComparer<UserOperation?>
{
public static readonly CompareUserOperationByNonce Instance = new();

private CompareUserOperationByNonce() { }

public int Compare(UserOperation? x, UserOperation? y)
{
if (ReferenceEquals(x, y)) return 0;
if (ReferenceEquals(null, y)) return 1;
if (ReferenceEquals(null, x)) return -1;
return x.Nonce.CompareTo(y.Nonce);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ namespace Nethermind.AccountAbstraction.Source
{
public class CompareUserOperationsByHash : IComparer<UserOperation>
{
public static readonly CompareUserOperationsByHash Default = new();
public static readonly CompareUserOperationsByHash Instance = new();

private CompareUserOperationsByHash() { }

public int Compare(UserOperation? x, UserOperation? y)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) 2021 Demerzel Solutions Limited
// This file is part of the Nethermind library.
//
// The Nethermind library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The Nethermind library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the Nethermind. If not, see <http://www.gnu.org/licenses/>.
//

using System;
using System.Collections.Generic;
using Nethermind.AccountAbstraction.Data;

namespace Nethermind.AccountAbstraction.Source
{
public class CompetingUserOperationEqualityComparer : IEqualityComparer<UserOperation?>
{
public static readonly CompetingUserOperationEqualityComparer Instance = new();

private CompetingUserOperationEqualityComparer() { }

public bool Equals(UserOperation? x, UserOperation? y)
{
if (ReferenceEquals(x, y)) return true;
if (ReferenceEquals(x, null)) return false;
if (ReferenceEquals(y, null)) return false;
if (x.GetType() != y.GetType()) return false;
return x.Sender.Equals(y.Sender) && x.Nonce.Equals(y.Nonce);
}

public int GetHashCode(UserOperation obj)
{
return HashCode.Combine(obj.Sender, obj.Nonce);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -262,13 +262,18 @@ private ResultWrapper<Keccak> ValidateUserOperation(UserOperation userOperation)
}
}

if (_userOperationSortedPool.UserOperationWouldOverflowSenderBucket(userOperation))
{
return ResultWrapper<Keccak>.Fail($"the pool already contains the maximum {_accountAbstractionConfig.MaximumUserOperationPerSender} user operations from the {userOperation.Sender} sender");
}

if (userOperation.MaxFeePerGas < _accountAbstractionConfig.MinimumGasPrice)
return ResultWrapper<Keccak>.Fail("maxFeePerGas below minimum gas price");
return ResultWrapper<Keccak>.Fail($"maxFeePerGas below minimum gas price {_accountAbstractionConfig.MinimumGasPrice} wei");

if (userOperation.CallGas < Transaction.BaseTxGasCost)
return ResultWrapper<Keccak>.Fail($"callGas too low, must be at least {Transaction.BaseTxGasCost}");

// make sure target account exists
// make sure target account exists or is going to be created
if (
userOperation.Sender == Address.Zero
|| !(_stateProvider.AccountExists(userOperation.Sender) || userOperation.InitCode != Bytes.Empty))
Expand Down
Loading