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

Fix/get block transactions count #4340

Merged
merged 14 commits into from
Aug 12, 2022
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// 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.Concurrent;
Expand Down Expand Up @@ -65,11 +65,11 @@ public class UserOperationPool : IUserOperationPool, IDisposable

private readonly Channel<BlockReplacementEventArgs> _headBlocksReplacementChannel = Channel.CreateUnbounded<BlockReplacementEventArgs>(new UnboundedChannelOptions() { SingleReader = true, SingleWriter = true });
private readonly Channel<BlockEventArgs> _headBlockChannel = Channel.CreateUnbounded<BlockEventArgs>(new UnboundedChannelOptions() { SingleReader = true, SingleWriter = true });

private readonly ulong _chainId;

private UInt256 _currentBaseFee;

public UserOperationPool(
IAccountAbstractionConfig accountAbstractionConfig,
IBlockTree blockTree,
Expand Down Expand Up @@ -106,7 +106,7 @@ ulong chainId
_userOperationEventTopic = new Keccak("0x33fd4d1f25a5461bea901784a6571de6debc16cd0831932c22c6969cd73ba994");

MemoryAllowance.MemPoolSize = accountAbstractionConfig.UserOperationPoolSize;

_blockTree.BlockAddedToMain += OnBlockAdded;
_blockTree.NewHeadBlock += OnNewHead;

Expand All @@ -129,7 +129,7 @@ private void OnBlockAdded(object? sender, BlockReplacementEventArgs e)
$"Couldn't correctly add or remove user operations from UserOperationPool after processing block {e.Block!.ToString(Block.Format.FullHashAndNumber)}.", exception);
}
}

private void OnNewHead(object? sender, BlockEventArgs e)
{
try
Expand Down Expand Up @@ -170,7 +170,7 @@ private void ProcessNewReplacementBlocks()
}
});
}

