From cd993ca41852616b356411543133942139c3bda8 Mon Sep 17 00:00:00 2001 From: tersec Date: Tue, 13 Dec 2022 15:58:59 +0000 Subject: [PATCH] consolidate slot sanity checks across forks (#4418) * consolidate slot sanity checks across forks * use `new` rather than `init` for constructor of `ref`s --- ConsensusSpecPreset-mainnet.md | 34 +++++-- ConsensusSpecPreset-minimal.md | 34 +++++-- beacon_chain/spec/forks.nim | 33 ++++--- beacon_chain/spec/state_transition.nim | 2 - .../altair/all_altair_fixtures.nim | 1 - .../altair/test_fixture_sanity_slots.nim | 53 ----------- .../bellatrix/all_bellatrix_fixtures.nim | 1 - .../bellatrix/test_fixture_sanity_slots.nim | 55 ------------ .../capella/all_capella_fixtures.nim | 1 - .../capella/test_fixture_sanity_slots.nim | 55 ------------ .../consensus_spec_tests_preset.nim | 3 +- .../eip4844/all_eip4844_fixtures.nim | 1 - .../eip4844/test_fixture_sanity_slots.nim | 55 ------------ .../phase0/all_phase0_fixtures.nim | 1 - .../phase0/test_fixture_sanity_slots.nim | 52 ----------- .../test_fixture_sanity_slots.nim | 90 +++++++++++++++++++ 16 files changed, 168 insertions(+), 303 deletions(-) delete mode 100644 tests/consensus_spec/altair/test_fixture_sanity_slots.nim delete mode 100644 tests/consensus_spec/bellatrix/test_fixture_sanity_slots.nim delete mode 100644 tests/consensus_spec/capella/test_fixture_sanity_slots.nim delete mode 100644 tests/consensus_spec/eip4844/test_fixture_sanity_slots.nim delete mode 100644 tests/consensus_spec/phase0/test_fixture_sanity_slots.nim create mode 100644 tests/consensus_spec/test_fixture_sanity_slots.nim diff --git a/ConsensusSpecPreset-mainnet.md b/ConsensusSpecPreset-mainnet.md index 715e785bd..6e7524275 100644 --- a/ConsensusSpecPreset-mainnet.md +++ b/ConsensusSpecPreset-mainnet.md @@ -36,6 +36,11 @@ ConsensusSpecPreset-mainnet + EF - Altair - Rewards - with_not_yet_activated_validators_leak [Preset: mainnet] OK + EF - Altair - Rewards - with_slashed_validators [Preset: mainnet] OK + EF - Altair - Rewards - with_slashed_validators_leak [Preset: mainnet] OK ++ EF - Altair - Slots - double_empty_epoch [Preset: mainnet] OK ++ EF - Altair - Slots - empty_epoch [Preset: mainnet] OK ++ EF - Altair - Slots - over_epoch_boundary [Preset: mainnet] OK ++ EF - Altair - Slots - slots_1 [Preset: mainnet] OK ++ EF - Altair - Slots - slots_2 [Preset: mainnet] OK + EF - Altair - Transition - normal_transition [Preset: mainnet] OK + EF - Altair - Transition - simple_transition [Preset: mainnet] OK + EF - Altair - Transition - transition_missing_first_post_block [Preset: mainnet] OK @@ -91,6 +96,11 @@ ConsensusSpecPreset-mainnet + EF - Bellatrix - Rewards - with_not_yet_activated_validators_leak [Preset: mainnet] OK + EF - Bellatrix - Rewards - with_slashed_validators [Preset: mainnet] OK + EF - Bellatrix - Rewards - with_slashed_validators_leak [Preset: mainnet] OK ++ EF - Bellatrix - Slots - double_empty_epoch [Preset: mainnet] OK ++ EF - Bellatrix - Slots - empty_epoch [Preset: mainnet] OK ++ EF - Bellatrix - Slots - over_epoch_boundary [Preset: mainnet] OK ++ EF - Bellatrix - Slots - slots_1 [Preset: mainnet] OK ++ EF - Bellatrix - Slots - slots_2 [Preset: mainnet] OK + EF - Bellatrix - Transition - normal_transition [Preset: mainnet] OK + EF - Bellatrix - Transition - simple_transition [Preset: mainnet] OK + EF - Bellatrix - Transition - transition_missing_first_post_block [Preset: mainnet] OK @@ -146,6 +156,11 @@ ConsensusSpecPreset-mainnet + EF - Capella - Rewards - with_not_yet_activated_validators_leak [Preset: mainnet] OK + EF - Capella - Rewards - with_slashed_validators [Preset: mainnet] OK + EF - Capella - Rewards - with_slashed_validators_leak [Preset: mainnet] OK ++ EF - Capella - Slots - double_empty_epoch [Preset: mainnet] OK ++ EF - Capella - Slots - empty_epoch [Preset: mainnet] OK ++ EF - Capella - Slots - over_epoch_boundary [Preset: mainnet] OK ++ EF - Capella - Slots - slots_1 [Preset: mainnet] OK ++ EF - Capella - Slots - slots_2 [Preset: mainnet] OK + EF - Capella - Transition - normal_transition [Preset: mainnet] OK + EF - Capella - Transition - simple_transition [Preset: mainnet] OK + EF - Capella - Transition - transition_missing_first_post_block [Preset: mainnet] OK @@ -201,6 +216,11 @@ ConsensusSpecPreset-mainnet + EF - EIP4844 - Rewards - with_not_yet_activated_validators_leak [Preset: mainnet] OK + EF - EIP4844 - Rewards - with_slashed_validators [Preset: mainnet] OK + EF - EIP4844 - Rewards - with_slashed_validators_leak [Preset: mainnet] OK ++ EF - EIP4844 - Slots - double_empty_epoch [Preset: mainnet] OK ++ EF - EIP4844 - Slots - empty_epoch [Preset: mainnet] OK ++ EF - EIP4844 - Slots - over_epoch_boundary [Preset: mainnet] OK ++ EF - EIP4844 - Slots - slots_1 [Preset: mainnet] OK ++ EF - EIP4844 - Slots - slots_2 [Preset: mainnet] OK + EF - EIP4844 - Transition - normal_transition [Preset: mainnet] OK + EF - EIP4844 - Transition - simple_transition [Preset: mainnet] OK + EF - EIP4844 - Transition - transition_missing_first_post_block [Preset: mainnet] OK @@ -271,6 +291,11 @@ ConsensusSpecPreset-mainnet + EF - Phase 0 - Rewards - with_not_yet_activated_validators_leak [Preset: mainnet] OK + EF - Phase 0 - Rewards - with_slashed_validators [Preset: mainnet] OK + EF - Phase 0 - Rewards - with_slashed_validators_leak [Preset: mainnet] OK ++ EF - Phase 0 - Slots - double_empty_epoch [Preset: mainnet] OK ++ EF - Phase 0 - Slots - empty_epoch [Preset: mainnet] OK ++ EF - Phase 0 - Slots - over_epoch_boundary [Preset: mainnet] OK ++ EF - Phase 0 - Slots - slots_1 [Preset: mainnet] OK ++ EF - Phase 0 - Slots - slots_2 [Preset: mainnet] OK + ForkChoice - mainnet/altair/fork_choice/ex_ante/pyspec_tests/ex_ante_attestations_is_great OK + ForkChoice - mainnet/altair/fork_choice/ex_ante/pyspec_tests/ex_ante_sandwich_with_boost_n OK + ForkChoice - mainnet/altair/fork_choice/ex_ante/pyspec_tests/ex_ante_sandwich_with_honest_ OK @@ -351,11 +376,6 @@ ConsensusSpecPreset-mainnet + Light client - Single merkle proof - mainnet/eip4844/light_client/single_merkle_proof/Beac OK + Light client - Single merkle proof - mainnet/eip4844/light_client/single_merkle_proof/Beac OK + Light client - Single merkle proof - mainnet/eip4844/light_client/single_merkle_proof/Beac OK -+ Slots - double_empty_epoch OK -+ Slots - empty_epoch OK -+ Slots - over_epoch_boundary OK -+ Slots - slots_1 OK -+ Slots - slots_2 OK + Sync - mainnet/bellatrix/sync/optimistic/pyspec_tests/from_syncing_to_invalid OK + Sync - mainnet/capella/sync/optimistic/pyspec_tests/from_syncing_to_invalid OK + [Invalid] EF - Altair - Sanity - Blocks - expected_deposit_in_block [Preset: mainnet] OK @@ -744,7 +764,7 @@ ConsensusSpecPreset-mainnet + fork_random_low_balances OK + fork_random_misc_balances OK ``` -OK: 733/741 Fail: 0/741 Skip: 8/741 +OK: 753/761 Fail: 0/761 Skip: 8/761 ## Attestation ```diff + [Invalid] EF - Altair - Operations - Attestation - after_epoch_slots OK @@ -2419,4 +2439,4 @@ OK: 63/63 Fail: 0/63 Skip: 0/63 OK: 51/51 Fail: 0/51 Skip: 0/51 ---TOTAL--- -OK: 2125/2133 Fail: 0/2133 Skip: 8/2133 +OK: 2145/2153 Fail: 0/2153 Skip: 8/2153 diff --git a/ConsensusSpecPreset-minimal.md b/ConsensusSpecPreset-minimal.md index 85c446e0a..6f5df9b21 100644 --- a/ConsensusSpecPreset-minimal.md +++ b/ConsensusSpecPreset-minimal.md @@ -36,6 +36,11 @@ ConsensusSpecPreset-minimal + EF - Altair - Rewards - with_not_yet_activated_validators_leak [Preset: minimal] OK + EF - Altair - Rewards - with_slashed_validators [Preset: minimal] OK + EF - Altair - Rewards - with_slashed_validators_leak [Preset: minimal] OK ++ EF - Altair - Slots - double_empty_epoch [Preset: minimal] OK ++ EF - Altair - Slots - empty_epoch [Preset: minimal] OK ++ EF - Altair - Slots - over_epoch_boundary [Preset: minimal] OK ++ EF - Altair - Slots - slots_1 [Preset: minimal] OK ++ EF - Altair - Slots - slots_2 [Preset: minimal] OK + EF - Altair - Transition - normal_transition [Preset: minimal] OK + EF - Altair - Transition - simple_transition [Preset: minimal] OK + EF - Altair - Transition - transition_missing_first_post_block [Preset: minimal] OK @@ -95,6 +100,11 @@ ConsensusSpecPreset-minimal + EF - Bellatrix - Rewards - with_not_yet_activated_validators_leak [Preset: minimal] OK + EF - Bellatrix - Rewards - with_slashed_validators [Preset: minimal] OK + EF - Bellatrix - Rewards - with_slashed_validators_leak [Preset: minimal] OK ++ EF - Bellatrix - Slots - double_empty_epoch [Preset: minimal] OK ++ EF - Bellatrix - Slots - empty_epoch [Preset: minimal] OK ++ EF - Bellatrix - Slots - over_epoch_boundary [Preset: minimal] OK ++ EF - Bellatrix - Slots - slots_1 [Preset: minimal] OK ++ EF - Bellatrix - Slots - slots_2 [Preset: minimal] OK + EF - Bellatrix - Transition - normal_transition [Preset: minimal] OK + EF - Bellatrix - Transition - simple_transition [Preset: minimal] OK + EF - Bellatrix - Transition - transition_missing_first_post_block [Preset: minimal] OK @@ -154,6 +164,11 @@ ConsensusSpecPreset-minimal + EF - Capella - Rewards - with_not_yet_activated_validators_leak [Preset: minimal] OK + EF - Capella - Rewards - with_slashed_validators [Preset: minimal] OK + EF - Capella - Rewards - with_slashed_validators_leak [Preset: minimal] OK ++ EF - Capella - Slots - double_empty_epoch [Preset: minimal] OK ++ EF - Capella - Slots - empty_epoch [Preset: minimal] OK ++ EF - Capella - Slots - over_epoch_boundary [Preset: minimal] OK ++ EF - Capella - Slots - slots_1 [Preset: minimal] OK ++ EF - Capella - Slots - slots_2 [Preset: minimal] OK + EF - Capella - Transition - normal_transition [Preset: minimal] OK + EF - Capella - Transition - simple_transition [Preset: minimal] OK + EF - Capella - Transition - transition_missing_first_post_block [Preset: minimal] OK @@ -213,6 +228,11 @@ ConsensusSpecPreset-minimal + EF - EIP4844 - Rewards - with_not_yet_activated_validators_leak [Preset: minimal] OK + EF - EIP4844 - Rewards - with_slashed_validators [Preset: minimal] OK + EF - EIP4844 - Rewards - with_slashed_validators_leak [Preset: minimal] OK ++ EF - EIP4844 - Slots - double_empty_epoch [Preset: minimal] OK ++ EF - EIP4844 - Slots - empty_epoch [Preset: minimal] OK ++ EF - EIP4844 - Slots - over_epoch_boundary [Preset: minimal] OK ++ EF - EIP4844 - Slots - slots_1 [Preset: minimal] OK ++ EF - EIP4844 - Slots - slots_2 [Preset: minimal] OK + EF - EIP4844 - Transition - normal_transition [Preset: minimal] OK + EF - EIP4844 - Transition - simple_transition [Preset: minimal] OK + EF - EIP4844 - Transition - transition_missing_first_post_block [Preset: minimal] OK @@ -287,6 +307,11 @@ ConsensusSpecPreset-minimal + EF - Phase 0 - Rewards - with_not_yet_activated_validators_leak [Preset: minimal] OK + EF - Phase 0 - Rewards - with_slashed_validators [Preset: minimal] OK + EF - Phase 0 - Rewards - with_slashed_validators_leak [Preset: minimal] OK ++ EF - Phase 0 - Slots - double_empty_epoch [Preset: minimal] OK ++ EF - Phase 0 - Slots - empty_epoch [Preset: minimal] OK ++ EF - Phase 0 - Slots - over_epoch_boundary [Preset: minimal] OK ++ EF - Phase 0 - Slots - slots_1 [Preset: minimal] OK ++ EF - Phase 0 - Slots - slots_2 [Preset: minimal] OK + ForkChoice - minimal/altair/fork_choice/ex_ante/pyspec_tests/ex_ante_sandwich_with_honest_ OK + ForkChoice - minimal/altair/fork_choice/ex_ante/pyspec_tests/ex_ante_sandwich_without_atte OK + ForkChoice - minimal/altair/fork_choice/ex_ante/pyspec_tests/ex_ante_vanilla OK @@ -415,11 +440,6 @@ ConsensusSpecPreset-minimal + Light client - Update ranking - minimal/bellatrix/light_client/update_ranking/pyspec_tests OK + Light client - Update ranking - minimal/capella/light_client/update_ranking/pyspec_tests/u OK + Light client - Update ranking - minimal/eip4844/light_client/update_ranking/pyspec_tests/u OK -+ Slots - double_empty_epoch OK -+ Slots - empty_epoch OK -+ Slots - over_epoch_boundary OK -+ Slots - slots_1 OK -+ Slots - slots_2 OK + Sync - minimal/bellatrix/sync/optimistic/pyspec_tests/from_syncing_to_invalid OK + Sync - minimal/capella/sync/optimistic/pyspec_tests/from_syncing_to_invalid OK + [Invalid] EF - Altair - Sanity - Blocks - expected_deposit_in_block [Preset: minimal] OK @@ -838,7 +858,7 @@ ConsensusSpecPreset-minimal + fork_random_low_balances OK + fork_random_misc_balances OK ``` -OK: 827/835 Fail: 0/835 Skip: 8/835 +OK: 847/855 Fail: 0/855 Skip: 8/855 ## Attestation ```diff + [Invalid] EF - Altair - Operations - Attestation - after_epoch_slots OK @@ -2591,4 +2611,4 @@ OK: 68/68 Fail: 0/68 Skip: 0/68 OK: 52/52 Fail: 0/52 Skip: 0/52 ---TOTAL--- -OK: 2281/2289 Fail: 0/2289 Skip: 8/2289 +OK: 2301/2309 Fail: 0/2309 Skip: 8/2309 diff --git a/beacon_chain/spec/forks.nim b/beacon_chain/spec/forks.nim index 501497394..9ffa2c4ff 100644 --- a/beacon_chain/spec/forks.nim +++ b/beacon_chain/spec/forks.nim @@ -266,17 +266,28 @@ template toFork*[T: eip4844.BeaconState | eip4844.HashedBeaconState]( t: type T): BeaconStateFork = BeaconStateFork.EIP4844 -# TODO these cause stack overflows due to large temporaries getting allocated -# template init*(T: type ForkedHashedBeaconState, data: phase0.HashedBeaconState): T = -# T(kind: BeaconStateFork.Phase0, phase0Data: data) -# template init*(T: type ForkedHashedBeaconState, data: altair.HashedBeaconState): T = -# T(kind: BeaconStateFork.Altair, altairData: data) -# template init*(T: type ForkedHashedBeaconState, data: bellatrix.HashedBeaconState): T = -# T(kind: BeaconStateFork.Bellatrix, bellatrixData: data) -# template init*(T: type ForkedHashedBeaconState, data: capella.HashedBeaconState): T = -# T(kind: BeaconStateFork.Capella, capellaData: data) -# template init*(T: type ForkedHashedBeaconState, data: eip4844.HashedBeaconState): T = -# T(kind: BeaconStateFork.EIP4844, eip4844Data: data) +# TODO when https://github.com/nim-lang/Nim/issues/21086 fixed, use return type +# `ref T` +func new*(T: type ForkedHashedBeaconState, data: phase0.BeaconState): + ref ForkedHashedBeaconState = + (ref T)(kind: BeaconStateFork.Phase0, phase0Data: phase0.HashedBeaconState( + data: data, root: hash_tree_root(data))) +func new*(T: type ForkedHashedBeaconState, data: altair.BeaconState): + ref ForkedHashedBeaconState = + (ref T)(kind: BeaconStateFork.Altair, altairData: altair.HashedBeaconState( + data: data, root: hash_tree_root(data))) +func new*(T: type ForkedHashedBeaconState, data: bellatrix.BeaconState): + ref ForkedHashedBeaconState = + (ref T)(kind: BeaconStateFork.Bellatrix, bellatrixData: bellatrix.HashedBeaconState( + data: data, root: hash_tree_root(data))) +func new*(T: type ForkedHashedBeaconState, data: capella.BeaconState): + ref ForkedHashedBeaconState = + (ref T)(kind: BeaconStateFork.Capella, capellaData: capella.HashedBeaconState( + data: data, root: hash_tree_root(data))) +func new*(T: type ForkedHashedBeaconState, data: eip4844.BeaconState): + ref ForkedHashedBeaconState = + (ref T)(kind: BeaconStateFork.EIP4844, eip4844Data: eip4844.HashedBeaconState( + data: data, root: hash_tree_root(data))) template init*(T: type ForkedBeaconBlock, blck: phase0.BeaconBlock): T = T(kind: BeaconBlockFork.Phase0, phase0Data: blck) diff --git a/beacon_chain/spec/state_transition.nim b/beacon_chain/spec/state_transition.nim index 4001b520b..c00522abb 100644 --- a/beacon_chain/spec/state_transition.nim +++ b/beacon_chain/spec/state_transition.nim @@ -205,8 +205,6 @@ func maybeUpgradeStateToCapella( capellaData: capella.HashedBeaconState( root: hash_tree_root(newState[]), data: newState[]))[] -from ./datatypes/eip4844 import HashedBeaconState - func maybeUpgradeStateToEIP4844( cfg: RuntimeConfig, state: var ForkedHashedBeaconState) = # Both process_slots() and state_transition_block() call this, so only run it diff --git a/tests/consensus_spec/altair/all_altair_fixtures.nim b/tests/consensus_spec/altair/all_altair_fixtures.nim index adf2e34fb..acb1c02eb 100644 --- a/tests/consensus_spec/altair/all_altair_fixtures.nim +++ b/tests/consensus_spec/altair/all_altair_fixtures.nim @@ -12,7 +12,6 @@ import ./test_fixture_light_client_sync_protocol, ./test_fixture_operations, ./test_fixture_sanity_blocks, - ./test_fixture_sanity_slots, ./test_fixture_ssz_consensus_objects, ./test_fixture_state_transition_epoch, ./test_fixture_transition diff --git a/tests/consensus_spec/altair/test_fixture_sanity_slots.nim b/tests/consensus_spec/altair/test_fixture_sanity_slots.nim deleted file mode 100644 index 10b9862de..000000000 --- a/tests/consensus_spec/altair/test_fixture_sanity_slots.nim +++ /dev/null @@ -1,53 +0,0 @@ -# beacon_chain -# Copyright (c) 2018-2022 Status Research & Development GmbH -# 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 - os, strutils, - # Beacon chain internals - ../../../beacon_chain/spec/[forks, state_transition], - ../../../beacon_chain/spec/datatypes/altair, - # Test utilities - ../../testutil, - ../fixtures_utils, - ../../helpers/debug_state - -const SanitySlotsDir = SszTestsDir/const_preset/"altair"/"sanity"/"slots"/"pyspec_tests" - -proc runTest(identifier: string) = - let - testDir = SanitySlotsDir / identifier - num_slots = readLines(testDir / "slots.yaml", 2)[0].parseInt.uint64 - - proc `testImpl _ slots _ identifier`() = - test "Slots - " & identifier: - var - preState = newClone(parseTest(testDir/"pre.ssz_snappy", SSZ, altair.BeaconState)) - fhPreState = (ref ForkedHashedBeaconState)( - altairData: altair.HashedBeaconState( - data: preState[], root: hash_tree_root(preState[])), - kind: BeaconStateFork.Altair) - cache = StateCache() - info: ForkedEpochInfo - let postState = newClone(parseTest(testDir/"post.ssz_snappy", SSZ, altair.BeaconState)) - - check: - process_slots( - defaultRuntimeConfig, fhPreState[], - getStateField(fhPreState[], slot) + num_slots, cache, info, {}).isOk() - - getStateRoot(fhPreState[]) == postState[].hash_tree_root() - let newPreState = newClone(fhPreState.altairData.data) - reportDiff(newPreState, postState) - - `testImpl _ slots _ identifier`() - -suite "EF - Altair - Sanity - Slots " & preset(): - for kind, path in walkDir(SanitySlotsDir, relative = true, checkDir = true): - runTest(path) diff --git a/tests/consensus_spec/bellatrix/all_bellatrix_fixtures.nim b/tests/consensus_spec/bellatrix/all_bellatrix_fixtures.nim index e951b091a..a0963798f 100644 --- a/tests/consensus_spec/bellatrix/all_bellatrix_fixtures.nim +++ b/tests/consensus_spec/bellatrix/all_bellatrix_fixtures.nim @@ -11,7 +11,6 @@ import ./test_fixture_fork, ./test_fixture_operations, ./test_fixture_sanity_blocks, - ./test_fixture_sanity_slots, ./test_fixture_ssz_consensus_objects, ./test_fixture_state_transition_epoch, ./test_fixture_transition diff --git a/tests/consensus_spec/bellatrix/test_fixture_sanity_slots.nim b/tests/consensus_spec/bellatrix/test_fixture_sanity_slots.nim deleted file mode 100644 index 45e3880c2..000000000 --- a/tests/consensus_spec/bellatrix/test_fixture_sanity_slots.nim +++ /dev/null @@ -1,55 +0,0 @@ -# beacon_chain -# Copyright (c) 2018-2022 Status Research & Development GmbH -# 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 - os, strutils, - # Beacon chain internals - ../../../beacon_chain/spec/[forks, state_transition], - ../../../beacon_chain/spec/datatypes/bellatrix, - # Test utilities - ../../testutil, - ../fixtures_utils, - ../../helpers/debug_state - -const SanitySlotsDir = SszTestsDir/const_preset/"bellatrix"/"sanity"/"slots"/"pyspec_tests" - -proc runTest(identifier: string) = - let - testDir = SanitySlotsDir / identifier - num_slots = readLines(testDir / "slots.yaml", 2)[0].parseInt.uint64 - - proc `testImpl _ slots _ identifier`() = - test "Slots - " & identifier: - var - preState = newClone(parseTest( - testDir/"pre.ssz_snappy", SSZ, bellatrix.BeaconState)) - fhPreState = (ref ForkedHashedBeaconState)( - bellatrixData: bellatrix.HashedBeaconState( - data: preState[], root: hash_tree_root(preState[])), - kind: BeaconStateFork.Bellatrix) - cache = StateCache() - info = ForkedEpochInfo() - let postState = newClone(parseTest( - testDir/"post.ssz_snappy", SSZ, bellatrix.BeaconState)) - - check: - process_slots( - defaultRuntimeConfig, fhPreState[], - getStateField(fhPreState[], slot) + num_slots, cache, info, {}).isOk() - - getStateRoot(fhPreState[]) == postState[].hash_tree_root() - let newPreState = newClone(fhPreState.bellatrixData.data) - reportDiff(newPreState, postState) - - `testImpl _ slots _ identifier`() - -suite "EF - Bellatrix - Sanity - Slots " & preset(): - for kind, path in walkDir(SanitySlotsDir, relative = true, checkDir = true): - runTest(path) diff --git a/tests/consensus_spec/capella/all_capella_fixtures.nim b/tests/consensus_spec/capella/all_capella_fixtures.nim index 00d5419fe..fe742f333 100644 --- a/tests/consensus_spec/capella/all_capella_fixtures.nim +++ b/tests/consensus_spec/capella/all_capella_fixtures.nim @@ -11,7 +11,6 @@ import ./test_fixture_fork, ./test_fixture_operations, ./test_fixture_sanity_blocks, - ./test_fixture_sanity_slots, ./test_fixture_ssz_consensus_objects, ./test_fixture_state_transition_epoch, ./test_fixture_transition diff --git a/tests/consensus_spec/capella/test_fixture_sanity_slots.nim b/tests/consensus_spec/capella/test_fixture_sanity_slots.nim deleted file mode 100644 index e807ea913..000000000 --- a/tests/consensus_spec/capella/test_fixture_sanity_slots.nim +++ /dev/null @@ -1,55 +0,0 @@ -# beacon_chain -# Copyright (c) 2022 Status Research & Development GmbH -# 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 - os, strutils, - # Beacon chain internals - ../../../beacon_chain/spec/[forks, state_transition], - ../../../beacon_chain/spec/datatypes/capella, - # Test utilities - ../../testutil, - ../fixtures_utils, - ../../helpers/debug_state - -const SanitySlotsDir = SszTestsDir/const_preset/"capella"/"sanity"/"slots"/"pyspec_tests" - -proc runTest(identifier: string) = - let - testDir = SanitySlotsDir / identifier - num_slots = readLines(testDir / "slots.yaml", 2)[0].parseInt.uint64 - - proc `testImpl _ slots _ identifier`() = - test "Slots - " & identifier: - var - preState = newClone(parseTest( - testDir/"pre.ssz_snappy", SSZ, capella.BeaconState)) - fhPreState = (ref ForkedHashedBeaconState)( - capellaData: capella.HashedBeaconState( - data: preState[], root: hash_tree_root(preState[])), - kind: BeaconStateFork.Capella) - cache = StateCache() - info = ForkedEpochInfo() - let postState = newClone(parseTest( - testDir/"post.ssz_snappy", SSZ, capella.BeaconState)) - - check: - process_slots( - defaultRuntimeConfig, fhPreState[], - getStateField(fhPreState[], slot) + num_slots, cache, info, {}).isOk() - - getStateRoot(fhPreState[]) == postState[].hash_tree_root() - let newPreState = newClone(fhPreState.capellaData.data) - reportDiff(newPreState, postState) - - `testImpl _ slots _ identifier`() - -suite "EF - Capella - Sanity - Slots " & preset(): - for kind, path in walkDir(SanitySlotsDir, relative = true, checkDir = true): - runTest(path) diff --git a/tests/consensus_spec/consensus_spec_tests_preset.nim b/tests/consensus_spec/consensus_spec_tests_preset.nim index a8a393c1f..fab085e1b 100644 --- a/tests/consensus_spec/consensus_spec_tests_preset.nim +++ b/tests/consensus_spec/consensus_spec_tests_preset.nim @@ -19,6 +19,7 @@ import ./test_fixture_fork_choice, ./test_fixture_light_client_single_merkle_proof, ./test_fixture_light_client_sync, - ./test_fixture_light_client_update_ranking + ./test_fixture_light_client_update_ranking, + ./test_fixture_sanity_slots summarizeLongTests("ConsensusSpecPreset") diff --git a/tests/consensus_spec/eip4844/all_eip4844_fixtures.nim b/tests/consensus_spec/eip4844/all_eip4844_fixtures.nim index 00d5419fe..fe742f333 100644 --- a/tests/consensus_spec/eip4844/all_eip4844_fixtures.nim +++ b/tests/consensus_spec/eip4844/all_eip4844_fixtures.nim @@ -11,7 +11,6 @@ import ./test_fixture_fork, ./test_fixture_operations, ./test_fixture_sanity_blocks, - ./test_fixture_sanity_slots, ./test_fixture_ssz_consensus_objects, ./test_fixture_state_transition_epoch, ./test_fixture_transition diff --git a/tests/consensus_spec/eip4844/test_fixture_sanity_slots.nim b/tests/consensus_spec/eip4844/test_fixture_sanity_slots.nim deleted file mode 100644 index 8a67f299f..000000000 --- a/tests/consensus_spec/eip4844/test_fixture_sanity_slots.nim +++ /dev/null @@ -1,55 +0,0 @@ -# beacon_chain -# Copyright (c) 2022 Status Research & Development GmbH -# 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 - os, strutils, - # Beacon chain internals - ../../../beacon_chain/spec/[forks, state_transition], - ../../../beacon_chain/spec/datatypes/eip4844, - # Test utilities - ../../testutil, - ../fixtures_utils, - ../../helpers/debug_state - -const SanitySlotsDir = SszTestsDir/const_preset/"eip4844"/"sanity"/"slots"/"pyspec_tests" - -proc runTest(identifier: string) = - let - testDir = SanitySlotsDir / identifier - num_slots = readLines(testDir / "slots.yaml", 2)[0].parseInt.uint64 - - proc `testImpl _ slots _ identifier`() = - test "Slots - " & identifier: - var - preState = newClone(parseTest( - testDir/"pre.ssz_snappy", SSZ, eip4844.BeaconState)) - fhPreState = (ref ForkedHashedBeaconState)( - eip4844Data: eip4844.HashedBeaconState( - data: preState[], root: hash_tree_root(preState[])), - kind: BeaconStateFork.EIP4844) - cache = StateCache() - info = ForkedEpochInfo() - let postState = newClone(parseTest( - testDir/"post.ssz_snappy", SSZ, eip4844.BeaconState)) - - check: - process_slots( - defaultRuntimeConfig, fhPreState[], - getStateField(fhPreState[], slot) + num_slots, cache, info, {}).isOk() - - getStateRoot(fhPreState[]) == postState[].hash_tree_root() - let newPreState = newClone(fhPreState.eip4844Data.data) - reportDiff(newPreState, postState) - - `testImpl _ slots _ identifier`() - -suite "EF - EIP4844 - Sanity - Slots " & preset(): - for kind, path in walkDir(SanitySlotsDir, relative = true, checkDir = true): - runTest(path) diff --git a/tests/consensus_spec/phase0/all_phase0_fixtures.nim b/tests/consensus_spec/phase0/all_phase0_fixtures.nim index ac71e6d00..d60dce514 100644 --- a/tests/consensus_spec/phase0/all_phase0_fixtures.nim +++ b/tests/consensus_spec/phase0/all_phase0_fixtures.nim @@ -10,6 +10,5 @@ import ./test_fixture_operations, ./test_fixture_sanity_blocks, - ./test_fixture_sanity_slots, ./test_fixture_ssz_consensus_objects, ./test_fixture_state_transition_epoch diff --git a/tests/consensus_spec/phase0/test_fixture_sanity_slots.nim b/tests/consensus_spec/phase0/test_fixture_sanity_slots.nim deleted file mode 100644 index 0d18777ca..000000000 --- a/tests/consensus_spec/phase0/test_fixture_sanity_slots.nim +++ /dev/null @@ -1,52 +0,0 @@ -# beacon_chain -# Copyright (c) 2018-2022 Status Research & Development GmbH -# 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 - os, strutils, - # Beacon chain internals - ../../../beacon_chain/spec/[forks, state_transition], - ../../../beacon_chain/spec/datatypes/phase0, - # Test utilities - ../../testutil, - ../fixtures_utils, - ../../helpers/debug_state - -const SanitySlotsDir = SszTestsDir/const_preset/"phase0"/"sanity"/"slots"/"pyspec_tests" - -proc runTest(identifier: string) = - let - testDir = SanitySlotsDir / identifier - num_slots = readLines(testDir / "slots.yaml", 2)[0].parseInt.uint64 - - proc `testImpl _ slots _ identifier`() = - test "Slots - " & identifier: - var - preState = newClone(parseTest(testDir/"pre.ssz_snappy", SSZ, phase0.BeaconState)) - fhPreState = (ref ForkedHashedBeaconState)(phase0Data: phase0.HashedBeaconState( - data: preState[], root: hash_tree_root(preState[])), kind: BeaconStateFork.Phase0) - cache = StateCache() - info: ForkedEpochInfo - let postState = newClone(parseTest(testDir/"post.ssz_snappy", SSZ, phase0.BeaconState)) - - check: - process_slots( - defaultRuntimeConfig, - fhPreState[], getStateField(fhPreState[], slot) + num_slots, cache, - info, {}).isOk() - - getStateRoot(fhPreState[]) == postState[].hash_tree_root() - let newPreState = newClone(fhPreState.phase0Data.data) - reportDiff(newPreState, postState) - - `testImpl _ slots _ identifier`() - -suite "EF - Phase 0 - Sanity - Slots " & preset(): - for kind, path in walkDir(SanitySlotsDir, relative = true, checkDir = true): - runTest(path) diff --git a/tests/consensus_spec/test_fixture_sanity_slots.nim b/tests/consensus_spec/test_fixture_sanity_slots.nim new file mode 100644 index 000000000..9a87b8a15 --- /dev/null +++ b/tests/consensus_spec/test_fixture_sanity_slots.nim @@ -0,0 +1,90 @@ +# beacon_chain +# Copyright (c) 2018-2022 Status Research & Development GmbH +# 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 ../../beacon_chain/spec/forks +from std/os import walkDir, `/` +from std/strutils import parseInt +from ./fixtures_utils import SszTestsDir, parseTest +from ../testutil import check, preset, suite, test +from ../../beacon_chain/spec/state_transition import process_slots +from ../helpers/debug_state import reportDiff + +proc runTest(T: type, testDir, forkName: static[string], identifier: string) = + let + testDir = testDir / identifier + num_slots = readLines(testDir / "slots.yaml", 2)[0].parseInt.uint64 + + proc `testImpl _ slots _ identifier`() = + test "EF - " & forkName & " - Slots - " & identifier & " [Preset: " & const_preset & "]": + let + preState = newClone(parseTest(testDir/"pre.ssz_snappy", SSZ, T)) + postState = newClone(parseTest(testDir/"post.ssz_snappy", SSZ, T)) + var + fhPreState = ForkedHashedBeaconState.new(preState[]) + cache = StateCache() + info: ForkedEpochInfo + + check: + process_slots( + defaultRuntimeConfig, + fhPreState[], getStateField(fhPreState[], slot) + num_slots, cache, + info, {}).isOk() + + getStateRoot(fhPreState[]) == postState[].hash_tree_root() + + withState(fhPreState[]): + when forkyState.data isnot typeof(postState[]): + doAssert false, "mismatched pre/post forks" + else: + reportDiff(forkyState.data, postState[]) + + `testImpl _ slots _ identifier`() + +func sanitySlotsDir(preset_dir: string): string {.compileTime.} = + SszTestsDir/const_preset/preset_dir/"sanity"/"slots"/"pyspec_tests" + +from ../../beacon_chain/spec/datatypes/phase0 import BeaconState + +suite "EF - Phase 0 - Sanity - Slots " & preset(): + const phase0SanitySlotsDir = sanitySlotsDir("phase0") + for kind, path in walkDir( + phase0SanitySlotsDir, relative = true, checkDir = true): + runTest(phase0.BeaconState, phase0SanitySlotsDir, "Phase 0", path) + +from ../../beacon_chain/spec/datatypes/altair import BeaconState + +suite "EF - Altair - Sanity - Slots " & preset(): + const altairSanitySlotsDir = sanitySlotsDir("altair") + for kind, path in walkDir( + altairSanitySlotsDir, relative = true, checkDir = true): + runTest(altair.BeaconState, altairSanitySlotsDir, "Altair", path) + +from ../../beacon_chain/spec/datatypes/bellatrix import BeaconState + +suite "EF - Bellatrix - Sanity - Slots " & preset(): + const bellatrixSanitySlotsDir = sanitySlotsDir("bellatrix") + for kind, path in walkDir( + bellatrixSanitySlotsDir, relative = true, checkDir = true): + runTest(bellatrix.BeaconState, bellatrixSanitySlotsDir, "Bellatrix", path) + +from ../../../beacon_chain/spec/datatypes/capella import BeaconState + +suite "EF - Capella - Sanity - Slots " & preset(): + const capellaSanitySlotsDir = sanitySlotsDir("capella") + for kind, path in walkDir( + capellaSanitySlotsDir, relative = true, checkDir = true): + runTest(capella.BeaconState, capellaSanitySlotsDir, "Capella", path) + +from ../../../beacon_chain/spec/datatypes/eip4844 import BeaconState + +suite "EF - EIP4844 - Sanity - Slots " & preset(): + const eip4844SanitySlotsDir = sanitySlotsDir("eip4844") + for kind, path in walkDir( + eip4844SanitySlotsDir, relative = true, checkDir = true): + runTest(eip4844.BeaconState, eip4844SanitySlotsDir, "EIP4844", path)