def dict_to_spoof_transaction( chain: BaseAsyncChain, header: BlockHeader, transaction_dict: Dict[str, Any]) -> SpoofTransaction: """ Convert dicts used in calls & gas estimates into a spoof transaction """ txn_dict = normalize_transaction_dict(transaction_dict) sender = txn_dict.get('from', ZERO_ADDRESS) if 'nonce' in txn_dict: nonce = txn_dict['nonce'] else: vm = chain.get_vm(header) nonce = vm.state.account_db.get_nonce(sender) gas_price = txn_dict.get('gasPrice', 0) gas = txn_dict.get('gas', header.gas_limit) unsigned = chain.get_vm_class(header).create_unsigned_transaction( nonce=nonce, gas_price=gas_price, gas=gas, to=txn_dict['to'], value=txn_dict['value'], data=txn_dict['data'], ) return SpoofTransaction(unsigned, from_=sender)
async def get_header(chain: BaseAsyncChain, at_block: Union[str, int]) -> BlockHeader: if at_block == 'pending': raise NotImplementedError("RPC interface does not support the 'pending' block at this time") 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? block = await chain.coro_get_canonical_block_by_number(BlockNumber(0)) at_header = block.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 block = await chain.coro_get_canonical_block_by_number(BlockNumber(0)) at_header = block.header else: raise TypeError("Unrecognized block reference: %r" % at_block) return at_header
def block_to_dict(block: BaseBlock, chain: BaseAsyncChain, 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
async def account_db_at_block(chain: BaseAsyncChain, at_block: Union[str, int], read_only: bool = True) -> BaseAccountDB: at_header = await get_header(chain, at_block) vm = chain.get_vm(at_header) return vm.state.account_db