Skip to content

Commit 453d6b9

Browse files
palascarbolymer
andcommitted
Add comments to document why we don't have ToCBOR instance
Co-authored-by: Mateusz Galazyn <[email protected]>
1 parent ee2b5a0 commit 453d6b9

File tree

2 files changed

+13
-0
lines changed
  • cardano-api
    • src/Cardano/Api/Tx/Internal
    • test/cardano-api-test/Test/Cardano/Api

2 files changed

+13
-0
lines changed

cardano-api/src/Cardano/Api/Tx/Internal/Output.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ instance (Typeable ctx, IsShelleyBasedEra era) => HTP.HasTypeProxy (TxOut ctx er
129129
proxyToAsType :: HTP.Proxy (TxOut ctx era) -> AsType (TxOut ctx era)
130130
proxyToAsType _ = AsTxOut (HTP.asType @era)
131131

132+
-- | We do not provide a 'ToCBOR' instance for 'TxOut' because 'TxOut's can contain
133+
-- supplemental datums and the ledger's CBOR representation does not support this.
134+
-- For this reason, if we were to serialise a 'TxOut' with a supplemental datum,
135+
-- we would lose information and the roundtrip property would not hold.
132136
instance (Typeable ctx, IsShelleyBasedEra era) => FromCBOR (TxOut ctx era) where
133137
fromCBOR :: Ledger.Decoder s (TxOut ctx era)
134138
fromCBOR =

cardano-api/test/cardano-api-test/Test/Cardano/Api/CBOR.hs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,15 @@ prop_roundtrip_tx_CBOR = H.property $ do
111111
x <- H.forAll $ genTx era
112112
shelleyBasedEraConstraints era $ H.trippingCbor (proxyToAsType Proxy) x
113113

114+
-- | The CBOR representation for 'TxOut' does not store supplemental datums.
115+
-- This means we cannot provide a lossless serialisation instance for which
116+
-- a roundtrip property would hold.
117+
--
118+
-- Therefore, we only provide a deserialisation instance. The serialisation
119+
-- implementation is included for testing purposes only.
120+
--
121+
-- For the roundtrip test, we hash any supplemental datum before serialisation
122+
-- to ensure the property holds.
114123
prop_roundtrip_tx_out_CBOR :: Property
115124
prop_roundtrip_tx_out_CBOR = H.property $ do
116125
AnyShelleyBasedEra era <- H.noteShowM . H.forAll $ Gen.element [minBound .. maxBound]

0 commit comments

Comments
 (0)