whyrusleeping / cbor-gen

Codegen for cbor codecs on your types
MIT License
35 stars 25 forks source link

Sort map keys according to RFC7049 & DAG-CBOR strict ordering #42

Closed rvagg closed 3 years ago

rvagg commented 4 years ago

I know this is disruptive where maps are used, and very late for Filecoin, so obviously proceed with caution.

This adds proper RFC7049 map key sorting, to both bare maps and structs in map representation. The other DAG-CBOR codec implementations strictly sort by length first, then bytes, as per https://github.com/ipld/specs/blob/master/block-layer/codecs/dag-cbor.md#strictness. The current implementation will render CBOR that won't produce stable CIDs with a round-trip using another DAG-CBOR encoder.

Illustrated by a failed round-trip attempt on this:

genesis_keysort

Stebalien commented 4 years ago

While the genesis block issue isn't actually related to this, I don't think we use maps as a part of consensus so this might not be too problematic. And I agree not having canonical CBOR would be unfortunate.