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 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)