def mark_finalized_head(self, block: BaseBeaconBlock) -> None: """ Marks the given ``block`` as finalized and stores each newly finalized state and block at their corresponding slot. """ if block.slot > 0: newly_finalized_states = self.get_state_parents( block.state_root, block.slot - self.get_finalized_head(BeaconBlock).slot) + ( block.state_root, ) else: newly_finalized_states = (block.state_root, ) for state_root in newly_finalized_states: slot = ssz.decode(self.db[SchemaV1.state_root_to_slot(state_root)], ssz.uint64) self.db[SchemaV1.slot_to_state_root(slot)] = state_root latest_block_header = self._read_state_block_header(state_root) slot_to_block_root = SchemaV1.slot_to_block_root( latest_block_header.slot) self.db[slot_to_block_root] = latest_block_header.hash_tree_root self.db[SchemaV1.slot_to_block_root(block.slot)] = block.hash_tree_root self.db[SchemaV1.finalized_head_root()] = block.hash_tree_root
def get_state_by_slot(self, slot: Slot, state_class: Type[BeaconState], config: Eth2Config) -> Optional[BeaconState]: key = SchemaV1.slot_to_state_root(slot) try: root = Root(Hash32(self.db[key])) except KeyError: return None return self.get_state_by_root(root, state_class, config)
def mark_canonical_block(self, block: BaseBeaconBlock) -> None: slot_to_block_root = SchemaV1.slot_to_block_root(block.slot) self.db[slot_to_block_root] = block.hash_tree_root slot_to_state_root = SchemaV1.slot_to_state_root(block.slot) self.db[slot_to_state_root] = block.state_root
def _read_state_root_at_slot(self, slot: Slot) -> Root: key = SchemaV1.slot_to_state_root(slot) return Root(Hash32(self.db[key]))