From 9e99bb6cd951817f14fe4b43d2ee6e9a348fae61 Mon Sep 17 00:00:00 2001 From: Jamie Lokier Date: Tue, 4 May 2021 05:54:39 +0100 Subject: [PATCH] Fixtures: Prepare fixtureSetupComputation to support fixtureCallEvm In the `text_vm_json` ("fixtures") test code, there is another variant of `rpcSetupComputation` and `txSetupComputation` with slightly different paremeters. The similarity is obvious. It is a special setup for testing, though, as it requires slightly different parameters. Signed-off-by: Jamie Lokier --- nimbus/transaction/call_evm.nim | 20 ++++++++++++++++++++ tests/test_vm_json.nim | 32 +++++++++++++++----------------- 2 files changed, 35 insertions(+), 17 deletions(-) diff --git a/nimbus/transaction/call_evm.nim b/nimbus/transaction/call_evm.nim index 7f3cac2e1..f1d6cabee 100644 --- a/nimbus/transaction/call_evm.nim +++ b/nimbus/transaction/call_evm.nim @@ -282,3 +282,23 @@ proc asmCallEvm*(blockNumber: Uint256, chainDB: BaseChainDB, code, data: seq[byt result.memory = c.memory result.vmState = c.vmState result.contractAddress = c.msg.contractAddress + +proc fixtureSetupComputation*(vmState: BaseVMState, call: RpcCallData, origin: EthAddress): Computation = + vmState.setupTxContext( + origin = origin, # Differs from `rpcSetupComputation` + gasPrice = call.gasPrice, + # fork is not set. + ) + + var msg = Message( + kind: if call.contractCreation: evmcCreate else: evmcCall, + depth: 0, + gas: call.gas, # Differs from `rpcSetupComputation` + sender: call.source, + contractAddress: call.to, # Differs from `rpcSetupComputation` + codeAddress: call.to, + value: call.value, + data: call.data + ) + + return newComputation(vmState, msg) diff --git a/tests/test_vm_json.nim b/tests/test_vm_json.nim index c60bcc6b8..856dec351 100644 --- a/tests/test_vm_json.nim +++ b/tests/test_vm_json.nim @@ -10,7 +10,8 @@ import stew/byteutils, eth/[rlp, common], eth/trie/db, ./test_helpers, ./test_allowed_to_fail, ../nimbus/vm_internals, ../nimbus/[constants, vm_state, vm_types, utils], - ../nimbus/db/[db_chain] + ../nimbus/db/[db_chain], + ../nimbus/transaction/call_evm func bytesToHex(x: openarray[byte]): string {.inline.} = ## TODO: use seq[byte] for raw data and delete this proc @@ -44,24 +45,21 @@ proc testFixture(fixtures: JsonNode, testStatusIMPL: var TestStatus) = vmState.mutateStateDB: setupStateDB(fixture{"pre"}, db) - vmState.setupTxContext( - origin = fexec{"origin"}.getStr.parseAddress, - gasPrice = fexec{"gasPrice"}.getHexadecimalInt - ) - + var call: RpcCallData let toAddress = fexec{"address"}.getStr.parseAddress - let message = Message( - kind: if toAddress == ZERO_ADDRESS: evmcCreate else: evmcCall, # assume ZERO_ADDRESS is a contract creation - depth: 0, - gas: fexec{"gas"}.getHexadecimalInt, - sender: fexec{"caller"}.getStr.parseAddress, - contractAddress: toAddress, - codeAddress: toAddress, - value: cast[uint64](fexec{"value"}.getHexadecimalInt).u256, # Cast workaround for negative value - data: fexec{"data"}.getStr.hexToSeqByte - ) + let origin = fexec{"origin"}.getStr.parseAddress - var computation = newComputation(vmState, message) + call.source = fexec{"caller"}.getStr.parseAddress + call.to = toAddress + call.gas = fexec{"gas"}.getHexadecimalInt + call.gasPrice = fexec{"gasPrice"}.getHexadecimalInt + # Cast workaround for negative value + call.value = cast[uint64](fexec{"value"}.getHexadecimalInt).u256 + call.data = fexec{"data"}.getStr.hexToSeqByte + # assume ZERO_ADDRESS is a contract creation + call.contractCreation = (toAddress == ZERO_ADDRESS) + + var computation = fixtureSetupComputation(vmState, call, origin) computation.executeOpcodes() if not fixture{"post"}.isNil: