Skip to content

Commit

Permalink
extend EF merkle tests for Bellatrix
Browse files Browse the repository at this point in the history
  • Loading branch information
etan-status committed Jul 15, 2022
1 parent da77fda commit 1341658
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 70 deletions.
12 changes: 7 additions & 5 deletions ConsensusSpecPreset-mainnet.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@ ConsensusSpecPreset-mainnet
ForkChoice - mainnet/phase0/fork_choice/on_block/pyspec_tests/on_block_future_block Skip
+ ForkChoice - mainnet/phase0/fork_choice/on_block/pyspec_tests/proposer_boost OK
+ ForkChoice - mainnet/phase0/fork_choice/on_block/pyspec_tests/proposer_boost_root_same_slo OK
+ Merkle - Single proof - mainnet/altair/merkle/single_proof/pyspec_tests/finality_root_merk OK
+ Merkle - Single proof - mainnet/altair/merkle/single_proof/pyspec_tests/next_sync_committe OK
+ Merkle - Single proof - mainnet/bellatrix/merkle/single_proof/pyspec_tests/finality_root_m OK
+ Merkle - Single proof - mainnet/bellatrix/merkle/single_proof/pyspec_tests/next_sync_commi OK
+ Slots - double_empty_epoch OK
+ Slots - empty_epoch OK
+ Slots - over_epoch_boundary OK
Expand Down Expand Up @@ -422,16 +426,14 @@ ConsensusSpecPreset-mainnet
+ bellatrix_fork_random_3 OK
+ bellatrix_fork_random_low_balances OK
+ bellatrix_fork_random_misc_balances OK
+ finality_root_merkle_proof OK
+ fork_base_state OK
+ fork_many_next_epoch OK
+ fork_next_epoch OK
+ fork_next_epoch_with_block OK
+ fork_random_low_balances OK
+ fork_random_misc_balances OK
+ next_sync_committee_merkle_proof OK
```
OK: 421/428 Fail: 0/428 Skip: 7/428
OK: 423/430 Fail: 0/430 Skip: 7/430
## Attestation
```diff
+ [Invalid] EF - Altair - Operations - Attestation - after_epoch_slots OK
Expand Down Expand Up @@ -852,7 +854,7 @@ OK: 1/1 Fail: 0/1 Skip: 0/1
+ Testing VoluntaryExit OK
```
OK: 35/35 Fail: 0/35 Skip: 0/35
## EF - Altair - Unittests - Sync protocol [Preset: mainnet]
## EF - Altair - Unittests - Light client - Sync protocol [Preset: mainnet]
```diff
+ process_light_client_update_finality_updated OK
+ process_light_client_update_timeout OK
Expand Down Expand Up @@ -1251,4 +1253,4 @@ OK: 44/44 Fail: 0/44 Skip: 0/44
OK: 27/27 Fail: 0/27 Skip: 0/27

