go-ethereum:无法检索账户nonce。

huangapple go评论98阅读模式
英文:

go-ethereum: failed to retrieve account nonce

问题

我为我的智能合约创建了go绑定,但在执行交易时遇到了问题。只有在显式指定txOpts.Nonce时才能正常工作(请参见已注释的行)。当我将该行注释掉时,会出现以下错误:

Failed to execute transaction: failed to retrieve account nonce: json: cannot unmarshal hex number with leading zero digits into Go value of type hexutil.Uint64`

以下是相关代码:

txOpts := bind.NewKeyedTransactor(key)

//txOpts.Nonce = big.NewInt(<nonce>)

tx, err := token.MyContract(txOpts, big.NewInt(1))
if err != nil {
    log.Fatalf("Failed to execute transaction: %v", err)
}

文档中说明,当txOpts.Noncenil时,它将从txOpts.From中检索待处理的nonce。

英文:

I create go bindings for my smart contract but have an issue when executing a transaction. It only works when I explicitly specify the txOpts.Nonce (see commented line). When I leave the line commented I get this error:

Failed to execute transaction: failed to retrieve account nonce: json: cannot unmarshal hex number with leading zero digits into Go value of type hexutil.Uint64`

Here is the relevant code:

txOpts := bind.NewKeyedTransactor(key)

//txOpts.Nonce = big.NewInt(<nonce>)

tx, err := token.MyContract(txOpts, big.NewInt(1))
if err != nil {
    log.Fatalf("Failed to execute transaction: %v", err)
}

The documentation tells it would retrieve the pending nonce from txOpts.From when txOpts.Nonce is nil.

答案1

得分: 1

对于其他人对此感到困惑的人,我在使用Truffle开发框架进行测试时遇到了这个错误。对我来说,这个问题是因为Truffle JSON-RPC服务器返回的十六进制值带有前导零,例如"0x04",这违反了这里的规范:

https://github.com/ethereum/wiki/wiki/JSON-RPC#hex-value-encoding

在编码QUANTITIES(整数,数字)时:以十六进制表示,前缀为"0x",这是最紧凑的表示形式(略有例外:零应表示为"0x0")。

错误示例:0x0400(不允许有前导零)

话虽如此,对于Truffle来说,这里已经有一个拉取请求:https://github.com/trufflesuite/ganache-core/pull/32

如果您使用的是另一个JSON-RPC服务器,您需要自行验证它是否真正遵循了这个规范。

英文:

For anyone else wondering about this, I encountered this error while testing with the Truffle develop framework. For me, this problem was because the Truffle JSON-RPC server was returning hex values with leading zeros like "0x04", which violates the spec here:

https://github.com/ethereum/wiki/wiki/JSON-RPC#hex-value-encoding

> When encoding QUANTITIES (integers, numbers): encode as hex, prefix with "0x", the most compact representation (slight exception: zero should be represented as "0x0").
>
> WRONG: 0x0400 (no leading zeroes allowed)

That said, for Truffle, there's already a pull request here: https://github.com/trufflesuite/ganache-core/pull/32

If you're using another JSON-RPC server, you'd have to verify for yourself that it's actually following this spec.

huangapple
  • 本文由 发表于 2017年8月7日 00:30:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/45534151.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定