def get_block_by_hash(self, block_hash: Hash32) -> BaseBlock: """ Returns the requested block as specified by block hash. """ validate_word(block_hash, title="Block Hash") block_header = self.get_block_header_by_hash(block_hash) return self.get_block_by_header(block_header)
def get_block_header_by_hash(self, block_hash: Hash32) -> BlockHeader: """ Returns the requested block header as specified by block hash. Raises BlockNotFound if there's no block header with the given hash in the db. """ validate_word(block_hash, title="Block Hash") return self.chaindb.get_block_header_by_hash(block_hash)
def _get_slot_by_root(db: DatabaseAPI, block_root: Hash32) -> Slot: validate_word(block_root, title="block root") try: encoded_slot = db[SchemaV1.make_block_root_to_slot_lookup_key( block_root)] except KeyError: raise BlockNotFound("No block with root {0} found".format( encode_hex(block_root))) return Slot(ssz.decode(encoded_slot, sedes=ssz.sedes.uint64))
def _get_slot_by_root(db: BaseDB, block_root: Hash32) -> SlotNumber: validate_word(block_root, title="block root") try: encoded_slot = db[SchemaV1.make_block_root_to_slot_lookup_key(block_root)] except KeyError: raise BlockNotFound("No block with root {0} found".format( encode_hex(block_root))) return SlotNumber(rlp.decode(encoded_slot, sedes=rlp.sedes.big_endian_int))
def get_block_by_root(self, block_root: SigningRoot) -> BaseBeaconBlock: """ Return the requested block as specified by block hash. Raise ``BlockNotFound`` if there's no block with the given hash in the db. """ validate_word(block_root, title="Block Signing Root") block_class = self.get_block_class(block_root) return self.chaindb.get_block_by_root(block_root, block_class)
def get_block_uncles(self, uncles_hash: Hash32) -> Tuple[BlockHeaderAPI, ...]: validate_word(uncles_hash, title="Uncles Hash") if uncles_hash == EMPTY_UNCLE_HASH: return () try: encoded_uncles = self.db[uncles_hash] except KeyError as exc: raise HeaderNotFound( f"No uncles found for hash {uncles_hash!r}" ) from exc else: return tuple(rlp.decode(encoded_uncles, sedes=rlp.sedes.CountableList(BlockHeader)))
def _get_block_signing_root_by_hash_tree_root( db: DatabaseAPI, block_root: HashTreeRoot) -> SigningRoot: validate_word(block_root, title="block hash tree root") key = SchemaV1.make_block_hash_tree_root_to_signing_root_lookup_key( block_root) try: signing_root = db[key] except KeyError: raise BlockNotFound( "No block with hash tree root {0} found".format( encode_hex(block_root))) return cast(SigningRoot, signing_root)
def _get_block_header_by_hash(db: DatabaseAPI, block_hash: Hash32) -> BlockHeaderAPI: """ Returns the requested block header as specified by block hash. Raises BlockNotFound if it is not present in the db. """ validate_word(block_hash, title="Block Hash") try: header_rlp = db[block_hash] except KeyError: raise HeaderNotFound(f"No header with hash {encode_hex(block_hash)} found") return _decode_block_header(header_rlp)
def get_block_header_by_hash(self, block_hash: Hash32) -> BlockHeader: """ Returns the requested block header as specified by block hash. Raises BlockNotFound if it is not present in the db. """ validate_word(block_hash, title="Block Hash") try: header_rlp = self.db[block_hash] except KeyError: raise HeaderNotFound("No header with hash {0} found".format( encode_hex(block_hash))) return _decode_block_header(header_rlp)
def _get_block_by_hash(db: BaseDB, block_hash: Hash32) -> BaseBeaconBlock: """ Return the requested block header as specified by block hash. Raise BlockNotFound if it is not present in the db. """ validate_word(block_hash, title="Block Hash") try: block_rlp = db[block_hash] except KeyError: raise BlockNotFound("No block with hash {0} found".format( encode_hex(block_hash))) return _decode_block(block_rlp)
def _get_block_by_root( db: DatabaseAPI, block_root: Hash32, block_class: Type[BaseBeaconBlock]) -> BaseBeaconBlock: """ Return the requested block header as specified by block root. Raise BlockNotFound if it is not present in the db. """ validate_word(block_root, title="block root") try: block_ssz = db[block_root] except KeyError: raise BlockNotFound("No block with root {0} found".format( encode_hex(block_root))) return _decode_block(block_ssz, block_class)
def get_block_uncles(self, uncles_hash: Hash32) -> List[BlockHeader]: """ Returns an iterable of uncle headers specified by the given uncles_hash """ validate_word(uncles_hash, title="Uncles Hash") if uncles_hash == EMPTY_UNCLE_HASH: return [] try: encoded_uncles = self.db[uncles_hash] except KeyError: raise HeaderNotFound( "No uncles found for hash {0}".format(uncles_hash)) else: return rlp.decode(encoded_uncles, sedes=rlp.sedes.CountableList(BlockHeader))
def _get_block_by_root( db: DatabaseAPI, block_root: Root, block_class: Type[BaseSignedBeaconBlock]) -> BaseBeaconBlock: """ Return the requested block header as specified by block root. Raise BlockNotFound if it is not present in the db. """ validate_word(block_root, title="block root") if block_root in block_cache and block_root in db: return block_cache[block_root] try: block_ssz = db[block_root] except KeyError: raise BlockNotFound("No block with root {0} found".format( encode_hex(block_root))) block = ssz.decode(block_ssz, block_class) block_cache[block_root] = block return block
def _header_exists(db: BaseDB, block_hash: Hash32) -> bool: validate_word(block_hash, title="Block Hash") return block_hash in db
def test_validate_word(value, is_valid): if is_valid: validate_word(value) else: with pytest.raises(ValidationError): validate_word(value)
def _block_exists(db: DatabaseAPI, block_root: Hash32) -> bool: validate_word(block_root, title="block root") return block_root in db
def header_exists(self, block_hash: Hash32) -> bool: validate_word(block_hash, title="Block Hash") return block_hash in self.db
def get_block_by_root(root): validate_word(root) if root in mock_root_to_block_db: return mock_root_to_block_db[root] else: raise BlockNotFound
def get_block_header_by_hash(self, block_hash: Hash32) -> BlockHeaderAPI: validate_word(block_hash, title="Block Hash") return self.chaindb.get_block_header_by_hash(block_hash)
def get_block_by_hash_tree_root(self, block_root: HashTreeRoot) -> BaseBeaconBlock: validate_word(block_root, title="Block Hash Tree Root") signing_root = self.chaindb.get_block_signing_root_by_hash_tree_root(block_root) return self.get_block_by_root(signing_root)
def get_block_by_hash(self, block_hash: Hash32) -> BlockAPI: validate_word(block_hash, title="Block Hash") block_header = self.get_block_header_by_hash(block_hash) return self.get_block_by_header(block_header)