nimbus-eth1/tests/test_vm_json.nim

118 lines
4.7 KiB
Nim
Raw Normal View History

# Nimbus
# Copyright (c) 2018 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
# at your option. This file may not be copied, modified, or distributed except according to those terms.
2018-02-13 17:18:08 +00:00
import
unittest, strformat, strutils, sequtils, tables, stint, json, ospaths, times,
./test_helpers,
2018-05-28 10:22:28 +00:00
../nimbus/[constants, errors, logging],
../nimbus/[chain, vm_state, computation, opcode, vm_types, opcode_table],
../nimbus/utils/[header, padding],
../nimbus/vm/[gas_meter, message, code_stream, stack],
../nimbus/vm/forks/vm_forks, ../nimbus/db/[db_chain, state_db, backends/memory_backend]
2018-02-13 17:18:08 +00:00
proc testFixture(fixtures: JsonNode, testStatusIMPL: var TestStatus)
2018-02-13 17:18:08 +00:00
suite "vm json tests":
jsonTest("VMTests", testFixture)
proc testFixture(fixtures: JsonNode, testStatusIMPL: var TestStatus) =
var fixture: JsonNode
for label, child in fixtures:
fixture = child
break
let header = BlockHeader(
2018-02-13 17:18:08 +00:00
coinbase: fixture{"env"}{"currentCoinbase"}.getStr,
difficulty: fixture{"env"}{"currentDifficulty"}.getHexadecimalInt.u256,
blockNumber: fixture{"env"}{"currentNumber"}.getHexadecimalInt.u256,
# gasLimit: fixture{"env"}{"currentGasLimit"}.getHexadecimalInt.u256,
timestamp: fixture{"env"}{"currentTimestamp"}.getHexadecimalInt.int64.fromUnix
)
var vm = newNimbusVM(header, newBaseChainDB(newMemoryDB()))
2018-02-13 17:18:08 +00:00
var code = ""
vm.state.db(readOnly=false):
setupStateDB(fixture{"pre"}, db)
code = db.getCode(fixture{"exec"}{"address"}.getStr)
code = fixture{"exec"}{"code"}.getStr
2018-02-13 17:18:08 +00:00
let message = newMessage(
to=fixture{"exec"}{"address"}.getStr,
sender=fixture{"exec"}{"caller"}.getStr,
value=fixture{"exec"}{"value"}.getHexadecimalInt.u256,
2018-02-13 17:18:08 +00:00
data=fixture{"exec"}{"data"}.getStr.mapIt(it.byte),
code=code,
gas=fixture{"exec"}{"gas"}.getHexadecimalInt,
gasPrice=fixture{"exec"}{"gasPrice"}.getHexadecimalInt,
2018-02-13 17:18:08 +00:00
options=newMessageOptions(origin=fixture{"exec"}{"origin"}.getStr))
#echo fixture{"exec"}
var c = newCodeStreamFromUnescaped(code)
if DEBUG:
2018-03-13 14:30:38 +00:00
c.displayDecompiled()
var computation = newBaseComputation(vm.state, message)
computation.accountsToDelete = initTable[string, string]()
computation.opcodes = OPCODE_TABLE
computation.precompiles = initTable[string, Opcode]()
computation = computation.applyComputation(vm.state, message)
2018-02-13 17:18:08 +00:00
if not fixture{"post"}.isNil:
# Success checks
check(not computation.isError)
if computation.isError:
echo "Computation error: ", computation.error.info
2018-02-13 17:18:08 +00:00
let logEntries = computation.getLogEntries()
if not fixture{"logs"}.isNil:
discard
# TODO hashLogEntries let actualLogsHash = hashLogEntries(logEntries)
# let expectedLogsHash = fixture{"logs"}.getStr
# check(expectedLogsHash == actualLogsHash)
elif logEntries.len > 0:
checkpoint(&"Got log entries: {logEntries}")
fail()
let expectedOutput = fixture{"out"}.getStr
check(computation.output == expectedOutput)
let gasMeter = computation.gasMeter
let expectedGasRemaining = fixture{"gas"}.getHexadecimalInt
2018-02-13 17:18:08 +00:00
let actualGasRemaining = gasMeter.gasRemaining
checkpoint(&"{actualGasRemaining} {expectedGasRemaining}")
check(actualGasRemaining == expectedGasRemaining or
computation.code.hasSStore() and
(actualGasRemaining > expectedGasRemaining and (actualGasRemaining - expectedGasRemaining) mod 15_000 == 0 or
expectedGasRemaining > actualGasRemaining and (expectedGasRemaining - actualGasRemaining) mod 15_000 == 0))
2018-02-13 17:18:08 +00:00
let callCreatesJson = fixture{"callcreates"}
var callCreates: seq[JsonNode] = @[]
if not callCreatesJson.isNil:
for next in callCreatesJson:
callCreates.add(next)
check(computation.children.len == callCreates.len)
for child in zip(computation.children, callCreates):
var (childComputation, createdCall) = child
let toAddress = createdCall{"destination"}.getStr
let data = createdCall{"data"}.getStr.mapIt(it.byte)
let gasLimit = createdCall{"gasLimit"}.getHexadecimalInt
let value = createdCall{"value"}.getHexadecimalInt.u256
2018-02-13 17:18:08 +00:00
check(childComputation.msg.to == toAddress)
check(data == childComputation.msg.data or childComputation.msg.code.len > 0)
check(gasLimit == childComputation.msg.gas)
check(value == childComputation.msg.value)
# TODO postState = fixture{"post"}
else:
# Error checks
check(computation.isError)
# TODO postState = fixture{"pre"}
# TODO with vm.state.stateDb(readOnly=True) as stateDb:
# verifyStateDb(postState, stateDb)