def _get_canonical_head(cls, db: BaseDB) -> BaseBeaconBlock: try: canonical_head_hash = db[ SchemaV1.make_canonical_head_hash_lookup_key()] except KeyError: raise CanonicalHeadNotFound("No canonical head set for this chain") return cls._get_block_by_hash(db, canonical_head_hash)
def _set_as_canonical_chain_head( cls, db: BaseDB, block_hash: Hash32 ) -> Tuple[Tuple[BaseBeaconBlock, ...], Tuple[BaseBeaconBlock, ...]]: """ Set the canonical chain HEAD to the block as specified by the given block hash. :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_hash(db, block_hash) except BlockNotFound: raise ValueError( "Cannot use unknown block hash as canonical head: {}".format( block_hash)) new_canonical_blocks = tuple( reversed(cls._find_new_ancestors(db, block))) old_canonical_blocks = [] for block in new_canonical_blocks: try: old_canonical_hash = cls._get_canonical_block_hash( db, block.slot_number) except BlockNotFound: # no old_canonical block, and no more possible break else: old_canonical_block = cls._get_block_by_hash( db, old_canonical_hash) old_canonical_blocks.append(old_canonical_block) for block in new_canonical_blocks: cls._add_block_slot_to_hash_lookup(db, block) db.set(SchemaV1.make_canonical_head_hash_lookup_key(), block.hash) return new_canonical_blocks, tuple(old_canonical_blocks)