---TOTAL---
OK: 1074/1081 Fail: 0/1081 Skip: 7/1081
OK: 1076/1083 Fail: 0/1083 Skip: 7/1083
12 changes: 7 additions & 5 deletions ConsensusSpecPreset-minimal.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,10 @@ ConsensusSpecPreset-minimal
+ ForkChoice - minimal/phase0/fork_choice/on_block/pyspec_tests/on_block_update_justified_ch OK
+ ForkChoice - minimal/phase0/fork_choice/on_block/pyspec_tests/proposer_boost OK
+ ForkChoice - minimal/phase0/fork_choice/on_block/pyspec_tests/proposer_boost_root_same_slo OK
+ Merkle - Single proof - minimal/altair/merkle/single_proof/pyspec_tests/finality_root_merk OK
+ Merkle - Single proof - minimal/altair/merkle/single_proof/pyspec_tests/next_sync_committe OK
+ Merkle - Single proof - minimal/bellatrix/merkle/single_proof/pyspec_tests/finality_root_m OK
+ Merkle - Single proof - minimal/bellatrix/merkle/single_proof/pyspec_tests/next_sync_commi OK
+ Slots - double_empty_epoch OK
+ Slots - empty_epoch OK
+ Slots - over_epoch_boundary OK
Expand Down Expand Up @@ -471,17 +475,15 @@ ConsensusSpecPreset-minimal
+ bellatrix_fork_random_large_validator_set OK
+ bellatrix_fork_random_low_balances OK
+ bellatrix_fork_random_misc_balances OK
+ finality_root_merkle_proof OK
+ fork_base_state OK
+ fork_many_next_epoch OK
+ fork_next_epoch OK
+ fork_next_epoch_with_block OK
+ fork_random_large_validator_set OK
+ fork_random_low_balances OK
+ fork_random_misc_balances OK
+ next_sync_committee_merkle_proof OK
```
OK: 471/478 Fail: 0/478 Skip: 7/478
OK: 473/480 Fail: 0/480 Skip: 7/480
## Attestation
```diff
+ [Invalid] EF - Altair - Operations - Attestation - after_epoch_slots OK
Expand Down Expand Up @@ -917,7 +919,7 @@ OK: 5/5 Fail: 0/5 Skip: 0/5
+ Testing VoluntaryExit OK
```
OK: 35/35 Fail: 0/35 Skip: 0/35
## EF - Altair - Unittests - Sync protocol [Preset: minimal]
## EF - Altair - Unittests - Light client - Sync protocol [Preset: minimal]
```diff
+ process_light_client_update_finality_updated OK
+ process_light_client_update_timeout OK
Expand Down Expand Up @@ -1342,4 +1344,4 @@ OK: 48/48 Fail: 0/48 Skip: 0/48
OK: 30/30 Fail: 0/30 Skip: 0/30

---TOTAL---
OK: 1157/1164 Fail: 0/1164 Skip: 7/1164
OK: 1159/1166 Fail: 0/1166 Skip: 7/1166
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func initialize_light_client_store(state: auto): LightClientStore =
current_max_active_participants: 0,
)

suite "EF - Altair - Unittests - Sync protocol" & preset():
suite "EF - Altair - Unittests - Light client - Sync protocol" & preset():
let
cfg = block:
var res = defaultRuntimeConfig
Expand Down
24 changes: 24 additions & 0 deletions tests/consensus_spec/fixtures_utils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import
# Standard library
std/[os, strutils, typetraits],
# Internals
../../beacon_chain/spec/datatypes/[phase0, altair, bellatrix],
../../beacon_chain/spec/[
eth2_merkleization, eth2_ssz_serialization, forks],
# Status libs,
Expand Down Expand Up @@ -112,3 +113,26 @@ proc parseTest*(path: string, Format: typedesc[SSZ], T: typedesc): T =
stderr.write $Format & " load issue for file \"", path, "\"\n"
stderr.write err.formatMsg(path), "\n"
quit 1

proc loadForkedState*(
path: string, fork: BeaconStateFork): ref ForkedHashedBeaconState =
# TODO stack usage. newClone and assignClone do not seem to
# prevent temporaries created by case objects
let forkedState = new ForkedHashedBeaconState
case fork
of BeaconStateFork.Bellatrix:
let state = newClone(parseTest(path, SSZ, bellatrix.BeaconState))
forkedState.kind = BeaconStateFork.Bellatrix
forkedState.bellatrixData.data = state[]
forkedState.bellatrixData.root = hash_tree_root(state[])
of BeaconStateFork.Altair:
let state = newClone(parseTest(path, SSZ, altair.BeaconState))
forkedState.kind = BeaconStateFork.Altair
forkedState.altairData.data = state[]
forkedState.altairData.root = hash_tree_root(state[])
of BeaconStateFork.Phase0:
let state = newClone(parseTest(path, SSZ, phase0.BeaconState))
forkedState.kind = BeaconStateFork.Phase0
forkedState.phase0Data.data = state[]
forkedState.phase0Data.root = hash_tree_root(state[])
forkedState
32 changes: 7 additions & 25 deletions tests/consensus_spec/test_fixture_fork_choice.nim
Original file line number Diff line number Diff line change
Expand Up @@ -62,32 +62,14 @@ proc initialLoad(
path: string, db: BeaconChainDB,
StateType, BlockType: typedesc
): tuple[dag: ChainDAGRef, fkChoice: ref ForkChoice] =
let state = newClone(parseTest(
path/"anchor_state.ssz_snappy",
SSZ, StateType
))

