def _get_finalized_head_root(db: DatabaseAPI) -> Hash32: try: finalized_head_root = db[ SchemaV1.make_finalized_head_root_lookup_key()] except KeyError: raise FinalizedHeadNotFound("No finalized head set for this chain") return finalized_head_root
def _update_finalized_head(self, finalized_root: Root) -> None: """ Unconditionally write the ``finalized_root`` as the root of the currently finalized block. """ self.db.set(SchemaV1.make_finalized_head_root_lookup_key(), finalized_root) self._finalized_root = finalized_root
def _get_finalized_head(cls, db: BaseDB, block_class: Type[BaseBeaconBlock]) -> BaseBeaconBlock: try: finalized_head_root = db[SchemaV1.make_finalized_head_root_lookup_key()] except KeyError: raise CanonicalHeadNotFound("No finalized head set for this chain") return cls._get_block_by_root(db, Hash32(finalized_head_root), block_class)
def _persist_block_chain( cls, db: BaseDB, blocks: Iterable[BaseBeaconBlock], block_class: Type[BaseBeaconBlock] ) -> Tuple[Tuple[BaseBeaconBlock, ...], Tuple[BaseBeaconBlock, ...]]: blocks_iterator = iter(blocks) try: first_block = first(blocks_iterator) except StopIteration: return tuple(), tuple() try: previous_canonical_head = cls._get_canonical_head( db, block_class).signed_root head_score = cls._get_score(db, previous_canonical_head) except CanonicalHeadNotFound: no_canonical_head = True else: no_canonical_head = False is_genesis = first_block.previous_block_root == GENESIS_PARENT_HASH if not is_genesis and not cls._block_exists( db, first_block.previous_block_root): raise ParentNotFound( "Cannot persist block ({}) with unknown parent ({})".format( encode_hex(first_block.signed_root), encode_hex(first_block.previous_block_root), )) if is_genesis: score = 0 # TODO: this should probably be done as part of the fork choice rule processing db.set( SchemaV1.make_finalized_head_root_lookup_key(), first_block.signed_root, ) else: score = first_block.slot curr_block_head = first_block db.set( curr_block_head.signed_root, ssz.encode(curr_block_head), ) cls._add_block_root_to_slot_lookup(db, curr_block_head) cls._set_block_scores_to_db(db, curr_block_head) orig_blocks_seq = concat([(first_block, ), blocks_iterator]) for parent, child in sliding_window(2, orig_blocks_seq): if parent.signed_root != child.previous_block_root: raise ValidationError( "Non-contiguous chain. Expected {} to have {} as parent but was {}" .format( encode_hex(child.signed_root), encode_hex(parent.signed_root), encode_hex(child.previous_block_root), )) curr_block_head = child db.set( curr_block_head.signed_root, ssz.encode(curr_block_head), ) cls._add_block_root_to_slot_lookup(db, curr_block_head) score = cls._set_block_scores_to_db(db, curr_block_head) if no_canonical_head: return cls._set_as_canonical_chain_head( db, curr_block_head.signed_root, block_class) if score > head_score: return cls._set_as_canonical_chain_head( db, curr_block_head.signed_root, block_class) else: return tuple(), tuple()
def _load_finalized_head(self) -> None: finalized_root = self.db.get( SchemaV1.make_finalized_head_root_lookup_key(), ZERO_ROOT) self._finalized_root = finalized_root