diff --git a/tests/core/pyspec/eth2spec/test/utils.py b/tests/core/pyspec/eth2spec/test/utils.py index d94aeb5aa..61fc75040 100644 --- a/tests/core/pyspec/eth2spec/test/utils.py +++ b/tests/core/pyspec/eth2spec/test/utils.py @@ -1,6 +1,6 @@ import inspect from typing import Dict, Any -from eth2spec.utils.ssz.ssz_typing import View, boolean, Container +from eth2spec.utils.ssz.ssz_typing import View from eth2spec.utils.ssz.ssz_impl import serialize @@ -96,11 +96,6 @@ def with_meta_tags(tags: Dict[str, Any]): return runner -class FlaggedContainer(Container): - flag: boolean - obj: Container - - def build_transition_test(fn, pre_fork_name, post_fork_name, fork_epoch=None): """ Handles the inner plumbing to generate `transition_test`s. @@ -109,11 +104,15 @@ def build_transition_test(fn, pre_fork_name, post_fork_name, fork_epoch=None): def _adapter(*args, **kwargs): post_spec = kwargs["phases"][post_fork_name] + pre_fork_counter = 0 + def pre_tag(obj): - return FlaggedContainer(flag=False, obj=obj) + nonlocal pre_fork_counter + pre_fork_counter += 1 + return obj def post_tag(obj): - return FlaggedContainer(flag=True, obj=obj) + return obj yield "post_fork", "meta", post_fork_name @@ -138,4 +137,7 @@ def build_transition_test(fn, pre_fork_name, post_fork_name, fork_epoch=None): has_fork_epoch = True yield part assert has_fork_epoch + + if pre_fork_counter > 0: + yield "fork_block", "meta", pre_fork_counter - 1 return _adapter diff --git a/tests/formats/transition/README.md b/tests/formats/transition/README.md index fb8b3b5ea..832f38ca2 100644 --- a/tests/formats/transition/README.md +++ b/tests/formats/transition/README.md @@ -11,30 +11,6 @@ Clients should assume forks happen sequentially in the following manner: For example, if a test case has `post_fork` of `altair`, the test consumer should assume the test begins in `phase0` and use that specification to process the initial state and any blocks up until the fork epoch. After the fork happens, the test consumer should use the specification according to the `altair` fork to process the remaining data. -## Encoding notes - -This test type contains objects that span fork boundaries. -In general, it may not be clear which objects belong to which fork so each -object is prefixed with a SSZ `boolean` to indicate if the object belongs to the post fork or if it belongs to the initial fork. -This "flagged" data should be used to select the appropriate version of the spec when interpreting the enclosed object. - -```python -class FlaggedContainer(Container): - flag: boolean - obj: Container -``` - -If `flag` is `False`, then the `obj` belongs to the **initial** fork. -If `flag` is `True`, then the `obj` belongs to the **post** fork. - -Unless stated otherwise, all references to spec types below refer to SSZ-snappy -encoded data `obj` with the relevant `flag` set: -`FlaggedContainer(flag=flag, obj=obj)`. - -For example, when testing the fork from Phase 0 to Altair, an Altair block is given -as the encoding of `FlaggedContainer(flag=True, obj=SignedBeaconBlock())` where -`SignedBeaconBlock` is the type defined in the Altair spec. - ## Test case format ### `meta.yaml` @@ -42,16 +18,17 @@ as the encoding of `FlaggedContainer(flag=True, obj=SignedBeaconBlock())` where ```yaml post_fork: string -- String name of the spec after the fork. fork_epoch: int -- The epoch at which the fork takes place. -blocks_count: int -- The number of blocks processed in this test. +fork_block: int -- Optional. The `` of the last block on the initial fork. +blocks_count: int -- Optional. The number of blocks processed in this test. ``` -*Note*: There may be a fork transition function to run at the `fork_epoch`. Refer to the specs for the relevant fork for further details. +*Note*: There may be a fork transition function to run at the `fork_epoch`. +Refer to the specs for the relevant fork for further details. ### `pre.ssz_snappy` -A SSZ-snappy encoded `BeaconState` according to the specification of the initial fork, the state before running the block transitions. - -*NOTE*: This object is _not_ "flagged" as it is assumed to always belong to the pre fork. +A SSZ-snappy encoded `BeaconState` according to the specification of +the initial fork, the state before running the block transitions. ### `blocks_.ssz_snappy` @@ -59,13 +36,37 @@ A series of files, with `` in range `[0, blocks_count)`. Blocks must be processed in order, following the main transition function (i.e. process slot and epoch transitions in between blocks as normal). -Blocks are encoded as `SignedBeaconBlock`s from the relevant spec version indicated by flag data as described in the `Encoding notes`. +*Note*: `blocks_count` will be missing if there are no blocks in this test. + +Blocks are encoded as `SignedBeaconBlock`s from the relevant spec version +as indicated by the `post_fork` and `fork_block` data in the `meta.yaml`. + +As blocks span fork boundaires, a `fork_block` number is given in +the `meta.yaml` to help resolve which blocks belong to which fork. + +The `fork_block` is the index in the test data of the **last** block +of the **initial** fork. + +To demonstrate, the following diagram shows slots with `_` and blocks +in those slots as `x`. The fork happens at the epoch delineated by the `|`. + +``` +x x x x +_ _ _ _ | _ _ _ _ +``` + +The `blocks_count` value in the `meta.yaml` in this case is `4` where the +`fork_block` value in the `meta.yaml` is `1`. If this particular example were +testing the fork from Phase 0 to Altair, blocks with indices `0, 1` represent +`SignedBeaconBlock`s defined in the Phase 0 spec and blocks with indices `2, 3` +represent `SignedBeaconBlock`s defined in the Altair spec. + +*Note*: `fork_block` will be missing if `blocks_count` is also missing. ### `post.ssz_snappy` -A SSZ-snappy encoded `BeaconState` according to the specification of the post fork, the state after running the block transitions. - -*NOTE*: This object is _not_ "flagged" as it is assumed to always belong to the post fork. +A SSZ-snappy encoded `BeaconState` according to the specification of +the post fork, the state after running the block transitions. ## Condition