def validate_voting_beacon_block(chain: BaseBeaconChain, attestation: Attestation) -> None: # Check that beacon blocks attested to by the attestation are validated try: chain.get_block_by_root(attestation.data.beacon_block_root) except BlockNotFound: raise InvalidGossipMessage( f"Failed to validate attestation={attestation}," f" attested block={encode_hex(attestation.data.beacon_block_root)}" " has not been not validated yet")
def get_blocks_from_fork_chain_by_root( chain: BaseBeaconChain, start_slot: Slot, peer_head_block: BaseBeaconBlock, slot_of_requested_blocks: Sequence[Slot], ) -> Iterable[BaseBeaconBlock]: # Peer's head block is on a fork chain, # start getting the requested blocks by # traversing the history from the head. # `slot_of_requested_blocks` starts with earliest slot # and end with most recent slot, so we start traversing # from the most recent slot. cur_index = len(slot_of_requested_blocks) - 1 block = peer_head_block if block.slot == slot_of_requested_blocks[cur_index]: yield block cur_index -= 1 while block.slot > start_slot and cur_index >= 0: try: block = chain.get_block_by_root(block.parent_root) except (BlockNotFound, ValidationError): # This should not happen as we only persist block if its # ancestors are also in the database. break else: while block.slot < slot_of_requested_blocks[cur_index]: if cur_index > 0: cur_index -= 1 else: break if block.slot == slot_of_requested_blocks[cur_index]: yield block
def get_requested_beacon_blocks( chain: BaseBeaconChain, request: BeaconBlocksByRangeRequest ) -> Tuple[BaseBeaconBlock, ...]: try: requested_head = chain.get_block_by_root( request.head_block_root ) except (BlockNotFound, ValidationError) as error: logger.info("Sending empty blocks, reason: %s", error) return tuple() # Check if slot of specified head block is greater than specified start slot if requested_head.slot < request.start_slot: raise InvalidRequest( f"head block slot({requested_head.slot}) lower than `start_slot`({request.start_slot})" ) try: requested_beacon_blocks = _get_requested_beacon_blocks( chain, request, requested_head ) return requested_beacon_blocks except ValidationError as val_error: raise InvalidRequest(str(val_error))
def get_beacon_blocks_by_root( chain: BaseBeaconChain, request: BeaconBlocksByRootRequest, ) -> Iterable[BaseBeaconBlock]: for block_root in request.block_roots: try: block = chain.get_block_by_root(block_root) except (BlockNotFound, ValidationError): pass else: yield block