2018-09-07 19:44:17 +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.
|
|
|
|
|
|
|
|
import
|
2023-05-10 15:40:48 +00:00
|
|
|
std/[strutils, tables, json, os, sets, options, times],
|
2019-11-20 15:25:09 +00:00
|
|
|
./test_helpers, ./test_allowed_to_fail,
|
2022-12-02 04:39:12 +00:00
|
|
|
../nimbus/core/executor, test_config,
|
2019-11-13 14:49:39 +00:00
|
|
|
../nimbus/transaction,
|
2022-12-02 04:39:12 +00:00
|
|
|
../nimbus/[vm_state, vm_types],
|
|
|
|
../nimbus/db/accounts_cache,
|
|
|
|
../nimbus/common/common,
|
2023-03-17 13:20:52 +00:00
|
|
|
../nimbus/utils/[utils, debug],
|
2023-01-10 17:02:21 +00:00
|
|
|
../tools/common/helpers as chp,
|
|
|
|
../tools/evmstate/helpers,
|
2023-01-09 14:39:36 +00:00
|
|
|
../tools/common/state_clearing,
|
2022-12-02 04:39:12 +00:00
|
|
|
eth/trie/trie_defs,
|
2022-01-10 09:04:06 +00:00
|
|
|
unittest2,
|
2022-09-26 16:14:12 +00:00
|
|
|
stew/[results, byteutils]
|
2018-09-07 19:44:17 +00:00
|
|
|
|
2019-03-18 01:55:02 +00:00
|
|
|
type
|
|
|
|
Tester = object
|
|
|
|
name: string
|
|
|
|
header: BlockHeader
|
|
|
|
pre: JsonNode
|
|
|
|
tx: Transaction
|
2023-01-10 17:02:21 +00:00
|
|
|
expectedHash: Hash256
|
|
|
|
expectedLogs: Hash256
|
|
|
|
chainConfig: ChainConfig
|
2019-03-18 01:55:02 +00:00
|
|
|
debugMode: bool
|
2019-08-19 14:12:32 +00:00
|
|
|
trace: bool
|
2019-03-21 09:01:26 +00:00
|
|
|
index: int
|
2023-03-17 13:20:52 +00:00
|
|
|
fork: string
|
2019-02-28 08:22:07 +00:00
|
|
|
|
2019-03-18 14:18:04 +00:00
|
|
|
proc toBytes(x: string): seq[byte] =
|
|
|
|
result = newSeq[byte](x.len)
|
|
|
|
for i in 0..<x.len: result[i] = x[i].byte
|
|
|
|
|
2022-01-18 16:19:32 +00:00
|
|
|
method getAncestorHash*(vmState: BaseVMState; blockNumber: BlockNumber): Hash256 {.gcsafe.} =
|
2019-03-18 14:18:04 +00:00
|
|
|
if blockNumber >= vmState.blockNumber:
|
|
|
|
return
|
|
|
|
elif blockNumber < 0:
|
|
|
|
return
|
|
|
|
elif blockNumber < vmState.blockNumber - 256:
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
return keccakHash(toBytes($blockNumber))
|
|
|
|
|
2023-03-17 13:20:52 +00:00
|
|
|
proc dumpDebugData(tester: Tester, vmState: BaseVMState, gasUsed: GasInt, success: bool) =
|
2019-08-19 14:12:32 +00:00
|
|
|
let tracingResult = if tester.trace: vmState.getTracingResult() else: %[]
|
2019-03-18 01:55:02 +00:00
|
|
|
let debugData = %{
|
|
|
|
"gasUsed": %gasUsed,
|
2019-08-19 14:12:32 +00:00
|
|
|
"structLogs": tracingResult,
|
2023-03-17 13:20:52 +00:00
|
|
|
"accounts": vmState.dumpAccounts()
|
2019-03-18 01:55:02 +00:00
|
|
|
}
|
2019-03-21 09:01:26 +00:00
|
|
|
let status = if success: "_success" else: "_failed"
|
2023-03-17 13:20:52 +00:00
|
|
|
writeFile(tester.name & "_" & tester.fork & "_" & $tester.index & status & ".json", debugData.pretty())
|
2019-03-18 01:55:02 +00:00
|
|
|
|
|
|
|
proc testFixtureIndexes(tester: Tester, testStatusIMPL: var TestStatus) =
|
2022-12-02 04:39:12 +00:00
|
|
|
let
|
2023-01-10 17:02:21 +00:00
|
|
|
com = CommonRef.new(newMemoryDB(), tester.chainConfig, getConfiguration().pruning)
|
2022-12-02 04:39:12 +00:00
|
|
|
parent = BlockHeader(stateRoot: emptyRlpHash)
|
2022-09-26 16:14:12 +00:00
|
|
|
|
|
|
|
let vmState = BaseVMState.new(
|
|
|
|
parent = parent,
|
2022-01-18 16:19:32 +00:00
|
|
|
header = tester.header,
|
2022-12-02 04:39:12 +00:00
|
|
|
com = com,
|
2022-01-18 16:19:32 +00:00
|
|
|
tracerFlags = (if tester.trace: {TracerFlags.EnableTracing} else: {}),
|
2022-12-02 04:39:12 +00:00
|
|
|
)
|
2019-11-28 10:02:11 +00:00
|
|
|
|
2019-03-21 09:01:26 +00:00
|
|
|
var gasUsed: GasInt
|
|
|
|
let sender = tester.tx.getSender()
|
2023-02-16 11:40:07 +00:00
|
|
|
let fork = com.toEVMFork(tester.header.forkDeterminationInfoForHeader)
|
2019-03-21 09:01:26 +00:00
|
|
|
|
2018-09-19 16:46:14 +00:00
|
|
|
vmState.mutateStateDB:
|
2019-03-18 01:55:02 +00:00
|
|
|
setupStateDB(tester.pre, db)
|
2018-09-19 16:46:14 +00:00
|
|
|
|
2020-06-01 04:49:56 +00:00
|
|
|
# this is an important step when using accounts_cache
|
|
|
|
# it will affect the account storage's location
|
|
|
|
# during the next call to `getComittedStorage`
|
|
|
|
db.persist()
|
2019-11-19 06:12:13 +00:00
|
|
|
|
2018-09-19 16:46:14 +00:00
|
|
|
defer:
|
2023-01-10 17:02:21 +00:00
|
|
|
let obtainedHash = vmState.readOnlyStateDB.rootHash
|
2019-03-18 01:55:02 +00:00
|
|
|
check obtainedHash == tester.expectedHash
|
2019-02-28 08:22:07 +00:00
|
|
|
let logEntries = vmState.getAndClearLogEntries()
|
2023-03-20 11:51:09 +00:00
|
|
|
let actualLogsHash = rlpHash(logEntries)
|
2023-01-10 17:02:21 +00:00
|
|
|
check(tester.expectedLogs == actualLogsHash)
|
2019-03-21 09:01:26 +00:00
|
|
|
if tester.debugMode:
|
2023-01-10 17:02:21 +00:00
|
|
|
let success = tester.expectedLogs == actualLogsHash and obtainedHash == tester.expectedHash
|
2023-03-17 13:20:52 +00:00
|
|
|
tester.dumpDebugData(vmState, gasUsed, success)
|
2018-09-19 16:46:14 +00:00
|
|
|
|
2022-01-10 09:04:06 +00:00
|
|
|
let rc = vmState.processTransaction(
|
2023-01-10 17:02:21 +00:00
|
|
|
tester.tx, sender, tester.header, fork)
|
2022-04-08 04:54:11 +00:00
|
|
|
if rc.isOk:
|
2022-01-10 09:04:06 +00:00
|
|
|
gasUsed = rc.value
|
2018-09-18 00:35:41 +00:00
|
|
|
|
2020-01-10 11:18:36 +00:00
|
|
|
let miner = tester.header.coinbase
|
2023-01-09 14:39:36 +00:00
|
|
|
coinbaseStateClearing(vmState, miner, fork)
|
2020-06-01 04:49:56 +00:00
|
|
|
|
2019-03-18 03:05:24 +00:00
|
|
|
proc testFixture(fixtures: JsonNode, testStatusIMPL: var TestStatus,
|
2023-01-10 17:02:21 +00:00
|
|
|
trace = false, debugMode = false) =
|
2019-03-18 01:55:02 +00:00
|
|
|
var tester: Tester
|
2018-09-19 16:46:14 +00:00
|
|
|
var fixture: JsonNode
|
|
|
|
for label, child in fixtures:
|
|
|
|
fixture = child
|
2019-03-18 01:55:02 +00:00
|
|
|
tester.name = label
|
2018-09-19 16:46:14 +00:00
|
|
|
break
|
2018-09-07 19:44:17 +00:00
|
|
|
|
2023-01-10 17:02:21 +00:00
|
|
|
tester.pre = fixture["pre"]
|
|
|
|
tester.header = parseHeader(fixture["env"])
|
2019-08-19 14:12:32 +00:00
|
|
|
tester.trace = trace
|
2019-03-18 01:55:02 +00:00
|
|
|
tester.debugMode = debugMode
|
2023-01-10 17:02:21 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
post = fixture["post"]
|
|
|
|
txData = fixture["transaction"]
|
|
|
|
conf = getConfiguration()
|
|
|
|
|
|
|
|
template prepareFork(forkName: string) =
|
|
|
|
try:
|
|
|
|
tester.chainConfig = getChainConfig(forkName)
|
|
|
|
except ValueError as ex:
|
|
|
|
debugEcho ex.msg
|
|
|
|
return
|
|
|
|
|
|
|
|
template runSubTest(subTest: JsonNode) =
|
|
|
|
tester.expectedHash = Hash256.fromJson(subTest["hash"])
|
|
|
|
tester.expectedLogs = Hash256.fromJson(subTest["logs"])
|
|
|
|
tester.tx = parseTx(txData, subTest["indexes"])
|
|
|
|
tester.testFixtureIndexes(testStatusIMPL)
|
|
|
|
|
|
|
|
if conf.fork.len > 0:
|
|
|
|
if not post.hasKey(conf.fork):
|
|
|
|
debugEcho "selected fork not available: " & conf.fork
|
|
|
|
return
|
|
|
|
|
2023-03-17 13:20:52 +00:00
|
|
|
tester.fork = conf.fork
|
2023-01-10 17:02:21 +00:00
|
|
|
let forkData = post[conf.fork]
|
|
|
|
prepareFork(conf.fork)
|
|
|
|
if conf.index.isNone:
|
|
|
|
for subTest in forkData:
|
|
|
|
runSubTest(subTest)
|
2023-03-17 13:20:52 +00:00
|
|
|
inc tester.index
|
2020-02-19 14:26:16 +00:00
|
|
|
else:
|
2023-03-17 13:20:52 +00:00
|
|
|
tester.index = conf.index.get()
|
|
|
|
if tester.index > forkData.len or tester.index < 0:
|
2023-01-10 17:02:21 +00:00
|
|
|
debugEcho "selected index out of range(0-$1), requested $2" %
|
2023-03-17 13:20:52 +00:00
|
|
|
[$forkData.len, $tester.index]
|
2023-01-10 17:02:21 +00:00
|
|
|
return
|
|
|
|
|
2023-03-17 13:20:52 +00:00
|
|
|
let subTest = forkData[tester.index]
|
2023-01-10 17:02:21 +00:00
|
|
|
runSubTest(subTest)
|
|
|
|
else:
|
|
|
|
for forkName, forkData in post:
|
|
|
|
prepareFork(forkName)
|
2023-03-17 13:20:52 +00:00
|
|
|
tester.fork = forkName
|
|
|
|
tester.index = 0
|
2023-01-10 17:02:21 +00:00
|
|
|
for subTest in forkData:
|
|
|
|
runSubTest(subTest)
|
2023-03-17 13:20:52 +00:00
|
|
|
inc tester.index
|
2019-03-18 03:05:24 +00:00
|
|
|
|
2019-09-21 05:45:23 +00:00
|
|
|
proc generalStateJsonMain*(debugMode = false) =
|
2021-01-06 10:02:19 +00:00
|
|
|
const
|
|
|
|
legacyFolder = "eth_tests" / "LegacyTests" / "Constantinople" / "GeneralStateTests"
|
|
|
|
newFolder = "eth_tests" / "GeneralStateTests"
|
|
|
|
|
2021-01-14 14:33:18 +00:00
|
|
|
let config = getConfiguration()
|
|
|
|
if config.testSubject == "" or not debugMode:
|
2019-03-18 01:55:02 +00:00
|
|
|
# run all test fixtures
|
2021-01-14 14:33:18 +00:00
|
|
|
if config.legacy:
|
|
|
|
suite "generalstate json tests":
|
2023-03-17 13:20:52 +00:00
|
|
|
jsonTest(legacyFolder, "GeneralStateTests", testFixture, skipGSTTests)
|
2021-01-14 14:33:18 +00:00
|
|
|
else:
|
|
|
|
suite "new generalstate json tests":
|
|
|
|
jsonTest(newFolder, "newGeneralStateTests", testFixture, skipNewGSTTests)
|
2019-03-18 01:55:02 +00:00
|
|
|
else:
|
|
|
|
# execute single test in debug mode
|
2019-03-18 03:05:24 +00:00
|
|
|
if config.testSubject.len == 0:
|
|
|
|
echo "missing test subject"
|
|
|
|
quit(QuitFailure)
|
|
|
|
|
2021-01-06 10:02:19 +00:00
|
|
|
let folder = if config.legacy: legacyFolder else: newFolder
|
2020-02-19 14:26:16 +00:00
|
|
|
let path = "tests" / "fixtures" / folder
|
2019-03-18 03:05:24 +00:00
|
|
|
let n = json.parseFile(path / config.testSubject)
|
2019-03-18 01:55:02 +00:00
|
|
|
var testStatusIMPL: TestStatus
|
2023-01-10 17:02:21 +00:00
|
|
|
testFixture(n, testStatusIMPL, config.trace, true)
|
2019-03-18 03:05:24 +00:00
|
|
|
|
|
|
|
when isMainModule:
|
|
|
|
var message: string
|
|
|
|
|
2023-05-10 15:40:48 +00:00
|
|
|
let start = getTime()
|
|
|
|
|
2019-03-18 03:05:24 +00:00
|
|
|
## Processing command line arguments
|
|
|
|
if processArguments(message) != Success:
|
|
|
|
echo message
|
|
|
|
quit(QuitFailure)
|
|
|
|
else:
|
|
|
|
if len(message) > 0:
|
|
|
|
echo message
|
|
|
|
quit(QuitSuccess)
|
2021-04-24 04:12:10 +00:00
|
|
|
|
2019-09-21 05:45:23 +00:00
|
|
|
generalStateJsonMain(true)
|
2023-05-10 15:40:48 +00:00
|
|
|
let elpd = getTime() - start
|
|
|
|
echo "TIME: ", elpd
|