2022-07-23 05:54:01 +00:00
|
|
|
# beacon_chain
|
2023-01-13 15:46:35 +00:00
|
|
|
# Copyright (c) 2022-2023 Status Research & Development GmbH
|
2022-07-23 05:54:01 +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.
|
|
|
|
|
|
|
|
{.used.}
|
|
|
|
|
|
|
|
import
|
|
|
|
# Standard library
|
2023-02-10 20:59:38 +00:00
|
|
|
std/[json, streams],
|
2023-01-18 14:32:57 +00:00
|
|
|
# Status libraries
|
|
|
|
stew/byteutils,
|
2022-07-23 05:54:01 +00:00
|
|
|
# Third-party
|
|
|
|
yaml,
|
|
|
|
# Beacon chain internals
|
2023-01-14 21:19:50 +00:00
|
|
|
../../../beacon_chain/spec/[forks, light_client_sync],
|
2022-07-23 05:54:01 +00:00
|
|
|
# Test utilities
|
|
|
|
../testutil,
|
2023-02-10 20:59:38 +00:00
|
|
|
./fixtures_utils, ./os_ops
|
2022-07-23 05:54:01 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
TestMeta = object
|
2023-01-18 14:32:57 +00:00
|
|
|
genesis_validators_root: Eth2Digest
|
|
|
|
trusted_block_root: Eth2Digest
|
|
|
|
fork_digests: ForkDigests
|
|
|
|
bootstrap_fork_digest: ForkDigest
|
|
|
|
store_fork_digest: ForkDigest
|
2022-07-23 05:54:01 +00:00
|
|
|
|
|
|
|
TestChecks = object
|
|
|
|
finalized_slot: Slot
|
2023-01-14 21:19:50 +00:00
|
|
|
finalized_beacon_root: Eth2Digest
|
2023-01-18 14:32:57 +00:00
|
|
|
finalized_execution_root: Eth2Digest
|
2022-07-23 05:54:01 +00:00
|
|
|
optimistic_slot: Slot
|
2023-01-14 21:19:50 +00:00
|
|
|
optimistic_beacon_root: Eth2Digest
|
2023-01-18 14:32:57 +00:00
|
|
|
optimistic_execution_root: Eth2Digest
|
2022-07-23 05:54:01 +00:00
|
|
|
|
|
|
|
TestStepKind {.pure.} = enum
|
|
|
|
ForceUpdate
|
|
|
|
ProcessUpdate
|
2023-01-18 14:32:57 +00:00
|
|
|
UpgradeStore
|
2022-07-23 05:54:01 +00:00
|
|
|
|
|
|
|
TestStep = object
|
|
|
|
case kind: TestStepKind
|
|
|
|
of TestStepKind.ForceUpdate:
|
|
|
|
discard
|
|
|
|
of TestStepKind.ProcessUpdate:
|
2023-01-14 21:19:50 +00:00
|
|
|
update: ForkedLightClientUpdate
|
2023-01-18 14:32:57 +00:00
|
|
|
of TestStepKind.UpgradeStore:
|
|
|
|
store_data_fork: LightClientDataFork
|
2022-07-23 05:54:01 +00:00
|
|
|
current_slot: Slot
|
|
|
|
checks: TestChecks
|
|
|
|
|
2023-01-14 21:19:50 +00:00
|
|
|
proc loadSteps(path: string, fork_digests: ForkDigests): seq[TestStep] =
|
2023-02-10 20:59:38 +00:00
|
|
|
let stepsYAML = os_ops.readFile(path/"steps.yaml")
|
2022-07-23 05:54:01 +00:00
|
|
|
let steps = yaml.loadToJson(stepsYAML)
|
|
|
|
|
|
|
|
result = @[]
|
|
|
|
for step in steps[0]:
|
|
|
|
func getChecks(c: JsonNode): TestChecks =
|
|
|
|
TestChecks(
|
|
|
|
finalized_slot:
|
|
|
|
c["finalized_header"]["slot"].getInt().Slot,
|
2023-01-14 21:19:50 +00:00
|
|
|
finalized_beacon_root:
|
2022-11-21 07:44:49 +00:00
|
|
|
Eth2Digest.fromHex(c["finalized_header"]["beacon_root"].getStr()),
|
2023-01-18 14:32:57 +00:00
|
|
|
finalized_execution_root:
|
|
|
|
Eth2Digest.fromHex(c["finalized_header"]{"execution_root"}.getStr()),
|
2022-07-23 05:54:01 +00:00
|
|
|
optimistic_slot:
|
|
|
|
c["optimistic_header"]["slot"].getInt().Slot,
|
2023-01-14 21:19:50 +00:00
|
|
|
optimistic_beacon_root:
|
2023-01-18 14:32:57 +00:00
|
|
|
Eth2Digest.fromHex(c["optimistic_header"]["beacon_root"].getStr()),
|
|
|
|
optimistic_execution_root:
|
|
|
|
Eth2Digest.fromHex(c["optimistic_header"]{"execution_root"}.getStr()))
|
2022-07-23 05:54:01 +00:00
|
|
|
|
|
|
|
if step.hasKey"force_update":
|
|
|
|
let s = step["force_update"]
|
2023-01-14 21:19:50 +00:00
|
|
|
|
|
|
|
result.add TestStep(
|
|
|
|
kind: TestStepKind.ForceUpdate,
|
|
|
|
current_slot: s["current_slot"].getInt().Slot,
|
|
|
|
checks: s["checks"].getChecks())
|
2022-07-23 05:54:01 +00:00
|
|
|
elif step.hasKey"process_update":
|
|
|
|
let
|
|
|
|
s = step["process_update"]
|
2023-01-18 14:32:57 +00:00
|
|
|
update_fork_digest =
|
|
|
|
distinctBase(ForkDigest).fromHex(s{"update_fork_digest"}.getStr(
|
|
|
|
distinctBase(fork_digests.altair).toHex())).ForkDigest
|
2023-03-12 02:34:38 +00:00
|
|
|
update_consensus_fork =
|
2023-03-11 16:58:48 +00:00
|
|
|
fork_digests.consensusForkForDigest(update_fork_digest)
|
2023-01-14 21:19:50 +00:00
|
|
|
.expect("Unknown update fork " & $update_fork_digest)
|
|
|
|
update_filename = s["update"].getStr()
|
|
|
|
|
|
|
|
var update {.noinit.}: ForkedLightClientUpdate
|
2023-03-12 02:34:38 +00:00
|
|
|
withLcDataFork(lcDataForkAtConsensusFork(update_consensus_fork)):
|
2023-01-14 21:19:50 +00:00
|
|
|
when lcDataFork > LightClientDataFork.None:
|
|
|
|
update = ForkedLightClientUpdate(kind: lcDataFork)
|
|
|
|
update.forky(lcDataFork) = parseTest(
|
|
|
|
path/update_filename & ".ssz_snappy", SSZ,
|
|
|
|
lcDataFork.LightClientUpdate)
|
|
|
|
else: raiseAssert "Unreachable update fork " & $update_fork_digest
|
|
|
|
|
|
|
|
result.add TestStep(
|
|
|
|
kind: TestStepKind.ProcessUpdate,
|
|
|
|
update: update,
|
|
|
|
current_slot: s["current_slot"].getInt().Slot,
|
|
|
|
checks: s["checks"].getChecks())
|
2023-01-18 14:32:57 +00:00
|
|
|
elif step.hasKey"upgrade_store":
|
|
|
|
let
|
|
|
|
s = step["upgrade_store"]
|
|
|
|
store_fork_digest =
|
|
|
|
distinctBase(ForkDigest).fromHex(
|
|
|
|
s["store_fork_digest"].getStr()).ForkDigest
|
2023-03-12 02:34:38 +00:00
|
|
|
store_consensus_fork =
|
2023-03-11 16:58:48 +00:00
|
|
|
fork_digests.consensusForkForDigest(store_fork_digest)
|
2023-01-18 14:32:57 +00:00
|
|
|
.expect("Unknown store fork " & $store_fork_digest)
|
|
|
|
|
|
|
|
result.add TestStep(
|
|
|
|
kind: TestStepKind.UpgradeStore,
|
2023-03-12 02:34:38 +00:00
|
|
|
store_data_fork: lcDataForkAtConsensusFork(store_consensus_fork),
|
2023-01-18 14:32:57 +00:00
|
|
|
checks: s["checks"].getChecks())
|
2022-07-23 05:54:01 +00:00
|
|
|
else:
|
|
|
|
doAssert false, "Unknown test step: " & $step
|
|
|
|
|
|
|
|
proc runTest(path: string) =
|
|
|
|
test "Light client - Sync - " & path.relativePath(SszTestsDir):
|
2023-01-18 14:32:57 +00:00
|
|
|
# Reduce stack size by making this a `proc`
|
|
|
|
proc loadTestMeta(): (RuntimeConfig, TestMeta) =
|
|
|
|
let (cfg, unknowns) = readRuntimeConfig(path/"config.yaml")
|
2023-02-15 14:44:09 +00:00
|
|
|
|
2023-02-21 12:58:08 +00:00
|
|
|
doAssert unknowns.len == 0, "Unknown config constants: " & $unknowns
|
2023-01-18 14:32:57 +00:00
|
|
|
|
|
|
|
type TestMetaYaml {.sparse.} = object
|
|
|
|
genesis_validators_root: string
|
|
|
|
trusted_block_root: string
|
|
|
|
bootstrap_fork_digest: Option[string]
|
|
|
|
store_fork_digest: Option[string]
|
|
|
|
let
|
|
|
|
meta = block:
|
|
|
|
var s = openFileStream(path/"meta.yaml")
|
|
|
|
defer: close(s)
|
|
|
|
var res: TestMetaYaml
|
|
|
|
yaml.load(s, res)
|
|
|
|
res
|
|
|
|
genesis_validators_root =
|
|
|
|
Eth2Digest.fromHex(meta.genesis_validators_root)
|
|
|
|
trusted_block_root =
|
|
|
|
Eth2Digest.fromHex(meta.trusted_block_root)
|
|
|
|
fork_digests =
|
|
|
|
ForkDigests.init(cfg, genesis_validators_root)
|
|
|
|
bootstrap_fork_digest =
|
|
|
|
distinctBase(ForkDigest).fromHex(meta.bootstrap_fork_digest.get(
|
|
|
|
distinctBase(fork_digests.altair).toHex())).ForkDigest
|
|
|
|
store_fork_digest =
|
|
|
|
distinctBase(ForkDigest).fromHex(meta.store_fork_digest.get(
|
|
|
|
distinctBase(fork_digests.altair).toHex())).ForkDigest
|
|
|
|
|
|
|
|
(cfg, TestMeta(
|
|
|
|
genesis_validators_root: genesis_validators_root,
|
|
|
|
trusted_block_root: trusted_block_root,
|
|
|
|
fork_digests: fork_digests,
|
|
|
|
bootstrap_fork_digest: bootstrap_fork_digest,
|
|
|
|
store_fork_digest: store_fork_digest))
|
|
|
|
|
2022-07-23 05:54:01 +00:00
|
|
|
let
|
2023-01-18 14:32:57 +00:00
|
|
|
(cfg, meta) = loadTestMeta()
|
|
|
|
steps = loadSteps(path, meta.fork_digests)
|
2023-01-14 21:19:50 +00:00
|
|
|
|
2023-01-18 14:32:57 +00:00
|
|
|
# Reduce stack size by making this a `proc`
|
|
|
|
proc loadBootstrap(): ForkedLightClientBootstrap =
|
2023-03-12 02:34:38 +00:00
|
|
|
let bootstrap_consensus_fork =
|
2023-03-11 16:58:48 +00:00
|
|
|
meta.fork_digests.consensusForkForDigest(meta.bootstrap_fork_digest)
|
2023-01-18 14:32:57 +00:00
|
|
|
.expect("Unknown bootstrap fork " & $meta.bootstrap_fork_digest)
|
|
|
|
var bootstrap {.noinit.}: ForkedLightClientBootstrap
|
2023-03-12 02:34:38 +00:00
|
|
|
withLcDataFork(lcDataForkAtConsensusFork(bootstrap_consensus_fork)):
|
2023-01-18 14:32:57 +00:00
|
|
|
when lcDataFork > LightClientDataFork.None:
|
|
|
|
bootstrap = ForkedLightClientBootstrap(kind: lcDataFork)
|
|
|
|
bootstrap.forky(lcDataFork) = parseTest(
|
|
|
|
path/"bootstrap.ssz_snappy", SSZ,
|
|
|
|
lcDataFork.LightClientBootstrap)
|
|
|
|
else:
|
|
|
|
raiseAssert "Unknown bootstrap fork " & $meta.bootstrap_fork_digest
|
|
|
|
bootstrap
|
|
|
|
|
|
|
|
# Reduce stack size by making this a `proc`
|
|
|
|
proc initializeStore(
|
|
|
|
bootstrap: ref ForkedLightClientBootstrap): ForkedLightClientStore =
|
2023-03-12 02:34:38 +00:00
|
|
|
let store_consensus_fork =
|
2023-03-11 16:58:48 +00:00
|
|
|
meta.fork_digests.consensusForkForDigest(meta.store_fork_digest)
|
2023-01-18 14:32:57 +00:00
|
|
|
.expect("Unknown store fork " & $meta.store_fork_digest)
|
|
|
|
var store {.noinit.}: ForkedLightClientStore
|
2023-03-12 02:34:38 +00:00
|
|
|
withLcDataFork(lcDataForkAtConsensusFork(store_consensus_fork)):
|
2023-01-18 14:32:57 +00:00
|
|
|
when lcDataFork > LightClientDataFork.None:
|
|
|
|
store = ForkedLightClientStore(kind: lcDataFork)
|
|
|
|
bootstrap[].migrateToDataFork(lcDataFork)
|
|
|
|
store.forky(lcDataFork) = initialize_light_client_store(
|
|
|
|
meta.trusted_block_root, bootstrap[].forky(lcDataFork), cfg).get
|
|
|
|
else: raiseAssert "Unreachable store fork " & $meta.store_fork_digest
|
|
|
|
store
|
|
|
|
|
|
|
|
let bootstrap = newClone(loadBootstrap())
|
|
|
|
var store = initializeStore(bootstrap)
|
|
|
|
|
|
|
|
# Reduce stack size by making this a `proc`
|
|
|
|
proc processStep(step: TestStep) =
|
2023-01-14 21:19:50 +00:00
|
|
|
withForkyStore(store):
|
|
|
|
when lcDataFork > LightClientDataFork.None:
|
|
|
|
case step.kind
|
|
|
|
of TestStepKind.ForceUpdate:
|
|
|
|
process_light_client_store_force_update(
|
|
|
|
forkyStore, step.current_slot)
|
|
|
|
of TestStepKind.ProcessUpdate:
|
|
|
|
check step.update.kind <= lcDataFork
|
|
|
|
let
|
|
|
|
upgradedUpdate = step.update.migratingToDataFork(lcDataFork)
|
|
|
|
res = process_light_client_update(
|
|
|
|
forkyStore, upgradedUpdate.forky(lcDataFork), step.current_slot,
|
2023-01-18 14:32:57 +00:00
|
|
|
cfg, meta.genesis_validators_root)
|
2023-01-14 21:19:50 +00:00
|
|
|
check res.isOk
|
2023-01-18 14:32:57 +00:00
|
|
|
of TestStepKind.UpgradeStore:
|
|
|
|
check step.store_data_fork >= lcDataFork
|
|
|
|
withLcDataFork(step.store_data_fork):
|
|
|
|
when lcDataFork > LightClientDataFork.None:
|
|
|
|
store.migrateToDataFork(lcDataFork)
|
2023-01-14 21:19:50 +00:00
|
|
|
else: raiseAssert "Unreachable"
|
|
|
|
|
|
|
|
withForkyStore(store):
|
|
|
|
when lcDataFork > LightClientDataFork.None:
|
|
|
|
let
|
|
|
|
finalized_slot =
|
|
|
|
forkyStore.finalized_header.beacon.slot
|
|
|
|
finalized_beacon_root =
|
|
|
|
hash_tree_root(forkyStore.finalized_header.beacon)
|
2023-01-18 14:32:57 +00:00
|
|
|
finalized_execution_root =
|
|
|
|
when lcDataFork >= LightClientDataFork.Capella:
|
|
|
|
get_lc_execution_root(forkyStore.finalized_header, cfg)
|
|
|
|
else:
|
|
|
|
ZERO_HASH
|
2023-01-14 21:19:50 +00:00
|
|
|
optimistic_slot =
|
|
|
|
forkyStore.optimistic_header.beacon.slot
|
|
|
|
optimistic_beacon_root =
|
|
|
|
hash_tree_root(forkyStore.optimistic_header.beacon)
|
2023-01-18 14:32:57 +00:00
|
|
|
optimistic_execution_root =
|
|
|
|
when lcDataFork >= LightClientDataFork.Capella:
|
|
|
|
get_lc_execution_root(forkyStore.optimistic_header, cfg)
|
|
|
|
else:
|
|
|
|
ZERO_HASH
|
2023-01-14 21:19:50 +00:00
|
|
|
check:
|
|
|
|
finalized_slot == step.checks.finalized_slot
|
|
|
|
finalized_beacon_root == step.checks.finalized_beacon_root
|
2023-01-18 14:32:57 +00:00
|
|
|
finalized_execution_root == step.checks.finalized_execution_root
|
2023-01-14 21:19:50 +00:00
|
|
|
optimistic_slot == step.checks.optimistic_slot
|
|
|
|
optimistic_beacon_root == step.checks.optimistic_beacon_root
|
2023-01-18 14:32:57 +00:00
|
|
|
optimistic_execution_root == step.checks.optimistic_execution_root
|
2023-01-14 21:19:50 +00:00
|
|
|
else: raiseAssert "Unreachable"
|
2022-07-23 05:54:01 +00:00
|
|
|
|
2023-01-18 14:32:57 +00:00
|
|
|
for step in steps:
|
|
|
|
processStep(step)
|
|
|
|
|
2022-07-23 05:54:01 +00:00
|
|
|
suite "EF - Light client - Sync" & preset():
|
|
|
|
const presetPath = SszTestsDir/const_preset
|
|
|
|
for kind, path in walkDir(presetPath, relative = true, checkDir = true):
|
|
|
|
let basePath =
|
|
|
|
presetPath/path/"light_client"/"sync"/"pyspec_tests"
|
|
|
|
if kind != pcDir or not dirExists(basePath):
|
|
|
|
continue
|
|
|
|
for kind, path in walkDir(basePath, relative = true, checkDir = true):
|
2022-10-27 11:54:39 +00:00
|
|
|
let combinedPath = basePath/path
|
2022-07-23 05:54:01 +00:00
|
|
|
runTest(basePath/path)
|