2022-12-14 07:42:25 +00:00
|
|
|
# beacon_chain
|
2024-01-06 14:26:56 +00:00
|
|
|
# Copyright (c) 2018-2024 Status Research & Development GmbH
|
2022-12-14 07:42:25 +00:00
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2024-02-24 03:41:58 +00:00
|
|
|
{.push raises: [].}
|
2022-12-14 07:42:25 +00:00
|
|
|
{.used.}
|
|
|
|
|
|
|
|
import
|
2022-12-15 22:51:04 +00:00
|
|
|
chronicles,
|
2023-10-05 12:01:40 +00:00
|
|
|
../../beacon_chain/spec/forks,
|
2022-12-14 07:42:25 +00:00
|
|
|
../../beacon_chain/spec/state_transition,
|
2023-02-10 20:59:38 +00:00
|
|
|
./os_ops,
|
2022-12-14 07:42:25 +00:00
|
|
|
../testutil
|
|
|
|
|
|
|
|
from std/sequtils import toSeq
|
2023-10-05 12:01:40 +00:00
|
|
|
from std/strutils import toLowerAscii
|
2023-09-06 08:17:59 +00:00
|
|
|
from ../../beacon_chain/spec/presets import
|
2022-12-14 07:42:25 +00:00
|
|
|
const_preset, defaultRuntimeConfig
|
|
|
|
from ./fixtures_utils import
|
|
|
|
SSZ, SszTestsDir, hash_tree_root, parseTest, readSszBytes, toSszType
|
|
|
|
|
|
|
|
proc runTest(
|
2023-10-05 12:01:40 +00:00
|
|
|
consensusFork: static ConsensusFork,
|
|
|
|
testName, testDir: static[string], suiteName, unitTestName: string) =
|
2022-12-14 07:42:25 +00:00
|
|
|
let testPath = testDir / unitTestName
|
|
|
|
|
2023-09-04 13:05:11 +00:00
|
|
|
let
|
|
|
|
hasPostState = fileExists(testPath/"post.ssz_snappy")
|
|
|
|
prefix = if hasPostState: "[Valid] " else: "[Invalid] "
|
2022-12-14 07:42:25 +00:00
|
|
|
|
2023-09-04 13:05:11 +00:00
|
|
|
test prefix & testName & " - " & unitTestName & preset():
|
2023-10-05 12:01:40 +00:00
|
|
|
let preState = newClone(parseTest(testPath/"pre.ssz_snappy",
|
|
|
|
SSZ, consensusFork.BeaconState))
|
2023-09-04 13:05:11 +00:00
|
|
|
var
|
|
|
|
fhPreState = ForkedHashedBeaconState.new(preState[])
|
|
|
|
cache = StateCache()
|
|
|
|
info = ForkedEpochInfo()
|
|
|
|
|
|
|
|
# In test cases with more than 10 blocks the first 10 aren't 0-prefixed,
|
|
|
|
# so purely lexicographic sorting wouldn't sort properly.
|
|
|
|
let numBlocks = toSeq(walkPattern(testPath/"blocks_*.ssz_snappy")).len
|
|
|
|
for i in 0 ..< numBlocks:
|
2023-10-05 12:01:40 +00:00
|
|
|
let blck = parseTest(testPath/"blocks_" & $i & ".ssz_snappy",
|
|
|
|
SSZ, consensusFork.SignedBeaconBlock)
|
2022-12-14 07:42:25 +00:00
|
|
|
|
2023-09-04 13:05:11 +00:00
|
|
|
if hasPostState:
|
2024-03-04 17:00:46 +00:00
|
|
|
# The return value is the block rewards, which aren't tested here;
|
2024-04-19 11:31:15 +00:00
|
|
|
# the .expect() already handles the validaty check.
|
2024-03-04 17:00:46 +00:00
|
|
|
discard state_transition(
|
2023-09-04 13:05:11 +00:00
|
|
|
defaultRuntimeConfig, fhPreState[], blck, cache, info, flags = {},
|
|
|
|
noRollback).expect("should apply block")
|
|
|
|
else:
|
|
|
|
let res = state_transition(
|
|
|
|
defaultRuntimeConfig, fhPreState[], blck, cache, info, flags = {},
|
|
|
|
noRollback)
|
|
|
|
doAssert (i + 1 < numBlocks) or not res.isOk(),
|
|
|
|
"We didn't expect these invalid blocks to be processed"
|
|
|
|
|
|
|
|
if hasPostState:
|
2023-10-05 12:01:40 +00:00
|
|
|
let postState = newClone(parseTest(testPath/"post.ssz_snappy",
|
|
|
|
SSZ, consensusFork.BeaconState))
|
2023-09-04 13:05:11 +00:00
|
|
|
when false:
|
|
|
|
reportDiff(hashedPreState.phase0Data.data, postState)
|
|
|
|
doAssert getStateRoot(fhPreState[]) == postState[].hash_tree_root()
|
2022-12-14 07:42:25 +00:00
|
|
|
|
2023-10-05 12:01:40 +00:00
|
|
|
template runForkBlockTests(consensusFork: static ConsensusFork) =
|
2022-12-14 07:42:25 +00:00
|
|
|
const
|
2023-10-05 12:01:40 +00:00
|
|
|
forkHumanName = $consensusFork
|
|
|
|
forkDirName = forkHumanName.toLowerAscii()
|
2022-12-14 07:42:25 +00:00
|
|
|
FinalityDir =
|
|
|
|
SszTestsDir/const_preset/forkDirName/"finality"/"finality"/"pyspec_tests"
|
|
|
|
RandomDir =
|
|
|
|
SszTestsDir/const_preset/forkDirName/"random"/"random"/"pyspec_tests"
|
|
|
|
SanityBlocksDir =
|
|
|
|
SszTestsDir/const_preset/forkDirName/"sanity"/"blocks"/"pyspec_tests"
|
|
|
|
|
|
|
|
suite "EF - " & forkHumanName & " - Sanity - Blocks " & preset():
|
|
|
|
for kind, path in walkDir(SanityBlocksDir, relative = true, checkDir = true):
|
2023-10-05 12:01:40 +00:00
|
|
|
consensusFork.runTest(
|
|
|
|
"EF - " & forkHumanName & " - Sanity - Blocks",
|
|
|
|
SanityBlocksDir, suiteName, path)
|
2022-12-14 07:42:25 +00:00
|
|
|
|
|
|
|
suite "EF - " & forkHumanName & " - Finality " & preset():
|
|
|
|
for kind, path in walkDir(FinalityDir, relative = true, checkDir = true):
|
2023-10-05 12:01:40 +00:00
|
|
|
consensusFork.runTest(
|
|
|
|
"EF - " & forkHumanName & " - Finality",
|
|
|
|
FinalityDir, suiteName, path)
|
2022-12-14 07:42:25 +00:00
|
|
|
|
|
|
|
suite "EF - " & forkHumanName & " - Random " & preset():
|
|
|
|
for kind, path in walkDir(RandomDir, relative = true, checkDir = true):
|
2023-10-05 12:01:40 +00:00
|
|
|
consensusFork.runTest(
|
|
|
|
"EF - " & forkHumanName & " - Random",
|
|
|
|
RandomDir, suiteName, path)
|
2022-12-14 07:42:25 +00:00
|
|
|
|
2023-10-05 12:01:40 +00:00
|
|
|
withAll(ConsensusFork):
|
2024-04-28 09:15:03 +00:00
|
|
|
runForkBlockTests(consensusFork)
|