2

I am using the web.py code below to try and send a transaction with 1 ETH on the Rinkeby testnet via a local geth node. I can see the transactions as submitted in live local ethereum node log stream, but they don't ever seem to be broadcast to the network (I can never see them on rinkeby.io block explorer). I am manually setting the nonce each time, but I read that if a previous nonce was used and it didn't broadcast it may be stuck? As a part of the answer it would be great if the nonce purpose/usage can be explained.

import web3, json, requests
from web3 import Web3, HTTPProvider
provider = HTTPProvider( 'http://localhost:8545' )
web3 = Web3(provider)

web3.eth.enable_unaudited_features()
with open('/Users/.../Library/Ethereum/rinkeby/keystore/UTC...') as keyfile:
    encrypted_key = keyfile.read()
    private_key = web3.eth.account.decrypt(encrypted_key, 'password')

nonce = web3.eth.getTransactionCount('<public_address_of_sending_account>')

tx = {'value': 1000000000000000000, 'to': '0xBa4DE7E3Fd62995ee0e1929Efaf7a19b73df028f', 'nonce': nonce, 'chainId': 4, 'gasLimit': 6994000, 'gasPrice': 1000000000 }
tx['gas'] = web3.eth.estimateGas(tx)

signed = web3.eth.account.signTransaction(tx, private_key)
web3.eth.sendRawTransaction(signed.rawTransaction)
4

1 回答 1

5

外部拥有账户 (EOA) 的随机数从 0 开始,每次交易都会增加 1。因此,账户发送的第一笔交易需要 nonce 0,第二笔交易需要 nonce 1,依此类推。

要获得正确的当前随机数,您可以使用web3.eth.getTransactionCount(<address>).

于 2018-04-10T16:57:57.873 回答