Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort metadata keys for no-schema json for canonical CBOR #517

Merged
merged 1 commit into from
Apr 12, 2024

Conversation

carbolymer
Copy link
Contributor

@carbolymer carbolymer commented Apr 10, 2024

Changelog

- description: |
    Sort metadata keys for no-schema json for canonical CBOR
# uncomment types applicable to the change:
  type:
  # - feature        # introduces a new feature
   - breaking       # the API has changed in a breaking way
  # - compatible     # the API has changed but is non-breaking
  # - optimisation   # measurable performance improvements
  # - improvement    # QoL changes e.g. refactoring
   - bugfix         # fixes a defect
  # - test           # fixes/modifies tests
  # - maintenance    # not directly related to the code
  # - release        # related to a new release preparation
  # - documentation  # change in code docs, haddocks...

Context

This PR changes sorting for --json-metadata-no-schema JSON to canonical CBOR. It changes transaction file output CBOR representation (reorders metadata keys for this case), so it's marked as a breaking change.

Note that there's always --json-metadata-schema JSON format available which allows to specify metadata keys ordering. See example.

Fixes IntersectMBO/cardano-node#4335

Checklist

  • Commit sequence broadly makes sense and commits have useful messages
  • New tests are added if needed and existing tests are updated. See Running tests for more details
  • Self-reviewed the diff

@carbolymer carbolymer force-pushed the mgalazyn/fix/sort-metadata-keys-for-cbor branch from 709f74c to 1cae747 Compare April 10, 2024 16:37
@carbolymer carbolymer changed the title Sort metadata keys for no-schema json Sort metadata keys for no-schema json for canonical CBOR Apr 10, 2024
. traverse (\(k,v) -> (,) (convKey k) <$> conv v)
. List.sortOn fst
Copy link
Contributor Author

@carbolymer carbolymer Apr 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The JSON map was previously sorted lexicographically by keys and then it was processed down the line as a list of pairs until it got serialised to CBOR. So this place was deciding about the sorting order of the object fields' names.

Copy link
Contributor

@Jimbo4350 Jimbo4350 Apr 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this will work but I think a more robust solution would be to do something similar to HashableScriptData. We should serialize the metadata to CBOR once and carry around the canonical representation. I think this distinction is useful for the reader.

Copy link
Contributor Author

@carbolymer carbolymer Apr 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. This would complicate things a bit because here we're converting here our type:

to ledger type (isomorphic to ours): https://github.com/IntersectMBO/cardano-ledger/blob/dbce4e4a3ad508b2bfd2df897b617c19b7f049de/eras/shelley/impl/src/Cardano/Ledger/Shelley/TxAuxData.hs#L95
So I guess I'd have to modify SerialiseAsCBOR to handle different metadata types 🤔

Copy link
Contributor

@Jimbo4350 Jimbo4350 Apr 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would look like HashableScriptData's SerialiseAsCBOR instance:

instance SerialiseAsCBOR HashableScriptData where
    serialiseToCBOR (HashableScriptData origBytes _) = origBytes

Upon constructing "HashableTxMetaData" you would already have the CBOR of the metadata.

@carbolymer carbolymer force-pushed the mgalazyn/fix/sort-metadata-keys-for-cbor branch from 1cae747 to 6597f5f Compare April 10, 2024 16:44
@carbolymer carbolymer force-pushed the mgalazyn/fix/sort-metadata-keys-for-cbor branch from 6597f5f to 6cd7f73 Compare April 10, 2024 16:46
Copy link
Contributor

@smelc smelc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work ❤️

Comment on lines +174 to +175
-> ByteString -- ^ json string to test
-> TxMetadata -- ^ expected metadata
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
-> ByteString -- ^ json string to test
-> TxMetadata -- ^ expected metadata
-> ByteString -- ^ Json string to test
-> TxMetadata -- ^ Expected metadata

tmkValue <-
fromShelleyMetadatum
<$> (CBOR.decodeFullDecoder' CBOR.shelleyProtVer "TxMetadataKey" CBOR.decCBOR tmkBytes
:: Either CBOR.DecoderError (Shelley.Metadatum))

Check warning

Code scanning / HLint

Redundant bracket

cardano-api/internal/Cardano/Api/TxMetadata.hs:120:42-60:&nbsp;Warning:&nbsp;Redundant&nbsp;bracket &nbsp;&nbsp; Found: &nbsp;&nbsp;(Shelley.Metadatum) &nbsp;&nbsp; Perhaps: &nbsp;&nbsp;Shelley.Metadatum
@carbolymer carbolymer force-pushed the mgalazyn/fix/sort-metadata-keys-for-cbor branch from d039af6 to 6cd7f73 Compare April 12, 2024 12:21
@carbolymer carbolymer enabled auto-merge April 12, 2024 12:23
@carbolymer carbolymer added this pull request to the merge queue Apr 12, 2024
Merged via the queue into main with commit f912577 Apr 12, 2024
36 checks passed
@carbolymer carbolymer deleted the mgalazyn/fix/sort-metadata-keys-for-cbor branch April 12, 2024 12:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] - Metadata ordering neither as in JSON input nor canonical
3 participants