def get_valid_custody_chunk_response(spec, state, chunk_challenge, challenge_index, block_length_or_custody_data, invalid_chunk_data=False): if isinstance(block_length_or_custody_data, int): custody_data = get_custody_test_vector(block_length_or_custody_data) else: custody_data = block_length_or_custody_data custody_data_block = ByteList[spec.MAX_SHARD_BLOCK_SIZE](custody_data) chunks = custody_chunkify(spec, custody_data_block) chunk_index = chunk_challenge.chunk_index leaf_index = chunk_index + 2**spec.CUSTODY_RESPONSE_DEPTH serialized_length = len(custody_data_block).to_bytes(32, 'little') data_branch = build_proof(custody_data_block.get_backing().get_left(), leaf_index) + [serialized_length] return spec.CustodyChunkResponse( challenge_index=challenge_index, chunk_index=chunk_index, chunk=chunks[chunk_index], branch=data_branch, )
def test_finality_root_tree(spec, state): finality_branch = build_proof(state.get_backing(), spec.FINALIZED_ROOT_INDEX) assert spec.is_valid_merkle_branch( leaf=state.finalized_checkpoint.root, branch=finality_branch, depth=spec.floorlog2(spec.FINALIZED_ROOT_INDEX), index=spec.get_subtree_index(spec.FINALIZED_ROOT_INDEX), root=state.hash_tree_root(), )
def test_process_light_client_update_timeout(spec, state): store = initialize_light_client_store(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)) 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(), ) # Sync committee signing the finalized_block_header sync_aggregate = get_sync_aggregate(spec, state, block_header, block_root=spec.Root( block_header.hash_tree_root())) # Sync committee is updated next_sync_committee_branch = build_proof(state.get_backing(), spec.NEXT_SYNC_COMMITTEE_INDEX) # Finality is unchanged finality_header = spec.BeaconBlockHeader() finality_branch = [ spec.Bytes32() for _ in range(spec.floorlog2(spec.FINALIZED_ROOT_INDEX)) ] update = spec.LightClientUpdate( attested_header=block_header, next_sync_committee=state.next_sync_committee, next_sync_committee_branch=next_sync_committee_branch, finalized_header=finality_header, finality_branch=finality_branch, sync_aggregate=sync_aggregate, fork_version=state.fork.current_version, ) pre_store = deepcopy(store) spec.process_light_client_update(store, update, state.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
def test_next_sync_committee_tree(spec, state): state.next_sync_committee: object = spec.SyncCommittee( pubkeys=[state.validators[i]for i in range(spec.SYNC_COMMITTEE_SIZE)] ) next_sync_committee_branch = build_proof(state.get_backing(), spec.NEXT_SYNC_COMMITTEE_INDEX) assert spec.is_valid_merkle_branch( leaf=state.next_sync_committee.hash_tree_root(), branch=next_sync_committee_branch, depth=spec.floorlog2(spec.NEXT_SYNC_COMMITTEE_INDEX), index=spec.get_subtree_index(spec.NEXT_SYNC_COMMITTEE_INDEX), root=state.hash_tree_root(), )
def test_next_sync_committee_merkle_proof(spec, state): yield "state", state next_sync_committee_branch = build_proof(state.get_backing(), spec.NEXT_SYNC_COMMITTEE_INDEX) yield "proof", { "leaf": "0x" + state.next_sync_committee.hash_tree_root().hex(), "leaf_index": spec.NEXT_SYNC_COMMITTEE_INDEX, "branch": ['0x' + root.hex() for root in next_sync_committee_branch] } assert spec.is_valid_merkle_branch( leaf=state.next_sync_committee.hash_tree_root(), branch=next_sync_committee_branch, depth=spec.floorlog2(spec.NEXT_SYNC_COMMITTEE_INDEX), index=spec.get_subtree_index(spec.NEXT_SYNC_COMMITTEE_INDEX), root=state.hash_tree_root(), )
def test_finality_root_merkle_proof(spec, state): yield "state", state finality_branch = build_proof(state.get_backing(), spec.FINALIZED_ROOT_INDEX) yield "proof", { "leaf": "0x" + state.finalized_checkpoint.root.hex(), "leaf_index": spec.FINALIZED_ROOT_INDEX, "branch": ['0x' + root.hex() for root in finality_branch] } assert spec.is_valid_merkle_branch( leaf=state.finalized_checkpoint.root, branch=finality_branch, depth=spec.floorlog2(spec.FINALIZED_ROOT_INDEX), index=spec.get_subtree_index(spec.FINALIZED_ROOT_INDEX), root=state.hash_tree_root(), )
def test_process_light_client_update_timeout(spec, state): pre_snapshot = spec.LightClientSnapshot( header=spec.BeaconBlockHeader(), current_sync_committee=state.current_sync_committee, next_sync_committee=state.next_sync_committee, ) store = spec.LightClientStore(snapshot=pre_snapshot, valid_updates=[]) # Forward to next sync committee period next_slots(spec, state, spec.SLOTS_PER_EPOCH * (spec.EPOCHS_PER_SYNC_COMMITTEE_PERIOD)) snapshot_period = spec.compute_epoch_at_slot( pre_snapshot.header.slot) // spec.EPOCHS_PER_SYNC_COMMITTEE_PERIOD update_period = spec.compute_epoch_at_slot( state.slot) // spec.EPOCHS_PER_SYNC_COMMITTEE_PERIOD 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(), ) # Sync committee signing the finalized_block_header committee = spec.get_sync_committee_indices(state, spec.get_current_epoch(state)) sync_committee_bits = [True] * len(committee) sync_committee_signature = compute_aggregate_sync_committee_signature( spec, state, block_header.slot, committee, block_root=spec.Root(block_header.hash_tree_root()), ) # Sync committee is updated next_sync_committee_branch = build_proof(state.get_backing(), spec.NEXT_SYNC_COMMITTEE_INDEX) # Finality is unchanged finality_header = spec.BeaconBlockHeader() finality_branch = [ spec.Bytes32() for _ in range(spec.floorlog2(spec.FINALIZED_ROOT_INDEX)) ] update = spec.LightClientUpdate( header=block_header, next_sync_committee=state.next_sync_committee, next_sync_committee_branch=next_sync_committee_branch, finality_header=finality_header, finality_branch=finality_branch, sync_committee_bits=sync_committee_bits, sync_committee_signature=sync_committee_signature, fork_version=state.fork.current_version, ) spec.process_light_client_update(store, update, state.slot, state.genesis_validators_root) # snapshot has been updated assert len(store.valid_updates) == 0 assert store.snapshot.header == update.header
def test_process_light_client_update_finality_updated(spec, state): pre_snapshot = spec.LightClientSnapshot( header=spec.BeaconBlockHeader(), current_sync_committee=state.current_sync_committee, next_sync_committee=state.next_sync_committee, ) store = spec.LightClientStore(snapshot=pre_snapshot, valid_updates=[]) # Change finality blocks = [] next_slots(spec, state, spec.SLOTS_PER_EPOCH * 2) for epoch in range(3): prev_state, new_blocks, state = next_epoch_with_attestations( spec, state, True, True) blocks += new_blocks # Ensure that finality checkpoint has changed assert state.finalized_checkpoint.epoch == 3 # Ensure that it's same period snapshot_period = spec.compute_epoch_at_slot( pre_snapshot.header.slot) // spec.EPOCHS_PER_SYNC_COMMITTEE_PERIOD update_period = spec.compute_epoch_at_slot( state.slot) // spec.EPOCHS_PER_SYNC_COMMITTEE_PERIOD assert snapshot_period == update_period # Updated sync_committee and finality 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 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 finalized_block_header committee = spec.get_sync_committee_indices(state, spec.get_current_epoch(state)) sync_committee_bits = [True] * len(committee) sync_committee_signature = compute_aggregate_sync_committee_signature( spec, state, block_header.slot, committee, block_root=spec.Root(block_header.hash_tree_root()), ) update = spec.LightClientUpdate( header=finalized_block_header, next_sync_committee=state.next_sync_committee, next_sync_committee_branch=next_sync_committee_branch, finality_header=block_header, # block_header is the signed header finality_branch=finality_branch, sync_committee_bits=sync_committee_bits, sync_committee_signature=sync_committee_signature, fork_version=state.fork.current_version, ) spec.process_light_client_update(store, update, state.slot, state.genesis_validators_root) # snapshot has been updated assert len(store.valid_updates) == 0 assert store.snapshot.header == update.header
def test_process_light_client_update_finality_updated(spec, state): store = initialize_light_client_store(spec, state) # Change finality blocks = [] next_slots(spec, state, spec.SLOTS_PER_EPOCH * 2) for epoch in range(3): prev_state, new_blocks, state = next_epoch_with_attestations( spec, state, True, True) blocks += new_blocks # 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)) update_period = spec.compute_sync_committee_period( spec.compute_epoch_at_slot(state.slot)) assert snapshot_period == update_period # Updated sync_committee and finality 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 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 finalized_block_header sync_aggregate = get_sync_aggregate(spec, state, block_header, block_root=spec.Root( block_header.hash_tree_root())) update = spec.LightClientUpdate( attested_header=block_header, next_sync_committee=state.next_sync_committee, next_sync_committee_branch=next_sync_committee_branch, finalized_header=finalized_block_header, finality_branch=finality_branch, sync_aggregate=sync_aggregate, fork_version=state.fork.current_version, ) spec.process_light_client_update(store, update, state.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