2021-09-11 08:01:05 +00:00
|
|
|
# beacon_chain
|
2024-01-06 14:26:56 +00:00
|
|
|
# Copyright (c) 2018-2024 Status Research & Development GmbH
|
2021-09-11 08:01:05 +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: [].}
|
2021-09-11 08:01:05 +00:00
|
|
|
{.used.}
|
|
|
|
|
|
|
|
import
|
2024-08-05 13:57:39 +00:00
|
|
|
# Standard library
|
|
|
|
std/[
|
|
|
|
strutils, streams, strformat,
|
|
|
|
macros],
|
2021-09-11 08:01:05 +00:00
|
|
|
# Third-party
|
|
|
|
yaml,
|
|
|
|
# Beacon chain internals
|
2024-08-05 13:57:39 +00:00
|
|
|
../../../beacon_chain/spec/datatypes/altair,
|
2021-09-11 08:01:05 +00:00
|
|
|
# Status libraries
|
|
|
|
snappy,
|
|
|
|
# Test utilities
|
2023-02-10 20:59:38 +00:00
|
|
|
../../testutil, ../fixtures_utils, ../os_ops
|
2021-09-11 08:01:05 +00:00
|
|
|
|
|
|
|
# SSZ tests of consensus objects (minimal/mainnet preset specific)
|
|
|
|
|
|
|
|
# Parsing definitions
|
|
|
|
# ----------------------------------------------------------------
|
|
|
|
|
|
|
|
const
|
2024-08-05 13:57:39 +00:00
|
|
|
SSZDir = SszTestsDir/const_preset/"altair"/"ssz_static"
|
2021-09-11 08:01:05 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
SSZHashTreeRoot = object
|
|
|
|
# The test files have the values at the "root"
|
|
|
|
# so we **must** use "root" as a field name
|
|
|
|
root: string
|
|
|
|
# Some have a signing_root field
|
|
|
|
signing_root {.defaultVal: "".}: string
|
|
|
|
|
|
|
|
# Note this only tracks HashTreeRoot
|
|
|
|
# Checking the values against the yaml file is TODO (require more flexible Yaml parser)
|
|
|
|
|
2024-02-29 10:28:32 +00:00
|
|
|
proc checkSSZ(
|
2024-08-05 13:57:39 +00:00
|
|
|
T: type altair.SignedBeaconBlock,
|
2024-02-29 10:28:32 +00:00
|
|
|
dir: string,
|
|
|
|
expectedHash: SSZHashTreeRoot
|
|
|
|
) {.raises: [IOError, SerializationError, UnconsumedInput].} =
|
2024-08-05 13:57:39 +00:00
|
|
|
# Deserialize into a ref object to not fill Nim stack
|
|
|
|
let encoded = snappy.decode(
|
|
|
|
readFileBytes(dir/"serialized.ssz_snappy"), MaxObjectSize)
|
|
|
|
let deserialized = newClone(sszDecodeEntireInput(encoded, T))
|
2021-09-11 08:01:05 +00:00
|
|
|
|
2024-08-05 13:57:39 +00:00
|
|
|
# SignedBeaconBlocks usually not hashed because they're identified by
|
|
|
|
# htr(BeaconBlock), so do it manually
|
|
|
|
check: expectedHash.root == "0x" & toLowerAscii($hash_tree_root(
|
|
|
|
[hash_tree_root(deserialized.message),
|
|
|
|
hash_tree_root(deserialized.signature)]))
|
2021-09-11 08:01:05 +00:00
|
|
|
|
2024-08-05 13:57:39 +00:00
|
|
|
check deserialized.root == hash_tree_root(deserialized.message)
|
|
|
|
check SSZ.encode(deserialized[]) == encoded
|
|
|
|
check sszSize(deserialized[]) == encoded.len
|
2021-09-11 08:01:05 +00:00
|
|
|
|
2024-08-05 13:57:39 +00:00
|
|
|
# TODO check the value (requires YAML loader)
|
2021-09-11 08:01:05 +00:00
|
|
|
|
2024-02-29 10:28:32 +00:00
|
|
|
proc checkSSZ(
|
|
|
|
T: type,
|
|
|
|
dir: string,
|
|
|
|
expectedHash: SSZHashTreeRoot
|
|
|
|
) {.raises: [IOError, SerializationError, UnconsumedInput].} =
|
2021-09-11 08:01:05 +00:00
|
|
|
# Deserialize into a ref object to not fill Nim stack
|
|
|
|
let encoded = snappy.decode(
|
|
|
|
readFileBytes(dir/"serialized.ssz_snappy"), MaxObjectSize)
|
2021-09-27 14:22:58 +00:00
|
|
|
let deserialized = newClone(sszDecodeEntireInput(encoded, T))
|
2021-09-11 08:01:05 +00:00
|
|
|
|
2022-04-14 10:47:14 +00:00
|
|
|
check: expectedHash.root == "0x" & toLowerAscii($hash_tree_root(deserialized[]))
|
2021-09-11 08:01:05 +00:00
|
|
|
|
|
|
|
check SSZ.encode(deserialized[]) == encoded
|
|
|
|
check sszSize(deserialized[]) == encoded.len
|
|
|
|
|
|
|
|
# TODO check the value (requires YAML loader)
|
|
|
|
|
2024-02-29 10:28:32 +00:00
|
|
|
proc loadExpectedHashTreeRoot(
|
|
|
|
dir: string
|
|
|
|
): SSZHashTreeRoot {.raises: [
|
|
|
|
Exception, IOError, OSError, YamlConstructionError, YamlParserError].} =
|
2022-02-20 20:13:06 +00:00
|
|
|
let s = openFileStream(dir/"roots.yaml")
|
2021-09-11 08:01:05 +00:00
|
|
|
yaml.load(s, result)
|
|
|
|
s.close()
|
|
|
|
|
|
|
|
# Test runner
|
|
|
|
# ----------------------------------------------------------------
|
|
|
|
|
2024-08-05 13:57:39 +00:00
|
|
|
suite "EF - Altair - SSZ consensus objects " & preset():
|
2022-03-24 00:38:48 +00:00
|
|
|
doAssert dirExists(SSZDir), "You need to run the \"download_test_vectors.sh\" script to retrieve the consensus spec test vectors."
|
2021-09-11 08:01:05 +00:00
|
|
|
for pathKind, sszType in walkDir(SSZDir, relative = true, checkDir = true):
|
|
|
|
doAssert pathKind == pcDir
|
|
|
|
|
|
|
|
test &" Testing {sszType}":
|
|
|
|
let path = SSZDir/sszType
|
|
|
|
for pathKind, sszTestKind in walkDir(
|
|
|
|
path, relative = true, checkDir = true):
|
|
|
|
doAssert pathKind == pcDir
|
|
|
|
let path = SSZDir/sszType/sszTestKind
|
|
|
|
for pathKind, sszTestCase in walkDir(
|
|
|
|
path, relative = true, checkDir = true):
|
|
|
|
let path = SSZDir/sszType/sszTestKind/sszTestCase
|
|
|
|
let hash = loadExpectedHashTreeRoot(path)
|
|
|
|
|
|
|
|
case sszType:
|
2024-08-05 13:57:39 +00:00
|
|
|
of "AggregateAndProof": checkSSZ(phase0.AggregateAndProof, path, hash)
|
|
|
|
of "Attestation": checkSSZ(phase0.Attestation, path, hash)
|
2021-09-11 08:01:05 +00:00
|
|
|
of "AttestationData": checkSSZ(AttestationData, path, hash)
|
2024-04-21 05:49:11 +00:00
|
|
|
of "AttesterSlashing": checkSSZ(phase0.AttesterSlashing, path, hash)
|
2024-08-05 13:57:39 +00:00
|
|
|
of "BeaconBlock": checkSSZ(altair.BeaconBlock, path, hash)
|
|
|
|
of "BeaconBlockBody": checkSSZ(altair.BeaconBlockBody, path, hash)
|
2021-09-11 08:01:05 +00:00
|
|
|
of "BeaconBlockHeader": checkSSZ(BeaconBlockHeader, path, hash)
|
2024-08-05 13:57:39 +00:00
|
|
|
of "BeaconState": checkSSZ(altair.BeaconState, path, hash)
|
2021-09-11 08:01:05 +00:00
|
|
|
of "Checkpoint": checkSSZ(Checkpoint, path, hash)
|
|
|
|
of "ContributionAndProof": checkSSZ(ContributionAndProof, path, hash)
|
|
|
|
of "Deposit": checkSSZ(Deposit, path, hash)
|
|
|
|
of "DepositData": checkSSZ(DepositData, path, hash)
|
|
|
|
of "DepositMessage": checkSSZ(DepositMessage, path, hash)
|
|
|
|
of "Eth1Block": checkSSZ(Eth1Block, path, hash)
|
|
|
|
of "Eth1Data": checkSSZ(Eth1Data, path, hash)
|
|
|
|
of "Fork": checkSSZ(Fork, path, hash)
|
|
|
|
of "ForkData": checkSSZ(ForkData, path, hash)
|
|
|
|
of "HistoricalBatch": checkSSZ(HistoricalBatch, path, hash)
|
2024-04-21 05:49:11 +00:00
|
|
|
of "IndexedAttestation":
|
|
|
|
checkSSZ(phase0.IndexedAttestation, path, hash)
|
2023-01-14 21:19:50 +00:00
|
|
|
of "LightClientBootstrap":
|
|
|
|
checkSSZ(altair.LightClientBootstrap, path, hash)
|
|
|
|
of "LightClientHeader":
|
|
|
|
checkSSZ(altair.LightClientHeader, path, hash)
|
|
|
|
of "LightClientUpdate":
|
|
|
|
checkSSZ(altair.LightClientUpdate, path, hash)
|
2022-05-23 12:02:54 +00:00
|
|
|
of "LightClientFinalityUpdate":
|
2023-01-14 21:19:50 +00:00
|
|
|
checkSSZ(altair.LightClientFinalityUpdate, path, hash)
|
2022-05-23 12:02:54 +00:00
|
|
|
of "LightClientOptimisticUpdate":
|
2023-01-14 21:19:50 +00:00
|
|
|
checkSSZ(altair.LightClientOptimisticUpdate, path, hash)
|
2021-09-11 08:01:05 +00:00
|
|
|
of "PendingAttestation": checkSSZ(PendingAttestation, path, hash)
|
|
|
|
of "ProposerSlashing": checkSSZ(ProposerSlashing, path, hash)
|
|
|
|
of "SignedAggregateAndProof":
|
2024-08-05 13:57:39 +00:00
|
|
|
checkSSZ(phase0.SignedAggregateAndProof, path, hash)
|
|
|
|
of "SignedBeaconBlock": checkSSZ(altair.SignedBeaconBlock, path, hash)
|
2021-09-11 08:01:05 +00:00
|
|
|
of "SignedBeaconBlockHeader":
|
|
|
|
checkSSZ(SignedBeaconBlockHeader, path, hash)
|
|
|
|
of "SignedContributionAndProof":
|
|
|
|
checkSSZ(SignedContributionAndProof, path, hash)
|
|
|
|
of "SignedVoluntaryExit": checkSSZ(SignedVoluntaryExit, path, hash)
|
|
|
|
of "SigningData": checkSSZ(SigningData, path, hash)
|
|
|
|
of "SyncAggregate": checkSSZ(SyncAggregate, path, hash)
|
|
|
|
of "SyncAggregatorSelectionData":
|
|
|
|
checkSSZ(SyncAggregatorSelectionData, path, hash)
|
|
|
|
of "SyncCommittee": checkSSZ(SyncCommittee, path, hash)
|
|
|
|
of "SyncCommitteeContribution":
|
|
|
|
checkSSZ(SyncCommitteeContribution, path, hash)
|
|
|
|
of "SyncCommitteeMessage": checkSSZ(SyncCommitteeMessage, path, hash)
|
|
|
|
of "Validator": checkSSZ(Validator, path, hash)
|
|
|
|
of "VoluntaryExit": checkSSZ(VoluntaryExit, path, hash)
|
|
|
|
else:
|
2024-04-21 05:49:11 +00:00
|
|
|
raise newException(ValueError, "Unsupported test: " & sszType)
|