def _get_score(db: BaseDB, block_root: Hash32) -> int: try: encoded_score = db[SchemaV1.make_block_root_to_score_lookup_key(block_root)] except KeyError: raise BlockNotFound("No block with hash {0} found".format( encode_hex(block_root))) return rlp.decode(encoded_score, sedes=rlp.sedes.big_endian_int)
def get(self, block_root: Root) -> BaseSignedBeaconBlock: for block in self._pool: if block.message.hash_tree_root == block_root: return block raise BlockNotFound( f"No block with message.hash_tree_root {block_root.hex()} is found" )
def _get_canonical_block_root(db: DatabaseAPI, slot: Slot) -> Root: slot_to_root_key = SchemaV1.make_block_slot_to_root_lookup_key(slot) try: root = db[slot_to_root_key] except KeyError: raise BlockNotFound("No canonical block for block slot #{0}".format(slot)) else: return cast(Root, root)
async def coro_get_receipts(self, block_hash: Hash32) -> List[Receipt]: peer = cast(LESPeer, self.peer_pool.highest_td_peer) self.logger.debug("Fetching %s receipts from %s", encode_hex(block_hash), peer) request_id = peer.sub_proto.send_get_receipts(block_hash) reply = await self._wait_for_reply(request_id) if not reply['receipts']: raise BlockNotFound(f"No block with hash {block_hash} found") return reply['receipts'][0]
async def coro_get_block_body_by_hash(self, block_hash: Hash32) -> BlockBody: peer = cast(LESPeer, self.peer_pool.highest_td_peer) self.logger.debug("Fetching block %s from %s", encode_hex(block_hash), peer) request_id = peer.sub_proto.send_get_block_bodies([block_hash]) reply = await self._wait_for_reply(request_id) if not reply['bodies']: raise BlockNotFound(f"Peer {peer} has no block with hash {block_hash}") return reply['bodies'][0]
def _get_score(db: DatabaseAPI, block_root: Hash32) -> int: try: encoded_score = db[SchemaV1.make_block_root_to_score_lookup_key( block_root)] except KeyError: raise BlockNotFound("No block with hash {0} found".format( encode_hex(block_root))) return ssz.decode(encoded_score, sedes=ssz.sedes.uint64)
async def coro_get_receipts(self, block_hash: Hash32) -> List[Receipt]: peer = cast(LESPeer, self.peer_pool.highest_td_peer) self.logger.debug("Fetching %s receipts from %s", encode_hex(block_hash), peer) request_id = peer.les_api.send_get_receipts((block_hash,)) receipts = await self._wait_for_reply(request_id) if not receipts.payload.receipts: raise BlockNotFound(f"No block with hash {block_hash.hex()} found") return receipts.payload.receipts[0]
async def coro_get_block_body_by_hash(self, block_hash: Hash32) -> BlockBody: peer = cast(LESPeer, self.peer_pool.highest_td_peer) self.logger.debug("Fetching block %s from %s", encode_hex(block_hash), peer) request_id = peer.les_api.send_get_block_bodies([block_hash]) block_bodies = await self._wait_for_reply(request_id) if not block_bodies.payload.bodies: raise BlockNotFound(f"Peer {peer} has no block with hash {block_hash.hex()}") return block_bodies.payload.bodies[0]
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: Root, block_class: Type[BaseBeaconBlock]) -> BaseBeaconBlock: key = SchemaV1.block_root_to_block(block_root) try: block_data = self.db[key] except KeyError: raise BlockNotFound() return ssz.decode(block_data, block_class)
def _get_score(db: DatabaseAPI, block_root: Root, score_class: Type[BaseScore]) -> BaseScore: try: encoded_score = db[SchemaV1.make_block_root_to_score_lookup_key( block_root)] except KeyError: raise BlockNotFound("No block with root {0} found".format( encode_hex(block_root))) return cast(BaseScore, score_class.deserialize(encoded_score))
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_canonical_block_root(db: DatabaseAPI, slot: Slot) -> Hash32: slot_to_root_key = SchemaV1.make_block_slot_to_root_lookup_key(slot) try: encoded_key = db[slot_to_root_key] except KeyError: raise BlockNotFound( "No canonical block for block slot #{0}".format(slot)) else: return ssz.decode(encoded_key, sedes=ssz.sedes.bytes32)
def get_block_signature_by_root(self, block_root: Root) -> BLSSignature: """ ``block_root`` is the hash tree root of a beacon block. This method provides a way to reconstruct the ``SignedBeaconBlock`` if required. """ key = SchemaV1.block_root_to_signature(block_root) try: return BLSSignature(self.db[key]) except KeyError: raise BlockNotFound()
def _get_canonical_block_hash(db: BaseDB, slot: int) -> Hash32: validate_slot(slot) slot_to_hash_key = SchemaV1.make_block_slot_to_hash_lookup_key(slot) try: encoded_key = db[slot_to_hash_key] except KeyError: raise BlockNotFound( "No canonical block for block slot #{0}".format(slot)) else: return rlp.decode(encoded_key, sedes=rlp.sedes.binary)
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_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 from_header(cls, header: BlockHeaderAPI, chaindb: ChainDatabaseAPI) -> "FrontierBlock": """ Returns the block denoted by the given block header. :raise eth.exceptions.BlockNotFound: if transactions or uncle headers are missing """ if header.uncles_hash == EMPTY_UNCLE_HASH: uncles: Tuple[BlockHeaderAPI, ...] = () else: try: uncles = chaindb.get_block_uncles(header.uncles_hash) except HeaderNotFound as exc: raise BlockNotFound(f"Uncles not found in database for {header}: {exc}") from exc try: transactions = chaindb.get_block_transactions(header, cls.get_transaction_builder()) except MissingTrieNode as exc: raise BlockNotFound(f"Transactions not found in database for {header}: {exc}") from exc return cls( header=header, transactions=transactions, uncles=uncles, )
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_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 get(self, block_root: SigningRoot) -> BaseBeaconBlock: for block in self._pool: if block.signing_root == block_root: return block raise BlockNotFound(f"No block with signing_root {block_root.hex()} is found")