Exemplo n.º 1
0
def get_blocks_from_canonical_chain_by_slot(
    chain: BaseBeaconChain,
    slot_of_requested_blocks: Sequence[Slot],
) -> Iterable[BaseBeaconBlock]:
    # If peer's head block is on our canonical chain,
    # start getting the requested blocks by slots.
    for slot in slot_of_requested_blocks:
        try:
            block = chain.get_canonical_block_by_slot(slot)
        except BlockNotFound:
            pass
        else:
            yield block
Exemplo n.º 2
0
def _get_requested_beacon_blocks(
    chain: BaseBeaconChain,
    beacon_blocks_request: BeaconBlocksByRangeRequest,
    requested_head_block: BaseBeaconBlock,
) -> Tuple[BaseBeaconBlock, ...]:
    slot_of_requested_blocks = tuple(
        beacon_blocks_request.start_slot + i * beacon_blocks_request.step
        for i in range(beacon_blocks_request.count)
    )
    logger.info("slot_of_requested_blocks: %s", slot_of_requested_blocks)
    slot_of_requested_blocks = tuple(
        filter(lambda slot: slot <= requested_head_block.slot, slot_of_requested_blocks)
    )

    if len(slot_of_requested_blocks) == 0:
        return tuple()

    # We have the peer's head block in our database,
    # next check if the head block is on our canonical chain.
    try:
        canonical_block_at_slot = chain.get_canonical_block_by_slot(
            requested_head_block.slot
        )
        block_match = canonical_block_at_slot == requested_head_block
    except BlockNotFound:
        logger.debug(
            (
                "The requested head block is not on our canonical chain  "
                "requested_head_block: %s"
            ),
            requested_head_block,
        )
        block_match = False
    finally:
        if block_match:
            # Peer's head block is on our canonical chain
            return get_blocks_from_canonical_chain_by_slot(
                chain,
                slot_of_requested_blocks,
            )
        else:
            # Peer's head block is not on our canonical chain
            # Validate `start_slot` is greater than our latest finalized slot
            validate_start_slot(chain, beacon_blocks_request.start_slot)
            return get_blocks_from_fork_chain_by_root(
                chain,
                beacon_blocks_request.start_slot,
                requested_head_block,
                slot_of_requested_blocks,
            )