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 <jamie@shareable.org>
This commit is contained in:
Jamie Lokier 2021-05-04 05:54:39 +01:00
parent 39ce2390ae
commit 9e99bb6cd9
No known key found for this signature in database
GPG Key ID: CBC25C68435C30A2
2 changed files with 35 additions and 17 deletions

View File

@ -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)

View File

@ -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: