2023-07-09 02:16:22 +00:00
|
|
|
# Nimbus
|
2024-01-13 01:41:57 +00:00
|
|
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
2023-07-09 02:16:22 +00:00
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
# at your option.
|
|
|
|
# This file may not be copied, modified, or distributed except according to
|
|
|
|
# those terms.
|
|
|
|
|
|
|
|
import
|
2024-06-14 07:31:08 +00:00
|
|
|
std/[os, json, strutils, times, typetraits],
|
|
|
|
stew/byteutils,
|
2024-10-16 01:34:12 +00:00
|
|
|
eth/common/eth_types_rlp,
|
2023-09-24 23:53:33 +00:00
|
|
|
json_rpc/rpcclient,
|
2023-12-08 09:35:50 +00:00
|
|
|
web3/execution_types,
|
2023-07-09 02:16:22 +00:00
|
|
|
../sim_utils,
|
|
|
|
../../../tools/common/helpers as chp,
|
|
|
|
../../../tools/evmstate/helpers as ehp,
|
2023-08-27 01:23:45 +00:00
|
|
|
../../../nimbus/beacon/web3_eth_conv,
|
|
|
|
../../../nimbus/beacon/payload_conv,
|
2023-09-24 23:53:33 +00:00
|
|
|
../../../nimbus/core/eip4844,
|
2023-07-09 02:16:22 +00:00
|
|
|
../engine/engine_client,
|
2024-11-02 09:58:18 +00:00
|
|
|
../engine/types,
|
2023-08-27 01:23:45 +00:00
|
|
|
./test_env
|
2023-07-09 02:16:22 +00:00
|
|
|
|
|
|
|
const
|
|
|
|
baseFolder = "hive_integration/nodocker/pyspec"
|
2023-09-24 23:53:33 +00:00
|
|
|
supportedNetwork = [
|
|
|
|
"Merge",
|
|
|
|
"Shanghai",
|
|
|
|
"MergeToShanghaiAtTime15k",
|
|
|
|
"Cancun",
|
|
|
|
"ShanghaiToCancunAtTime15k",
|
|
|
|
]
|
|
|
|
|
|
|
|
type
|
|
|
|
Payload = object
|
2023-10-05 03:04:12 +00:00
|
|
|
badBlock: bool
|
2024-11-02 09:58:18 +00:00
|
|
|
payload: ExecutableData
|
2023-09-24 23:53:33 +00:00
|
|
|
|
2023-10-05 03:04:12 +00:00
|
|
|
proc getPayload(node: JsonNode): Payload =
|
|
|
|
try:
|
|
|
|
let
|
|
|
|
rlpBytes = hexToSeqByte(node.getStr)
|
|
|
|
blk = rlp.decode(rlpBytes, EthBlock)
|
|
|
|
Payload(
|
|
|
|
badBlock: false,
|
2024-11-02 09:58:18 +00:00
|
|
|
payload: ExecutableData(
|
|
|
|
basePayload: executionPayload(blk),
|
|
|
|
beaconRoot: blk.header.parentBeaconBlockRoot,
|
|
|
|
)
|
2023-10-05 03:04:12 +00:00
|
|
|
)
|
|
|
|
except RlpError:
|
|
|
|
Payload(
|
|
|
|
badBlock: true,
|
|
|
|
)
|
2023-07-09 02:16:22 +00:00
|
|
|
|
|
|
|
proc validatePostState(node: JsonNode, t: TestEnv): bool =
|
|
|
|
# check nonce, balance & storage of accounts in final block against fixture values
|
|
|
|
for account, genesisAccount in postState(node["postState"]):
|
|
|
|
# get nonce & balance from last block (end of test execution)
|
|
|
|
let nonceRes = t.rpcClient.nonceAt(account)
|
|
|
|
if nonceRes.isErr:
|
|
|
|
echo "unable to call nonce from account: " & account.toHex
|
|
|
|
echo nonceRes.error
|
|
|
|
return false
|
|
|
|
|
|
|
|
let balanceRes = t.rpcClient.balanceAt(account)
|
|
|
|
if balanceRes.isErr:
|
|
|
|
echo "unable to call balance from account: " & account.toHex
|
|
|
|
echo balanceRes.error
|
|
|
|
return false
|
|
|
|
|
|
|
|
# check final nonce & balance matches expected in fixture
|
|
|
|
if genesisAccount.nonce != nonceRes.value:
|
|
|
|
echo "nonce recieved from account 0x",
|
|
|
|
account.toHex,
|
|
|
|
" doesn't match expected ",
|
|
|
|
genesisAccount.nonce,
|
|
|
|
" got ",
|
|
|
|
nonceRes.value
|
|
|
|
return false
|
|
|
|
|
|
|
|
if genesisAccount.balance != balanceRes.value:
|
|
|
|
echo "balance recieved from account 0x",
|
|
|
|
account.toHex,
|
|
|
|
" doesn't match expected ",
|
|
|
|
genesisAccount.balance,
|
|
|
|
" got ",
|
|
|
|
balanceRes.value
|
|
|
|
return false
|
|
|
|
|
|
|
|
# check final storage
|
|
|
|
if genesisAccount.storage.len > 0:
|
|
|
|
for slot, val in genesisAccount.storage:
|
|
|
|
let sRes = t.rpcClient.storageAt(account, slot)
|
|
|
|
if sRes.isErr:
|
|
|
|
echo "unable to call storage from account: 0x",
|
|
|
|
account.toHex,
|
|
|
|
" at slot 0x",
|
|
|
|
slot.toHex
|
|
|
|
echo sRes.error
|
|
|
|
return false
|
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
if val.to(Bytes32) != sRes.value:
|
2023-07-09 02:16:22 +00:00
|
|
|
echo "storage recieved from account 0x",
|
|
|
|
account.toHex,
|
|
|
|
" at slot 0x",
|
|
|
|
slot.toHex,
|
|
|
|
" doesn't match expected 0x",
|
|
|
|
val.toHex,
|
|
|
|
" got 0x",
|
|
|
|
sRes.value.toHex
|
|
|
|
return false
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
proc runTest(node: JsonNode, network: string): TestStatus =
|
|
|
|
let conf = getChainConfig(network)
|
2024-09-04 09:54:54 +00:00
|
|
|
var env = setupELClient(conf, node)
|
2023-07-09 02:16:22 +00:00
|
|
|
|
|
|
|
let blks = node["blocks"]
|
2023-09-24 23:53:33 +00:00
|
|
|
var
|
2024-10-16 01:34:12 +00:00
|
|
|
latestValidHash = default(Hash32)
|
2023-09-24 23:53:33 +00:00
|
|
|
latestVersion: Version
|
|
|
|
|
2023-07-09 02:16:22 +00:00
|
|
|
result = TestStatus.OK
|
|
|
|
for blkNode in blks:
|
|
|
|
let expectedStatus = if "expectException" in blkNode:
|
|
|
|
PayloadExecutionStatus.invalid
|
|
|
|
else:
|
|
|
|
PayloadExecutionStatus.valid
|
2023-10-05 03:04:12 +00:00
|
|
|
let
|
|
|
|
badBlock = blkNode.hasKey("expectException")
|
|
|
|
payload = getPayload(blkNode["rlp"])
|
|
|
|
|
|
|
|
if badBlock == payload.badBlock and badBlock == true:
|
|
|
|
# It could be the rlp decoding succeed, but the actual
|
|
|
|
# block validation is failed in engine api
|
|
|
|
# So, we skip newPayload call only if decoding is also
|
|
|
|
# failed
|
|
|
|
break
|
|
|
|
|
2023-09-24 23:53:33 +00:00
|
|
|
latestVersion = payload.payload.version
|
2023-10-05 03:04:12 +00:00
|
|
|
|
2024-11-02 09:58:18 +00:00
|
|
|
let res = env.rpcClient.newPayload(latestVersion, payload.payload)
|
2023-07-09 02:16:22 +00:00
|
|
|
if res.isErr:
|
|
|
|
result = TestStatus.Failed
|
2023-09-24 23:53:33 +00:00
|
|
|
echo "unable to send block ",
|
|
|
|
payload.payload.blockNumber.uint64, ": ", res.error
|
2023-07-09 02:16:22 +00:00
|
|
|
break
|
|
|
|
|
|
|
|
let pStatus = res.value
|
|
|
|
if pStatus.status == PayloadExecutionStatus.valid:
|
2024-10-16 01:34:12 +00:00
|
|
|
latestValidHash = pStatus.latestValidHash.get
|
2023-07-09 02:16:22 +00:00
|
|
|
|
|
|
|
if pStatus.status != expectedStatus:
|
|
|
|
result = TestStatus.Failed
|
2023-09-24 23:53:33 +00:00
|
|
|
echo "payload status mismatch for block ",
|
|
|
|
payload.payload.blockNumber.uint64,
|
|
|
|
", status: ", pStatus.status,
|
|
|
|
",expected: ", expectedStatus
|
2023-07-09 02:16:22 +00:00
|
|
|
if pStatus.validationError.isSome:
|
|
|
|
echo pStatus.validationError.get
|
|
|
|
break
|
|
|
|
|
2024-09-04 09:54:54 +00:00
|
|
|
block blockOne:
|
2023-07-09 02:16:22 +00:00
|
|
|
# only update head of beacon chain if valid response occurred
|
2024-10-16 01:34:12 +00:00
|
|
|
if latestValidHash != default(Hash32):
|
2023-07-09 02:16:22 +00:00
|
|
|
# update with latest valid response
|
2024-10-16 01:34:12 +00:00
|
|
|
let fcState = ForkchoiceStateV1(headBlockHash: Hash32 latestValidHash.data)
|
2024-09-04 09:54:54 +00:00
|
|
|
let res = env.rpcClient.forkchoiceUpdated(latestVersion, fcState)
|
2023-07-09 02:16:22 +00:00
|
|
|
if res.isErr:
|
|
|
|
result = TestStatus.Failed
|
|
|
|
echo "unable to update head of beacon chain: ", res.error
|
2024-09-04 09:54:54 +00:00
|
|
|
break blockOne
|
2023-07-09 02:16:22 +00:00
|
|
|
|
2024-09-04 09:54:54 +00:00
|
|
|
if not validatePostState(node, env):
|
2023-07-09 02:16:22 +00:00
|
|
|
result = TestStatus.Failed
|
2024-09-04 09:54:54 +00:00
|
|
|
break blockOne
|
2023-07-09 02:16:22 +00:00
|
|
|
|
2024-09-04 09:54:54 +00:00
|
|
|
env.stopELClient()
|
2023-07-09 02:16:22 +00:00
|
|
|
|
2023-10-05 03:04:12 +00:00
|
|
|
const
|
|
|
|
skipName = [
|
2023-10-18 02:16:11 +00:00
|
|
|
"nothing skipped",
|
2023-10-05 03:04:12 +00:00
|
|
|
]
|
|
|
|
|
2023-10-31 02:52:31 +00:00
|
|
|
caseFolderCancun = "tests/fixtures/eth_tests/BlockchainTests/Pyspecs"
|
2023-10-05 03:04:12 +00:00
|
|
|
caseFolderShanghai = baseFolder & "/testcases"
|
|
|
|
|
|
|
|
proc collectTestVectors(): seq[string] =
|
|
|
|
for fileName in walkDirRec(caseFolderCancun):
|
|
|
|
result.add fileName
|
|
|
|
|
|
|
|
for fileName in walkDirRec(caseFolderShanghai):
|
|
|
|
result.add fileName
|
|
|
|
|
2023-07-09 02:16:22 +00:00
|
|
|
proc main() =
|
|
|
|
var stat: SimStat
|
|
|
|
let start = getTime()
|
|
|
|
|
2023-09-24 23:53:33 +00:00
|
|
|
let res = loadKzgTrustedSetup()
|
|
|
|
if res.isErr:
|
|
|
|
echo "FATAL: ", res.error
|
|
|
|
quit(QuitFailure)
|
|
|
|
|
2023-10-05 03:04:12 +00:00
|
|
|
let testVectors = collectTestVectors()
|
|
|
|
for fileName in testVectors:
|
2023-07-09 02:16:22 +00:00
|
|
|
if not fileName.endsWith(".json"):
|
|
|
|
continue
|
|
|
|
|
2023-10-05 03:04:12 +00:00
|
|
|
let suspect = splitPath(fileName)
|
|
|
|
if suspect.tail in skipName:
|
|
|
|
let fixtureTests = json.parseFile(fileName)
|
|
|
|
for name, fixture in fixtureTests:
|
|
|
|
stat.inc(name, TestStatus.Skipped)
|
|
|
|
continue
|
|
|
|
|
2023-07-09 02:16:22 +00:00
|
|
|
let fixtureTests = json.parseFile(fileName)
|
|
|
|
for name, fixture in fixtureTests:
|
|
|
|
let network = fixture["network"].getStr
|
|
|
|
if network notin supportedNetwork:
|
|
|
|
# skip pre Merge tests
|
|
|
|
continue
|
|
|
|
|
2023-09-24 23:53:33 +00:00
|
|
|
try:
|
|
|
|
let status = runTest(fixture, network)
|
|
|
|
stat.inc(name, status)
|
|
|
|
except CatchableError as ex:
|
|
|
|
debugEcho ex.msg
|
|
|
|
stat.inc(name, TestStatus.Failed)
|
2023-07-09 02:16:22 +00:00
|
|
|
|
|
|
|
let elpd = getTime() - start
|
|
|
|
print(stat, elpd, "pyspec")
|
|
|
|
|
|
|
|
main()
|