@@ -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,27 +41,64 @@ 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- -- This is the same test as prop_roundtrip_witness_CBOR but uses the
45- -- new function `serialiseTxLedgerCddl` instead of the deprecated
46- -- `serialiseToTextEnvelope`. `deserialiseTxLedgerCddl` must be
47- -- compatible with both during the transition.
48- prop_forward_compatibility_txbody_CBOR :: Property
49- prop_forward_compatibility_txbody_CBOR = H. property $ do
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+
79+ prop_text_envelope_roundtrip_txbody_CBOR :: Property
80+ prop_text_envelope_roundtrip_txbody_CBOR = H. property $ do
5081 AnyShelleyBasedEra era <- H. noteShowM . H. forAll $ Gen. element [minBound .. maxBound ]
5182 x <- H. forAll $ makeSignedTransaction [] . fst <$> genValidTxBody era
5283 shelleyBasedEraConstraints
5384 era
5485 ( H. tripping
5586 x
5687 (serialiseToTextEnvelope (Just (TextEnvelopeDescr " Ledger Cddl Format" )))
57- (deserialiseTxLedgerCddl era)
88+ (deserialiseFromTextEnvelope (shelleyBasedEraConstraints era $ proxyToAsType Proxy ) )
5889 )
5990
60- prop_roundtrip_txbody_CBOR :: Property
61- prop_roundtrip_txbody_CBOR = H. property $ do
91+ prop_text_envelope_roundtrip_tx_CBOR :: Property
92+ prop_text_envelope_roundtrip_tx_CBOR = H. property $ do
6293 AnyShelleyBasedEra era <- H. noteShowM . H. forAll $ Gen. element [minBound .. maxBound ]
63- x <- H. forAll $ makeSignedTransaction [] . fst <$> genValidTxBody era
64- H. tripping x (serialiseTxLedgerCddl era) (deserialiseTxLedgerCddl era)
94+ x <- H. forAll $ genTx era
95+ shelleyBasedEraConstraints
96+ era
97+ ( H. tripping
98+ x
99+ (serialiseToTextEnvelope (Just (TextEnvelopeDescr " Ledger Cddl Format" )))
100+ (deserialiseFromTextEnvelope (shelleyBasedEraConstraints era $ proxyToAsType Proxy ))
101+ )
65102
66103prop_roundtrip_tx_CBOR :: Property
67104prop_roundtrip_tx_CBOR = H. property $ do
@@ -215,7 +252,7 @@ prop_roundtrip_non_double_encoded_always_succeeds_plutus_V3_CBOR = H.property $
215252prop_decode_only_double_wrapped_plutus_script_bytes_CBOR :: Property
216253prop_decode_only_double_wrapped_plutus_script_bytes_CBOR = H. property $ do
217254 let alwaysSucceedsDoubleEncoded = Base16. decodeLenient " 46450101002499"
218- decodeOnlyPlutusScriptBytes
255+ H. decodeOnlyPlutusScriptBytes
219256 ShelleyBasedEraConway
220257 PlutusScriptV3
221258 alwaysSucceedsDoubleEncoded
@@ -224,7 +261,7 @@ prop_decode_only_double_wrapped_plutus_script_bytes_CBOR = H.property $ do
224261prop_decode_only_wrapped_plutus_script_V1_CBOR :: Property
225262prop_decode_only_wrapped_plutus_script_V1_CBOR = H. property $ do
226263 PlutusScriptSerialised shortBs <- H. forAll $ genPlutusScript PlutusScriptV1
227- decodeOnlyPlutusScriptBytes
264+ H. decodeOnlyPlutusScriptBytes
228265 ShelleyBasedEraConway
229266 PlutusScriptV1
230267 (SBS. fromShort shortBs)
@@ -233,7 +270,7 @@ prop_decode_only_wrapped_plutus_script_V1_CBOR = H.property $ do
233270prop_decode_only_wrapped_plutus_script_V2_CBOR :: Property
234271prop_decode_only_wrapped_plutus_script_V2_CBOR = H. property $ do
235272 PlutusScriptSerialised shortBs <- H. forAll $ genPlutusScript PlutusScriptV2
236- decodeOnlyPlutusScriptBytes
273+ H. decodeOnlyPlutusScriptBytes
237274 ShelleyBasedEraConway
238275 PlutusScriptV2
239276 (SBS. fromShort shortBs)
@@ -242,7 +279,7 @@ prop_decode_only_wrapped_plutus_script_V2_CBOR = H.property $ do
242279prop_decode_only_wrapped_plutus_script_V3_CBOR :: Property
243280prop_decode_only_wrapped_plutus_script_V3_CBOR = H. property $ do
244281 PlutusScriptSerialised shortBs <- H. forAll $ genPlutusScript PlutusScriptV3
245- decodeOnlyPlutusScriptBytes
282+ H. decodeOnlyPlutusScriptBytes
246283 ShelleyBasedEraConway
247284 PlutusScriptV3
248285 (SBS. fromShort shortBs)
@@ -289,12 +326,6 @@ prop_TxWitness_cddlTypeToEra = H.property $ do
289326 getProxy :: forall a . a -> Proxy a
290327 getProxy _ = Proxy
291328
292- prop_roundtrip_Tx_Cddl :: Property
293- prop_roundtrip_Tx_Cddl = H. property $ do
294- AnyShelleyBasedEra era <- H. noteShowM . H. forAll $ Gen. element [minBound .. maxBound ]
295- x <- forAll $ genTx era
296- H. tripping x (serialiseTxLedgerCddl era) (deserialiseTxLedgerCddl era)
297-
298329prop_roundtrip_TxWitness_Cddl :: Property
299330prop_roundtrip_TxWitness_Cddl = H. property $ do
300331 AnyShelleyBasedEra sbe <- H. noteShowM . H. forAll $ Gen. element [minBound .. maxBound ]
@@ -303,19 +334,22 @@ prop_roundtrip_TxWitness_Cddl = H.property $ do
303334
304335prop_roundtrip_GovernancePoll_CBOR :: Property
305336prop_roundtrip_GovernancePoll_CBOR = property $ do
306- trippingCbor AsGovernancePoll =<< forAll genGovernancePoll
337+ H. trippingCbor AsGovernancePoll =<< forAll genGovernancePoll
307338
308339prop_roundtrip_GovernancePollAnswer_CBOR :: Property
309340prop_roundtrip_GovernancePollAnswer_CBOR = property $ do
310- trippingCbor AsGovernancePollAnswer =<< forAll genGovernancePollAnswer
341+ H. trippingCbor AsGovernancePollAnswer =<< forAll genGovernancePollAnswer
311342
312343-- -----------------------------------------------------------------------------
313344
314345tests :: TestTree
315346tests =
316347 testGroup
317348 " Test.Cardano.Api.Typed.CBOR"
318- [ testProperty " roundtrip witness CBOR" prop_roundtrip_witness_CBOR
349+ [ testProperty " rountrip txbody text envelope" prop_text_envelope_roundtrip_txbody_CBOR
350+ , testProperty " txbody backwards compatibility" prop_txbody_backwards_compatibility
351+ , testProperty " rountrip tx text envelope" prop_text_envelope_roundtrip_tx_CBOR
352+ , testProperty " roundtrip witness CBOR" prop_roundtrip_witness_CBOR
319353 , testProperty
320354 " roundtrip operational certificate CBOR"
321355 prop_roundtrip_operational_certificate_CBOR
@@ -404,9 +438,6 @@ tests =
404438 " roundtrip UpdateProposal CBOR"
405439 prop_roundtrip_UpdateProposal_CBOR
406440 , testProperty " roundtrip ScriptData CBOR" prop_roundtrip_ScriptData_CBOR
407- , testProperty " roundtrip txbody forward compatibility CBOR" prop_forward_compatibility_txbody_CBOR
408- , testProperty " roundtrip txbody CBOR" prop_roundtrip_txbody_CBOR
409- , testProperty " roundtrip Tx Cddl" prop_roundtrip_Tx_Cddl
410441 , testProperty " roundtrip TxWitness Cddl" prop_roundtrip_TxWitness_Cddl
411442 , testProperty " roundtrip tx CBOR" prop_roundtrip_tx_CBOR
412443 , testProperty
0 commit comments