Example #1
0
 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)
Example #2
0
 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)
Example #3
0
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
Example #4
0
 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)
Example #5
0
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()
        },
    }
Example #6
0
 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)
Example #7
0
    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
Example #8
0
    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
Example #9
0
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
Example #10
0
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
Example #11
0
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()
        },
    }
Example #12
0
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
Example #13
0
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
Example #14
0
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
Example #15
0
    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
Example #16
0
    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
Example #17
0
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
Example #18
0
def outputSyncingFormatter(result):
    result["startingBlock"] = to_decimal(result["startingBlock"])
    result["currentBlock"] = to_decimal(result["currentBlock"])
    result["highestBlock"] = to_decimal(result["highestBlock"])

    return result
Example #19
0
 def get_chain_nonce(self, addr):
     chain_nonce = to_decimal(self.request_blocking(
         'eth_getTransactionCount',
         [addr, 'pending']
     ))
     return chain_nonce
Example #20
0
def test_text_if_str_on_text_to_decimal_py2(val):
    assert text_if_str(to_decimal, val) == to_decimal(text=val)
Example #21
0
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)
Example #22
0
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)
Example #23
0
 def get_chain_nonce(self, addr):
     chain_nonce = to_decimal(self.request_blocking(
         'eth_getTransactionCount',
         [addr, 'pending']
     ))
     return chain_nonce
Example #24
0
def test_conversion_rount_trip(value):
    intermediate_value = from_decimal(value)
    result_value = to_decimal(intermediate_value)
    assert result_value == value