def test_parent_from_same_slot(spec, state): yield 'pre', state parent_block = build_empty_block_for_next_slot(spec, state) signed_parent_block = state_transition_and_sign_block( spec, state, parent_block) child_block = parent_block.copy() child_block.parent_root = state.latest_block_header.hash_tree_root() if is_post_merge(spec): child_block.body.execution_payload = build_empty_execution_payload( spec, state) # Show that normal path through transition fails failed_state = state.copy() expect_assertion_error(lambda: spec.state_transition( failed_state, spec.SignedBeaconBlock(message=child_block), validate_result=False)) # Artificially bypass the restriction in the state transition to transition and sign block for test vectors signed_child_block = process_and_sign_block_without_header_validations( spec, state, child_block) yield 'blocks', [signed_parent_block, signed_child_block] yield 'post', None
def build_empty_block(spec, state, slot=None): """ Build empty block for ``slot``, built upon the latest block header seen by ``state``. Slot must be greater than or equal to the current slot in ``state``. """ if slot is None: slot = state.slot if slot < state.slot: raise Exception("build_empty_block cannot build blocks for past slots") if state.slot < slot: # transition forward in copied state to grab relevant data from state state = state.copy() spec.process_slots(state, slot) state, parent_block_root = get_state_and_beacon_parent_root_at_slot( spec, state, slot) empty_block = spec.BeaconBlock() empty_block.slot = slot empty_block.proposer_index = spec.get_beacon_proposer_index(state) empty_block.body.eth1_data.deposit_count = state.eth1_deposit_index empty_block.parent_root = parent_block_root if is_post_altair(spec): empty_block.body.sync_aggregate.sync_committee_signature = spec.G2_POINT_AT_INFINITY if is_post_merge(spec): empty_block.body.execution_payload = build_empty_execution_payload( spec, state) apply_randao_reveal(spec, state, empty_block) return empty_block
def process_and_sign_block_without_header_validations(spec, state, block): """ Artificially bypass the restrictions in the state transition to transition and sign block WARNING UNSAFE: Only use when generating valid-looking invalid blocks for test vectors """ # Perform single mutation in `process_block_header` state.latest_block_header = spec.BeaconBlockHeader( slot=block.slot, proposer_index=block.proposer_index, parent_root=block.parent_root, state_root=spec.Bytes32(), body_root=block.body.hash_tree_root(), ) if is_post_merge(spec): if spec.is_execution_enabled(state, block.body): spec.process_execution_payload(state, block.body.execution_payload, spec.EXECUTION_ENGINE) # Perform rest of process_block transitions spec.process_randao(state, block.body) spec.process_eth1_data(state, block.body) spec.process_operations(state, block.body) if is_post_altair(spec): spec.process_sync_aggregate(state, block.body.sync_aggregate) # Insert post-state rot block.state_root = state.hash_tree_root() # Sign block return sign_block(spec, state, block)