nimbus-eth1/tests/test_generalstate_json.nim

216 lines
6.5 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.
import
2023-06-12 04:29:03 +00:00
std/[strutils, tables, json, os, sets, options],
./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,
../nimbus/utils/[utils, debug],
../nimbus/evm/tracer/legacy_tracer,
../tools/common/helpers as chp,
../tools/evmstate/helpers,
../tools/common/state_clearing,
2022-12-02 04:39:12 +00:00
eth/trie/trie_defs,
unittest2,
stew/[results, byteutils]
2019-03-18 01:55:02 +00:00
type
Tester = object
name: string
header: BlockHeader
pre: JsonNode
tx: Transaction
expectedHash: Hash256
expectedLogs: Hash256
chainConfig: ChainConfig
2019-03-18 01:55:02 +00:00
debugMode: bool
trace: bool
2019-03-21 09:01:26 +00:00
index: int
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
Redesign of BaseVMState descriptor (#923) * Redesign of BaseVMState descriptor why: BaseVMState provides an environment for executing transactions. The current descriptor also provides data that cannot generally be known within the execution environment, e.g. the total gasUsed which is available not before after all transactions have finished. Also, the BaseVMState constructor has been replaced by a constructor that does not need pre-initialised input of the account database. also: Previous constructor and some fields are provided with a deprecated annotation (producing a lot of noise.) * Replace legacy directives in production sources * Replace legacy directives in unit test sources * fix CI (missing premix update) * Remove legacy directives * chase CI problem * rebased * Re-introduce 'AccountsCache' constructor optimisation for 'BaseVmState' re-initialisation why: Constructing a new 'AccountsCache' descriptor can be avoided sometimes when the current state root is properly positioned already. Such a feature existed already as the update function 'initStateDB()' for the 'BaseChanDB' where the accounts cache was linked into this desctiptor. The function 'initStateDB()' was removed and re-implemented into the 'BaseVmState' constructor without optimisation. The old version was of restricted use as a wrong accounts cache state would unconditionally throw an exception rather than conceptually ask for a remedy. The optimised 'BaseVmState' re-initialisation has been implemented for the 'persistBlocks()' function. also: moved some test helpers to 'test/replay' folder * Remove unused & undocumented fields from Chain descriptor why: Reduces attack surface in general & improves reading the code.
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))
proc dumpDebugData(tester: Tester, vmState: BaseVMState, gasUsed: GasInt, success: bool) =
let tracerInst = LegacyTracer(vmState.tracer)
let tracingResult = if tester.trace: tracerInst.getTracingResult() else: %[]
2019-03-18 01:55:02 +00:00
let debugData = %{
"gasUsed": %gasUsed,
"structLogs": tracingResult,
"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"
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
Unified database frontend integration (#1670) * Nimbus folder environment update details: * Integrated `CoreDbRef` for the sources in the `nimbus` sub-folder. * The `nimbus` program does not compile yet as it needs the updates in the parallel `stateless` sub-folder. * Stateless environment update details: * Integrated `CoreDbRef` for the sources in the `stateless` sub-folder. * The `nimbus` program compiles now. * Premix environment update details: * Integrated `CoreDbRef` for the sources in the `premix` sub-folder. * Fluffy environment update details: * Integrated `CoreDbRef` for the sources in the `fluffy` sub-folder. * Tools environment update details: * Integrated `CoreDbRef` for the sources in the `tools` sub-folder. * Nodocker environment update details: * Integrated `CoreDbRef` for the sources in the `hive_integration/nodocker` sub-folder. * Tests environment update details: * Integrated `CoreDbRef` for the sources in the `tests` sub-folder. * The unit tests compile and run cleanly now. * Generalise `CoreDbRef` to any `select_backend` supported database why: Generalisation was just missed due to overcoming some compiler oddity which was tied to rocksdb for testing. * Suppress compiler warning for `newChainDB()` why: Warning was added to this function which must be wrapped so that any `CatchableError` is re-raised as `Defect`. * Split off persistent `CoreDbRef` constructor into separate file why: This allows to compile a memory only database version without linking the backend library. * Use memory `CoreDbRef` database by default detail: Persistent DB constructor needs to import `db/core_db/persistent why: Most tests use memory DB anyway. This avoids linking `-lrocksdb` or any other backend by default. * fix `toLegacyBackend()` availability check why: got garbled after memory/persistent split. * Clarify raw access to MPT for snap sync handler why: Logically, `kvt` is not the raw access for the hexary trie (although this holds for the legacy database)
2023-08-04 11:10:09 +00:00
com = CommonRef.new(newCoreDbRef LegacyDbMemory, tester.chainConfig, getConfiguration().pruning)
2022-12-02 04:39:12 +00:00
parent = BlockHeader(stateRoot: emptyRlpHash)
tracer = if tester.trace:
newLegacyTracer({})
else:
LegacyTracer(nil)
let vmState = BaseVMState.new(
parent = parent,
header = tester.header,
com = com,
tracer = tracer,
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()
let fork = com.toEVMFork(tester.header.forkDeterminationInfoForHeader)
2019-03-21 09:01:26 +00:00
vmState.mutateStateDB:
2019-03-18 01:55:02 +00:00
setupStateDB(tester.pre, db)
# 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
defer:
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()
let actualLogsHash = rlpHash(logEntries)
check(tester.expectedLogs == actualLogsHash)
2019-03-21 09:01:26 +00:00
if tester.debugMode:
let success = tester.expectedLogs == actualLogsHash and obtainedHash == tester.expectedHash
tester.dumpDebugData(vmState, gasUsed, success)
let rc = vmState.processTransaction(
tester.tx, sender, tester.header, fork)
2022-04-08 04:54:11 +00:00
if rc.isOk:
gasUsed = rc.value
2018-09-18 00:35:41 +00:00
2020-01-10 11:18:36 +00:00
let miner = tester.header.coinbase
coinbaseStateClearing(vmState, miner, fork)
2019-03-18 03:05:24 +00:00
proc testFixture(fixtures: JsonNode, testStatusIMPL: var TestStatus,
trace = false, debugMode = false) =
2019-03-18 01:55:02 +00:00
var tester: Tester
var fixture: JsonNode
for label, child in fixtures:
fixture = child
2019-03-18 01:55:02 +00:00
tester.name = label
break
tester.pre = fixture["pre"]
tester.header = parseHeader(fixture["env"])
tester.trace = trace
2019-03-18 01:55:02 +00:00
tester.debugMode = debugMode
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
tester.fork = conf.fork
let forkData = post[conf.fork]
prepareFork(conf.fork)
if conf.index.isNone:
for subTest in forkData:
runSubTest(subTest)
inc tester.index
else:
tester.index = conf.index.get()
if tester.index > forkData.len or tester.index < 0:
debugEcho "selected index out of range(0-$1), requested $2" %
[$forkData.len, $tester.index]
return
let subTest = forkData[tester.index]
runSubTest(subTest)
else:
for forkName, forkData in post:
prepareFork(forkName)
tester.fork = forkName
tester.index = 0
for subTest in forkData:
runSubTest(subTest)
inc tester.index
2019-03-18 03:05:24 +00:00
2019-09-21 05:45:23 +00:00
proc generalStateJsonMain*(debugMode = false) =
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":
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)
let folder = if config.legacy: legacyFolder else: newFolder
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
testFixture(n, testStatusIMPL, config.trace, true)
2019-03-18 03:05:24 +00:00
when isMainModule:
import std/times
2019-03-18 03:05:24 +00:00
var message: string
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)
2019-09-21 05:45:23 +00:00
generalStateJsonMain(true)
let elpd = getTime() - start
echo "TIME: ", elpd