【问题标题】:Web3.py transactions are not broadcast on Ethereum Rinkby testnetWeb3.py 交易不会在以太坊 Rinkby 测试网上广播
【发布时间】:2018-09-19 13:01:07
【问题描述】:

我正在使用下面的 web.py 代码尝试通过本地 geth 节点在 Rinkeby 测试网上发送 1 ETH 的交易。我可以看到在实时本地以太坊节点日志流中提交的交易,但它们似乎从未被广播到网络(我永远无法在 rinkeby.io 区块浏览器上看到它们)。我每次都手动设置随机数,但我读到如果使用了以前的随机数并且它没有广播它可能会卡住?作为答案的一部分,如果可以解释 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)

【问题讨论】:

    标签: python ethereum web3 go-ethereum


    【解决方案1】:

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

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

    【讨论】:

    • 这成功了!谢谢你,兄弟!我更新了上面的代码以反映您的答案。
    猜你喜欢
    • 2018-11-04
    • 2021-10-02
    • 2018-06-23
    • 2022-07-19
    • 2019-12-03
    • 2022-11-11
    • 1970-01-01
    • 2021-05-29
    • 2020-07-23
    相关资源
    最近更新 更多