2022-12-05 21:36:53 +00:00
|
|
|
# beacon_chain
|
2023-01-09 14:15:43 +00:00
|
|
|
# Copyright (c) 2022-2023 Status Research & Development GmbH
|
2022-12-05 21:36:53 +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
|
|
|
strutils, streams, strformat,
|
2022-12-05 21:36:53 +00:00
|
|
|
macros, sets,
|
|
|
|
# Third-party
|
|
|
|
yaml,
|
|
|
|
# Beacon chain internals
|
2023-02-21 12:58:08 +00:00
|
|
|
../../beacon_chain/spec/datatypes/[altair, deneb],
|
2022-12-05 21:36:53 +00:00
|
|
|
# Status libraries
|
|
|
|
snappy,
|
|
|
|
# Test utilities
|
2023-02-10 20:59:38 +00:00
|
|
|
../../testutil, ../fixtures_utils, ../os_ops
|
2022-12-05 21:36:53 +00:00
|
|
|
|
|
|
|
from ../../beacon_chain/spec/datatypes/bellatrix import PowBlock
|
|
|
|
from ../../beacon_chain/spec/datatypes/capella import
|
2023-01-09 14:15:43 +00:00
|
|
|
BLSToExecutionChange, SignedBLSToExecutionChange, HistoricalSummary,
|
|
|
|
Withdrawal
|
2022-12-05 21:36:53 +00:00
|
|
|
|
|
|
|
# SSZ tests of consensus objects (minimal/mainnet preset specific)
|
|
|
|
|
|
|
|
# Parsing definitions
|
|
|
|
# ----------------------------------------------------------------
|
|
|
|
|
|
|
|
const
|
2023-02-21 12:58:08 +00:00
|
|
|
SSZDir = SszTestsDir/const_preset/"deneb"/"ssz_static"
|
2022-12-05 21:36:53 +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)
|
|
|
|
|
2023-02-21 12:58:08 +00:00
|
|
|
proc checkSSZ(T: type deneb.SignedBeaconBlock, dir: string, expectedHash: SSZHashTreeRoot) =
|
2022-12-05 21:36:53 +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))
|
|
|
|
|
|
|
|
# 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)]))
|
|
|
|
|
|
|
|
check deserialized.root == hash_tree_root(deserialized.message)
|
|
|
|
check SSZ.encode(deserialized[]) == encoded
|
|
|
|
check sszSize(deserialized[]) == encoded.len
|
|
|
|
|
|
|
|
# TODO check the value (requires YAML loader)
|
|
|
|
|
|
|
|
proc checkSSZ(T: type, dir: string, expectedHash: SSZHashTreeRoot) =
|
|
|
|
# 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))
|
|
|
|
|
|
|
|
check: expectedHash.root == "0x" & toLowerAscii($hash_tree_root(deserialized[]))
|
|
|
|
|
|
|
|
check SSZ.encode(deserialized[]) == encoded
|
|
|
|
check sszSize(deserialized[]) == encoded.len
|
|
|
|
|
|
|
|
# TODO check the value (requires YAML loader)
|
|
|
|
|
|
|
|
proc loadExpectedHashTreeRoot(dir: string): SSZHashTreeRoot =
|
|
|
|
let s = openFileStream(dir/"roots.yaml")
|
|
|
|
yaml.load(s, result)
|
|
|
|
s.close()
|
|
|
|
|
|
|
|
# Test runner
|
|
|
|
# ----------------------------------------------------------------
|
|
|
|
|
2023-02-14 13:32:43 +00:00
|
|
|
suite "EF - Deneb - SSZ consensus objects " & preset():
|
2022-12-05 21:36:53 +00:00
|
|
|
doAssert dirExists(SSZDir), "You need to run the \"download_test_vectors.sh\" script to retrieve the consensus spec test vectors."
|
|
|
|
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:
|
|
|
|
of "AggregateAndProof": checkSSZ(AggregateAndProof, path, hash)
|
|
|
|
of "Attestation": checkSSZ(Attestation, path, hash)
|
|
|
|
of "AttestationData": checkSSZ(AttestationData, path, hash)
|
|
|
|
of "AttesterSlashing": checkSSZ(AttesterSlashing, path, hash)
|
2023-02-21 12:58:08 +00:00
|
|
|
of "BeaconBlock": checkSSZ(deneb.BeaconBlock, path, hash)
|
|
|
|
of "BeaconBlockBody": checkSSZ(deneb.BeaconBlockBody, path, hash)
|
2022-12-05 21:36:53 +00:00
|
|
|
of "BeaconBlockHeader": checkSSZ(BeaconBlockHeader, path, hash)
|
2023-02-21 12:58:08 +00:00
|
|
|
of "BeaconState": checkSSZ(deneb.BeaconState, path, hash)
|
|
|
|
of "BlobIdentifier": checkSSZ(BlobIdentifier, path, hash)
|
|
|
|
of "BlobSidecar": checkSSZ(BlobSidecar, path, hash)
|
2022-12-05 21:36:53 +00:00
|
|
|
of "BLSToExecutionChange": checkSSZ(BLSToExecutionChange, path, hash)
|
|
|
|
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 "ExecutionPayload": checkSSZ(ExecutionPayload, path, hash)
|
|
|
|
of "ExecutionPayloadHeader":
|
|
|
|
checkSSZ(ExecutionPayloadHeader, path, hash)
|
|
|
|
of "Fork": checkSSZ(Fork, path, hash)
|
|
|
|
of "ForkData": checkSSZ(ForkData, path, hash)
|
|
|
|
of "HistoricalBatch": checkSSZ(HistoricalBatch, path, hash)
|
2023-01-09 14:15:43 +00:00
|
|
|
of "HistoricalSummary": checkSSZ(HistoricalSummary, path, hash)
|
2022-12-05 21:36:53 +00:00
|
|
|
of "IndexedAttestation": checkSSZ(IndexedAttestation, path, hash)
|
2023-01-14 21:19:50 +00:00
|
|
|
of "LightClientBootstrap":
|
2023-02-21 12:58:08 +00:00
|
|
|
checkSSZ(deneb.LightClientBootstrap, path, hash)
|
|
|
|
of "LightClientHeader": checkSSZ(deneb.LightClientHeader, path, hash)
|
|
|
|
of "LightClientUpdate": checkSSZ(deneb.LightClientUpdate, path, hash)
|
2022-12-05 21:36:53 +00:00
|
|
|
of "LightClientFinalityUpdate":
|
2023-02-21 12:58:08 +00:00
|
|
|
checkSSZ(deneb.LightClientFinalityUpdate, path, hash)
|
2022-12-05 21:36:53 +00:00
|
|
|
of "LightClientOptimisticUpdate":
|
2023-02-21 12:58:08 +00:00
|
|
|
checkSSZ(deneb.LightClientOptimisticUpdate, path, hash)
|
2022-12-05 21:36:53 +00:00
|
|
|
of "PendingAttestation": checkSSZ(PendingAttestation, path, hash)
|
|
|
|
of "PowBlock": checkSSZ(PowBlock, path, hash)
|
|
|
|
of "ProposerSlashing": checkSSZ(ProposerSlashing, path, hash)
|
|
|
|
of "SignedAggregateAndProof":
|
|
|
|
checkSSZ(SignedAggregateAndProof, path, hash)
|
|
|
|
of "SignedBeaconBlock":
|
2023-02-21 12:58:08 +00:00
|
|
|
checkSSZ(deneb.SignedBeaconBlock, path, hash)
|
2022-12-05 21:36:53 +00:00
|
|
|
of "SignedBeaconBlockHeader":
|
|
|
|
checkSSZ(SignedBeaconBlockHeader, path, hash)
|
2023-02-21 12:58:08 +00:00
|
|
|
of "SignedBlobSidecar": checkSSZ(SignedBlobSidecar, path, hash)
|
2022-12-05 21:36:53 +00:00
|
|
|
of "SignedBLSToExecutionChange":
|
|
|
|
checkSSZ(SignedBLSToExecutionChange, 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 "Withdrawal": checkSSZ(Withdrawal, path, hash)
|
|
|
|
of "Validator": checkSSZ(Validator, path, hash)
|
|
|
|
of "VoluntaryExit": checkSSZ(VoluntaryExit, path, hash)
|
|
|
|
else:
|
|
|
|
raise newException(ValueError, "Unsupported test: " & sszType)
|