2019-11-12 15:21:15 +00:00
|
|
|
# beacon_chain
|
2021-02-25 13:37:22 +00:00
|
|
|
# Copyright (c) 2018-2021 Status Research & Development GmbH
|
2019-11-12 15:21:15 +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-11-12 15:21:15 +00:00
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2021-05-28 20:32:26 +00:00
|
|
|
{.used.}
|
|
|
|
|
2019-11-12 15:21:15 +00:00
|
|
|
import
|
|
|
|
# Standard library
|
2021-04-28 16:41:02 +00:00
|
|
|
os, strutils, streams, strformat,
|
2019-11-12 15:21:15 +00:00
|
|
|
macros, sets,
|
|
|
|
# Third-party
|
|
|
|
yaml,
|
|
|
|
# Beacon chain internals
|
2021-05-28 15:25:58 +00:00
|
|
|
../../beacon_chain/spec/[crypto, digest],
|
|
|
|
../../beacon_chain/spec/datatypes/altair,
|
2019-11-12 15:21:15 +00:00
|
|
|
../../beacon_chain/ssz,
|
2021-05-24 08:42:40 +00:00
|
|
|
# Status libraries
|
|
|
|
snappy,
|
2019-11-12 15:21:15 +00:00
|
|
|
# Test utilities
|
2021-05-28 20:32:26 +00:00
|
|
|
../../testutil, ../fixtures_utils
|
2019-11-12 15:21:15 +00:00
|
|
|
|
|
|
|
# SSZ tests of consensus objects (minimal/mainnet preset specific)
|
|
|
|
|
|
|
|
# Parsing definitions
|
|
|
|
# ----------------------------------------------------------------
|
|
|
|
|
|
|
|
const
|
2021-05-28 15:25:58 +00:00
|
|
|
SSZDir = SszTestsDir/const_preset/"altair"/"ssz_static"
|
2019-11-12 15:21:15 +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
|
2020-11-24 18:07:58 +00:00
|
|
|
signing_root {.defaultVal: "".}: string
|
2019-11-12 15:21:15 +00:00
|
|
|
|
2021-05-28 15:25:58 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v1.0.1/specs/altair/validator.md#eth1block
|
2020-12-03 04:30:35 +00:00
|
|
|
Eth1Block* = object
|
|
|
|
timestamp*: uint64
|
|
|
|
deposit_root*: Eth2Digest
|
|
|
|
deposit_count*: uint64
|
|
|
|
# All other eth1 block fields
|
|
|
|
|
2019-12-16 18:08:50 +00:00
|
|
|
# Note this only tracks HashTreeRoot
|
2019-11-12 15:21:15 +00:00
|
|
|
# Checking the values against the yaml file is TODO (require more flexible Yaml parser)
|
|
|
|
|
2020-04-29 20:12:07 +00:00
|
|
|
proc checkSSZ(T: type SignedBeaconBlock, dir: string, expectedHash: SSZHashTreeRoot) =
|
|
|
|
# Deserialize into a ref object to not fill Nim stack
|
2021-05-24 08:42:40 +00:00
|
|
|
let encoded = snappy.decode(
|
|
|
|
readFileBytes(dir/"serialized.ssz_snappy"), MaxObjectSize)
|
2020-05-28 08:28:14 +00:00
|
|
|
var deserialized = newClone(sszDecodeEntireInput(encoded, T))
|
2020-04-29 20:12:07 +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)]))
|
|
|
|
|
2020-07-16 13:16:51 +00:00
|
|
|
check deserialized.root == hash_tree_root(deserialized.message)
|
2020-05-11 16:27:44 +00:00
|
|
|
check SSZ.encode(deserialized[]) == encoded
|
|
|
|
check sszSize(deserialized[]) == encoded.len
|
|
|
|
|
|
|
|
# TODO check the value (requires YAML loader)
|
2020-04-29 20:12:07 +00:00
|
|
|
|
2020-05-11 16:27:44 +00:00
|
|
|
proc checkSSZ(T: type, dir: string, expectedHash: SSZHashTreeRoot) =
|
2019-11-12 15:21:15 +00:00
|
|
|
# Deserialize into a ref object to not fill Nim stack
|
2021-05-24 08:42:40 +00:00
|
|
|
let encoded = snappy.decode(
|
|
|
|
readFileBytes(dir/"serialized.ssz_snappy"), MaxObjectSize)
|
2020-05-28 08:28:14 +00:00
|
|
|
var deserialized = newClone(sszDecodeEntireInput(encoded, T))
|
2019-11-12 15:21:15 +00:00
|
|
|
|
2020-04-29 20:12:07 +00:00
|
|
|
check: expectedHash.root == "0x" & toLowerASCII($hash_tree_root(deserialized[]))
|
2019-11-12 15:21:15 +00:00
|
|
|
|
2020-05-11 16:27:44 +00:00
|
|
|
check SSZ.encode(deserialized[]) == encoded
|
|
|
|
check sszSize(deserialized[]) == encoded.len
|
|
|
|
|
|
|
|
# TODO check the value (requires YAML loader)
|
2019-11-12 15:21:15 +00:00
|
|
|
|
|
|
|
proc loadExpectedHashTreeRoot(dir: string): SSZHashTreeRoot =
|
|
|
|
var s = openFileStream(dir/"roots.yaml")
|
|
|
|
yaml.load(s, result)
|
|
|
|
s.close()
|
|
|
|
|
|
|
|
# Test runner
|
|
|
|
# ----------------------------------------------------------------
|
|
|
|
|
2021-05-28 20:32:26 +00:00
|
|
|
suite "Official - Altair - SSZ consensus objects " & preset():
|
2019-11-14 10:40:18 +00:00
|
|
|
doAssert existsDir(SSZDir), "You need to run the \"download_test_vectors.sh\" script to retrieve the official test vectors."
|
2019-11-12 15:21:15 +00:00
|
|
|
for pathKind, sszType in walkDir(SSZDir, relative = true):
|
2019-11-22 15:47:08 +00:00
|
|
|
doAssert pathKind == pcDir
|
2019-11-12 15:21:15 +00:00
|
|
|
|
2021-04-28 16:41:02 +00:00
|
|
|
test &" Testing {sszType}":
|
2019-11-12 15:21:15 +00:00
|
|
|
let path = SSZDir/sszType
|
|
|
|
for pathKind, sszTestKind in walkDir(path, relative = true):
|
2019-11-22 15:47:08 +00:00
|
|
|
doAssert pathKind == pcDir
|
2019-11-12 15:21:15 +00:00
|
|
|
let path = SSZDir/sszType/sszTestKind
|
|
|
|
for pathKind, sszTestCase in walkDir(path, relative = true):
|
|
|
|
let path = SSZDir/sszType/sszTestKind/sszTestCase
|
|
|
|
let hash = loadExpectedHashTreeRoot(path)
|
|
|
|
|
|
|
|
case sszType:
|
2019-12-16 18:08:50 +00:00
|
|
|
of "AggregateAndProof": checkSSZ(AggregateAndProof, path, hash)
|
2019-11-12 15:21:15 +00:00
|
|
|
of "Attestation": checkSSZ(Attestation, path, hash)
|
|
|
|
of "AttestationData": checkSSZ(AttestationData, path, hash)
|
|
|
|
of "AttesterSlashing": checkSSZ(AttesterSlashing, path, hash)
|
|
|
|
of "BeaconBlock": checkSSZ(BeaconBlock, path, hash)
|
|
|
|
of "BeaconBlockBody": checkSSZ(BeaconBlockBody, path, hash)
|
2019-11-19 11:04:51 +00:00
|
|
|
of "BeaconBlockHeader": checkSSZ(BeaconBlockHeader, path, hash)
|
2019-11-12 15:21:15 +00:00
|
|
|
of "BeaconState": checkSSZ(BeaconState, path, hash)
|
|
|
|
of "Checkpoint": checkSSZ(Checkpoint, path, hash)
|
2021-05-28 15:25:58 +00:00
|
|
|
of "ContributionAndProof": checkSSZ(ContributionAndProof, path, hash)
|
2019-11-12 15:21:15 +00:00
|
|
|
of "Deposit": checkSSZ(Deposit, path, hash)
|
|
|
|
of "DepositData": checkSSZ(DepositData, path, hash)
|
2019-12-16 18:08:50 +00:00
|
|
|
of "DepositMessage": checkSSZ(DepositMessage, path, hash)
|
2020-01-14 17:46:58 +00:00
|
|
|
of "Eth1Block": checkSSZ(Eth1Block, path, hash)
|
2019-11-12 15:21:15 +00:00
|
|
|
of "Eth1Data": checkSSZ(Eth1Data, path, hash)
|
|
|
|
of "Fork": checkSSZ(Fork, path, hash)
|
2020-03-14 21:54:45 +00:00
|
|
|
of "ForkData": checkSSZ(ForkData, path, hash)
|
2019-11-12 15:21:15 +00:00
|
|
|
of "HistoricalBatch": checkSSZ(HistoricalBatch, path, hash)
|
|
|
|
of "IndexedAttestation": checkSSZ(IndexedAttestation, path, hash)
|
2021-05-28 15:25:58 +00:00
|
|
|
of "LightClientSnapshot": checkSSZ(LightClientSnapshot, path, hash)
|
|
|
|
of "LightClientUpdate": checkSSZ(LightClientUpdate, path, hash)
|
2019-11-12 15:21:15 +00:00
|
|
|
of "PendingAttestation": checkSSZ(PendingAttestation, path, hash)
|
|
|
|
of "ProposerSlashing": checkSSZ(ProposerSlashing, path, hash)
|
2020-03-14 21:54:45 +00:00
|
|
|
of "SignedAggregateAndProof":
|
|
|
|
checkSSZ(SignedAggregateAndProof, path, hash)
|
2019-12-16 18:08:50 +00:00
|
|
|
of "SignedBeaconBlock": checkSSZ(SignedBeaconBlock, path, hash)
|
|
|
|
of "SignedBeaconBlockHeader":
|
|
|
|
checkSSZ(SignedBeaconBlockHeader, path, hash)
|
2021-05-28 15:25:58 +00:00
|
|
|
of "SignedContributionAndProof":
|
|
|
|
checkSSZ(SignedContributionAndProof, path, hash)
|
2019-12-16 18:08:50 +00:00
|
|
|
of "SignedVoluntaryExit": checkSSZ(SignedVoluntaryExit, path, hash)
|
2021-05-28 15:25:58 +00:00
|
|
|
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 "SyncCommitteeSignature":
|
|
|
|
checkSSZ(SyncCommitteeSignature, path, hash)
|
2019-11-19 11:04:51 +00:00
|
|
|
of "Validator": checkSSZ(Validator, path, hash)
|
|
|
|
of "VoluntaryExit": checkSSZ(VoluntaryExit, path, hash)
|
2019-11-12 15:21:15 +00:00
|
|
|
else:
|
|
|
|
raise newException(ValueError, "Unsupported test: " & sszType)
|
|
|
|
|
2020-03-10 04:00:19 +00:00
|
|
|
summarizeLongTests("FixtureSSZConsensus")
|