# TODO stack usage. newClone and assignClone do not seem to
# prevent temporaries created by case objects
let forkedState = new ForkedHashedBeaconState
when StateType is bellatrix.BeaconState:
forkedState.kind = BeaconStateFork.Bellatrix
forkedState.bellatrixData.data = state[]
forkedState.bellatrixData.root = hash_tree_root(state[])
elif StateType is altair.BeaconState:
forkedState.kind = BeaconStateFork.Altair
forkedState.altairData.data = state[]
forkedState.altairData.root = hash_tree_root(state[])
elif StateType is phase0.BeaconState:
forkedState.kind = BeaconStateFork.Phase0
forkedState.phase0Data.data = state[]
forkedState.phase0Data.root = hash_tree_root(state[])
else: {.error: "Unknown state fork: " & name(StateType).}
let
forkedState = loadForkedState(
path/"anchor_state.ssz_snappy",
StateType.toFork)

let blck = parseTest(
path/"anchor_block.ssz_snappy",
SSZ, BlockType
)
blck = parseTest(
path/"anchor_block.ssz_snappy",
SSZ, BlockType)

when BlockType is bellatrix.BeaconBlock:
let signedBlock = ForkedSignedBeaconBlock.init(bellatrix.SignedBeaconBlock(
Expand Down
71 changes: 37 additions & 34 deletions tests/consensus_spec/test_fixture_merkle_single_proof.nim
Original file line number Diff line number Diff line change
Expand Up @@ -21,43 +21,46 @@ import
../testutil,
./fixtures_utils

const TestsDir =
SszTestsDir/const_preset/"altair"/"merkle"/"single_proof"/"pyspec_tests"

proc runTest(identifier: string) =
let testDir = TestsDir / identifier

proc `testImpl _ merkle_single_proof _ identifier`() =
test identifier:
type
TestProof = object
leaf: string
leaf_index: GeneralizedIndex
branch: seq[string]

let
proof = block:
let s = openFileStream(testDir/"proof.yaml")
defer: close(s)
var res: TestProof
yaml.load(s, res)
res

state = newClone(parseTest(testDir/"state.ssz_snappy", SSZ,
altair.BeaconState))
proc runTest(path: string, fork: BeaconStateFork) =
test "Merkle - Single proof - " & path.relativePath(SszTestsDir):
type
TestProof = object
leaf: string
leaf_index: GeneralizedIndex
branch: seq[string]

let
proof = block:
let s = openFileStream(path/"proof.yaml")
defer: close(s)
var res: TestProof
yaml.load(s, res)
res

state = loadForkedState(path/"state.ssz_snappy", fork)

withState(state[]):
var computedProof = newSeq[Eth2Digest](log2trunc(proof.leaf_index))
build_proof(state[], proof.leaf_index, computedProof).get
build_proof(state.data, proof.leaf_index, computedProof).get

check:
computedProof == proof.branch.mapIt(Eth2Digest.fromHex(it))
is_valid_merkle_branch(Eth2Digest.fromHex(proof.leaf), computedProof,
log2trunc(proof.leaf_index),
get_subtree_index(proof.leaf_index),
hash_tree_root(state[]))

`testImpl _ merkle_single_proof _ identifier`()
is_valid_merkle_branch(
Eth2Digest.fromHex(proof.leaf),
computedProof,
log2trunc(proof.leaf_index),
get_subtree_index(proof.leaf_index),
state.root)

suite "EF - Altair - Merkle - Single proof" & preset():
for kind, path in walkDir(TestsDir, relative = true, checkDir = true):
runTest(path)
suite "EF - Merkle - Single proof" & preset():
const presetPath = SszTestsDir/const_preset
for kind, path in walkDir(presetPath, relative = true, checkDir = true):
let testsPath = presetPath/path/"merkle"/"single_proof"
if kind != pcDir or not dirExists(testsPath):
continue
let
fork = forkForPathComponent(path).valueOr:
raiseAssert "Unknown test fork: " & testsPath
basePath = testsPath/"pyspec_tests"
for kind, path in walkDir(basePath, relative = true, checkDir = true):
runTest(basePath/path, fork)

0 comments on commit 1341658

Please sign in to comment.