Ejemplo n.º 1
0
 def _get_canonical_head_root(db: DatabaseAPI) -> Hash32:
     try:
         canonical_head_root = db[
             SchemaV1.make_canonical_head_root_lookup_key()]
     except KeyError:
         raise CanonicalHeadNotFound("No canonical head set for this chain")
     return canonical_head_root
Ejemplo n.º 2
0
    def _set_as_canonical_chain_head(
        cls,
        db: DatabaseAPI,
        block_root: SigningRoot,
        block_class: Type[BaseBeaconBlock],
    ) -> Tuple[Tuple[BaseBeaconBlock, ...], Tuple[BaseBeaconBlock, ...]]:
        """
        Set the canonical chain HEAD to the block as specified by the
        given block root.

        :return: a tuple of the blocks that are newly in the canonical chain, and the blocks that
            are no longer in the canonical chain
        """
        try:
            block = cls._get_block_by_root(db, block_root, block_class)
        except BlockNotFound:
            raise ValueError(
                "Cannot use unknown block root as canonical head: {block_root.hex()}"
            )

        new_canonical_blocks = tuple(
            reversed(cls._find_new_ancestors(db, block, block_class)))
        old_canonical_blocks = []

        for block in new_canonical_blocks:
            try:
                old_canonical_root = cls._get_canonical_block_root(
                    db, block.slot)
            except BlockNotFound:
                # no old_canonical block, and no more possible
                break
            else:
                old_canonical_block = cls._get_block_by_root(
                    db, old_canonical_root, block_class)
                old_canonical_blocks.append(old_canonical_block)

        for block in new_canonical_blocks:
            cls._add_block_slot_to_root_lookup(db, block)

        db.set(SchemaV1.make_canonical_head_root_lookup_key(),
               block.signing_root)

        return new_canonical_blocks, tuple(old_canonical_blocks)