From 2732af99eb98d56a44cee9cdd5994412843d11b1 Mon Sep 17 00:00:00 2001 From: Jamie Lokier Date: Mon, 3 May 2021 09:08:03 +0100 Subject: [PATCH] RPC: Move EVM-calling function doCall to rpcDoCall `doCall` used by JSON-RPC is another way to setup and call the EVM. Move it to `transaction/call_evm`. Signed-off-by: Jamie Lokier --- nimbus/rpc/p2p.nim | 5 +++-- nimbus/rpc/rpc_utils.nim | 13 ------------- nimbus/transaction/call_evm.nim | 16 +++++++++++++++- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/nimbus/rpc/p2p.nim b/nimbus/rpc/p2p.nim index b9f641b84..7663a92ba 100644 --- a/nimbus/rpc/p2p.nim +++ b/nimbus/rpc/p2p.nim @@ -15,7 +15,8 @@ import ../transaction, ../config, ../vm_state, ../constants, ../vm_types, ../utils, ../db/[db_chain, state_db], rpc_types, rpc_utils, ../vm_message, ../vm_computation, - ../vm_types2 + ../vm_types2, + ../transaction/call_evm #[ Note: @@ -261,7 +262,7 @@ proc setupEthRpc*(node: EthereumNode, chain: BaseChainDB , server: RpcServer) = let header = headerFromTag(chain, quantityTag) callData = callData(call, true, chain) - result = doCall(callData, header, chain) + result = rpcDoCall(callData, header, chain) server.rpc("eth_estimateGas") do(call: EthCall, quantityTag: string) -> HexQuantityStr: ## Generates and returns an estimate of how much gas is necessary to allow the transaction to complete. diff --git a/nimbus/rpc/rpc_utils.nim b/nimbus/rpc/rpc_utils.nim index 0eec165c9..b137541c8 100644 --- a/nimbus/rpc/rpc_utils.nim +++ b/nimbus/rpc/rpc_utils.nim @@ -180,19 +180,6 @@ proc callData*(call: EthCall, callMode: bool = true, chain: BaseChainDB): RpcCal if call.data.isSome: result.data = hexToSeqByte(call.data.get.string) -proc doCall*(call: RpcCallData, header: BlockHeader, chain: BaseChainDB): HexDataStr = - var - # we use current header stateRoot, unlike block validation - # which use previous block stateRoot - vmState = newBaseVMState(header.stateRoot, header, chain) - fork = toFork(chain.config, header.blockNumber) - comp = rpcSetupComputation(vmState, call, fork) - - comp.execComputation() - result = hexDataStr(comp.output) - # TODO: handle revert and error - # TODO: handle contract ABI - proc estimateGas*(call: RpcCallData, header: BlockHeader, chain: BaseChainDB, haveGasLimit: bool): GasInt = var # we use current header stateRoot, unlike block validation diff --git a/nimbus/transaction/call_evm.nim b/nimbus/transaction/call_evm.nim index 9ba5b4133..d9deb687f 100644 --- a/nimbus/transaction/call_evm.nim +++ b/nimbus/transaction/call_evm.nim @@ -8,7 +8,8 @@ import eth/common/eth_types, stint, options, - ".."/[vm_types, vm_types2, vm_state, vm_computation] + ".."/[vm_types, vm_types2, vm_state, vm_computation], + ".."/[db/db_chain, config, vm_state_transactions, rpc/hexstrings] type RpcCallData* = object @@ -39,3 +40,16 @@ proc rpcSetupComputation*(vmState: BaseVMState, call: RpcCallData, fork: Fork): ) return newComputation(vmState, msg) + +proc rpcDoCall*(call: RpcCallData, header: BlockHeader, chain: BaseChainDB): HexDataStr = + # TODO: handle revert and error + # TODO: handle contract ABI + var + # we use current header stateRoot, unlike block validation + # which use previous block stateRoot + vmState = newBaseVMState(header.stateRoot, header, chain) + fork = toFork(chain.config, header.blockNumber) + comp = rpcSetupComputation(vmState, call, fork) + + comp.execComputation() + result = hexDataStr(comp.output)