Skip to content

Conversation

tbagrel1
Copy link

@tbagrel1 tbagrel1 commented Sep 2, 2025

Closes github.com/tweag/cardano-peras/issues/85.

@tbagrel1 tbagrel1 linked an issue Sep 2, 2025 that may be closed by this pull request
Move encode/decode functions for certs to proper file
@tbagrel1 tbagrel1 changed the title [WIP] Add NodeToNode Codec for PerasCertDiffusion Add NodeToNode Codec for PerasCertDiffusion Sep 3, 2025
@tbagrel1
Copy link
Author

tbagrel1 commented Sep 3, 2025

Given what we said in the weekly meeting today, I think this should be enough to complete ticket tweag/cardano-peras#85. Let me know if I missed something :)

Comment on lines +79 to +85
encodePerasRoundNo :: PerasRoundNo -> CBOR.Encoding
encodePerasRoundNo (PerasRoundNo{..}) =
CBOR.encodeWord64 unPerasRoundNo

decodePerasRoundNo :: forall s. CBOR.Decoder s PerasRoundNo
decodePerasRoundNo =
PerasRoundNo <$> CBOR.decodeWord64
Copy link
Member

Choose a reason for hiding this comment

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

For really simple types like PerasRoundNo, we usually define a Serialise instance. This way, we can avoid defining new en-/decoding functions, but can rather use the generic encode/decode functions.

Comment on lines +89 to +90
encodePerasRoundNo pcCertRound
<> (encodePoint (encodeRawHash p) pcCertBoostedBlock)
Copy link
Member

Choose a reason for hiding this comment

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

When we serialize a self-contained entity (like a cert/block/tx/whatever), we want it to be valid CBOR by itself. Currently, this isn't valid, but rather the juxtaposition of two valid CBOR items. To make it well-formed, you can add add a encodeListLen 2. (Dually, the decoder should use decodeListLenOf 2.)

Related: We should eventually test stuff like this; I created tweag/cardano-peras#103 for that.


Also see the validFlatTerm function for testing such stuff interactively, eg:

 Λ import Codec.CBOR.Encoding
 Λ import Codec.CBOR.FlatTerm
 Λ validFlatTerm $ toFlatTerm $ encodeWord64 123 <> encodeWord64 456
False
 Λ validFlatTerm $ toFlatTerm $ encodeListLen 2 <> encodeWord64 123 <> encodeWord64 456
True

in a cabal repl -b cborg.

Comment on lines +87 to +88
encodePerasCert :: forall blk. ConvertRawHash blk => Proxy blk -> PerasCert blk -> CBOR.Encoding
encodePerasCert p (PerasCert{..}) =
Copy link
Member

Choose a reason for hiding this comment

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

Instead of exposing en-/decoding functions, we could also already define a SerialiseNodeToNode instace for PerasCert (which will be required by tweag/cardano-peras#73 in any case)

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.

Define serialization for certificates
2 participants