Unexpected Behaviour Hex-Encoding Negative `BigInt`s

未关闭
#4,222 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
25/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
rust
领域
api, blockchain

调研方向

首先阅读 HostExportstypeConversions.bigIntToHex 的实现以及 num::BigInt::to_bytes_be 的行为。将文档中说明的 go-ethereum 编码参考与针对负值提出的三种行为进行比较,然后确定在不破坏主机函数兼容性的情况下,哪项策略是可接受的。行为及其兼容性影响得到确定,并由相关测试或文档覆盖后,即视为完成。

由索引模型根据 Issue 内容生成。

描述

apiVersion area/runtime bug Stale

I would like to report some behaviour that seems unexpected to me.

What is the current behavior?

Encoding a negative BigInt value will return the hex of its absolute value. So -42 will be encoded as 0x2a.

What is the expected behavior?

Looking into the code, it looks like num::BigInt::to_bytes_be returns the big-endian bytes for its absolute value. This causes typeConversions.bigIntToHex to encode absolute values of its BigInt parameter.

This is a bit unexpected to me, and I would have expected either:

  1. To sign prefix the hex value (so 42 becomes 0x2a and -42 becomes -0x2a). This is more inline with how num::BigInt would hex-format values.
  2. To hex encode the value in 2's compliment notation. (so 42 becomes 0x2a and -42 becomes 0xd6). In looking at the code, this encoding implementation requires no leading 0s in the hex representation. This would mean that with 2's compliment, there would be multiple numbers with the same hex representation (for example 255 would be 0xff and -1 would also be 0xff).
  3. Disallow hex-encoding negative big integers. From the documentation for the typeConversions.bigIntToHex implementation in the HostExports, it mentions go-ethereum encoding rules as the reference. By those rules, hex marshalling for negative numbers is explicitly prohibited, so following that here would also make sense:

    Negative integers are not supported at this time. Attempting to marshal them will return an error. Values larger than 256bits are rejected by Unmarshal but will be marshaled without error.

Unfortunately, all of these suggestions would be breaking changes (which I imagine should be avoided for subgraph host function implementations).

主要语言
Rust
星标
3.2k
派生
1.1k
平均合并
4 天 1 小时
30 天内合并 PR
1

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

graphprotocol/graph-node 的其他 Issue

查看 graphprotocol/graph-node 的全部 Issue

相似的 Issue

更多 Rust Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。