async def get_block_by_hash(self, block_hash: bytes) -> BaseBlock: peer = await self.get_best_peer() try: header = self.chaindb.get_block_header_by_hash(block_hash) except BlockNotFound: self.logger.debug("Fetching header %s from %s", encode_hex(block_hash), peer) request_id = gen_request_id() max_headers = 1 peer.les_proto.send_get_block_headers(block_hash, max_headers, request_id) reply = await peer.wait_for_reply(request_id) if len(reply['headers']) == 0: raise BlockNotFound("Peer {} has no block with hash {}".format(peer, block_hash)) header = reply['headers'][0] self.logger.debug("Fetching block %s from %s", encode_hex(block_hash), peer) request_id = gen_request_id() peer.les_proto.send_get_block_bodies([block_hash], request_id) reply = await peer.wait_for_reply(request_id) if len(reply['bodies']) == 0: raise BlockNotFound("Peer {} has no block with hash {}".format(peer, block_hash)) body = reply['bodies'][0] block_class = self.get_vm_class_for_block_number(header.block_number).get_block_class() transactions = [ block_class.transaction_class.from_base_transaction(tx) for tx in body.transactions ] return block_class( header=header, transactions=transactions, uncles=body.uncles, )
async def get_receipts(self, block_hash: bytes, cancel_token: CancelToken) -> List[Receipt]: request_id = gen_request_id() self.sub_proto.send_get_receipts(block_hash, request_id) reply = await self._wait_for_reply(request_id, cancel_token) if not reply['receipts']: raise BlockNotFound("No block with hash {} found".format(block_hash)) return reply['receipts'][0]
def get_logs(self, address: Address=None, topics: List[Union[str, None]]=None, from_block: Union[int, str]=None, to_block: Union[int, str]=None) -> List[Dict[str, Any]]: filter_params = { 'address': address, 'topics': topics, } # type: Dict[str, Any] current_block_number = self.w3.eth.blockNumber if from_block is None: # Search from the start of current period if from_block is not given filter_params['fromBlock'] = current_block_number - \ current_block_number % self.period_length else: if from_block > current_block_number: raise BlockNotFound( "Try to search from block number {} while current block number is {}".format( from_block, current_block_number ) ) filter_params['fromBlock'] = from_block if to_block is None: filter_params['toBlock'] = 'latest' else: filter_params['toBlock'] = min(current_block_number, to_block) return self.w3.eth.getLogs(filter_params)
async def get_block_by_hash(self, block_hash: bytes) -> les.LESBlockBody: request_id = gen_request_id() self.sub_proto.send_get_block_bodies([block_hash], request_id) reply = await self._wait_for_reply(request_id) if not reply['bodies']: raise BlockNotFound("Peer {} has no block with hash {}".format(self, block_hash)) return reply['bodies'][0]
async def get_canonical_block_by_number(self, block_number): try: block_hash = self.chaindb.lookup_block_hash(block_number) except KeyError: raise BlockNotFound( "No block with number {} found on local chain".format( block_number)) return await self.get_block_by_hash(block_hash)
async def get_block_header_by_hash(self, block_hash: bytes) -> BlockHeader: request_id = gen_request_id() max_headers = 1 self.sub_proto.send_get_block_headers(block_hash, max_headers, request_id) reply = await self._wait_for_reply(request_id) if not reply['headers']: raise BlockNotFound("Peer {} has no block with hash {}".format(self, block_hash)) return reply['headers'][0]
async def 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 = gen_request_id() peer.sub_proto.send_get_receipts(block_hash, request_id) reply = await self._wait_for_reply(request_id) if not reply['receipts']: raise BlockNotFound("No block with hash {} found".format(block_hash)) return reply['receipts'][0]
async def 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 = gen_request_id() peer.sub_proto.send_get_block_bodies([block_hash], request_id) reply = await self._wait_for_reply(request_id) if not reply['bodies']: raise BlockNotFound("Peer {} has no block with hash {}".format(peer, block_hash)) return reply['bodies'][0]
async def get_receipts(self, block_hash: bytes) -> List[Receipt]: peer = await self.get_best_peer() self.logger.debug("Fetching %s receipts from %s", encode_hex(block_hash), peer) request_id = gen_request_id() peer.les_proto.send_get_receipts(block_hash, request_id) reply = await peer.wait_for_reply(request_id) if len(reply['receipts']) == 0: raise BlockNotFound("No block with hash {} found".format(block_hash)) return reply['receipts'][0]
def get_block_header_by_hash(self, block_hash): """ Returns the block header by hash. """ for value in self.prev_headers: if value.hash == block_hash: return value raise BlockNotFound( "No block header with hash {0} found in self.perv_headers".format( encode_hex(block_hash), ))
async def get_canonical_block_by_number(self, block_number: int) -> BaseBlock: """Return the block with the given number from the canonical chain. Raises BlockNotFound if it is not found. """ try: block_hash = self.chaindb.lookup_block_hash(block_number) except KeyError: raise BlockNotFound( "No block with number {} found on local chain".format(block_number)) return await self.get_block_by_hash(block_hash)
def get_block_header_by_hash(self, block_hash): """ 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: block = self.db.get(block_hash) except KeyError: raise BlockNotFound("No block with hash {0} found".format( encode_hex(block_hash))) return rlp.decode(block, sedes=BlockHeader)
async def get_block_by_hash(self, block_hash: bytes) -> BaseBlock: peer = await self.get_best_peer() self.logger.debug("Fetching block %s from %s", encode_hex(block_hash), peer) request_id = gen_request_id() peer.les_proto.send_get_block_bodies([block_hash], request_id) reply = await peer.wait_for_reply(request_id) if len(reply['bodies']) == 0: raise BlockNotFound("No block with hash {} found".format(block_hash)) body = reply['bodies'][0] # This will raise a BlockNotFound if we don't have the header in our DB, which is correct # because it means our peer doesn't know about it. header = self.chaindb.get_block_header_by_hash(block_hash) block_class = self.get_vm_class_for_block_number(header.block_number).get_block_class() return block_class( header=header, transactions=body.transactions, uncles=body.uncles, chaindb=self.chaindb, )