diff --git a/tests/core/pyspec/eth2spec/test/altair/unittests/test_sync_protocol.py b/tests/core/pyspec/eth2spec/test/altair/unittests/test_sync_protocol.py index a252f0e68d..17c450f170 100644 --- a/tests/core/pyspec/eth2spec/test/altair/unittests/test_sync_protocol.py +++ b/tests/core/pyspec/eth2spec/test/altair/unittests/test_sync_protocol.py @@ -7,19 +7,16 @@ ) from eth2spec.test.helpers.attestations import ( next_epoch_with_attestations, -) -from eth2spec.test.helpers.block import ( - build_empty_block, - build_empty_block_for_next_slot, + state_transition_with_full_block, ) from eth2spec.test.helpers.constants import MINIMAL from eth2spec.test.helpers.light_client import ( get_sync_aggregate, initialize_light_client_store, + signed_block_to_header, ) from eth2spec.test.helpers.state import ( next_slots, - state_transition_and_sign_block, ) from eth2spec.test.helpers.merkle import build_proof @@ -30,18 +27,11 @@ def test_process_light_client_update_not_timeout(spec, state): store = initialize_light_client_store(spec, state) # Block at slot 1 doesn't increase sync committee period, so it won't force update store.finalized_header - block = build_empty_block_for_next_slot(spec, state) - signed_block = state_transition_and_sign_block(spec, state, block) - block_header = spec.BeaconBlockHeader( - slot=signed_block.message.slot, - proposer_index=signed_block.message.proposer_index, - parent_root=signed_block.message.parent_root, - state_root=signed_block.message.state_root, - body_root=signed_block.message.body.hash_tree_root(), - ) + attested_block = state_transition_with_full_block(spec, state, False, False) + attested_header = signed_block_to_header(spec, attested_block) - # Sync committee signing the block_header - sync_aggregate, signature_slot = get_sync_aggregate(spec, state, block_header) + # Sync committee signing the attested_header + sync_aggregate, signature_slot = get_sync_aggregate(spec, state) next_sync_committee = spec.SyncCommittee() next_sync_committee_branch = [spec.Bytes32() for _ in range(spec.floorlog2(spec.NEXT_SYNC_COMMITTEE_INDEX))] @@ -52,7 +42,7 @@ def test_process_light_client_update_not_timeout(spec, state): finality_branch = [spec.Bytes32() for _ in range(spec.floorlog2(spec.FINALIZED_ROOT_INDEX))] update = spec.LightClientUpdate( - attested_header=block_header, + attested_header=attested_header, next_sync_committee=next_sync_committee, next_sync_committee_branch=next_sync_committee_branch, finalized_header=finality_header, @@ -65,10 +55,10 @@ def test_process_light_client_update_not_timeout(spec, state): spec.process_light_client_update(store, update, signature_slot, state.genesis_validators_root) - assert store.current_max_active_participants > 0 - assert store.optimistic_header == update.attested_header assert store.finalized_header == pre_store.finalized_header assert store.best_valid_update == update + assert store.optimistic_header == update.attested_header + assert store.current_max_active_participants > 0 @with_altair_and_later @@ -79,22 +69,15 @@ def test_process_light_client_update_at_period_boundary(spec, state): # Forward to slot before next sync committee period so that next block is final one in period next_slots(spec, state, spec.UPDATE_TIMEOUT - 2) - snapshot_period = spec.compute_sync_committee_period(spec.compute_epoch_at_slot(store.optimistic_header.slot)) + store_period = spec.compute_sync_committee_period(spec.compute_epoch_at_slot(store.optimistic_header.slot)) update_period = spec.compute_sync_committee_period(spec.compute_epoch_at_slot(state.slot)) - assert snapshot_period == update_period - - block = build_empty_block_for_next_slot(spec, state) - signed_block = state_transition_and_sign_block(spec, state, block) - block_header = spec.BeaconBlockHeader( - slot=signed_block.message.slot, - proposer_index=signed_block.message.proposer_index, - parent_root=signed_block.message.parent_root, - state_root=signed_block.message.state_root, - body_root=signed_block.message.body.hash_tree_root(), - ) + assert store_period == update_period + + attested_block = state_transition_with_full_block(spec, state, False, False) + attested_header = signed_block_to_header(spec, attested_block) - # Sync committee signing the block_header - sync_aggregate, signature_slot = get_sync_aggregate(spec, state, block_header) + # Sync committee signing the attested_header + sync_aggregate, signature_slot = get_sync_aggregate(spec, state) next_sync_committee = spec.SyncCommittee() next_sync_committee_branch = [spec.Bytes32() for _ in range(spec.floorlog2(spec.NEXT_SYNC_COMMITTEE_INDEX))] @@ -103,7 +86,7 @@ def test_process_light_client_update_at_period_boundary(spec, state): finality_branch = [spec.Bytes32() for _ in range(spec.floorlog2(spec.FINALIZED_ROOT_INDEX))] update = spec.LightClientUpdate( - attested_header=block_header, + attested_header=attested_header, next_sync_committee=next_sync_committee, next_sync_committee_branch=next_sync_committee_branch, finalized_header=finality_header, @@ -116,10 +99,10 @@ def test_process_light_client_update_at_period_boundary(spec, state): spec.process_light_client_update(store, update, signature_slot, state.genesis_validators_root) - assert store.current_max_active_participants > 0 - assert store.optimistic_header == update.attested_header - assert store.best_valid_update == update assert store.finalized_header == pre_store.finalized_header + assert store.best_valid_update == update + assert store.optimistic_header == update.attested_header + assert store.current_max_active_participants > 0 @with_altair_and_later @@ -130,22 +113,15 @@ def test_process_light_client_update_timeout(spec, state): # Forward to next sync committee period next_slots(spec, state, spec.UPDATE_TIMEOUT) - snapshot_period = spec.compute_sync_committee_period(spec.compute_epoch_at_slot(store.optimistic_header.slot)) + store_period = spec.compute_sync_committee_period(spec.compute_epoch_at_slot(store.optimistic_header.slot)) update_period = spec.compute_sync_committee_period(spec.compute_epoch_at_slot(state.slot)) - assert snapshot_period + 1 == update_period - - block = build_empty_block_for_next_slot(spec, state) - signed_block = state_transition_and_sign_block(spec, state, block) - block_header = spec.BeaconBlockHeader( - slot=signed_block.message.slot, - proposer_index=signed_block.message.proposer_index, - parent_root=signed_block.message.parent_root, - state_root=signed_block.message.state_root, - body_root=signed_block.message.body.hash_tree_root(), - ) + assert store_period + 1 == update_period + + attested_block = state_transition_with_full_block(spec, state, False, False) + attested_header = signed_block_to_header(spec, attested_block) - # Sync committee signing the block_header - sync_aggregate, signature_slot = get_sync_aggregate(spec, state, block_header) + # Sync committee signing the attested_header + sync_aggregate, signature_slot = get_sync_aggregate(spec, state) # Sync committee is updated next_sync_committee = state.next_sync_committee @@ -155,7 +131,7 @@ def test_process_light_client_update_timeout(spec, state): finality_branch = [spec.Bytes32() for _ in range(spec.floorlog2(spec.FINALIZED_ROOT_INDEX))] update = spec.LightClientUpdate( - attested_header=block_header, + attested_header=attested_header, next_sync_committee=next_sync_committee, next_sync_committee_branch=next_sync_committee_branch, finalized_header=finality_header, @@ -168,10 +144,10 @@ def test_process_light_client_update_timeout(spec, state): spec.process_light_client_update(store, update, signature_slot, state.genesis_validators_root) - assert store.current_max_active_participants > 0 - assert store.optimistic_header == update.attested_header - assert store.best_valid_update == update assert store.finalized_header == pre_store.finalized_header + assert store.best_valid_update == update + assert store.optimistic_header == update.attested_header + assert store.current_max_active_participants > 0 @with_altair_and_later @@ -189,36 +165,30 @@ def test_process_light_client_update_finality_updated(spec, state): # Ensure that finality checkpoint has changed assert state.finalized_checkpoint.epoch == 3 # Ensure that it's same period - snapshot_period = spec.compute_sync_committee_period(spec.compute_epoch_at_slot(store.optimistic_header.slot)) + store_period = spec.compute_sync_committee_period(spec.compute_epoch_at_slot(store.optimistic_header.slot)) update_period = spec.compute_sync_committee_period(spec.compute_epoch_at_slot(state.slot)) - assert snapshot_period == update_period + assert store_period == update_period + + attested_block = blocks[-1] + attested_header = signed_block_to_header(spec, attested_block) + + # Sync committee signing the attested_header + sync_aggregate, signature_slot = get_sync_aggregate(spec, state) # Updated sync_committee and finality next_sync_committee = spec.SyncCommittee() next_sync_committee_branch = [spec.Bytes32() for _ in range(spec.floorlog2(spec.NEXT_SYNC_COMMITTEE_INDEX))] - finalized_block_header = blocks[spec.SLOTS_PER_EPOCH - 1].message - assert finalized_block_header.slot == spec.compute_start_slot_at_epoch(state.finalized_checkpoint.epoch) - assert finalized_block_header.hash_tree_root() == state.finalized_checkpoint.root + finalized_block = blocks[spec.SLOTS_PER_EPOCH - 1] + finalized_header = signed_block_to_header(spec, finalized_block) + assert finalized_header.slot == spec.compute_start_slot_at_epoch(state.finalized_checkpoint.epoch) + assert finalized_header.hash_tree_root() == state.finalized_checkpoint.root finality_branch = build_proof(state.get_backing(), spec.FINALIZED_ROOT_INDEX) - # Build block header - block = build_empty_block(spec, state) - block_header = spec.BeaconBlockHeader( - slot=block.slot, - proposer_index=block.proposer_index, - parent_root=block.parent_root, - state_root=state.hash_tree_root(), - body_root=block.body.hash_tree_root(), - ) - - # Sync committee signing the block_header - sync_aggregate, signature_slot = get_sync_aggregate(spec, state, block_header) - update = spec.LightClientUpdate( - attested_header=block_header, + attested_header=attested_header, next_sync_committee=next_sync_committee, next_sync_committee_branch=next_sync_committee_branch, - finalized_header=finalized_block_header, + finalized_header=finalized_header, finality_branch=finality_branch, sync_aggregate=sync_aggregate, signature_slot=signature_slot, @@ -226,7 +196,7 @@ def test_process_light_client_update_finality_updated(spec, state): spec.process_light_client_update(store, update, signature_slot, state.genesis_validators_root) - assert store.current_max_active_participants > 0 - assert store.optimistic_header == update.attested_header assert store.finalized_header == update.finalized_header assert store.best_valid_update is None + assert store.optimistic_header == update.attested_header + assert store.current_max_active_participants > 0 diff --git a/tests/core/pyspec/eth2spec/test/helpers/light_client.py b/tests/core/pyspec/eth2spec/test/helpers/light_client.py index 7dcad319ad..090036094b 100644 --- a/tests/core/pyspec/eth2spec/test/helpers/light_client.py +++ b/tests/core/pyspec/eth2spec/test/helpers/light_client.py @@ -7,6 +7,16 @@ ) +def signed_block_to_header(spec, block): + return spec.BeaconBlockHeader( + slot=block.message.slot, + proposer_index=block.message.proposer_index, + parent_root=block.message.parent_root, + state_root=block.message.state_root, + body_root=block.message.body.hash_tree_root(), + ) + + def initialize_light_client_store(spec, state): return spec.LightClientStore( finalized_header=spec.BeaconBlockHeader(), @@ -19,10 +29,10 @@ def initialize_light_client_store(spec, state): ) -def get_sync_aggregate(spec, state, block_header, signature_slot=None): +def get_sync_aggregate(spec, state, signature_slot=None): # By default, the sync committee signs the previous slot if signature_slot is None: - signature_slot = block_header.slot + 1 + signature_slot = state.slot + 1 # Ensure correct sync committee and fork version are selected signature_state = state.copy() @@ -39,7 +49,6 @@ def get_sync_aggregate(spec, state, block_header, signature_slot=None): signature_state, signature_slot, committee_indices, - block_root=spec.Root(block_header.hash_tree_root()), ) sync_aggregate = spec.SyncAggregate( sync_committee_bits=sync_committee_bits,