From 181745613ca0c4095a2421141fd60f7b1ac7bac7 Mon Sep 17 00:00:00 2001 From: smartprogrammer93 <33181301+smartprogrammer93@users.noreply.github.com> Date: Fri, 19 Aug 2022 21:21:32 +0300 Subject: [PATCH] Add optional argument Address in parity_pendingTransactions (#4413) --- .../Modules/ParityRpcModuleTests.cs | 20 ++++++++++++++++++- .../Modules/Parity/IParityRpcModule.cs | 4 ++-- .../Modules/Parity/ParityRpcModule.cs | 13 ++++++++---- src/Nethermind/Nethermind.TxPool/ITxPool.cs | 6 ++++++ .../Nethermind.TxPool/NullTxPool.cs | 3 +++ src/Nethermind/Nethermind.TxPool/TxPool.cs | 3 +++ 6 files changed, 42 insertions(+), 7 deletions(-) diff --git a/src/Nethermind/Nethermind.JsonRpc.Test/Modules/ParityRpcModuleTests.cs b/src/Nethermind/Nethermind.JsonRpc.Test/Modules/ParityRpcModuleTests.cs index 075c1e915ae..1b7303aa190 100644 --- a/src/Nethermind/Nethermind.JsonRpc.Test/Modules/ParityRpcModuleTests.cs +++ b/src/Nethermind/Nethermind.JsonRpc.Test/Modules/ParityRpcModuleTests.cs @@ -267,7 +267,25 @@ public async Task parity_pendingTransactions() string expectedResult = "{\"jsonrpc\":\"2.0\",\"result\":[{\"hash\":\"0xd4720d1b81c70ed4478553a213a83bd2bf6988291677f5d05c6aae0b287f947e\",\"nonce\":\"0x0\",\"blockHash\":null,\"blockNumber\":null,\"transactionIndex\":null,\"from\":\"0x0000000000000000000000000000000000000002\",\"to\":\"0x0000000000000000000000000000000000000000\",\"value\":\"0x1\",\"gasPrice\":\"0x1\",\"gas\":\"0x5208\",\"input\":\"0x\",\"raw\":\"0xf85f8001825208940000000000000000000000000000000000000000018025a0ef2effb79771cbe42fc7f9cc79440b2a334eedad6e528ea45c2040789def4803a0515bdfe298808be2e07879faaeacd0ad17f3b13305b9f971647bbd5d5b584642\",\"creates\":null,\"publicKey\":\"0x15a1cc027cfd2b970c8aa2b3b22dfad04d29171109f6502d5fb5bde18afe86dddd44b9f8d561577527f096860ee03f571cc7f481ea9a14cb48cc7c20c964373a\",\"chainId\":\"0x1\",\"condition\":null,\"r\":\"0xef2effb79771cbe42fc7f9cc79440b2a334eedad6e528ea45c2040789def4803\",\"s\":\"0x515bdfe298808be2e07879faaeacd0ad17f3b13305b9f971647bbd5d5b584642\",\"v\":\"0x25\",\"standardV\":\"0x0\"}],\"id\":67}"; Assert.AreEqual(expectedResult, serialized); } - + + [Test] + public async Task parity_pendingTransactions_With_Address() + { + await Task.Delay(100); + string serialized = RpcTest.TestSerializedRequest(_parityRpcModule, "parity_pendingTransactions", "0x0000000000000000000000000000000000000002"); + string expectedResult = "{\"jsonrpc\":\"2.0\",\"result\":[{\"hash\":\"0xd4720d1b81c70ed4478553a213a83bd2bf6988291677f5d05c6aae0b287f947e\",\"nonce\":\"0x0\",\"blockHash\":null,\"blockNumber\":null,\"transactionIndex\":null,\"from\":\"0x0000000000000000000000000000000000000002\",\"to\":\"0x0000000000000000000000000000000000000000\",\"value\":\"0x1\",\"gasPrice\":\"0x1\",\"gas\":\"0x5208\",\"input\":\"0x\",\"raw\":\"0xf85f8001825208940000000000000000000000000000000000000000018025a0ef2effb79771cbe42fc7f9cc79440b2a334eedad6e528ea45c2040789def4803a0515bdfe298808be2e07879faaeacd0ad17f3b13305b9f971647bbd5d5b584642\",\"creates\":null,\"publicKey\":\"0x15a1cc027cfd2b970c8aa2b3b22dfad04d29171109f6502d5fb5bde18afe86dddd44b9f8d561577527f096860ee03f571cc7f481ea9a14cb48cc7c20c964373a\",\"chainId\":\"0x1\",\"condition\":null,\"r\":\"0xef2effb79771cbe42fc7f9cc79440b2a334eedad6e528ea45c2040789def4803\",\"s\":\"0x515bdfe298808be2e07879faaeacd0ad17f3b13305b9f971647bbd5d5b584642\",\"v\":\"0x25\",\"standardV\":\"0x0\"}],\"id\":67}"; + Assert.AreEqual(expectedResult, serialized); + } + + [Test] + public async Task parity_pendingTransactions_With_Address_Empty_Result() + { + await Task.Delay(100); + string serialized = RpcTest.TestSerializedRequest(_parityRpcModule, "parity_pendingTransactions", "0x0000000000000000000000000000000000000005"); + string expectedResult = "{\"jsonrpc\":\"2.0\",\"result\":[],\"id\":67}"; + Assert.AreEqual(expectedResult, serialized); + } + [Test] public void parity_getBlockReceipts() { diff --git a/src/Nethermind/Nethermind.JsonRpc/Modules/Parity/IParityRpcModule.cs b/src/Nethermind/Nethermind.JsonRpc/Modules/Parity/IParityRpcModule.cs index b62f27cde6f..aa2a13fcfb2 100644 --- a/src/Nethermind/Nethermind.JsonRpc/Modules/Parity/IParityRpcModule.cs +++ b/src/Nethermind/Nethermind.JsonRpc/Modules/Parity/IParityRpcModule.cs @@ -24,10 +24,10 @@ namespace Nethermind.JsonRpc.Modules.Parity [RpcModule(ModuleType.Parity)] public interface IParityRpcModule : IRpcModule { - [JsonRpcMethod(Description = "Returns a list of transactions currently in the queue.", + [JsonRpcMethod(Description = "Returns a list of transactions currently in the queue. If address is provided, returns transactions only with given sender address.", IsImplemented = true, ExampleResponse = "{\"hash\":\"0x9372fe18622fd45569ef117644d4cda4af51d11bb3c72fa27690e78c9b0d7808\",\"nonce\":\"0x11b55\",\"blockHash\":null,\"blockNumber\":null,\"transactionIndex\":null,\"from\":\"0x89a3fc1d3c68f927be68d3de139980940a89fc80\",\"to\":\"0x89a3fc1d3c68f927be68d3de139980940a89fc80\",\"value\":\"0x0\",\"gasPrice\":\"0x3b9aca08\",\"gas\":\"0x7530\",\"input\":\"0x2f47e6a5c13bb151cad6f7297ceb6a197a9be6fdb3acbcfe1df3cad362525932\",\"raw\":\"0xf88683011b55843b9aca088275309489a3fc1d3c68f927be68d3de139980940a89fc8080a02f47e6a5c13bb151cad6f7297ceb6a197a9be6fdb3acbcfe1df3cad3625259322ba04cfe3030a781f8af08ebe69286a4fab707f00ce4e535c392ba8249527bdae5e5a002203d6802596ff141506437f7ae72b4391b2bdffafba45f8cb561cf5d24b456\",\"creates\":null,\"publicKey\":\"0xf409402c0b151206bb98e1031630681df4c046f0c278f920174daa14a34549fa2da52016ca659c0fe254c542fc3034c5a8da9f4d145fec6150db5ed19b4bc7ce\",\"chainId\":4,\"condition\":null,\"r\":\"0x4cfe3030a781f8af08ebe69286a4fab707f00ce4e535c392ba8249527bdae5e5\",\"s\":\"0x02203d6802596ff141506437f7ae72b4391b2bdffafba45f8cb561cf5d24b456\",\"v\":\"0x2b\",\"standardV\":\"0x0\"}, (...)")] - ResultWrapper parity_pendingTransactions(); + ResultWrapper parity_pendingTransactions([JsonRpcParameter(ExampleValue = "[\"0x78467cada5f1883e79fcf0f3ebfa50abeec8c820\"]")] Address? address = null); [JsonRpcMethod(Description = "Get receipts from all transactions from particular block, more efficient than fetching the receipts one-by-one.", IsImplemented = true, diff --git a/src/Nethermind/Nethermind.JsonRpc/Modules/Parity/ParityRpcModule.cs b/src/Nethermind/Nethermind.JsonRpc/Modules/Parity/ParityRpcModule.cs index 8819a10c469..4bf825cc00f 100644 --- a/src/Nethermind/Nethermind.JsonRpc/Modules/Parity/ParityRpcModule.cs +++ b/src/Nethermind/Nethermind.JsonRpc/Modules/Parity/ParityRpcModule.cs @@ -67,11 +67,16 @@ public ParityRpcModule( _peerManager = peerManager ?? throw new ArgumentNullException(nameof(peerManager)); } - public ResultWrapper parity_pendingTransactions() - => ResultWrapper.Success(_txPool.GetPendingTransactions().Where(pt => pt.SenderAddress != null) + public ResultWrapper parity_pendingTransactions(Address? address = null) + { + IEnumerable enumerable = address == null + ? _txPool.GetPendingTransactions() + : _txPool.GetPendingTransactionsBySender(address); + return ResultWrapper.Success(enumerable .Select(t => new ParityTransaction(t, Rlp.Encode(t).Bytes, - t.IsSigned ? _ecdsa.RecoverPublicKey(t.Signature, t.Hash) : null)).ToArray()); - + t.IsSigned ? _ecdsa.RecoverPublicKey(t.Signature, t.Hash) : null)).ToArray()); + } + public ResultWrapper parity_getBlockReceipts(BlockParameter blockParameter) { SearchResult searchResult = _blockFinder.SearchForBlock(blockParameter); diff --git a/src/Nethermind/Nethermind.TxPool/ITxPool.cs b/src/Nethermind/Nethermind.TxPool/ITxPool.cs index dbe76e51c8e..d38511b7b1f 100644 --- a/src/Nethermind/Nethermind.TxPool/ITxPool.cs +++ b/src/Nethermind/Nethermind.TxPool/ITxPool.cs @@ -32,6 +32,12 @@ public interface ITxPool /// /// IDictionary GetPendingTransactionsBySender(); + + /// + /// from a specific sender, sorted by nonce and later tx pool sorting + /// + /// + Transaction[] GetPendingTransactionsBySender(Address address); void AddPeer(ITxPoolPeer peer); void RemovePeer(PublicKey nodeId); AcceptTxResult SubmitTx(Transaction tx, TxHandlingOptions handlingOptions); diff --git a/src/Nethermind/Nethermind.TxPool/NullTxPool.cs b/src/Nethermind/Nethermind.TxPool/NullTxPool.cs index 723fb409fed..6f60d4c91d7 100644 --- a/src/Nethermind/Nethermind.TxPool/NullTxPool.cs +++ b/src/Nethermind/Nethermind.TxPool/NullTxPool.cs @@ -34,6 +34,8 @@ private NullTxPool() { } public Transaction[] GetOwnPendingTransactions() => Array.Empty(); + public Transaction[] GetPendingTransactionsBySender(Address address) => Array.Empty(); + public IDictionary GetPendingTransactionsBySender() => new Dictionary(); public void AddPeer(ITxPoolPeer peer) { } @@ -55,6 +57,7 @@ public bool TryGetPendingTransaction(Keccak hash, out Transaction? transaction) public UInt256 ReserveOwnTransactionNonce(Address address) => UInt256.Zero; public UInt256 GetLatestPendingNonce(Address address) => 0; + public event EventHandler NewDiscovered { add { } diff --git a/src/Nethermind/Nethermind.TxPool/TxPool.cs b/src/Nethermind/Nethermind.TxPool/TxPool.cs index d2b0fc7877a..a72a7dc95fe 100644 --- a/src/Nethermind/Nethermind.TxPool/TxPool.cs +++ b/src/Nethermind/Nethermind.TxPool/TxPool.cs @@ -133,6 +133,9 @@ public TxPool( public IDictionary GetPendingTransactionsBySender() => _transactions.GetBucketSnapshot(); + + public Transaction[] GetPendingTransactionsBySender(Address address) => + _transactions.GetBucketSnapshot(address); internal Transaction[] GetOwnPendingTransactions() => _broadcaster.GetSnapshot();