Skip to content

Commit 2f1e19c

Browse files
committed
Add comments and TypeError to document why we don't have ToCBOR instance
1 parent 2e6c666 commit 2f1e19c

File tree

2 files changed

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

2 files changed

+27
-0
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ import Data.Typeable (Typeable)
104104
import Data.Word
105105
import GHC.Exts (IsList (..))
106106
import GHC.Stack
107+
import GHC.TypeLits as TL
107108
import Lens.Micro hiding (ix)
108109

109110
-- ----------------------------------------------------------------------------
@@ -134,6 +135,23 @@ instance (Typeable ctx, IsShelleyBasedEra era) => FromCBOR (TxOut ctx era) where
134135
shelleyBasedEraConstraints (shelleyBasedEra @era) $
135136
pure (fromShelleyTxOut shelleyBasedEra) <*> L.fromEraCBOR @(ShelleyLedgerEra era)
136137

138+
-- | We do not provide a 'ToCBOR' instance for 'TxOut' because 'TxOut's can
139+
-- contain supplemental datums and the CBOR representation does not support this.
140+
-- For this reason, if we were to serialise a 'TxOut' with a supplemental datum,
141+
-- we would lose information and the roundtrip property would not hold.
142+
instance
143+
( TypeError
144+
( TL.Text "Cannot serialise 'TxOut' to CBOR."
145+
:$$: TL.Text
146+
"Serialisation for 'TxOut' type is not implemented because they may contain supplemental datums and the CBOR representation does not support this."
147+
)
148+
, Typeable ctx
149+
, IsShelleyBasedEra era
150+
)
151+
=> ToCBOR (TxOut ctx era)
152+
where
153+
toCBOR = error "This shouldn't happen"
154+
137155
deriving instance Eq (TxOut ctx era)
138156

139157
deriving instance Show (TxOut ctx era)

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
@@ -110,6 +110,15 @@ prop_roundtrip_tx_CBOR = H.property $ do
110110
x <- H.forAll $ genTx era
111111
shelleyBasedEraConstraints era $ H.trippingCbor (proxyToAsType Proxy) x
112112

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

0 commit comments

Comments
 (0)