-
Notifications
You must be signed in to change notification settings - Fork 462
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
+287
−21
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
} | ||
}); | ||
|
@@ -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) | ||
|
@@ -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); | ||
} | ||
}); | ||
|
@@ -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_() | ||
{ | ||
_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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
{ | ||
|
@@ -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); | ||
|
@@ -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( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/Nethermind/Nethermind.AccountAbstraction/Source/CompareReplacedUserOperationByFee.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/Nethermind/Nethermind.AccountAbstraction/Source/CompareUserOperationByNonce.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...Nethermind/Nethermind.AccountAbstraction/Source/CompetingUserOperationEqualityComparer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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