2018-04-06 14:52:10 +00:00
|
|
|
# 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
|
2018-05-11 13:32:10 +00:00
|
|
|
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
|
|
|
|
2018-02-14 16:38:01 +00:00
|
|
|
proc testFixture(fixtures: JsonNode, testStatusIMPL: var TestStatus)
|
2018-02-13 17:18:08 +00:00
|
|
|
|
|
|
|
suite "vm json tests":
|
|
|
|
jsonTest("VMTests", testFixture)
|
|
|
|
|
2018-02-14 16:38:01 +00:00
|
|
|
proc testFixture(fixtures: JsonNode, testStatusIMPL: var TestStatus) =
|
|
|
|
var fixture: JsonNode
|
|
|
|
for label, child in fixtures:
|
|
|
|
fixture = child
|
|
|
|
break
|
2018-04-14 10:40:41 +00:00
|
|
|
let header = BlockHeader(
|
2018-02-13 17:18:08 +00:00
|
|
|
coinbase: fixture{"env"}{"currentCoinbase"}.getStr,
|
2018-02-20 17:27:43 +00:00
|
|
|
difficulty: fixture{"env"}{"currentDifficulty"}.getHexadecimalInt.u256,
|
|
|
|
blockNumber: fixture{"env"}{"currentNumber"}.getHexadecimalInt.u256,
|
2018-05-11 13:32:10 +00:00
|
|
|
# gasLimit: fixture{"env"}{"currentGasLimit"}.getHexadecimalInt.u256,
|
2018-05-24 10:01:59 +00:00
|
|
|
timestamp: fixture{"env"}{"currentTimestamp"}.getHexadecimalInt.int64.fromUnix
|
|
|
|
)
|
|
|
|
var vm = newNimbusVM(header, newBaseChainDB(newMemoryDB()))
|
2018-04-06 14:52:10 +00:00
|
|
|
|
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)
|
|
|
|
|
2018-02-14 16:38:01 +00:00
|
|
|
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,
|
2018-02-20 17:27:43 +00:00
|
|
|
value=fixture{"exec"}{"value"}.getHexadecimalInt.u256,
|
2018-02-13 17:18:08 +00:00
|
|
|
data=fixture{"exec"}{"data"}.getStr.mapIt(it.byte),
|
|
|
|
code=code,
|
2018-05-25 10:25:19 +00:00
|
|
|
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))
|
2018-02-14 16:38:01 +00:00
|
|
|
|
2018-02-20 17:27:43 +00:00
|
|
|
#echo fixture{"exec"}
|
2018-02-14 16:38:01 +00:00
|
|
|
var c = newCodeStreamFromUnescaped(code)
|
2018-02-20 17:27:43 +00:00
|
|
|
if DEBUG:
|
2018-03-13 14:30:38 +00:00
|
|
|
c.displayDecompiled()
|
2018-02-14 16:38:01 +00:00
|
|
|
|
|
|
|
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)
|
2018-05-17 08:08:28 +00:00
|
|
|
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
|
|
|
|
|
2018-05-25 10:25:19 +00:00
|
|
|
let expectedGasRemaining = fixture{"gas"}.getHexadecimalInt
|
2018-02-13 17:18:08 +00:00
|
|
|
let actualGasRemaining = gasMeter.gasRemaining
|
2018-02-28 15:06:05 +00:00
|
|
|
checkpoint(&"{actualGasRemaining} {expectedGasRemaining}")
|
2018-04-06 14:52:10 +00:00
|
|
|
check(actualGasRemaining == expectedGasRemaining or
|
|
|
|
computation.code.hasSStore() and
|
2018-02-27 18:10:34 +00:00
|
|
|
(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)
|
2018-05-25 10:25:19 +00:00
|
|
|
let gasLimit = createdCall{"gasLimit"}.getHexadecimalInt
|
2018-02-20 17:27:43 +00:00
|
|
|
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)
|