diff --git a/tests/consensus_spec/altair/all_altair_fixtures.nim b/tests/consensus_spec/altair/all_altair_fixtures.nim index acb1c02eb..efa2ff3bf 100644 --- a/tests/consensus_spec/altair/all_altair_fixtures.nim +++ b/tests/consensus_spec/altair/all_altair_fixtures.nim @@ -11,7 +11,6 @@ import ./test_fixture_fork, ./test_fixture_light_client_sync_protocol, ./test_fixture_operations, - ./test_fixture_sanity_blocks, ./test_fixture_ssz_consensus_objects, ./test_fixture_state_transition_epoch, ./test_fixture_transition diff --git a/tests/consensus_spec/altair/test_fixture_sanity_blocks.nim b/tests/consensus_spec/altair/test_fixture_sanity_blocks.nim deleted file mode 100644 index 386268941..000000000 --- a/tests/consensus_spec/altair/test_fixture_sanity_blocks.nim +++ /dev/null @@ -1,77 +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, sequtils, chronicles, - # Beacon chain internals - ../../../beacon_chain/spec/[forks, state_transition], - ../../../beacon_chain/spec/datatypes/altair, - # Test utilities - ../../testutil, - ../fixtures_utils - -const - FinalityDir = SszTestsDir/const_preset/"altair"/"finality"/"finality"/"pyspec_tests" - RandomDir = SszTestsDir/const_preset/"altair"/"random"/"random"/"pyspec_tests" - SanityBlocksDir = SszTestsDir/const_preset/"altair"/"sanity"/"blocks"/"pyspec_tests" - -proc runTest(testName, testDir, unitTestName: string) = - let testPath = testDir / unitTestName - - proc `testImpl _ blck _ testName`() = - let - hasPostState = fileExists(testPath/"post.ssz_snappy") - prefix = if hasPostState: "[Valid] " else: "[Invalid] " - - test prefix & testName & " - " & unitTestName & preset(): - var - preState = newClone(parseTest(testPath/"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() - - # In test cases with more than 10 blocks the first 10 aren't 0-prefixed, - # so purely lexicographic sorting wouldn't sort properly. - let numBlocks = toSeq(walkPattern(testPath/"blocks_*.ssz_snappy")).len - for i in 0 ..< numBlocks: - let blck = parseTest(testPath/"blocks_" & $i & ".ssz_snappy", SSZ, altair.SignedBeaconBlock) - - if hasPostState: - let res = state_transition( - defaultRuntimeConfig, fhPreState[], blck, cache, info, flags = {}, - noRollback) - res.expect("block should apply: " & $i) - else: - let res = state_transition( - defaultRuntimeConfig, fhPreState[], blck, cache, info, flags = {}, - noRollback) - doAssert (i + 1 < numBlocks) or not res.isOk(), - "We didn't expect these invalid blocks to be processed" - - if hasPostState: - let postState = newClone(parseTest(testPath/"post.ssz_snappy", SSZ, altair.BeaconState)) - when false: - reportDiff(fhPreState.altairData.data, postState) - doAssert getStateRoot(fhPreState[]) == postState[].hash_tree_root() - - `testImpl _ blck _ testName`() - -suite "EF - Altair - Sanity - Blocks " & preset(): - for kind, path in walkDir(SanityBlocksDir, relative = true, checkDir = true): - runTest("EF - Altair - Sanity - Blocks", SanityBlocksDir, path) - -suite "EF - Altair - Finality " & preset(): - for kind, path in walkDir(FinalityDir, relative = true, checkDir = true): - runTest("EF - Altair - Finality", FinalityDir, path) - -suite "EF - Altair - Random" & preset(): - for kind, path in walkDir(RandomDir, relative = true, checkDir = true): - runTest("EF - Altair - Random", RandomDir, path) diff --git a/tests/consensus_spec/altair/test_fixture_transition.nim b/tests/consensus_spec/altair/test_fixture_transition.nim index fe771dd4a..bb1a3b930 100644 --- a/tests/consensus_spec/altair/test_fixture_transition.nim +++ b/tests/consensus_spec/altair/test_fixture_transition.nim @@ -8,6 +8,7 @@ {.used.} import + chronicles, yaml, # Standard library os, sequtils, diff --git a/tests/consensus_spec/bellatrix/all_bellatrix_fixtures.nim b/tests/consensus_spec/bellatrix/all_bellatrix_fixtures.nim index a0963798f..de87a4444 100644 --- a/tests/consensus_spec/bellatrix/all_bellatrix_fixtures.nim +++ b/tests/consensus_spec/bellatrix/all_bellatrix_fixtures.nim @@ -10,7 +10,6 @@ import ./test_fixture_fork, ./test_fixture_operations, - ./test_fixture_sanity_blocks, ./test_fixture_ssz_consensus_objects, ./test_fixture_state_transition_epoch, ./test_fixture_transition diff --git a/tests/consensus_spec/bellatrix/test_fixture_sanity_blocks.nim b/tests/consensus_spec/bellatrix/test_fixture_sanity_blocks.nim deleted file mode 100644 index 20ab861f9..000000000 --- a/tests/consensus_spec/bellatrix/test_fixture_sanity_blocks.nim +++ /dev/null @@ -1,88 +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 - std/[os, sequtils], - # Nimble - chronicles, - # Beacon chain internals - ../../../beacon_chain/spec/[forks, state_transition], - ../../../beacon_chain/spec/datatypes/bellatrix, - # Test utilities - ../../testutil, - ../../helpers/debug_state, - ../fixtures_utils - -const - FinalityDir = SszTestsDir/const_preset/"bellatrix"/"finality"/"finality"/"pyspec_tests" - RandomDir = SszTestsDir/const_preset/"bellatrix"/"random"/"random"/"pyspec_tests" - SanityBlocksDir = SszTestsDir/const_preset/"bellatrix"/"sanity"/"blocks"/"pyspec_tests" - -proc runTest(testName, testDir, unitTestName: string) = - let testPath = testDir / unitTestName - - proc `testImpl _ blck _ testName`() = - let - hasPostState = fileExists(testPath/"post.ssz_snappy") - prefix = if hasPostState: "[Valid] " else: "[Invalid] " - - test prefix & testName & " - " & unitTestName & preset(): - var - preState = newClone(parseTest( - testPath/"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() - - # In test cases with more than 10 blocks the first 10 aren't 0-prefixed, - # so purely lexicographic sorting wouldn't sort properly. - let numBlocks = toSeq(walkPattern(testPath/"blocks_*.ssz_snappy")).len - if numBlocks > 1: - return - for i in 0 ..< numBlocks: - let blck = parseTest( - testPath/"blocks_" & $i & ".ssz_snappy", SSZ, - bellatrix.SignedBeaconBlock) - - if hasPostState: - let res = state_transition( - defaultRuntimeConfig, fhPreState[], blck, cache, info, flags = {}, - noRollback) - res.expect("no failure when applying block " & $i) - else: - let res = state_transition( - defaultRuntimeConfig, fhPreState[], blck, cache, info, flags = {}, - noRollback) - doAssert (i + 1 < numBlocks) or not res.isOk, - "We didn't expect these invalid blocks to be processed" - - if hasPostState: - let postState = newClone(parseTest( - testPath/"post.ssz_snappy", SSZ, bellatrix.BeaconState)) - when false: - reportDiff(fhPreState.bellatrixData.data, postState) - doAssert getStateRoot(fhPreState[]) == postState[].hash_tree_root() - - `testImpl _ blck _ testName`() - -suite "EF - Bellatrix - Sanity - Blocks " & preset(): - for kind, path in walkDir(SanityBlocksDir, relative = true, checkDir = true): - runTest("EF - Bellatrix - Sanity - Blocks", SanityBlocksDir, path) - -suite "EF - Bellatrix - Random " & preset(): - for kind, path in walkDir(RandomDir, relative = true, checkDir = true): - runTest("EF - Bellatrix - Random", RandomDir, path) - -suite "EF - Bellatrix - Finality " & preset(): - for kind, path in walkDir(FinalityDir, relative = true, checkDir = true): - runTest("EF - Bellatrix - Finality", FinalityDir, path) diff --git a/tests/consensus_spec/bellatrix/test_fixture_transition.nim b/tests/consensus_spec/bellatrix/test_fixture_transition.nim index 3c4051487..5812137c6 100644 --- a/tests/consensus_spec/bellatrix/test_fixture_transition.nim +++ b/tests/consensus_spec/bellatrix/test_fixture_transition.nim @@ -8,6 +8,7 @@ {.used.} import + chronicles, yaml, # Standard library std/[os, sequtils, strutils], diff --git a/tests/consensus_spec/capella/all_capella_fixtures.nim b/tests/consensus_spec/capella/all_capella_fixtures.nim index fe742f333..2b500d909 100644 --- a/tests/consensus_spec/capella/all_capella_fixtures.nim +++ b/tests/consensus_spec/capella/all_capella_fixtures.nim @@ -10,7 +10,6 @@ import ./test_fixture_fork, ./test_fixture_operations, - ./test_fixture_sanity_blocks, ./test_fixture_ssz_consensus_objects, ./test_fixture_state_transition_epoch, ./test_fixture_transition diff --git a/tests/consensus_spec/capella/test_fixture_sanity_blocks.nim b/tests/consensus_spec/capella/test_fixture_sanity_blocks.nim deleted file mode 100644 index c691e884c..000000000 --- a/tests/consensus_spec/capella/test_fixture_sanity_blocks.nim +++ /dev/null @@ -1,88 +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 - std/[os, sequtils], - # Nimble - chronicles, - # Beacon chain internals - ../../../beacon_chain/spec/[forks, state_transition], - ../../../beacon_chain/spec/datatypes/capella, - # Test utilities - ../../testutil, - ../../helpers/debug_state, - ../fixtures_utils - -const - FinalityDir = SszTestsDir/const_preset/"capella"/"finality"/"finality"/"pyspec_tests" - RandomDir = SszTestsDir/const_preset/"capella"/"random"/"random"/"pyspec_tests" - SanityBlocksDir = SszTestsDir/const_preset/"capella"/"sanity"/"blocks"/"pyspec_tests" - -proc runTest(testName, testDir, unitTestName: string) = - let testPath = testDir / unitTestName - - proc `testImpl _ blck _ testName`() = - let - hasPostState = fileExists(testPath/"post.ssz_snappy") - prefix = if hasPostState: "[Valid] " else: "[Invalid] " - - test prefix & testName & " - " & unitTestName & preset(): - var - preState = newClone(parseTest( - testPath/"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() - - # In test cases with more than 10 blocks the first 10 aren't 0-prefixed, - # so purely lexicographic sorting wouldn't sort properly. - let numBlocks = toSeq(walkPattern(testPath/"blocks_*.ssz_snappy")).len - if numBlocks > 1: - return - for i in 0 ..< numBlocks: - let blck = parseTest( - testPath/"blocks_" & $i & ".ssz_snappy", SSZ, - capella.SignedBeaconBlock) - - if hasPostState: - let res = state_transition( - defaultRuntimeConfig, fhPreState[], blck, cache, info, flags = {}, - noRollback) - res.expect("no failure when applying block " & $i) - else: - let res = state_transition( - defaultRuntimeConfig, fhPreState[], blck, cache, info, flags = {}, - noRollback) - doAssert (i + 1 < numBlocks) or not res.isOk, - "We didn't expect these invalid blocks to be processed" - - if hasPostState: - let postState = newClone(parseTest( - testPath/"post.ssz_snappy", SSZ, capella.BeaconState)) - when false: - reportDiff(fhPreState.capellaData.data, postState) - doAssert getStateRoot(fhPreState[]) == postState[].hash_tree_root() - - `testImpl _ blck _ testName`() - -suite "EF - Capella - Sanity - Blocks " & preset(): - for kind, path in walkDir(SanityBlocksDir, relative = true, checkDir = true): - runTest("EF - Capella - Sanity - Blocks", SanityBlocksDir, path) - -suite "EF - Capella - Random " & preset(): - for kind, path in walkDir(RandomDir, relative = true, checkDir = true): - runTest("EF - Capella - Random", RandomDir, path) - -suite "EF - Capella - Finality " & preset(): - for kind, path in walkDir(FinalityDir, relative = true, checkDir = true): - runTest("EF - Capella - Finality", FinalityDir, path) diff --git a/tests/consensus_spec/capella/test_fixture_transition.nim b/tests/consensus_spec/capella/test_fixture_transition.nim index ca7cd9e5b..0834ad36f 100644 --- a/tests/consensus_spec/capella/test_fixture_transition.nim +++ b/tests/consensus_spec/capella/test_fixture_transition.nim @@ -8,6 +8,7 @@ {.used.} import + chronicles, yaml, # Standard library std/[os, sequtils, strutils], diff --git a/tests/consensus_spec/consensus_spec_tests_preset.nim b/tests/consensus_spec/consensus_spec_tests_preset.nim index fab085e1b..0582d35c9 100644 --- a/tests/consensus_spec/consensus_spec_tests_preset.nim +++ b/tests/consensus_spec/consensus_spec_tests_preset.nim @@ -20,6 +20,7 @@ import ./test_fixture_light_client_single_merkle_proof, ./test_fixture_light_client_sync, ./test_fixture_light_client_update_ranking, + ./test_fixture_sanity_blocks, ./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 fe742f333..2b500d909 100644 --- a/tests/consensus_spec/eip4844/all_eip4844_fixtures.nim +++ b/tests/consensus_spec/eip4844/all_eip4844_fixtures.nim @@ -10,7 +10,6 @@ import ./test_fixture_fork, ./test_fixture_operations, - ./test_fixture_sanity_blocks, ./test_fixture_ssz_consensus_objects, ./test_fixture_state_transition_epoch, ./test_fixture_transition diff --git a/tests/consensus_spec/eip4844/test_fixture_sanity_blocks.nim b/tests/consensus_spec/eip4844/test_fixture_sanity_blocks.nim deleted file mode 100644 index 9718de1cc..000000000 --- a/tests/consensus_spec/eip4844/test_fixture_sanity_blocks.nim +++ /dev/null @@ -1,88 +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 - std/[os, sequtils], - # Nimble - chronicles, - # Beacon chain internals - ../../../beacon_chain/spec/[forks, state_transition], - ../../../beacon_chain/spec/datatypes/eip4844, - # Test utilities - ../../testutil, - ../../helpers/debug_state, - ../fixtures_utils - -const - FinalityDir = SszTestsDir/const_preset/"eip4844"/"finality"/"finality"/"pyspec_tests" - RandomDir = SszTestsDir/const_preset/"eip4844"/"random"/"random"/"pyspec_tests" - SanityBlocksDir = SszTestsDir/const_preset/"eip4844"/"sanity"/"blocks"/"pyspec_tests" - -proc runTest(testName, testDir, unitTestName: string) = - let testPath = testDir / unitTestName - - proc `testImpl _ blck _ testName`() = - let - hasPostState = fileExists(testPath/"post.ssz_snappy") - prefix = if hasPostState: "[Valid] " else: "[Invalid] " - - test prefix & testName & " - " & unitTestName & preset(): - var - preState = newClone(parseTest( - testPath/"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() - - # In test cases with more than 10 blocks the first 10 aren't 0-prefixed, - # so purely lexicographic sorting wouldn't sort properly. - let numBlocks = toSeq(walkPattern(testPath/"blocks_*.ssz_snappy")).len - if numBlocks > 1: - return - for i in 0 ..< numBlocks: - let blck = parseTest( - testPath/"blocks_" & $i & ".ssz_snappy", SSZ, - eip4844.SignedBeaconBlock) - - if hasPostState: - let res = state_transition( - defaultRuntimeConfig, fhPreState[], blck, cache, info, flags = {}, - noRollback) - res.expect("no failure when applying block " & $i) - else: - let res = state_transition( - defaultRuntimeConfig, fhPreState[], blck, cache, info, flags = {}, - noRollback) - doAssert (i + 1 < numBlocks) or not res.isOk, - "We didn't expect these invalid blocks to be processed" - - if hasPostState: - let postState = newClone(parseTest( - testPath/"post.ssz_snappy", SSZ, eip4844.BeaconState)) - when false: - reportDiff(fhPreState.eip4844Data.data, postState) - doAssert getStateRoot(fhPreState[]) == postState[].hash_tree_root() - - `testImpl _ blck _ testName`() - -suite "EF - EIP4844 - Sanity - Blocks " & preset(): - for kind, path in walkDir(SanityBlocksDir, relative = true, checkDir = true): - runTest("EF - EIP4844 - Sanity - Blocks", SanityBlocksDir, path) - -suite "EF - EIP4844 - Random " & preset(): - for kind, path in walkDir(RandomDir, relative = true, checkDir = true): - runTest("EF - EIP4844 - Random", RandomDir, path) - -suite "EF - EIP4844 - Finality " & preset(): - for kind, path in walkDir(FinalityDir, relative = true, checkDir = true): - runTest("EF - EIP4844 - Finality", FinalityDir, path) diff --git a/tests/consensus_spec/eip4844/test_fixture_transition.nim b/tests/consensus_spec/eip4844/test_fixture_transition.nim index 37fb5ee1a..52abbc240 100644 --- a/tests/consensus_spec/eip4844/test_fixture_transition.nim +++ b/tests/consensus_spec/eip4844/test_fixture_transition.nim @@ -8,6 +8,7 @@ {.used.} import + chronicles, yaml, # Standard library std/[os, sequtils, strutils], diff --git a/tests/consensus_spec/phase0/all_phase0_fixtures.nim b/tests/consensus_spec/phase0/all_phase0_fixtures.nim index d60dce514..4ad904682 100644 --- a/tests/consensus_spec/phase0/all_phase0_fixtures.nim +++ b/tests/consensus_spec/phase0/all_phase0_fixtures.nim @@ -9,6 +9,5 @@ import ./test_fixture_operations, - ./test_fixture_sanity_blocks, ./test_fixture_ssz_consensus_objects, ./test_fixture_state_transition_epoch diff --git a/tests/consensus_spec/phase0/test_fixture_sanity_blocks.nim b/tests/consensus_spec/phase0/test_fixture_sanity_blocks.nim deleted file mode 100644 index 8bc7e83d6..000000000 --- a/tests/consensus_spec/phase0/test_fixture_sanity_blocks.nim +++ /dev/null @@ -1,76 +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, sequtils, chronicles, - # Beacon chain internals - ../../../beacon_chain/spec/[forks, state_transition], - ../../../beacon_chain/spec/datatypes/phase0, - # Test utilities - ../../testutil, - ../fixtures_utils - -const - FinalityDir = SszTestsDir/const_preset/"phase0"/"finality"/"finality"/"pyspec_tests" - RandomDir = SszTestsDir/const_preset/"phase0"/"random"/"random"/"pyspec_tests" - SanityBlocksDir = SszTestsDir/const_preset/"phase0"/"sanity"/"blocks"/"pyspec_tests" - -proc runTest(testName, testDir, unitTestName: string) = - let testPath = testDir / unitTestName - - proc `testImpl _ blck _ testName`() = - let - hasPostState = fileExists(testPath/"post.ssz_snappy") - prefix = if hasPostState: "[Valid] " else: "[Invalid] " - - test prefix & testName & " - " & unitTestName & preset(): - var - preState = newClone(parseTest(testPath/"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() - - # In test cases with more than 10 blocks the first 10 aren't 0-prefixed, - # so purely lexicographic sorting wouldn't sort properly. - let numBlocks = toSeq(walkPattern(testPath/"blocks_*.ssz_snappy")).len - for i in 0 ..< numBlocks: - let blck = parseTest(testPath/"blocks_" & $i & ".ssz_snappy", SSZ, phase0.SignedBeaconBlock) - - if hasPostState: - state_transition( - defaultRuntimeConfig, fhPreState[], blck, cache, info, flags = {}, - noRollback).expect("should apply block") - else: - let res = state_transition( - defaultRuntimeConfig, fhPreState[], blck, cache, info, flags = {}, - noRollback) - doAssert (i + 1 < numBlocks) or not res.isOk(), - "We didn't expect these invalid blocks to be processed" - - if hasPostState: - let postState = newClone(parseTest(testPath/"post.ssz_snappy", SSZ, phase0.BeaconState)) - when false: - reportDiff(hashedPreState.phase0Data.data, postState) - doAssert getStateRoot(fhPreState[]) == postState[].hash_tree_root() - - `testImpl _ blck _ testName`() - -suite "EF - Phase 0 - Sanity - Blocks " & preset(): - for kind, path in walkDir(SanityBlocksDir, relative = true, checkDir = true): - runTest("EF - Phase 0 - Sanity - Blocks", SanityBlocksDir, path) - -suite "EF - Phase 0 - Finality " & preset(): - for kind, path in walkDir(FinalityDir, relative = true, checkDir = true): - runTest("EF - Phase 0 - Finality", FinalityDir, path) - -suite "EF - Phase 0 - Random " & preset(): - for kind, path in walkDir(RandomDir, relative = true, checkDir = true): - runTest("EF - Phase 0 - Random", RandomDir, path) diff --git a/tests/consensus_spec/test_fixture_sanity_blocks.nim b/tests/consensus_spec/test_fixture_sanity_blocks.nim new file mode 100644 index 000000000..e3f411e41 --- /dev/null +++ b/tests/consensus_spec/test_fixture_sanity_blocks.nim @@ -0,0 +1,115 @@ +# 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/datatypes/phase0, + ../../beacon_chain/spec/state_transition, + ../testutil + +from std/os import fileExists, walkDir, walkPattern, `/` +from std/sequtils import toSeq +from ../../../beacon_chain/spec/forks import + ForkedEpochInfo, ForkedHashedBeaconState, fromSszBytes, getStateRoot, new +from ../../../beacon_chain/spec/presets import + const_preset, defaultRuntimeConfig +from ./fixtures_utils import + SSZ, SszTestsDir, hash_tree_root, parseTest, readSszBytes, toSszType + +proc runTest( + BS, SBB: type, testName, testDir: static[string], unitTestName: string) = + let testPath = testDir / unitTestName + + proc `testImpl _ blck _ testName`() = + let + hasPostState = fileExists(testPath/"post.ssz_snappy") + prefix = if hasPostState: "[Valid] " else: "[Invalid] " + + test prefix & testName & " - " & unitTestName & preset(): + let preState = newClone(parseTest(testPath/"pre.ssz_snappy", SSZ, BS)) + var + fhPreState = ForkedHashedBeaconState.new(preState[]) + cache = StateCache() + info = ForkedEpochInfo() + + # In test cases with more than 10 blocks the first 10 aren't 0-prefixed, + # so purely lexicographic sorting wouldn't sort properly. + let numBlocks = toSeq(walkPattern(testPath/"blocks_*.ssz_snappy")).len + for i in 0 ..< numBlocks: + let blck = parseTest(testPath/"blocks_" & $i & ".ssz_snappy", SSZ, SBB) + + if hasPostState: + state_transition( + defaultRuntimeConfig, fhPreState[], blck, cache, info, flags = {}, + noRollback).expect("should apply block") + else: + let res = state_transition( + defaultRuntimeConfig, fhPreState[], blck, cache, info, flags = {}, + noRollback) + doAssert (i + 1 < numBlocks) or not res.isOk(), + "We didn't expect these invalid blocks to be processed" + + if hasPostState: + let postState = newClone(parseTest(testPath/"post.ssz_snappy", SSZ, BS)) + when false: + reportDiff(hashedPreState.phase0Data.data, postState) + doAssert getStateRoot(fhPreState[]) == postState[].hash_tree_root() + + `testImpl _ blck _ testName`() + +template runForkBlockTests( + forkDirName, forkHumanName: static[string], BeaconStateType, + BeaconBlockType: untyped) = + const + FinalityDir = + SszTestsDir/const_preset/forkDirName/"finality"/"finality"/"pyspec_tests" + RandomDir = + SszTestsDir/const_preset/forkDirName/"random"/"random"/"pyspec_tests" + SanityBlocksDir = + SszTestsDir/const_preset/forkDirName/"sanity"/"blocks"/"pyspec_tests" + + suite "EF - " & forkHumanName & " - Sanity - Blocks " & preset(): + for kind, path in walkDir(SanityBlocksDir, relative = true, checkDir = true): + runTest( + BeaconStateType, BeaconBlockType, + "EF - " & forkHumanName & " - Sanity - Blocks", SanityBlocksDir, path) + + suite "EF - " & forkHumanName & " - Finality " & preset(): + for kind, path in walkDir(FinalityDir, relative = true, checkDir = true): + runTest( + BeaconStateType, BeaconBlockType, + "EF - " & forkHumanName & " - Finality", FinalityDir, path) + + suite "EF - " & forkHumanName & " - Random " & preset(): + for kind, path in walkDir(RandomDir, relative = true, checkDir = true): + runTest( + BeaconStateType, BeaconBlockType, + "EF - " & forkHumanName & " - Random", RandomDir, path) + +runForkBlockTests( + "phase0", "Phase 0", phase0.BeaconState, phase0.SignedBeaconBlock) + +from ../../../beacon_chain/spec/datatypes/altair import + BeaconState, SignedBeaconBlock +runForkBlockTests( + "altair", "Altair", altair.BeaconState, altair.SignedBeaconBlock) + +from ../../../beacon_chain/spec/datatypes/bellatrix import + BeaconState, SignedBeaconBlock +runForkBlockTests( + "bellatrix", "Bellatrix", bellatrix.BeaconState, bellatrix.SignedBeaconBlock) + +from ../../../beacon_chain/spec/datatypes/capella import + BeaconState, SignedBeaconBlock +runForkBlockTests( + "capella", "Capella", capella.BeaconState, capella.SignedBeaconBlock) + +from ../../../beacon_chain/spec/datatypes/eip4844 import + BeaconState, SignedBeaconBlock +runForkBlockTests( + "eip4844", "EIP4844", eip4844.BeaconState, eip4844.SignedBeaconBlock)