private void ProcessNewHeadBlocks()
{
Task.Factory.StartNew(async () =>
Expand Down Expand Up @@ -242,14 +242,14 @@ public bool CanInsert(UserOperation userOperation)
}

// we only want to increment opsSeen for ops whose maxFeePerGas passes baseFee
// else paymasters could be griefed by submitting many ops which might never
// else paymasters could be griefed by submitting many ops which might never
// make it on chain but will increase opsSeen
private void IncrementOpsSeenForOpsSurpassingBaseFee()
{
_userOperationSortedPool.UpdatePool(RemoveOpsSurpassingBaseFee);
}

private IEnumerable<(UserOperation Tx, Action<UserOperation>? Change)> RemoveOpsSurpassingBaseFee(Address address, ICollection<UserOperation> userOperations)
private IEnumerable<(UserOperation Tx, Action<UserOperation>? Change)> RemoveOpsSurpassingBaseFee(Address address, IReadOnlyCollection<UserOperation> userOperations)
{
foreach (UserOperation op in userOperations)
{
Expand Down Expand Up @@ -280,7 +280,7 @@ public ResultWrapper<Keccak> AddUserOperation(UserOperation userOperation)

UserOperationEventArgs userOperationEventArgs = new(userOperation, _entryPointAddress);
NewReceived?.Invoke(this, userOperationEventArgs);

UpdateCurrentBaseFee();
ResultWrapper<Keccak> result = ValidateUserOperation(userOperation);
if (result.Result == Result.Success)
Expand All @@ -295,9 +295,9 @@ public ResultWrapper<Keccak> AddUserOperation(UserOperation userOperation)
_paymasterThrottler.IncrementOpsSeen(userOperation.Paymaster);
}
if (_logger.IsDebug) _logger.Debug($"UserOperation {userOperation.RequestId!} inserted into pool");
_userOperationBroadcaster.BroadcastOnce(new UserOperationWithEntryPoint(userOperation, _entryPointAddress));
_userOperationBroadcaster.BroadcastOnce(new UserOperationWithEntryPoint(userOperation, _entryPointAddress));
NewPending?.Invoke(this, userOperationEventArgs);

return ResultWrapper<Keccak>.Success(userOperation.RequestId!);
}

Expand Down Expand Up @@ -371,7 +371,7 @@ private ResultWrapper<Keccak> ValidateUserOperation(UserOperation userOperation)
{
return ResultWrapper<Keccak>.Fail($"maxFeePerGas must be at least 70% of baseFee to be accepted into pool");
}


PaymasterStatus paymasterStatus =
_paymasterThrottler.GetPaymasterStatus(userOperation.Paymaster);
Expand Down
24 changes: 12 additions & 12 deletions src/Nethermind/Nethermind.Core/Collections/DictionarySortedSet.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
// 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 System.Runtime.Serialization;

namespace Nethermind.Core.Collections
{
public class DictionarySortedSet<TKey, TValue> : SortedSet<KeyValuePair<TKey, TValue>>
public class DictionarySortedSet<TKey, TValue> : EnhancedSortedSet<KeyValuePair<TKey, TValue>>
{
public DictionarySortedSet() : base(GetComparer()) { }

Expand All @@ -32,12 +32,12 @@ public class DictionarySortedSet<TKey, TValue> : SortedSet<KeyValuePair<TKey, TV
public DictionarySortedSet(IEnumerable<KeyValuePair<TKey, TValue>> collection, IComparer<TKey> comparer) : base(collection, GetComparer(comparer)) { }

protected DictionarySortedSet(SerializationInfo info, StreamingContext context) : base(info, context) { }
private static IComparer<KeyValuePair<TKey, TValue>> GetComparer(IComparer<TKey>? comparer = null) =>

private static IComparer<KeyValuePair<TKey, TValue>> GetComparer(IComparer<TKey>? comparer = null) =>
new KeyValuePairKeyOnlyComparer(comparer ?? Comparer<TKey>.Default);

public bool Add(TKey key, TValue value) => Add(new KeyValuePair<TKey, TValue>(key, value));

#pragma warning disable 8604
// fixed C# 9
public bool Remove(TKey key) => Remove(new KeyValuePair<TKey, TValue>(key, default));
Expand All @@ -60,7 +60,7 @@ public bool TryGetValue(TKey key, out TValue value)
#pragma warning restore 8601
return false;
}

#pragma warning disable 8604
// fixed C# 9
public bool ContainsKey(TKey key) => Contains(new KeyValuePair<TKey, TValue>(key, default));
Expand All @@ -75,9 +75,9 @@ public KeyValuePairKeyOnlyComparer(IComparer<TKey>? keyComparer = null)
_keyComparer = keyComparer ?? Comparer<TKey>.Default;
}

public override int Compare(KeyValuePair<TKey, TValue> x, KeyValuePair<TKey, TValue> y)
public override int Compare(KeyValuePair<TKey, TValue> x, KeyValuePair<TKey, TValue> y)
=> _keyComparer.Compare(x.Key, y.Key);
}

}
}
26 changes: 26 additions & 0 deletions src/Nethermind/Nethermind.Core/Collections/IReadOnlySortedSet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 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;

namespace Nethermind.Core.Collections;

public interface IReadOnlySortedSet<T> : IReadOnlySet<T>
{
public T? Max { get; }
public T? Min { get; }
}
30 changes: 30 additions & 0 deletions src/Nethermind/Nethermind.Core/Collections/SortedSet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// 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 System.Runtime.Serialization;

namespace Nethermind.Core.Collections;

public class EnhancedSortedSet<T> : SortedSet<T>, IReadOnlySortedSet<T>
{
public EnhancedSortedSet() { }
public EnhancedSortedSet(IComparer<T>? comparer) : base(comparer) { }
public EnhancedSortedSet(IEnumerable<T> collection) : base(collection) { }
public EnhancedSortedSet(IEnumerable<T> collection, IComparer<T>? comparer) : base(collection, comparer) { }
protected EnhancedSortedSet(SerializationInfo info, StreamingContext context) : base(info, context) { }
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// 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/>.

Expand Down Expand Up @@ -75,7 +75,7 @@ public async Task eth_getTransactionCount_should_invoke_client_method()
var address = TestItem.AddressA;
var blockParameter = BlockParameterModel.Latest;
await _proxy.eth_getTransactionCount(address, blockParameter);
await _client.Received().SendAsync<UInt256?>(nameof(_proxy.eth_getTransactionCount),
await _client.Received().SendAsync<UInt256>(nameof(_proxy.eth_getTransactionCount),
address, blockParameter.Type);
}

Expand Down Expand Up @@ -114,7 +114,7 @@ public async Task eth_getTransactionByHash_should_invoke_client_method()
await _proxy.eth_getTransactionByHash(hash);
await _client.Received().SendAsync<TransactionModel>(nameof(_proxy.eth_getTransactionByHash), hash);
}

