cardano-ledger icon indicating copy to clipboard operation
cardano-ledger copied to clipboard

Mention if duplicates are allowed in maps in the CDDL files

Open itsfarseen opened this issue 1 year ago • 2 comments

In conway.cddl, there are many types which are maps. As per CDDL spec, maps are allowed to have duplicate keys. But the node doesn't allow duplicate keys in some places, and allows in some other places.

For example, the multiasset type is defined as a map: https://github.com/IntersectMBO/cardano-ledger/blob/28ab3884cac8edbb7270fd4b8628a16429d2ec9e/eras/conway/impl/cddl-files/conway.cddl#L564

The mint type, which is an instance of multiasset, can have duplicate keys. https://github.com/IntersectMBO/cardano-ledger/blob/28ab3884cac8edbb7270fd4b8628a16429d2ec9e/eras/conway/impl/cddl-files/conway.cddl#L576

But the multiasset field of the value type can't have. https://github.com/IntersectMBO/cardano-ledger/blob/28ab3884cac8edbb7270fd4b8628a16429d2ec9e/eras/conway/impl/cddl-files/conway.cddl#L574

It would be helpful for library/tools authors to have comments in the CDDL file around map types, describing whether duplicate keys are allowed or not.

itsfarseen avatar May 16 '24 09:05 itsfarseen

Duplicate keys are not allowed in any of the maps in cbor, simply because the mapping must be unique from keys to values. The difference is in some cases we fail hard on duplicates while in others we discard the duplicate mapping from the map. It is a bit unfortunate, but it has to do with the limitation of current implementation of serialization that we have (See #4009). Although we do have strong plans on fixing it throughout all of the ledger types. As far as the tooling is concerned, IMHO it should not allow duplicate keys, because that is the ultimate goal. That being said, I am totally for documenting this discrepancy for every map/set occurrence in the cddl

lehins avatar May 21 '24 03:05 lehins

@lehins, could you also provide documentation on how map duplicates are handled in each case? Additionally, could you explain how the discard logic will work? Will it retain the first or the last value, and will it be sorted, etc.?

lisicky avatar Sep 05 '24 12:09 lisicky