2022-07-23 05:54:01 +00:00
|
|
|
# beacon_chain
|
2024-01-06 14:26:56 +00:00
|
|
|
# Copyright (c) 2022-2024 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.
|
|
|
|
|
2024-02-29 10:28:32 +00:00
|
|
|
{.push raises: [].}
|
2022-07-23 05:54:01 +00:00
|
|
|
{.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
|
2024-07-17 19:50:29 +00:00
|
|
|
yaml, yaml/tojson,
|
2022-07-23 05:54:01 +00:00
|
|
|
# Beacon chain internals
|
2023-09-06 08:17:59 +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
|
|
|
|
|
2024-02-29 10:28:32 +00:00
|
|
|
proc loadSteps(
|
|
|
|
path: string,
|
|
|
|
fork_digests: ForkDigests
|
|
|
|
): seq[TestStep] {.raises: [
|
|
|
|
KeyError, ValueError, YamlConstructionError, YamlParserError].} =
|
2023-02-10 20:59:38 +00:00
|
|
|
let stepsYAML = os_ops.readFile(path/"steps.yaml")
|
2024-07-17 19:50:29 +00:00
|
|
|
let steps = loadToJson(stepsYAML)
|
2022-07-23 05:54:01 +00:00
|
|
|
|
|
|
|
result = @[]
|
|
|
|
for step in steps[0]:
|
2024-02-29 10:28:32 +00:00
|
|
|
func getChecks(c: JsonNode): TestChecks {.raises: [KeyError].} =
|
2022-07-23 05:54:01 +00:00
|
|
|
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:
|
2023-10-04 16:11:45 +00:00
|
|
|
update = ForkedLightClientUpdate.init(parseTest(
|
2023-01-14 21:19:50 +00:00
|
|
|
path/update_filename & ".ssz_snappy", SSZ,
|
2023-10-04 16:11:45 +00:00
|
|
|
lcDataFork.LightClientUpdate))
|
2023-01-14 21:19:50 +00:00
|
|
|
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
|
|
|
|
|
2023-09-04 13:05:11 +00:00
|
|
|
proc runTest(suiteName, path: string) =
|
2024-02-29 10:28:32 +00:00
|
|
|
let relativePathComponent = path.relativeTestPathComponent()
|
|
|
|
test "Light client - Sync - " & relativePathComponent:
|
2023-01-18 14:32:57 +00:00
|
|
|
# Reduce stack size by making this a `proc`
|
2024-02-29 10:28:32 +00:00
|
|
|
proc loadTestMeta(): (RuntimeConfig, TestMeta) {.raises: [
|
|
|
|
Exception, IOError, PresetFileError, PresetIncompatibleError].} =
|
2024-01-21 06:55:03 +00:00
|
|
|
let (cfg, _) = readRuntimeConfig(path/"config.yaml")
|
2023-02-15 14:44:09 +00:00
|
|
|
|
2023-05-25 07:55:00 +00:00
|
|
|
when false:
|
|
|
|
# TODO evaluate whether this is useful and if so, fix it
|
|
|
|
# Unhandled defect: nimbus-eth2/tests/consensus_spec/test_fixture_light_client_sync.nim(131, 16) `unknowns.len == 0` Unknown config constants: @["MAXIMUM_GOSSIP_CLOCK_DISPARITY", "ATTESTATION_PROPAGATION_SLOT_RANGE", "MAX_REQUEST_BLOCKS", "SUBNETS_PER_NODE", "TTFB_TIMEOUT", "MIN_EPOCHS_FOR_BLOCK_REQUESTS", "MESSAGE_DOMAIN_VALID_SNAPPY", "ATTESTATION_SUBNET_EXTRA_BITS", "MAX_CHUNK_SIZE", "EPOCHS_PER_SUBNET_SUBSCRIPTION", "GOSSIP_MAX_SIZE", "ATTESTATION_SUBNET_PREFIX_BITS", "MESSAGE_DOMAIN_INVALID_SNAPPY", "RESP_TIMEOUT"] [AssertionDefect]
|
|
|
|
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:
|
2023-10-04 16:11:45 +00:00
|
|
|
bootstrap = ForkedLightClientBootstrap.init(parseTest(
|
2023-01-18 14:32:57 +00:00
|
|
|
path/"bootstrap.ssz_snappy", SSZ,
|
2023-10-04 16:11:45 +00:00
|
|
|
lcDataFork.LightClientBootstrap))
|
2023-01-18 14:32:57 +00:00
|
|
|
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:
|
|
|
|
bootstrap[].migrateToDataFork(lcDataFork)
|
2023-10-04 16:11:45 +00:00
|
|
|
store = ForkedLightClientStore.init(initialize_light_client_store(
|
|
|
|
meta.trusted_block_root, bootstrap[].forky(lcDataFork), cfg).get)
|
2023-01-18 14:32:57 +00:00
|
|
|
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):
|
2023-09-04 13:05:11 +00:00
|
|
|
runTest(suiteName, basePath/path)
|