graphprotocol / graphprotocol/graph-node
Unexpected Behaviour Hex-Encoding Negative `BigInt`s
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 3.2k
- Forks
- 1.1k
- Avg merge
- 4d 1h
- Merged PRs (30d)
- 1
Description
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:
- To sign prefix the hex value (so
42becomes0x2aand-42becomes-0x2a). This is more inline with hownum::BigIntwould hex-format values. - To hex encode the value in 2's compliment notation. (so
42becomes0x2aand-42becomes0xd6). 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 example255would be0xffand-1would also be0xff). - Disallow hex-encoding negative big integers. From the documentation for the
typeConversions.bigIntToHeximplementation in theHostExports, it mentionsgo-ethereumencoding 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).
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading the typeConversions.bigIntToHex implementation in the HostExports and the behavior of num::BigInt::to_bytes_be. Compare its documented go-ethereum encoding reference with the three proposed negative-value behaviors, then determine which policy is acceptable without breaking host-function compatibility. Done means the behavior and compatibility implications are decided and covered by the relevant tests or documentation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- api, blockchain
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100