[Test]
public async Task eth_pendingTransactions_should_invoke_client_method()
{
Expand Down
20 changes: 10 additions & 10 deletions src/Nethermind/Nethermind.Facade/Proxy/EthJsonRpcClientProxy.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// 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/>.

Expand Down Expand Up @@ -41,12 +41,12 @@ public Task<RpcResult<UInt256>> eth_chainId()
public Task<RpcResult<UInt256?>> eth_getBalance(Address address, BlockParameterModel blockParameter = null)
=> _proxy.SendAsync<UInt256?>(nameof(eth_getBalance), address, MapBlockParameter(blockParameter));

public Task<RpcResult<UInt256?>> eth_getTransactionCount(Address address, BlockParameterModel blockParameter = null)
=> _proxy.SendAsync<UInt256?>(nameof(eth_getTransactionCount), address, MapBlockParameter(blockParameter));
public Task<RpcResult<UInt256>> eth_getTransactionCount(Address address, BlockParameterModel blockParameter = null)
=> _proxy.SendAsync<UInt256>(nameof(eth_getTransactionCount), address, MapBlockParameter(blockParameter));

public Task<RpcResult<ReceiptModel>> eth_getTransactionReceipt(Keccak transactionHash)
=> _proxy.SendAsync<ReceiptModel>(nameof(eth_getTransactionReceipt), transactionHash);

public Task<RpcResult<byte[]>> eth_call(CallTransactionModel transaction,
BlockParameterModel blockParameter = null)
=> _proxy.SendAsync<byte[]>(nameof(eth_call), transaction, MapBlockParameter(blockParameter));
Expand All @@ -56,7 +56,7 @@ public Task<RpcResult<byte[]>> eth_getCode(Address address, BlockParameterModel

public Task<RpcResult<TransactionModel>> eth_getTransactionByHash(Keccak transactionHash)
=> _proxy.SendAsync<TransactionModel>(nameof(eth_getTransactionByHash), transactionHash);

public Task<RpcResult<TransactionModel[]>> eth_pendingTransactions()
=> _proxy.SendAsync<TransactionModel[]>(nameof(eth_pendingTransactions));

Expand All @@ -65,7 +65,7 @@ public Task<RpcResult<Keccak>> eth_sendRawTransaction(byte[] transaction)

public Task<RpcResult<Keccak>> eth_sendTransaction(TransactionModel transaction)
=> _proxy.SendAsync<Keccak>(nameof(eth_sendTransaction), transaction);

public Task<RpcResult<byte[]>> eth_estimateGas(TransactionModel transaction, BlockParameterModel blockParameter = null)
=> _proxy.SendAsync<byte[]>(nameof(eth_estimateGas), transaction);

Expand All @@ -77,12 +77,12 @@ public Task<RpcResult<byte[]>> eth_estimateGas(TransactionModel transaction, Bl
bool returnFullTransactionObjects = false)
=> _proxy.SendAsync<BlockModel<Keccak>>(nameof(eth_getBlockByNumber), MapBlockParameter(blockParameter),
returnFullTransactionObjects);

public Task<RpcResult<BlockModel<TransactionModel>>> eth_getBlockByNumberWithTransactionDetails(BlockParameterModel blockParameter,
bool returnFullTransactionObjects = false)
=> _proxy.SendAsync<BlockModel<TransactionModel>>(nameof(eth_getBlockByNumber), MapBlockParameter(blockParameter),
returnFullTransactionObjects);

public Task<RpcResult<string>> net_version()
=> _proxy.SendAsync<string>(nameof(net_version));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// 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/>.

Expand All @@ -27,7 +27,7 @@ public interface IEthJsonRpcClientProxy
Task<RpcResult<UInt256>> eth_chainId();
Task<RpcResult<long?>> eth_blockNumber();
Task<RpcResult<UInt256?>> eth_getBalance(Address address, BlockParameterModel blockParameter = null);
Task<RpcResult<UInt256?>> eth_getTransactionCount(Address address, BlockParameterModel blockParameter = null);
Task<RpcResult<UInt256>> eth_getTransactionCount(Address address, BlockParameterModel blockParameter = null);
Task<RpcResult<ReceiptModel>> eth_getTransactionReceipt(Keccak transactionHash);
Task<RpcResult<byte[]>> eth_call(CallTransactionModel transaction, BlockParameterModel blockParameter = null);
Task<RpcResult<byte[]>> eth_getCode(Address address, BlockParameterModel blockParameter = null);
Expand Down
Loading