From 63ce2a912719d27ba669a2ea6220716f8c8fbb5f Mon Sep 17 00:00:00 2001 From: Andres Adjimann Date: Thu, 29 Sep 2022 11:00:33 -0300 Subject: [PATCH 1/3] fix eth_getBlockTransactionCountByHash to work with forks --- src/chains/ethereum/ethereum/src/api.ts | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/chains/ethereum/ethereum/src/api.ts b/src/chains/ethereum/ethereum/src/api.ts index c314523b67..3de63332a1 100644 --- a/src/chains/ethereum/ethereum/src/api.ts +++ b/src/chains/ethereum/ethereum/src/api.ts @@ -1274,14 +1274,12 @@ export default class EthereumApi implements Api { @assertArgLength(1) async eth_getBlockTransactionCountByHash(hash: DATA) { const { blocks } = this.#blockchain; - const blockNum = await blocks.getNumberFromHash(hash); - if (!blockNum) return null; - - const rawBlock = await blocks.getRawByBlockNumber(Quantity.from(blockNum)); - if (!rawBlock) return null; - - const [, rawTransactions] = decode(rawBlock); - return Quantity.from(rawTransactions.length); + const block = await blocks + .getByHash(hash) + .catch(_ => null); + if (!block) return null; + const transactions = block.getTransactions(); + return Quantity.from(transactions.length); } /** From 66080cd1e1c859f09c34ba94de8189e1c6636c63 Mon Sep 17 00:00:00 2001 From: Andres Adjimann Date: Thu, 29 Sep 2022 11:22:56 -0300 Subject: [PATCH 2/3] test: add a specific test for eth_getBlockTransactionCountByHash --- src/chains/ethereum/ethereum/tests/forking/block.test.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/chains/ethereum/ethereum/tests/forking/block.test.ts b/src/chains/ethereum/ethereum/tests/forking/block.test.ts index 296d535683..5978567e00 100644 --- a/src/chains/ethereum/ethereum/tests/forking/block.test.ts +++ b/src/chains/ethereum/ethereum/tests/forking/block.test.ts @@ -84,5 +84,12 @@ describe("forking", function () { const block = await provider.send("eth_getBlockByNumber", ["0x0", true]); assert.deepStrictEqual(block, block0); }); + + it("should get TransactionCountByHash from the original chain", async () => { + const block = await provider.send("eth_getBlockByNumber", ["0xB443", true]); + const blockTransactionCountByHash = await provider.send("eth_getBlockTransactionCountByHash", [block.hash]); + assert.deepStrictEqual(block.transactions.length, parseInt(blockTransactionCountByHash)); + }); + }); }); From a3be825f23a9b34155d7dbee92e7772d187592cb Mon Sep 17 00:00:00 2001 From: adjisb <85941014+adjisb@users.noreply.github.com> Date: Mon, 3 Oct 2022 15:02:37 -0300 Subject: [PATCH 3/3] Update src/chains/ethereum/ethereum/tests/forking/block.test.ts Co-authored-by: Micaiah Reid --- src/chains/ethereum/ethereum/tests/forking/block.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/chains/ethereum/ethereum/tests/forking/block.test.ts b/src/chains/ethereum/ethereum/tests/forking/block.test.ts index 5978567e00..7289f5cc95 100644 --- a/src/chains/ethereum/ethereum/tests/forking/block.test.ts +++ b/src/chains/ethereum/ethereum/tests/forking/block.test.ts @@ -85,7 +85,7 @@ describe("forking", function () { assert.deepStrictEqual(block, block0); }); - it("should get TransactionCountByHash from the original chain", async () => { + it("should get transaction count by hash from the original chain", async () => { const block = await provider.send("eth_getBlockByNumber", ["0xB443", true]); const blockTransactionCountByHash = await provider.send("eth_getBlockTransactionCountByHash", [block.hash]); assert.deepStrictEqual(block.transactions.length, parseInt(blockTransactionCountByHash));