2022-12-10 12:53:24 +00:00
|
|
|
# Nimbus
|
2024-02-24 02:38:50 +00:00
|
|
|
# Copyright (c) 2022-2024 Status Research & Development GmbH
|
2022-12-10 12:53:24 +00:00
|
|
|
# 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.
|
|
|
|
|
2022-10-26 15:46:13 +00:00
|
|
|
import
|
2023-08-02 10:17:40 +00:00
|
|
|
std/[json, strutils, sets, tables, options, streams],
|
2022-11-17 08:27:26 +00:00
|
|
|
chronicles,
|
2024-10-08 05:52:32 +00:00
|
|
|
eth/common/keys,
|
2024-10-04 14:34:31 +00:00
|
|
|
eth/common/transaction_utils,
|
2024-05-30 12:54:03 +00:00
|
|
|
stew/byteutils,
|
|
|
|
results,
|
2022-10-26 15:46:13 +00:00
|
|
|
stint,
|
2022-12-02 04:39:12 +00:00
|
|
|
eth/trie/[trie_defs],
|
2024-06-17 07:56:39 +00:00
|
|
|
../../nimbus/[evm/types, evm/state],
|
2023-12-12 19:12:56 +00:00
|
|
|
../../nimbus/db/ledger,
|
2022-10-26 15:46:13 +00:00
|
|
|
../../nimbus/transaction,
|
2022-12-02 04:39:12 +00:00
|
|
|
../../nimbus/core/executor,
|
|
|
|
../../nimbus/common/common,
|
2023-08-02 10:17:40 +00:00
|
|
|
../../nimbus/evm/tracer/json_tracer,
|
2023-09-21 15:21:21 +00:00
|
|
|
../../nimbus/core/eip4844,
|
2023-10-19 06:02:12 +00:00
|
|
|
../../nimbus/utils/state_dump,
|
2023-01-10 17:02:21 +00:00
|
|
|
../common/helpers as chp,
|
2023-01-09 14:39:36 +00:00
|
|
|
"."/[config, helpers],
|
|
|
|
../common/state_clearing
|
2022-10-26 15:46:13 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
StateContext = object
|
|
|
|
name: string
|
2024-10-08 05:52:32 +00:00
|
|
|
parent: Header
|
|
|
|
header: Header
|
2022-10-26 15:46:13 +00:00
|
|
|
tx: Transaction
|
2024-10-08 05:52:32 +00:00
|
|
|
expectedHash: Hash32
|
|
|
|
expectedLogs: Hash32
|
2023-02-23 02:15:58 +00:00
|
|
|
forkStr: string
|
2023-01-10 17:02:21 +00:00
|
|
|
chainConfig: ChainConfig
|
2022-10-26 15:46:13 +00:00
|
|
|
index: int
|
|
|
|
tracerFlags: set[TracerFlags]
|
|
|
|
error: string
|
2023-09-21 15:21:21 +00:00
|
|
|
trustedSetupLoaded: bool
|
2022-10-26 15:46:13 +00:00
|
|
|
|
|
|
|
StateResult = object
|
|
|
|
name : string
|
|
|
|
pass : bool
|
2024-10-08 05:52:32 +00:00
|
|
|
root : Hash32
|
2022-10-26 15:46:13 +00:00
|
|
|
fork : string
|
|
|
|
error: string
|
|
|
|
state: StateDump
|
|
|
|
|
2022-12-15 03:46:28 +00:00
|
|
|
TestVMState = ref object of BaseVMState
|
|
|
|
|
2022-10-26 15:46:13 +00:00
|
|
|
proc extractNameAndFixture(ctx: var StateContext, n: JsonNode): JsonNode =
|
|
|
|
for label, child in n:
|
|
|
|
result = child
|
|
|
|
ctx.name = label
|
|
|
|
return
|
|
|
|
doAssert(false, "unreachable")
|
|
|
|
|
|
|
|
proc toBytes(x: string): seq[byte] =
|
|
|
|
result = newSeq[byte](x.len)
|
|
|
|
for i in 0..<x.len: result[i] = x[i].byte
|
|
|
|
|
2024-10-08 05:52:32 +00:00
|
|
|
method getAncestorHash(vmState: TestVMState; blockNumber: BlockNumber): Hash32 =
|
|
|
|
keccak256(toBytes($blockNumber))
|
2022-10-26 15:46:13 +00:00
|
|
|
|
2024-10-08 05:52:32 +00:00
|
|
|
proc verifyResult(ctx: var StateContext, vmState: BaseVMState, obtainedHash: Hash32) =
|
2024-10-04 14:34:31 +00:00
|
|
|
ctx.error = ""
|
2022-10-26 15:46:13 +00:00
|
|
|
if obtainedHash != ctx.expectedHash:
|
|
|
|
ctx.error = "post state root mismatch: got $1, want $2" %
|
2022-12-14 09:42:55 +00:00
|
|
|
[($obtainedHash).toLowerAscii, $ctx.expectedHash]
|
2022-10-26 15:46:13 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
let logEntries = vmState.getAndClearLogEntries()
|
|
|
|
let actualLogsHash = rlpHash(logEntries)
|
|
|
|
if actualLogsHash != ctx.expectedLogs:
|
|
|
|
ctx.error = "post state log hash mismatch: got $1, want $2" %
|
2022-12-14 09:42:55 +00:00
|
|
|
[($actualLogsHash).toLowerAscii, $ctx.expectedLogs]
|
2022-10-26 15:46:13 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
proc writeResultToStdout(stateRes: seq[StateResult]) =
|
|
|
|
var n = newJArray()
|
|
|
|
for res in stateRes:
|
|
|
|
let z = %{
|
|
|
|
"name" : %(res.name),
|
|
|
|
"pass" : %(res.pass),
|
2022-12-14 09:42:55 +00:00
|
|
|
"stateRoot" : %(res.root),
|
2022-10-26 15:46:13 +00:00
|
|
|
"fork" : %(res.fork),
|
|
|
|
"error": %(res.error)
|
|
|
|
}
|
|
|
|
if res.state.isNil.not:
|
|
|
|
z["state"] = %(res.state)
|
|
|
|
n.add(z)
|
|
|
|
|
|
|
|
stdout.write(n.pretty)
|
|
|
|
stdout.write("\n")
|
|
|
|
|
2024-10-08 05:52:32 +00:00
|
|
|
proc writeRootHashToStderr(stateRoot: Hash32) =
|
2022-12-14 09:42:55 +00:00
|
|
|
let stateRoot = %{
|
2024-07-25 12:23:53 +00:00
|
|
|
"stateRoot": %(stateRoot)
|
2022-12-14 09:42:55 +00:00
|
|
|
}
|
2023-08-02 10:17:40 +00:00
|
|
|
stderr.writeLine($stateRoot)
|
2022-10-26 15:46:13 +00:00
|
|
|
|
|
|
|
proc runExecution(ctx: var StateContext, conf: StateConf, pre: JsonNode): StateResult =
|
|
|
|
let
|
2024-05-20 10:17:51 +00:00
|
|
|
com = CommonRef.new(newCoreDbRef DefaultDbMemory, ctx.chainConfig)
|
2023-08-02 10:17:40 +00:00
|
|
|
stream = newFileStream(stderr)
|
|
|
|
tracer = if conf.jsonEnabled:
|
2024-02-24 02:38:50 +00:00
|
|
|
newJsonTracer(stream, ctx.tracerFlags, conf.pretty)
|
2023-08-02 10:17:40 +00:00
|
|
|
else:
|
|
|
|
JsonTracer(nil)
|
2022-10-26 15:46:13 +00:00
|
|
|
|
2023-09-21 15:21:21 +00:00
|
|
|
if com.isCancunOrLater(ctx.header.timestamp):
|
|
|
|
if not ctx.trustedSetupLoaded:
|
|
|
|
let res = loadKzgTrustedSetup()
|
|
|
|
if res.isErr:
|
|
|
|
echo "FATAL: ", res.error
|
2023-10-19 06:02:12 +00:00
|
|
|
quit(QuitFailure)
|
2023-09-21 15:21:21 +00:00
|
|
|
ctx.trustedSetupLoaded = true
|
2023-10-19 06:02:12 +00:00
|
|
|
|
2022-12-15 03:46:28 +00:00
|
|
|
let vmState = TestVMState()
|
|
|
|
vmState.init(
|
2023-09-21 15:21:21 +00:00
|
|
|
parent = ctx.parent,
|
2023-08-02 10:17:40 +00:00
|
|
|
header = ctx.header,
|
|
|
|
com = com,
|
|
|
|
tracer = tracer)
|
2022-10-26 15:46:13 +00:00
|
|
|
|
|
|
|
var gasUsed: GasInt
|
2024-10-04 14:34:31 +00:00
|
|
|
let sender = ctx.tx.recoverSender().expect("valid signature")
|
2022-10-26 15:46:13 +00:00
|
|
|
|
|
|
|
vmState.mutateStateDB:
|
|
|
|
setupStateDB(pre, db)
|
2024-06-02 19:21:29 +00:00
|
|
|
db.persist(clearEmptyAccount = false) # settle accounts storage
|
2022-10-26 15:46:13 +00:00
|
|
|
|
|
|
|
defer:
|
2024-07-25 12:23:53 +00:00
|
|
|
let stateRoot = vmState.readOnlyStateDB.rootHash
|
|
|
|
ctx.verifyResult(vmState, stateRoot)
|
2022-10-26 15:46:13 +00:00
|
|
|
result = StateResult(
|
|
|
|
name : ctx.name,
|
|
|
|
pass : ctx.error.len == 0,
|
2024-07-25 12:23:53 +00:00
|
|
|
root : stateRoot,
|
2023-02-23 02:15:58 +00:00
|
|
|
fork : ctx.forkStr,
|
2022-10-26 15:46:13 +00:00
|
|
|
error: ctx.error
|
|
|
|
)
|
|
|
|
if conf.dumpEnabled:
|
2023-10-19 06:02:12 +00:00
|
|
|
result.state = dumpState(vmState.stateDB)
|
2022-10-26 15:46:13 +00:00
|
|
|
if conf.jsonEnabled:
|
2024-07-25 12:23:53 +00:00
|
|
|
writeRootHashToStderr(stateRoot)
|
2022-10-26 15:46:13 +00:00
|
|
|
|
2023-08-30 13:38:32 +00:00
|
|
|
try:
|
|
|
|
let rc = vmState.processTransaction(
|
2024-07-04 13:48:36 +00:00
|
|
|
ctx.tx, sender, ctx.header)
|
2023-08-30 13:38:32 +00:00
|
|
|
if rc.isOk:
|
|
|
|
gasUsed = rc.value
|
2023-09-21 15:21:21 +00:00
|
|
|
|
2023-08-30 13:38:32 +00:00
|
|
|
let miner = ctx.header.coinbase
|
2024-07-04 13:48:36 +00:00
|
|
|
coinbaseStateClearing(vmState, miner)
|
2023-08-30 13:38:32 +00:00
|
|
|
except CatchableError as ex:
|
|
|
|
echo "FATAL: ", ex.msg
|
|
|
|
quit(QuitFailure)
|
|
|
|
except AssertionDefect as ex:
|
|
|
|
echo "FATAL: ", ex.msg
|
|
|
|
quit(QuitFailure)
|
2022-10-26 15:46:13 +00:00
|
|
|
|
2024-02-24 02:38:50 +00:00
|
|
|
proc toTracerFlags(conf: StateConf): set[TracerFlags] =
|
2022-10-26 15:46:13 +00:00
|
|
|
result = {
|
2023-08-02 10:17:40 +00:00
|
|
|
TracerFlags.DisableStateDiff
|
2022-10-26 15:46:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if conf.disableMemory : result.incl TracerFlags.DisableMemory
|
2024-02-24 02:38:50 +00:00
|
|
|
if conf.disableStack : result.incl TracerFlags.DisableStack
|
2022-10-26 15:46:13 +00:00
|
|
|
if conf.disableReturnData: result.incl TracerFlags.DisableReturnData
|
|
|
|
if conf.disableStorage : result.incl TracerFlags.DisableStorage
|
|
|
|
|
|
|
|
template hasError(ctx: StateContext): bool =
|
|
|
|
ctx.error.len > 0
|
|
|
|
|
|
|
|
proc prepareAndRun(ctx: var StateContext, conf: StateConf): bool =
|
|
|
|
let
|
|
|
|
fixture = json.parseFile(conf.inputFile)
|
|
|
|
n = ctx.extractNameAndFixture(fixture)
|
|
|
|
txData = n["transaction"]
|
|
|
|
post = n["post"]
|
|
|
|
pre = n["pre"]
|
|
|
|
|
2023-09-21 15:21:21 +00:00
|
|
|
ctx.parent = parseParentHeader(n["env"])
|
2022-10-26 15:46:13 +00:00
|
|
|
ctx.header = parseHeader(n["env"])
|
|
|
|
|
|
|
|
if conf.debugEnabled or conf.jsonEnabled:
|
|
|
|
ctx.tracerFlags = toTracerFlags(conf)
|
|
|
|
|
|
|
|
var
|
|
|
|
stateRes = newSeqOfCap[StateResult](post.len)
|
|
|
|
index = 1
|
|
|
|
hasError = false
|
|
|
|
|
|
|
|
template prepareFork(forkName: string) =
|
2023-01-10 17:02:21 +00:00
|
|
|
try:
|
2023-02-23 02:15:58 +00:00
|
|
|
ctx.forkStr = forkName
|
2023-01-10 17:02:21 +00:00
|
|
|
ctx.chainConfig = getChainConfig(forkName)
|
|
|
|
except ValueError as ex:
|
|
|
|
debugEcho ex.msg
|
|
|
|
return false
|
2022-10-26 15:46:13 +00:00
|
|
|
ctx.index = index
|
|
|
|
inc index
|
|
|
|
|
|
|
|
template runSubTest(subTest: JsonNode) =
|
2024-10-08 05:52:32 +00:00
|
|
|
ctx.expectedHash = Hash32.fromJson(subTest["hash"])
|
|
|
|
ctx.expectedLogs = Hash32.fromJson(subTest["logs"])
|
2022-10-26 15:46:13 +00:00
|
|
|
ctx.tx = parseTx(txData, subTest["indexes"])
|
|
|
|
let res = ctx.runExecution(conf, pre)
|
|
|
|
stateRes.add res
|
|
|
|
hasError = hasError or ctx.hasError
|
|
|
|
|
|
|
|
if conf.fork.len > 0:
|
|
|
|
if not post.hasKey(conf.fork):
|
|
|
|
stdout.writeLine("selected fork not available: " & conf.fork)
|
|
|
|
return false
|
|
|
|
|
|
|
|
let forkData = post[conf.fork]
|
|
|
|
prepareFork(conf.fork)
|
|
|
|
if conf.index.isNone:
|
|
|
|
for subTest in forkData:
|
|
|
|
runSubTest(subTest)
|
|
|
|
else:
|
|
|
|
let index = conf.index.get()
|
|
|
|
if index > forkData.len or index < 0:
|
|
|
|
stdout.writeLine("selected index out of range(0-$1), requested $2" %
|
|
|
|
[$forkData.len, $index])
|
|
|
|
return false
|
|
|
|
|
|
|
|
let subTest = forkData[index]
|
|
|
|
runSubTest(subTest)
|
|
|
|
else:
|
|
|
|
for forkName, forkData in post:
|
|
|
|
prepareFork(forkName)
|
|
|
|
for subTest in forkData:
|
|
|
|
runSubTest(subTest)
|
|
|
|
|
|
|
|
writeResultToStdout(stateRes)
|
|
|
|
not hasError
|
|
|
|
|
2022-11-17 08:27:26 +00:00
|
|
|
when defined(chronicles_runtime_filtering):
|
2023-01-10 17:02:21 +00:00
|
|
|
type Lev = chronicles.LogLevel
|
|
|
|
proc toLogLevel(v: int): Lev =
|
2022-11-17 08:27:26 +00:00
|
|
|
case v
|
2023-01-10 17:02:21 +00:00
|
|
|
of 1: Lev.ERROR
|
|
|
|
of 2: Lev.WARN
|
|
|
|
of 3: Lev.INFO
|
|
|
|
of 4: Lev.DEBUG
|
|
|
|
of 5: Lev.TRACE
|
|
|
|
else: Lev.NONE
|
2022-11-17 08:27:26 +00:00
|
|
|
|
|
|
|
proc setVerbosity(v: int) =
|
|
|
|
let level = v.toLogLevel
|
|
|
|
setLogLevel(level)
|
|
|
|
|
2022-10-26 15:46:13 +00:00
|
|
|
proc main() =
|
|
|
|
let conf = StateConf.init()
|
2022-11-17 08:27:26 +00:00
|
|
|
when defined(chronicles_runtime_filtering):
|
|
|
|
setVerbosity(conf.verbosity)
|
2022-10-26 15:46:13 +00:00
|
|
|
var ctx: StateContext
|
|
|
|
if not ctx.prepareAndRun(conf):
|
|
|
|
quit(QuitFailure)
|
|
|
|
|
|
|
|
main()
|