def _add_slot_to_state_root_lookup(self, slot: Slot, state_root: Hash32) -> None: """ Set a record in the database to allow looking up the state root by slot number. """ slot_to_state_root_key = SchemaV1.make_slot_to_state_root_lookup_key(slot) self.db.set(slot_to_state_root_key, state_root)
def _get_state_root_by_slot(db: DatabaseAPI, slot: Slot) -> Hash32: """ Return the requested beacon state as specified by slot. Raises StateNotFound if it is not present in the db. """ slot_to_state_root_key = SchemaV1.make_slot_to_state_root_lookup_key(slot) try: state_root = db[slot_to_state_root_key] except KeyError: raise StateNotFound("No state root for slot #{0}".format(slot)) else: return Hash32(state_root)
def _get_state_by_slot(db: BaseDB, slot: Slot, state_class: Type[BeaconState]) -> BeaconState: """ Return the requested beacon state as specified by slot. Raises StateSlotNotFound if it is not present in the db. """ slot_to_state_root_key = SchemaV1.make_slot_to_state_root_lookup_key( slot) try: state_root_ssz = db[slot_to_state_root_key] except KeyError: raise StateSlotNotFound("No state root for slot #{0}".format(slot)) state_root = ssz.decode(state_root_ssz, sedes=ssz.sedes.bytes32) try: state_ssz = db[state_root] except KeyError: raise StateRootNotFound( f"No state with root {encode_hex(state_root)} found") return _decode_state(state_ssz, state_class)