首页 文章

Web3.py事务不在以太坊Rinkby testnet上广播

提问于
浏览
1

我使用下面的web.py代码尝试通过本地geth节点在Rinkeby testnet上发送1 ETH的事务 . 我可以看到在实时本地以太坊节点日志流中提交的事务,但它们似乎从未向网络广播(我永远无法在rinkeby.io块浏览器上看到它们) . 我每次都手动设置nonce,但我读到如果使用了之前的nonce并且它没有广播它可能会被卡住?作为答案的一部分,如果可以解释nonce目的/用法将是很好的 .

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)

1 回答

  • 3

    外部拥有帐户(EOA)的随机数从0开始,每次交易增加1 . 所以账号发送的第一笔交易需要有nonce 0,第二笔交易需要有nonce 1等 .

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

相关问题