graphprotocol / graphprotocol/graph-node

Unexpected Behaviour Hex-Encoding Negative `BigInt`s

Offen
#4,222 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

apiVersion area/runtime bug Stale
Vorherrschende Sprache
Rust
Sterne
3.2k
Forks
1.1k
Ø Merge
4 T. 1 Std.
Gemergte PRs (30 T.)
1

Beschreibung

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).

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit dem Lesen der Implementierung von typeConversions.bigIntToHex in HostExports und des Verhaltens von num::BigInt::to_bytes_be. Vergleiche die dokumentierte go-ethereum-Kodierungsreferenz mit den drei vorgeschlagenen Verhaltensweisen für negative Werte und bestimme anschließend, welche Richtlinie akzeptabel ist, ohne die Kompatibilität von Host-Funktionen zu beeinträchtigen. Die Aufgabe ist abgeschlossen, wenn das Verhalten und die Kompatibilitätsauswirkungen entschieden und durch die relevanten Tests oder die Dokumentation abgedeckt sind.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
rust
Bereich
api, blockchain
Issue-Typ
Bug
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.