def get_chain_nonce(self, addr): signer_nonce = to_decimal( self.signing_manager.request_blocking('eth_getTransactionCount', [addr, 'pending'])) wrapped_nonce = to_decimal( self.wrapped_manager.request_blocking('eth_getTransactionCount', [addr, 'pending'])) return max(signer_nonce, wrapped_nonce)
def get_chain_nonce(self, addr): signer_nonce = to_decimal(self.signing_manager.request_blocking( 'eth_getTransactionCount', [addr, 'pending'] )) wrapped_nonce = to_decimal(self.wrapped_manager.request_blocking( 'eth_getTransactionCount', [addr, 'pending'] )) return max(signer_nonce, wrapped_nonce)
def outputLogFormatter(log): """ Formats the output of a log """ if log.get("blockNumber"): log["blockNumber"] = to_decimal(log["blockNumber"]) if log.get("transactionIndex"): log["transactionIndex"] = to_decimal(log["transactionIndex"]) if log.get("logIndex"): log["logIndex"] = to_decimal(log["logIndex"]) return log
def sign_and_serialize_transaction(self, transaction): txn_from = to_address(transaction['from']) if txn_from not in self.keys: raise KeyError("No signing key registered for from address: {0}".format(txn_from)) transaction = Transaction( nonce=to_decimal(transaction['nonce']), gasprice=to_decimal(transaction['gasPrice']), startgas=to_decimal(transaction['gas']), to=transaction['to'], value=to_decimal(transaction['value']), data=decode_hex(transaction['data']), ) transaction.sign(self.keys[txn_from]) assert to_address(transaction.sender) == txn_from return rlp.encode(transaction, Transaction)
def transaction_pool_formatter(value, txn_formatter): return { 'pending': { sender: { to_decimal(nonce): [txn_formatter(txn) for txn in txns] for nonce, txns in txns_by_sender.items() } for sender, txns_by_sender in value.get('pending', {}).items() }, 'queued': { sender: { to_decimal(nonce): [txn_formatter(txn) for txn in txns] for nonce, txns in txns_by_sender.items() } for sender, txns_by_sender in value.get('queued', {}).items() }, }
def sign_and_serialize_transaction(self, transaction): txn_from = to_normalized_address(transaction['from']) if txn_from not in self.keys: raise KeyError("No signing key registered for from address: {0}".format(txn_from)) transaction = Transaction( nonce=to_decimal(transaction['nonce']), gasprice=to_decimal(transaction['gasPrice']), startgas=to_decimal(transaction['gas']), to=transaction['to'], value=to_decimal(transaction['value']), data=decode_hex(transaction['data']), ) transaction.sign(self.keys[txn_from]) assert to_normalized_address(transaction.sender) == txn_from return rlp.encode(transaction, Transaction)
def request_blocking(self, method, params): if method == 'eth_sendTransaction': base_transaction = params[0] # create a fully signed transaction and send through the # `eth_sendRawTransaction` endpoint instead. full_transaction = self.construct_full_transaction(base_transaction) raw_transaction_bytes = self.sign_and_serialize_transaction( full_transaction, ) raw_transaction_bytes_as_hex = encode_hex(raw_transaction_bytes) return self.request_blocking( 'eth_sendRawTransaction', [raw_transaction_bytes_as_hex], ) result = super(BaseSendRawTransactionMixin, self).request_blocking( method, params, ) if method in self.TXN_SENDING_METHODS: if method == 'eth_sendRawTransaction': txn = rlp.decode(decode_hex(params[0]), Transaction) self._known_transactions[to_address(txn.sender)].add(result) self._known_nonces[to_address(txn.sender)].add(txn.nonce) else: txn = params[0] self._known_transactions[to_address(txn['from'])].add(result) if 'nonce' in txn: self._known_nonces[to_address(txn['from'])].add( to_decimal(txn['nonce']) ) return result
def request_blocking(self, method, params): if method == 'eth_sendTransaction': base_transaction = params[0] # create a fully signed transaction and send through the # `eth_sendRawTransaction` endpoint instead. full_transaction = self.construct_full_transaction(base_transaction) raw_transaction_bytes = self.sign_and_serialize_transaction( full_transaction, ) raw_transaction_bytes_as_hex = encode_hex(raw_transaction_bytes) return self.request_blocking( 'eth_sendRawTransaction', [raw_transaction_bytes_as_hex], ) result = super(BaseSendRawTransactionMixin, self).request_blocking( method, params, ) if method in self.TXN_SENDING_METHODS: if method == 'eth_sendRawTransaction': txn = rlp.decode(decode_hex(params[0]), Transaction) self._known_transactions[to_normalized_address(txn.sender)].add(result) self._known_nonces[to_normalized_address(txn.sender)].add(txn.nonce) else: txn = params[0] self._known_transactions[to_normalized_address(txn['from'])].add(result) if 'nonce' in txn: self._known_nonces[to_normalized_address(txn['from'])].add( to_decimal(txn['nonce']) ) return result
def outputPostFormatter(post): """ Formats the output of a received post message """ post["expiry"] = to_decimal(post["expiry"]) post["sent"] = to_decimal(post["sent"]) post["ttl"] = to_decimal(post["ttl"]) post["workProved"] = to_decimal(post["workProved"]) if not post.get("topics"): post["topics"] = [] post["topics"] = [decode_hex(topic) for topic in post["topics"]] return post
def test_conversion_rount_trip(value): intermediate_value = from_decimal(value) result_value = to_decimal(intermediate_value) error_msg = "Expected: {0!r}, Result: {1!r}, Intermediate: {2!r}".format( value, result_value, intermediate_value, ) assert result_value == value, error_msg
def outputTransactionReceiptFormatter(receipt): """ Formats the output of a transaction receipt to its proper values """ if receipt is None: return None if receipt.get("blockNumber"): receipt["blockNumber"] = to_decimal(receipt["blockNumber"]) if receipt.get("transactionIndex"): receipt["transactionIndex"] = to_decimal(receipt["transactionIndex"]) receipt["cumulativeGasUsed"] = to_decimal(receipt["cumulativeGasUsed"]) receipt["gasUsed"] = to_decimal(receipt["gasUsed"]) if is_array(receipt.get("logs")): receipt["logs"] = [outputLogFormatter(log) for log in receipt["logs"]] return receipt
def outputTransactionFormatter(tx): """ Formats the output of a transaction to its proper values """ if tx.get("blockNumber"): tx["blockNumber"] = to_decimal(tx["blockNumber"]) if tx.get("transactionIndex"): tx["transactionIndex"] = to_decimal(tx["transactionIndex"]) tx["nonce"] = to_decimal(tx["nonce"]) tx["gas"] = to_decimal(tx["gas"]) tx["gasPrice"] = to_decimal(tx["gasPrice"]) tx["value"] = to_decimal(tx["value"]) return tx
def _get_nonces_and_cleanup(self, addr, chain_nonce): all_txns = { txn_hash: self.request_blocking( 'eth_getTransactionByHash', [txn_hash], ) for txn_hash in self._known_transactions[addr] } for txn_hash, txn in all_txns.items(): if txn is None: continue txn_nonce = to_decimal(txn['nonce']) if txn_nonce < chain_nonce: self._known_transactions[addr].discard(txn_hash) else: yield txn_nonce all_known_nonces = tuple(self._known_nonces[addr]) for nonce in all_known_nonces: if nonce < chain_nonce: self._known_nonces[addr].discard(nonce) else: yield nonce
def outputBlockFormatter(block): """ Formats the output of a block to its proper values """ # Transform to number block["gasLimit"] = to_decimal(block["gasLimit"]) block["gasUsed"] = to_decimal(block["gasUsed"]) block["size"] = to_decimal(block["size"]) block["timestamp"] = to_decimal(block["timestamp"]) if block.get("number"): block["number"] = to_decimal(block["number"]) block["difficulty"] = to_decimal(block["difficulty"]) block["totalDifficulty"] = to_decimal(block["totalDifficulty"]) if is_array(block.get("transactions")): for item in block["transactions"]: if not is_string(item): item = outputTransactionFormatter(item) return block
def outputSyncingFormatter(result): result["startingBlock"] = to_decimal(result["startingBlock"]) result["currentBlock"] = to_decimal(result["currentBlock"]) result["highestBlock"] = to_decimal(result["highestBlock"]) return result
def get_chain_nonce(self, addr): chain_nonce = to_decimal(self.request_blocking( 'eth_getTransactionCount', [addr, 'pending'] )) return chain_nonce
def test_text_if_str_on_text_to_decimal_py2(val): assert text_if_str(to_decimal, val) == to_decimal(text=val)
def to_standard_signature_bytes(ethereum_signature_bytes): rs = ethereum_signature_bytes[:-1] v = to_decimal(ethereum_signature_bytes[-1]) standard_v = to_standard_v(v) return rs + to_bytes(standard_v)
def test_conversion_rount_trip(value): intermediate_value = from_decimal(value) result_value = to_decimal(intermediate_value) assert result_value == value, "Expected: {0!r}, Result: {1!r}, Intermediate: {2!r}".format(value, result_value, intermediate_value)
def test_conversion_rount_trip(value): intermediate_value = from_decimal(value) result_value = to_decimal(intermediate_value) assert result_value == value