2019-09-03 03:12:09 +00:00
|
|
|
# beacon_chain
|
2022-01-05 08:42:56 +00:00
|
|
|
# Copyright (c) 2018-2022 Status Research & Development GmbH
|
2019-09-03 03:12:09 +00:00
|
|
|
# Licensed and distributed under either of
|
2019-11-25 15:30:02 +00:00
|
|
|
# * 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).
|
2019-09-03 03:12:09 +00:00
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
import
|
|
|
|
# Standard library
|
2021-08-18 18:57:58 +00:00
|
|
|
std/[os, strutils, typetraits],
|
2019-09-03 03:12:09 +00:00
|
|
|
# Internals
|
2022-07-23 05:54:01 +00:00
|
|
|
../../beacon_chain/spec/datatypes/[phase0, altair, bellatrix],
|
2021-08-18 18:57:58 +00:00
|
|
|
../../beacon_chain/spec/[
|
2022-07-06 10:33:02 +00:00
|
|
|
eth2_merkleization, eth2_ssz_serialization, forks],
|
2021-05-24 08:42:40 +00:00
|
|
|
# Status libs,
|
|
|
|
snappy,
|
2021-08-18 18:57:58 +00:00
|
|
|
stew/byteutils
|
2019-09-03 03:12:09 +00:00
|
|
|
|
2021-08-18 18:57:58 +00:00
|
|
|
export
|
|
|
|
eth2_merkleization, eth2_ssz_serialization
|
2019-09-03 03:12:09 +00:00
|
|
|
|
2020-01-27 10:56:32 +00:00
|
|
|
# Process current EF test format
|
2019-09-03 03:12:09 +00:00
|
|
|
# ---------------------------------------------
|
|
|
|
|
2022-07-06 10:33:02 +00:00
|
|
|
# #######################
|
|
|
|
# Path parsing
|
|
|
|
|
|
|
|
func forkForPathComponent*(forkPath: string): Opt[BeaconStateFork] =
|
|
|
|
for fork in BeaconStateFork:
|
|
|
|
if ($fork).toLowerAscii() == forkPath:
|
|
|
|
return ok fork
|
|
|
|
err()
|
|
|
|
|
2019-09-03 03:12:09 +00:00
|
|
|
# #######################
|
|
|
|
# JSON deserialization
|
|
|
|
|
2021-05-24 08:42:40 +00:00
|
|
|
func readValue*(r: var JsonReader, a: var seq[byte]) =
|
2019-09-03 03:12:09 +00:00
|
|
|
## Custom deserializer for seq[byte]
|
|
|
|
a = hexToSeqByte(r.readValue(string))
|
|
|
|
|
2022-07-06 10:33:02 +00:00
|
|
|
# #######################
|
|
|
|
# Mock RuntimeConfig
|
|
|
|
|
|
|
|
func genesisTestRuntimeConfig*(stateFork: BeaconStateFork): RuntimeConfig =
|
|
|
|
var res = defaultRuntimeConfig
|
|
|
|
case stateFork
|
2022-11-02 16:23:30 +00:00
|
|
|
of BeaconStateFork.Capella:
|
|
|
|
res.CAPELLA_FORK_EPOCH = GENESIS_EPOCH
|
|
|
|
res.BELLATRIX_FORK_EPOCH = GENESIS_EPOCH
|
|
|
|
res.ALTAIR_FORK_EPOCH = GENESIS_EPOCH
|
2022-07-06 10:33:02 +00:00
|
|
|
of BeaconStateFork.Bellatrix:
|
|
|
|
res.BELLATRIX_FORK_EPOCH = GENESIS_EPOCH
|
|
|
|
res.ALTAIR_FORK_EPOCH = GENESIS_EPOCH
|
|
|
|
of BeaconStateFork.Altair:
|
|
|
|
res.ALTAIR_FORK_EPOCH = GENESIS_EPOCH
|
|
|
|
of BeaconStateFork.Phase0:
|
|
|
|
discard
|
|
|
|
res
|
|
|
|
|
2019-09-03 03:12:09 +00:00
|
|
|
# #######################
|
|
|
|
# Test helpers
|
|
|
|
|
2020-05-11 16:27:44 +00:00
|
|
|
type
|
|
|
|
UnconsumedInput* = object of CatchableError
|
|
|
|
TestSizeError* = object of ValueError
|
|
|
|
|
2022-05-31 11:15:31 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/tree/v1.2.0-rc.1/tests/formats/rewards#rewards-tests
|
2021-10-19 14:38:41 +00:00
|
|
|
Deltas* = object
|
|
|
|
rewards*: List[uint64, Limit VALIDATOR_REGISTRY_LIMIT]
|
|
|
|
penalties*: List[uint64, Limit VALIDATOR_REGISTRY_LIMIT]
|
|
|
|
|
2022-11-08 02:37:28 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.3.0-alpha.0/specs/phase0/validator.md#eth1block
|
2021-10-20 11:36:38 +00:00
|
|
|
Eth1Block* = object
|
|
|
|
timestamp*: uint64
|
|
|
|
deposit_root*: Eth2Digest
|
|
|
|
deposit_count*: uint64
|
|
|
|
# All other eth1 block fields
|
|
|
|
|
2019-09-03 03:12:09 +00:00
|
|
|
const
|
2020-06-16 15:12:59 +00:00
|
|
|
FixturesDir* =
|
|
|
|
currentSourcePath.rsplit(DirSep, 1)[0] / ".." / ".." / "vendor" / "nim-eth2-scenarios"
|
2022-01-05 13:41:39 +00:00
|
|
|
SszTestsDir* = FixturesDir / "tests-v" & SPEC_VERSION
|
2021-05-24 08:42:40 +00:00
|
|
|
MaxObjectSize* = 3_000_000
|
2019-09-03 03:12:09 +00:00
|
|
|
|
2021-05-24 08:42:40 +00:00
|
|
|
proc parseTest*(path: string, Format: typedesc[Json], T: typedesc): T =
|
2019-09-03 03:12:09 +00:00
|
|
|
try:
|
|
|
|
# debugEcho " [Debug] Loading file: \"", path, '\"'
|
|
|
|
result = Format.loadFile(path, T)
|
|
|
|
except SerializationError as err:
|
|
|
|
writeStackTrace()
|
|
|
|
stderr.write $Format & " load issue for file \"", path, "\"\n"
|
|
|
|
stderr.write err.formatMsg(path), "\n"
|
|
|
|
quit 1
|
2020-05-11 16:27:44 +00:00
|
|
|
|
|
|
|
template readFileBytes*(path: string): seq[byte] =
|
|
|
|
cast[seq[byte]](readFile(path))
|
|
|
|
|
2020-10-28 18:35:31 +00:00
|
|
|
proc sszDecodeEntireInput*(input: openArray[byte], Decoded: type): Decoded =
|
2022-02-20 20:13:06 +00:00
|
|
|
let stream = unsafeMemoryInput(input)
|
2020-05-28 12:36:37 +00:00
|
|
|
var reader = init(SszReader, stream)
|
2020-05-27 15:04:43 +00:00
|
|
|
reader.readValue(result)
|
2020-05-11 16:27:44 +00:00
|
|
|
|
|
|
|
if stream.readable:
|
|
|
|
raise newException(UnconsumedInput, "Remaining bytes in the input")
|
2020-10-22 18:53:40 +00:00
|
|
|
|
2021-10-19 14:38:41 +00:00
|
|
|
iterator walkTests*(dir: static string): string =
|
|
|
|
for kind, path in walkDir(
|
|
|
|
dir/"pyspec_tests", relative = true, checkDir = true):
|
|
|
|
yield path
|
|
|
|
|
2021-05-24 08:42:40 +00:00
|
|
|
proc parseTest*(path: string, Format: typedesc[SSZ], T: typedesc): T =
|
|
|
|
try:
|
|
|
|
# debugEcho " [Debug] Loading file: \"", path, '\"'
|
|
|
|
sszDecodeEntireInput(snappy.decode(readFileBytes(path), MaxObjectSize), T)
|
|
|
|
except SerializationError as err:
|
|
|
|
writeStackTrace()
|
|
|
|
stderr.write $Format & " load issue for file \"", path, "\"\n"
|
|
|
|
stderr.write err.formatMsg(path), "\n"
|
|
|
|
quit 1
|
2022-07-23 05:54:01 +00:00
|
|
|
|
2022-11-09 17:32:10 +00:00
|
|
|
from ../../beacon_chain/spec/datatypes/capella import BeaconState
|
|
|
|
|
2022-07-23 05:54:01 +00:00
|
|
|
proc loadForkedState*(
|
|
|
|
path: string, fork: BeaconStateFork): ref ForkedHashedBeaconState =
|
|
|
|
# TODO stack usage. newClone and assignClone do not seem to
|
|
|
|
# prevent temporaries created by case objects
|
|
|
|
let forkedState = new ForkedHashedBeaconState
|
|
|
|
case fork
|
2022-11-02 16:23:30 +00:00
|
|
|
of BeaconStateFork.Capella:
|
2022-11-09 17:32:10 +00:00
|
|
|
let state = newClone(parseTest(path, SSZ, capella.BeaconState))
|
|
|
|
forkedState.kind = BeaconStateFork.Capella
|
|
|
|
forkedState.capellaData.data = state[]
|
|
|
|
forkedState.capellaData.root = hash_tree_root(state[])
|
2022-07-23 05:54:01 +00:00
|
|
|
of BeaconStateFork.Bellatrix:
|
|
|
|
let state = newClone(parseTest(path, SSZ, bellatrix.BeaconState))
|
|
|
|
forkedState.kind = BeaconStateFork.Bellatrix
|
|
|
|
forkedState.bellatrixData.data = state[]
|
|
|
|
forkedState.bellatrixData.root = hash_tree_root(state[])
|
|
|
|
of BeaconStateFork.Altair:
|
|
|
|
let state = newClone(parseTest(path, SSZ, altair.BeaconState))
|
|
|
|
forkedState.kind = BeaconStateFork.Altair
|
|
|
|
forkedState.altairData.data = state[]
|
|
|
|
forkedState.altairData.root = hash_tree_root(state[])
|
|
|
|
of BeaconStateFork.Phase0:
|
|
|
|
let state = newClone(parseTest(path, SSZ, phase0.BeaconState))
|
|
|
|
forkedState.kind = BeaconStateFork.Phase0
|
|
|
|
forkedState.phase0Data.data = state[]
|
|
|
|
forkedState.phase0Data.root = hash_tree_root(state[])
|
|
|
|
forkedState
|