# 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. import unittest2, strformat, strutils, tables, json, os, times, sequtils, 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/transaction/call_evm func bytesToHex(x: openarray[byte]): string {.inline.} = ## TODO: use seq[byte] for raw data and delete this proc foldl(x, a & b.int.toHex(2).toLowerAscii, "0x") proc testFixture(fixtures: JsonNode, testStatusIMPL: var TestStatus) proc vmJsonMain*() = suite "vm json tests": jsonTest("eth_tests" / "VMTests", "VMTests", testFixture, skipVMTests) proc testFixture(fixtures: JsonNode, testStatusIMPL: var TestStatus) = var fixture: JsonNode for label, child in fixtures: fixture = child break let fenv = fixture["env"] var emptyRlpHash = keccakHash(rlp.encode("")) let header = BlockHeader( coinbase: fenv{"currentCoinbase"}.getStr.parseAddress, difficulty: fromHex(UInt256, fenv{"currentDifficulty"}.getStr), blockNumber: fenv{"currentNumber"}.getHexadecimalInt.u256, gasLimit: fenv{"currentGasLimit"}.getHexadecimalInt.GasInt, timestamp: fenv{"currentTimestamp"}.getHexadecimalInt.int64.fromUnix, stateRoot: emptyRlpHash ) var vmState = newBaseVMState(emptyRlpHash, header, newBaseChainDB(newMemoryDB())) let fexec = fixture["exec"] vmState.mutateStateDB: setupStateDB(fixture{"pre"}, db) var call: RpcCallData let toAddress = fexec{"address"}.getStr.parseAddress let origin = fexec{"origin"}.getStr.parseAddress 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 fixtureResult = fixtureCallEvm(vmState, call, origin) if not fixture{"post"}.isNil: # Success checks check(not fixtureResult.isError) if fixtureResult.isError: echo "Computation error: ", fixtureResult.error.info let logEntries = fixtureResult.logEntries if not fixture{"logs"}.isNil: let actualLogsHash = hashLogEntries(logEntries) let expectedLogsHash = toLowerAscii(fixture{"logs"}.getStr) check(expectedLogsHash == actualLogsHash) elif logEntries.len > 0: checkpoint(&"Got log entries: {logEntries}") fail() let expectedOutput = fixture{"out"}.getStr check(fixtureResult.output.bytesToHex == expectedOutput) let expectedGasRemaining = fixture{"gas"}.getHexadecimalInt let actualGasRemaining = call.gas - fixtureResult.gasUsed checkpoint(&"Remaining: {actualGasRemaining} - Expected: {expectedGasRemaining}") check(actualGasRemaining == expectedGasRemaining) if not fixture{"post"}.isNil: verifyStateDb(fixture{"post"}, fixtureResult.vmState.readOnlyStateDB) else: # Error checks check(fixtureResult.isError) if not fixture{"pre"}.isNil: verifyStateDb(fixture{"pre"}, fixtureResult.vmState.readOnlyStateDB) when isMainModule: vmJsonMain()