@@ -20,6 +20,7 @@ import qualified Data.ByteString.Base16 as Base16
2020import qualified Data.ByteString.Lazy as LBS
2121import qualified Data.ByteString.Short as SBS
2222import Data.Proxy (Proxy (.. ))
23+ import Data.Text (Text )
2324import qualified Data.Text as T
2425
2526import Test.Gen.Cardano.Api.Hardcoded
@@ -32,7 +33,6 @@ import qualified Hedgehog as H
3233import qualified Hedgehog.Extras as H
3334import qualified Hedgehog.Gen as Gen
3435import qualified Test.Hedgehog.Roundtrip.CBOR as H
35- import Test.Hedgehog.Roundtrip.CBOR
3636import Test.Tasty (TestTree , testGroup )
3737import Test.Tasty.Hedgehog (testProperty )
3838
@@ -41,6 +41,41 @@ import Test.Tasty.Hedgehog (testProperty)
4141-- TODO: Need to add PaymentExtendedKey roundtrip tests however
4242-- we can't derive an Eq instance for Crypto.HD.XPrv
4343
44+ prop_txbody_backwards_compatibility :: Property
45+ prop_txbody_backwards_compatibility = H. property $ do
46+ AnyShelleyBasedEra era <- H. noteShowM . H. forAll $ Gen. element [minBound .. maxBound ]
47+ x <- H. forAll $ makeSignedTransaction [] . fst <$> genValidTxBody era
48+ shelleyBasedEraConstraints
49+ era
50+ ( H. tripping
51+ x
52+ (serialiseTxLedgerCddl era)
53+ (deserialiseFromTextEnvelope (shelleyBasedEraConstraints era $ proxyToAsType Proxy ))
54+ )
55+ where
56+ -- This is the old implementation of serialisation for txbodies, and it is
57+ -- now deprecated. But we keep it here for testing for backwards compatibility.
58+ serialiseTxLedgerCddl :: ShelleyBasedEra era -> Tx era -> TextEnvelope
59+ serialiseTxLedgerCddl era tx =
60+ shelleyBasedEraConstraints era $
61+ (serialiseToTextEnvelope (Just (TextEnvelopeDescr " Ledger Cddl Format" )) tx)
62+ { teType = TextEnvelopeType $ T. unpack $ genType tx
63+ }
64+ where
65+ genType :: Tx era -> Text
66+ genType tx' = case getTxWitnesses tx' of
67+ [] -> " Unwitnessed " <> genTxType
68+ _ -> " Witnessed " <> genTxType
69+ genTxType :: Text
70+ genTxType =
71+ case era of
72+ ShelleyBasedEraShelley -> " Tx ShelleyEra"
73+ ShelleyBasedEraAllegra -> " Tx AllegraEra"
74+ ShelleyBasedEraMary -> " Tx MaryEra"
75+ ShelleyBasedEraAlonzo -> " Tx AlonzoEra"
76+ ShelleyBasedEraBabbage -> " Tx BabbageEra"
77+ ShelleyBasedEraConway -> " Tx ConwayEra"
78+
4479prop_text_envelope_roundtrip_txbody_CBOR :: Property
4580prop_text_envelope_roundtrip_txbody_CBOR = H. property $ do
4681 AnyShelleyBasedEra era <- H. noteShowM . H. forAll $ Gen. element [minBound .. maxBound ]
@@ -217,7 +252,7 @@ prop_roundtrip_non_double_encoded_always_succeeds_plutus_V3_CBOR = H.property $
217252prop_decode_only_double_wrapped_plutus_script_bytes_CBOR :: Property
218253prop_decode_only_double_wrapped_plutus_script_bytes_CBOR = H. property $ do
219254 let alwaysSucceedsDoubleEncoded = Base16. decodeLenient " 46450101002499"
220- decodeOnlyPlutusScriptBytes
255+ H. decodeOnlyPlutusScriptBytes
221256 ShelleyBasedEraConway
222257 PlutusScriptV3
223258 alwaysSucceedsDoubleEncoded
@@ -226,7 +261,7 @@ prop_decode_only_double_wrapped_plutus_script_bytes_CBOR = H.property $ do
226261prop_decode_only_wrapped_plutus_script_V1_CBOR :: Property
227262prop_decode_only_wrapped_plutus_script_V1_CBOR = H. property $ do
228263 PlutusScriptSerialised shortBs <- H. forAll $ genPlutusScript PlutusScriptV1
229- decodeOnlyPlutusScriptBytes
264+ H. decodeOnlyPlutusScriptBytes
230265 ShelleyBasedEraConway
231266 PlutusScriptV1
232267 (SBS. fromShort shortBs)
@@ -235,7 +270,7 @@ prop_decode_only_wrapped_plutus_script_V1_CBOR = H.property $ do
235270prop_decode_only_wrapped_plutus_script_V2_CBOR :: Property
236271prop_decode_only_wrapped_plutus_script_V2_CBOR = H. property $ do
237272 PlutusScriptSerialised shortBs <- H. forAll $ genPlutusScript PlutusScriptV2
238- decodeOnlyPlutusScriptBytes
273+ H. decodeOnlyPlutusScriptBytes
239274 ShelleyBasedEraConway
240275 PlutusScriptV2
241276 (SBS. fromShort shortBs)
@@ -244,7 +279,7 @@ prop_decode_only_wrapped_plutus_script_V2_CBOR = H.property $ do
244279prop_decode_only_wrapped_plutus_script_V3_CBOR :: Property
245280prop_decode_only_wrapped_plutus_script_V3_CBOR = H. property $ do
246281 PlutusScriptSerialised shortBs <- H. forAll $ genPlutusScript PlutusScriptV3
247- decodeOnlyPlutusScriptBytes
282+ H. decodeOnlyPlutusScriptBytes
248283 ShelleyBasedEraConway
249284 PlutusScriptV3
250285 (SBS. fromShort shortBs)
@@ -299,11 +334,11 @@ prop_roundtrip_TxWitness_Cddl = H.property $ do
299334
300335prop_roundtrip_GovernancePoll_CBOR :: Property
301336prop_roundtrip_GovernancePoll_CBOR = property $ do
302- trippingCbor AsGovernancePoll =<< forAll genGovernancePoll
337+ H. trippingCbor AsGovernancePoll =<< forAll genGovernancePoll
303338
304339prop_roundtrip_GovernancePollAnswer_CBOR :: Property
305340prop_roundtrip_GovernancePollAnswer_CBOR = property $ do
306- trippingCbor AsGovernancePollAnswer =<< forAll genGovernancePollAnswer
341+ H. trippingCbor AsGovernancePollAnswer =<< forAll genGovernancePollAnswer
307342
308343-- -----------------------------------------------------------------------------
309344
@@ -312,6 +347,7 @@ tests =
312347 testGroup
313348 " Test.Cardano.Api.Typed.CBOR"
314349 [ testProperty " rountrip txbody text envelope" prop_text_envelope_roundtrip_txbody_CBOR
350+ , testProperty " txbody backwards compatibility" prop_txbody_backwards_compatibility
315351 , testProperty " rountrip tx text envelope" prop_text_envelope_roundtrip_tx_CBOR
316352 , testProperty " roundtrip witness CBOR" prop_roundtrip_witness_CBOR
317353 , testProperty
0 commit comments