Skip to content

Commit b18c8c2

Browse files
authored
Merge pull request #746 from IntersectMBO/remove-deprecated-functions
Remove deprecated functions and update types and update `writeTxFileTextEnvelopeCddl` to use new format
2 parents efe2960 + 3a74bcb commit b18c8c2

File tree

3 files changed

+73
-72
lines changed

3 files changed

+73
-72
lines changed

cardano-api/src/Cardano/Api.hs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -736,8 +736,6 @@ module Cardano.Api
736736
, deserialiseFromTextEnvelopeCddlAnyOf
737737
, writeTxFileTextEnvelopeCddl
738738
, writeTxWitnessFileTextEnvelopeCddl
739-
, serialiseTxLedgerCddl
740-
, deserialiseTxLedgerCddl
741739
, deserialiseByronTxCddl
742740
, serialiseWitnessLedgerCddl
743741
, deserialiseWitnessLedgerCddl

cardano-api/src/Cardano/Api/Internal/SerialiseLedgerCddl.hs

Lines changed: 14 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ module Cardano.Api.Internal.SerialiseLedgerCddl
2222
, writeTxFileTextEnvelopeCddl
2323
, writeTxWitnessFileTextEnvelopeCddl
2424
-- Exported for testing
25-
, serialiseTxLedgerCddl
26-
, deserialiseTxLedgerCddl
2725
, deserialiseByronTxCddl
2826
, serialiseWitnessLedgerCddl
2927
, deserialiseWitnessLedgerCddl
@@ -122,46 +120,6 @@ instance Error TextEnvelopeCddlError where
122120
TextEnvelopeCddlErrByronKeyWitnessUnsupported ->
123121
"TextEnvelopeCddl error: Byron key witnesses are currently unsupported."
124122

