def get_block_at_number(chain: BaseChain, at_block: Union[str, int]) -> BaseBlock: # mypy doesn't have user defined type guards yet # https://github.com/python/mypy/issues/5206 if is_integer(at_block) and at_block >= 0: # type: ignore # optimization to avoid requesting block, then header, then block again return chain.get_canonical_block_by_number(at_block) else: at_header = get_header(chain, at_block) return chain.get_block_by_header(at_header)
def get_header(chain: BaseChain, at_block: Union[str, int]) -> BlockHeader: if at_block == 'pending': at_header = chain.header elif at_block == 'latest': at_header = chain.get_canonical_head() elif at_block == 'earliest': # TODO find if genesis block can be non-zero. Why does 'earliest' option even exist? at_header = chain.get_canonical_block_by_number(0).header # mypy doesn't have user defined type guards yet # https://github.com/python/mypy/issues/5206 elif is_integer(at_block) and at_block >= 0: # type: ignore at_header = chain.get_canonical_block_by_number(at_block).header else: raise TypeError("Unrecognized block reference: %r" % at_block) return at_header
def __init__(self, chain: BaseChain, initial_tx_validation_block_number: int) -> None: self.chain = chain self._initial_tx_class = self._get_tx_class_for_block_number( initial_tx_validation_block_number ) self._ordered_tx_classes = [ vm_class.get_transaction_class() for _, vm_class in chain.get_vm_configuration() ] self._initial_tx_class_index = self._ordered_tx_classes.index(self._initial_tx_class)
def block_to_dict( block: BaseBlock, chain: BaseChain, include_transactions: bool) -> Dict[str, Union[str, List[str]]]: header_dict = header_to_dict(block.header) block_dict: Dict[str, Union[str, List[str]]] = dict( header_dict, totalDifficulty=hex(chain.get_score(block.hash)), uncles=[encode_hex(uncle.hash) for uncle in block.uncles], size=hex(len(rlp.encode(block))), ) if include_transactions: # block_dict['transactions'] = map(transaction_to_dict, block.transactions) raise NotImplementedError( "Cannot return transaction object with block, yet") else: block_dict['transactions'] = [ encode_hex(tx.hash) for tx in block.transactions ] return block_dict
def account_db_at_block(chain: BaseChain, at_block: Union[str, int], read_only: bool = True) -> BaseAccountDB: at_header = get_header(chain, at_block) vm = chain.get_vm(at_header) return vm.state.account_db