From eb67e78fde9800bca2073ec45853e08dd6216137 Mon Sep 17 00:00:00 2001 From: andri lim Date: Fri, 22 Mar 2024 23:40:16 +0700 Subject: [PATCH] =?UTF-8?q?Implement=20RPC=20methods:=20debug=5FgetRawBloc?= =?UTF-8?q?k,=20debug=5FgetRawHeader,=20debug=5Fg=E2=80=A6=20(#2098)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Implement RPC methods: debug_getRawBlock, debug_getRawHeader, debug_getRawReceipts, debug_getRawTransaction * Fix engine api sim when calling setupDebugRpc --- .../nodocker/engine/engine_env.nim | 2 +- nimbus/rpc.nim | 2 +- nimbus/rpc/debug.nim | 49 ++++++++++++++++++- 3 files changed, 50 insertions(+), 3 deletions(-) diff --git a/hive_integration/nodocker/engine/engine_env.nim b/hive_integration/nodocker/engine/engine_env.nim index 186f25408..c2877e1fb 100644 --- a/hive_integration/nodocker/engine/engine_env.nim +++ b/hive_integration/nodocker/engine/engine_env.nim @@ -126,7 +126,7 @@ proc newEngineEnv*(conf: var NimbusConf, chainFile: string, enableAuth: bool): E setupEthRpc(node, ctx, com, txPool, server) setupEngineAPI(beaconEngine, server) - setupDebugRpc(com, server) + setupDebugRpc(com, txPool, server) # Do not start clique sealing engine if we are using a Proof of Work chain file if chainFile.len > 0: diff --git a/nimbus/rpc.nim b/nimbus/rpc.nim index f0e1d3ddd..c04e9303a 100644 --- a/nimbus/rpc.nim +++ b/nimbus/rpc.nim @@ -57,7 +57,7 @@ proc installRPC(server: RpcServer, setupEthRpc(nimbus.ethNode, nimbus.ctx, com, nimbus.txPool, server) if RpcFlag.Debug in flags: - setupDebugRpc(com, server) + setupDebugRpc(com, nimbus.txPool, server) if RpcFlag.Exp in flags: setupExpRpc(com, server) diff --git a/nimbus/rpc/debug.nim b/nimbus/rpc/debug.nim index 66d9c5e27..c7fdf41c7 100644 --- a/nimbus/rpc/debug.nim +++ b/nimbus/rpc/debug.nim @@ -15,6 +15,7 @@ import ../tracer, ../vm_types, ../common/common, ../beacon/web3_eth_conv, + ../core/tx_pool, web3/conversions {.push raises: [].} @@ -41,7 +42,7 @@ proc traceOptionsToFlags(options: Option[TraceOptions]): set[TracerFlags] = if opts.disableState.isTrue : result.incl TracerFlags.DisableState if opts.disableStateDiff.isTrue: result.incl TracerFlags.DisableStateDiff -proc setupDebugRpc*(com: CommonRef, rpcsrv: RpcServer) = +proc setupDebugRpc*(com: CommonRef, txPool: TxPoolRef, rpcsrv: RpcServer) = let chainDB = com.db rpcsrv.rpc("debug_traceTransaction") do(data: Web3Hash, options: Option[TraceOptions]) -> JsonNode: @@ -130,3 +131,49 @@ proc setupDebugRpc*(com: CommonRef, rpcsrv: RpcServer) = let header = chainDB.headerFromTag(quantityTag) result = chainDB.setHead(header) + + rpcsrv.rpc("debug_getRawBlock") do(quantityTag: BlockTag) -> seq[byte]: + ## Returns an RLP-encoded block. + let + header = chainDB.headerFromTag(quantityTag) + blockHash = chainDB.getBlockHash(header.blockNumber) + + var + body = chainDB.getBlockBody(blockHash) + ethBlock = EthBlock( + header: header, + txs: system.move(body.transactions), + uncles: system.move(body.uncles), + withdrawals: system.move(body.withdrawals), + ) + + result = rlp.encode(ethBlock) + + rpcsrv.rpc("debug_getRawHeader") do(quantityTag: BlockTag) -> seq[byte]: + ## Returns an RLP-encoded header. + let header = chainDB.headerFromTag(quantityTag) + result = rlp.encode(header) + + rpcsrv.rpc("debug_getRawReceipts") do(quantityTag: BlockTag) -> seq[seq[byte]]: + ## Returns an array of EIP-2718 binary-encoded receipts. + let header = chainDB.headerFromTag(quantityTag) + for receipt in chainDB.getReceipts(header.receiptRoot): + result.add rlp.encode(receipt) + + rpcsrv.rpc("debug_getRawTransaction") do(data: Web3Hash) -> seq[byte]: + ## Returns an EIP-2718 binary-encoded transaction. + let txHash = ethHash data + let res = txPool.getItem(txHash) + if res.isOk: + return rlp.encode(res.get().tx) + + let txDetails = chainDB.getTransactionKey(txHash) + if txDetails.index < 0: + raise newException(ValueError, "Transaction not found " & data.toHex) + + let header = chainDB.getBlockHeader(txDetails.blockNumber) + var tx: Transaction + if chainDB.getTransaction(header.txRoot, txDetails.index, tx): + return rlp.encode(tx) + + raise newException(ValueError, "Transaction not found " & data.toHex)