125-
{-# DEPRECATED
126-
serialiseTxLedgerCddl
127-
"Use 'serialiseToTextEnvelope' from 'Cardano.Api.Internal.SerialiseTextEnvelope' instead."
128-
#-}
129-
serialiseTxLedgerCddl :: ShelleyBasedEra era -> Tx era -> TextEnvelope
130-
serialiseTxLedgerCddl era tx =
131-
shelleyBasedEraConstraints era $
132-
(serialiseToTextEnvelope (Just (TextEnvelopeDescr "Ledger Cddl Format")) tx)
133-
{ teType = TextEnvelopeType $ T.unpack $ genType tx
134-
}
135-
where
136-
genType :: Tx era -> Text
137-
genType tx' = case getTxWitnesses tx' of
138-
[] -> "Unwitnessed " <> genTxType
139-
_ -> "Witnessed " <> genTxType
140-
genTxType :: Text
141-
genTxType =
142-
case era of
143-
ShelleyBasedEraShelley -> "Tx ShelleyEra"
144-
ShelleyBasedEraAllegra -> "Tx AllegraEra"
145-
ShelleyBasedEraMary -> "Tx MaryEra"
146-
ShelleyBasedEraAlonzo -> "Tx AlonzoEra"
147-
ShelleyBasedEraBabbage -> "Tx BabbageEra"
148-
ShelleyBasedEraConway -> "Tx ConwayEra"
149-
150-
{-# DEPRECATED
151-
deserialiseTxLedgerCddl
152-
"Use 'deserialiseFromTextEnvelope' from 'Cardano.Api.Internal.SerialiseTextEnvelope' instead."
153-
#-}
154-
deserialiseTxLedgerCddl
155-
:: forall era
156-
. ShelleyBasedEra era
157-
-> TextEnvelope
158-
-> Either TextEnvelopeError (Tx era)
159-
deserialiseTxLedgerCddl era =
160-
shelleyBasedEraConstraints era $ deserialiseFromTextEnvelope asType
161-
where
162-
asType :: AsType (Tx era)
163-
asType = shelleyBasedEraConstraints era $ proxyToAsType Proxy
164-
165123
writeByronTxFileTextEnvelopeCddl
166124
:: File content Out
167125
-> Byron.ATxAux ByteString
@@ -254,6 +212,11 @@ writeTxFileTextEnvelopeCddl era path tx =
254212
where
255213
txJson = encodePretty' textEnvelopeCddlJSONConfig (serialiseTxLedgerCddl era tx) <> "\n"
256214

215+
serialiseTxLedgerCddl :: ShelleyBasedEra era -> Tx era -> TextEnvelope
216+
serialiseTxLedgerCddl era' tx' =
217+
shelleyBasedEraConstraints era' $
218+
serialiseToTextEnvelope (Just (TextEnvelopeDescr "Ledger Cddl Format")) tx'
219+
257220
writeTxWitnessFileTextEnvelopeCddl
258221
:: ShelleyBasedEra era
259222
-> File () Out
@@ -312,6 +275,15 @@ deserialiseFromTextEnvelopeCddlAnyOf types teCddl =
312275
matching (FromCDDLTx ttoken _f) = TextEnvelopeType (T.unpack ttoken) `legacyComparison` teType teCddl
313276
matching (FromCDDLWitness ttoken _f) = TextEnvelopeType (T.unpack ttoken) `legacyComparison` teType teCddl
314277

278+
deserialiseTxLedgerCddl
279+
:: forall era
280+
. ShelleyBasedEra era
281+
-> TextEnvelope
282+
-> Either TextEnvelopeError (Tx era)
283+
deserialiseTxLedgerCddl era =
284+
shelleyBasedEraConstraints era $
285+
deserialiseFromTextEnvelope (shelleyBasedEraConstraints era $ proxyToAsType Proxy)
286+
315287
-- Parse the text into types because this will increase code readability and
316288
-- will make it easier to keep track of the different Cddl descriptions via
317289
-- a single sum data type.

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

Lines changed: 59 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import qualified Data.ByteString.Base16 as Base16
2020
import qualified Data.ByteString.Lazy as LBS
2121
import qualified Data.ByteString.Short as SBS
2222
import Data.Proxy (Proxy (..))
23+
import Data.Text (Text)
2324
import qualified Data.Text as T
2425

2526
import Test.Gen.Cardano.Api.Hardcoded
@@ -32,7 +33,6 @@ import qualified Hedgehog as H
3233
import qualified Hedgehog.Extras as H
3334
import qualified Hedgehog.Gen as Gen
3435
import qualified Test.Hedgehog.Roundtrip.CBOR as H
35-
import Test.Hedgehog.Roundtrip.CBOR
3636
import Test.Tasty (TestTree, testGroup)
3737
import 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

66103
prop_roundtrip_tx_CBOR :: Property
67104
prop_roundtrip_tx_CBOR = H.property $ do
@@ -215,7 +252,7 @@ prop_roundtrip_non_double_encoded_always_succeeds_plutus_V3_CBOR = H.property $
215252
prop_decode_only_double_wrapped_plutus_script_bytes_CBOR :: Property
216253
prop_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
224261
prop_decode_only_wrapped_plutus_script_V1_CBOR :: Property
225262
prop_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
233270
prop_decode_only_wrapped_plutus_script_V2_CBOR :: Property
234271
prop_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
242279
prop_decode_only_wrapped_plutus_script_V3_CBOR :: Property
243280
prop_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-
298329
prop_roundtrip_TxWitness_Cddl :: Property
299330
prop_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

304335
prop_roundtrip_GovernancePoll_CBOR :: Property
305336
prop_roundtrip_GovernancePoll_CBOR = property $ do
306-
trippingCbor AsGovernancePoll =<< forAll genGovernancePoll
337+
H.trippingCbor AsGovernancePoll =<< forAll genGovernancePoll
307338

308339
prop_roundtrip_GovernancePollAnswer_CBOR :: Property
309340
prop_roundtrip_GovernancePollAnswer_CBOR = property $ do
310-
trippingCbor AsGovernancePollAnswer =<< forAll genGovernancePollAnswer
341+
H.trippingCbor AsGovernancePollAnswer =<< forAll genGovernancePollAnswer
311342

312343
-- -----------------------------------------------------------------------------
313344

314345
tests :: TestTree
315346
tests =
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

Comments
 (0)