Skip to content

Commit

Permalink
fix tests. add new generator
Browse files Browse the repository at this point in the history
  • Loading branch information
djrtwo committed Oct 3, 2021
1 parent af262be commit 6b95a5b
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 3 deletions.
7 changes: 5 additions & 2 deletions specs/merge/beacon-chain.md
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,8 @@ Modifications include:
def initialize_beacon_state_from_eth1(eth1_block_hash: Bytes32,
eth1_timestamp: uint64,
deposits: Sequence[Deposit],
execution_payload_header: ExecutionPayloadHeader) -> BeaconState:
execution_payload_header: Union[None, ExecutionPayloadHeader]=None
) -> BeaconState:
fork = Fork(
previous_version=MERGE_FORK_VERSION, # [Modified in Merge] for testing only
current_version=MERGE_FORK_VERSION, # [Modified in Merge]
Expand Down Expand Up @@ -403,7 +404,9 @@ def initialize_beacon_state_from_eth1(eth1_block_hash: Bytes32,
state.next_sync_committee = get_next_sync_committee(state)

# [New in Merge] Initialize the execution payload header
state.latest_execution_payload_header = execution_payload_header
# If empty, will initialize a chain that has not yet gone through the Merge transition
if execution_payload_header is not None:
state.latest_execution_payload_header = execution_payload_header

return state
```
129 changes: 129 additions & 0 deletions tests/core/pyspec/eth2spec/test/merge/genesis/test_initialization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
from eth2spec.test.context import (
single_phase,
spec_test,
with_presets,
with_merge_and_later,
)
from eth2spec.test.helpers.constants import MINIMAL
from eth2spec.test.helpers.deposits import (
prepare_full_genesis_deposits,
)


def eth1_init_data(eth1_block_hash, eth1_timestamp):
yield 'eth1', {
'eth1_block_hash': '0x' + eth1_block_hash.hex(),
'eth1_timestamp': int(eth1_timestamp),
}


@with_merge_and_later
@spec_test
@single_phase
@with_presets([MINIMAL], reason="too slow")
def test_initialize_pre_transition_none(spec):
deposit_count = spec.config.MIN_GENESIS_ACTIVE_VALIDATOR_COUNT
deposits, deposit_root, _ = prepare_full_genesis_deposits(
spec,
spec.MAX_EFFECTIVE_BALANCE,
deposit_count,
signed=True,
)

eth1_block_hash = b'\x12' * 32
eth1_timestamp = spec.config.MIN_GENESIS_TIME

yield from eth1_init_data(eth1_block_hash, eth1_timestamp)
yield 'deposits', deposits

# initialize beacon_state *without* an execution_payload_header
state = spec.initialize_beacon_state_from_eth1(eth1_block_hash, eth1_timestamp, deposits)

assert not spec.is_merge_comp(state)

# yield state
yield 'state', state


@with_merge_and_later
@spec_test
@single_phase
@with_presets([MINIMAL], reason="too slow")
def test_initialize_pre_transition_empty_payload(spec):
deposit_count = spec.config.MIN_GENESIS_ACTIVE_VALIDATOR_COUNT
deposits, deposit_root, _ = prepare_full_genesis_deposits(
spec,
spec.MAX_EFFECTIVE_BALANCE,
deposit_count,
signed=True,
)

eth1_block_hash = b'\x12' * 32
eth1_timestamp = spec.config.MIN_GENESIS_TIME

yield from eth1_init_data(eth1_block_hash, eth1_timestamp)
yield 'deposits', deposits

# initialize beacon_state *without* an execution_payload_header
state = spec.initialize_beacon_state_from_eth1(
eth1_block_hash,
eth1_timestamp,
deposits,
spec.ExecutionPayloadHeader()
)

assert not spec.is_merge_complete(state)

yield 'execution_payload_header', spec.ExecutionPayloadHeader()

# yield state
yield 'state', state


@with_merge_and_later
@spec_test
@single_phase
@with_presets([MINIMAL], reason="too slow")
def test_initialize_post_transition(spec):
deposit_count = spec.config.MIN_GENESIS_ACTIVE_VALIDATOR_COUNT
deposits, deposit_root, _ = prepare_full_genesis_deposits(
spec,
spec.MAX_EFFECTIVE_BALANCE,
deposit_count,
signed=True,
)

eth1_block_hash = b'\x12' * 32
eth1_timestamp = spec.config.MIN_GENESIS_TIME

yield from eth1_init_data(eth1_block_hash, eth1_timestamp)
yield 'deposits', deposits

# initialize beacon_state *with* an execution_payload_header
genesis_execution_payload_header = spec.ExecutionPayloadHeader(
parent_hash=b'\x30' * 32,
coinbase=b'\x42' * 20,
state_root=b'\x20' * 32,
receipt_root=b'\x20' * 32,
logs_bloom=b'\x35' * spec.BYTES_PER_LOGS_BLOOM,
random=b'\x55' * 32,
block_number=0,
gas_limit=30000000,
base_fee_per_gas=b'\x10' * 32,
block_hash=b'\x99' * 32,
transactions_root=spec.Root(b'\x56' * 32),

)
state = spec.initialize_beacon_state_from_eth1(
eth1_block_hash,
eth1_timestamp,
deposits,
genesis_execution_payload_header,
)

yield 'execution_payload_header', genesis_execution_payload_header

assert spec.is_merge_complete(state)

# yield state
yield 'state', state
10 changes: 9 additions & 1 deletion tests/formats/genesis/initialization.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,19 @@ deposits_count: int -- Amount of deposits.
A series of files, with `<index>` in range `[0, deposits_count)`. Deposits need to be processed in order.
Each file is a SSZ-snappy encoded `Deposit` object.

### `execution_payload_header.ssz_snappy`

*Note*: Param added only for the Merge and subsequent forks.

The execution payload header that state is initialized with. An SSZ-snappy encoded `BeaconState` object.

The absence of this file means `None` is passed into the function.


### `state.ssz_snappy`

The expected genesis state. An SSZ-snappy encoded `BeaconState` object.


## Processing

To process this test, build a genesis state with the provided `eth1_block_hash`, `eth1_timestamp` and `deposits`:
Expand Down

0 comments on commit 6b95a5b

Please sign in to comment.