Skip to content

Commit 07aa3fe

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

File tree

2 files changed

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

2 files changed

+23
-0
lines changed

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

Lines changed: 16 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,21 @@ 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 they may
139+
-- contain supplemental datums and the CBOR representation does not support this.
140+
instance
141+
( TypeError
142+
( TL.Text "Cannot serialise 'TxOut' to CBOR."
143+
:$$: TL.Text
144+
"Serialisation for 'TxOut' type is not implemented because they may contain supplemental datums and the CBOR representation does not support this."
145+
)
146+
, Typeable ctx
147+
, IsShelleyBasedEra era
148+
)
149+
=> ToCBOR (TxOut ctx era)
150+
where
151+
toCBOR = error "This shouldn't happen"
152+
137153
deriving instance Eq (TxOut ctx era)
138154

139155
deriving instance Show (TxOut ctx era)

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,13 @@ 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 fo TxOut does not allow for storing
114+
-- suplemental datums, so we cannot provide a serialization mechanism
115+
-- that is lossless and for which the roundtrip property would hold.
116+
-- Therefore, we only provide deserialisation functionality and we
117+
-- include serialization only for testing purposes. Additionally,
118+
-- we convert hash any supplemental datum generated before testing
119+
-- because, otherwise, the property would not hold.
113120
prop_roundtrip_tx_out_CBOR :: Property
114121
prop_roundtrip_tx_out_CBOR = H.property $ do
115122
AnyShelleyBasedEra era <- H.noteShowM . H.forAll $ Gen.element [minBound .. maxBound]

0 commit comments

Comments
 (0)