def _get_canonical_head(cls, db: BaseDB) -> BaseBeaconBlock: try: canonical_head_root = db[ SchemaV1.make_canonical_head_root_lookup_key()] except KeyError: raise CanonicalHeadNotFound("No canonical head set for this chain") return cls._get_block_by_root(db, Hash32(canonical_head_root))
def _set_as_canonical_chain_head( cls, db: BaseDB, block_root: Hash32 ) -> 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) except BlockNotFound: raise ValueError( "Cannot use unknown block root as canonical head: {}".format( block_root)) new_canonical_blocks = tuple( reversed(cls._find_new_ancestors(db, block))) 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) 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.root) return new_canonical_blocks, tuple(old_canonical_blocks)