ethereum / web3.py

A python interface for interacting with the Ethereum blockchain and ecosystem.
http://web3py.readthedocs.io
MIT License
4.89k stars 1.67k forks source link

Still waiting for signed transaction to clear #593

Closed dangell7 closed 6 years ago

dangell7 commented 6 years ago

What was wrong?

I'm working on a test chain created by populus. I'm using the following to create and send a raw transaction.

    `gas_price = w3.eth.gasPrice

    gas_price_hex = Web3.toHex(gas_price)
    gas_limit_hex = Web3.toHex(7000000)

    nonce = w3.eth.getTransactionCount(w3.eth.coinbase)
    nonce_hex = Web3.toHex(nonce)

    transaction = {
        'to': address,
        'from': w3.eth.coinbase,
        'data': '',
        'value': 1000,
        'gas': gas_limit_hex,
        'gasPrice': gas_price_hex,
        'nonce': nonce_hex,
        'chainId': 1
    }

    key = decrypt_account(w3.eth.coinbase, 'Coinbase-Master')

    signed = w3.eth.account.signTransaction(transaction, key)

    print(signed)

    tx_hash = w3.eth.sendRawTransaction(signed.rawTransaction)

    print(tx_hash)

    while w3.eth.getTransactionReceipt(tx_hash) == None:
        time.sleep(5)
        print('Waiting on Tranasaction')

    tx_receipt = w3.eth.getTransactionReceipt(tx_hash)

    print(tx_receipt)`

The transaction never gets mined or "confirmed" However, if I use

tx_hash = w3.eth.sendTransaction({'to': address, 'from': w3.eth.coinbase, 'value': 1000})

After unlocking the account, the transaction in mined within seconds. Is there something I'm doing wrong?

Is anybody else having trouble or prob just me? lol I love this product and would really like to know what I'm doing wrong. I can post my genesis.json if thats a problem area?

dangell7 commented 6 years ago

Heres the AttributeDict. In the example it shows r and s as hexbytes. Is this the problem?

AttributeDict({'rawTransaction': HexBytes('0xf86780850430e23400836acfc094729ec65621d4ef975a48244c1373c4356e9119898203e88026a01bca82a7e6243a810a8adcff3cee3604aa6d5eb70b9e441e2a569678c6061851a02f08d9713df59ceb217ad0bbc78e3fd189c3cc2a4a883109a9b58930c9e951c8'), 's': 21274339391888063372739717545903057402080716410845875744289537671013908173256, 'hash': HexBytes('0x66741cb84b881ebc9f2246a83ebf781c34114d9c2a455348897f74ade791fc72'), 'v': 38, 'r': 12570251772406332945300025074538573159520329277164531372497217672440701130833})

dangell7 commented 6 years ago

I solved my problem by setting the chainId to None and removing the chainId setting in the genesis.json.

nueverest commented 6 years ago

Where can I find genesis.json?

I am testing on Kovan. Chain Id: 42

I never get a Transaction Receipt.

with open(private_key_file_path) as key_file:
    encrypted_key = key_file.read()
    private_key = Account.decrypt(encrypted_key, password)

    print('Balance of token testing:', contract_concise.balanceOf(token_testing))

    # Test Minting 200 Tokens with 18 decimals.
    nonce = w3.eth.getTransactionCount(contract_address) + w3.eth.blockNumber

    print('token_testing:', token_testing)

    gas_price = w3.eth.gasPrice
    gas_limit = 6900000

    # Build a Transaction to be signed locally.
    mintTransaction = contract.functions.mintTokens(
        target=token_testing,
        mintedAmount=200000000000000000000,
    ).buildTransaction({
        'chainId': kovan,       # kovan == 42    # None - also tried but no success.
        'gas': gas_limit,
        'gasPrice': gas_price,
        'nonce': nonce,
    })

    for k, v in mintTransaction.items():
        print(k, v)

    signedTransaction = Account.signTransaction(transaction_dict=mintTransaction, private_key=private_key)
    print('signedTransaction:', signedTransaction)

    expectedHash = w3.toHex(w3.sha3(signedTransaction.rawTransaction))
    print('expectedHash:', expectedHash)

    rawHash = w3.eth.sendRawTransaction(signedTransaction.rawTransaction)
    print('rawHash', rawHash)

    hexHash = Web3.toHex(rawHash)
    print('hexHash', hexHash)

    i = 1
    while w3.eth.getTransactionReceipt(hexHash) is None:
        time.sleep(5)
        print('Waiting on Transaction', 5*i, 'seconds')
        i += 1

    # These lines do not execute.
    print('Transaction Receipt', w3.eth.getTransactionReceipt(hexHash))
    print('expect == actual:', expectedHash == hexHash)