Esempio n. 1
0
async def process_metrics(chain: BaseBeaconChain,
                          event_bus: EndpointAPI) -> None:
    # Networking
    libp2p_peers = await event_bus.request(Libp2pPeersRequest())
    metrics.libp2p_peers.set(len(libp2p_peers.result))

    # Per slot info
    beacon_slot = chain.get_head_state_slot()
    metrics.beacon_slot.set(beacon_slot)

    # Per block info
    head_block = chain.get_canonical_head()
    metrics.beacon_head_slot.set(head_block.slot)
    metrics.beacon_head_root.set(root_to_int(head_block.hash_tree_root))

    # Per epoch info
    epoch_info = chain.get_canonical_epoch_info()
    metrics.beacon_previous_justified_epoch.set(
        epoch_info.previous_justified_checkpoint.epoch)
    metrics.beacon_previous_justified_root.set(
        root_to_int(epoch_info.previous_justified_checkpoint.root), )
    metrics.beacon_current_justified_epoch.set(
        epoch_info.current_justified_checkpoint.epoch)
    metrics.beacon_current_justified_root.set(
        root_to_int(epoch_info.current_justified_checkpoint.root), )
    metrics.beacon_finalized_epoch.set(epoch_info.finalized_checkpoint.epoch)
    metrics.beacon_finalized_root.set(
        root_to_int(epoch_info.finalized_checkpoint.root))
Esempio n. 2
0
def peer_is_ahead(chain: BaseBeaconChain, peer_status: Status) -> bool:
    checkpoint = chain.get_head_state().finalized_checkpoint
    head_block = chain.get_canonical_head()

    peer_has_higher_finalized_epoch = peer_status.finalized_epoch > checkpoint.epoch
    peer_has_equal_finalized_epoch = peer_status.finalized_epoch == checkpoint.epoch
    peer_has_higher_head_slot = peer_status.head_slot > head_block.slot
    return (peer_has_higher_finalized_epoch
            or (peer_has_equal_finalized_epoch and peer_has_higher_head_slot))
Esempio n. 3
0
def get_my_status(chain: BaseBeaconChain) -> Status:
    state = chain.get_head_state()
    head = chain.get_canonical_head()
    finalized_checkpoint = state.finalized_checkpoint
    return Status.create(
        head_fork_version=state.fork.current_version,
        finalized_root=finalized_checkpoint.root,
        finalized_epoch=finalized_checkpoint.epoch,
        head_root=head.message.hash_tree_root,
        head_slot=head.slot,
    )
Esempio n. 4
0
def get_blocks(
    chain: BaseBeaconChain,
    parent_block: SerenityBeaconBlock = None,
    num_blocks: int = 3,
) -> Tuple[SerenityBeaconBlock, ...]:
    if parent_block is None:
        parent_block = chain.get_canonical_head()
    blocks = []
    for _ in range(num_blocks):
        block = chain.create_block_from_parent(parent_block=parent_block,
                                               block_params=FromBlockParams())
        blocks.append(block)
        parent_block = block
    return tuple(blocks)
Esempio n. 5
0
def compare_chain_tip_and_finalized_epoch(chain: BaseBeaconChain,
                                          peer_status: Status) -> None:
    checkpoint = chain.get_head_state().finalized_checkpoint
    head_block = chain.get_canonical_head()

    peer_has_higher_finalized_epoch = peer_status.finalized_epoch > checkpoint.epoch
    peer_has_equal_finalized_epoch = peer_status.finalized_epoch == checkpoint.epoch
    peer_has_higher_head_slot = peer_status.head_slot > head_block.slot
    if (peer_has_higher_finalized_epoch
            or (peer_has_equal_finalized_epoch and peer_has_higher_head_slot)):
        # TODO: kickoff syncing process with this peer
        logger.debug(
            "Peer's chain is ahead of us, start syncing with the peer.")
        pass