Skip to content

Commit d135ede

Browse files
committed
Update tx golden test with the CDDL format
1 parent 876a7be commit d135ede

File tree

16 files changed

+96
-48
lines changed

16 files changed

+96
-48
lines changed

cardano-api/gen/Gen/Hedgehog/Roundtrip/CBOR.hs

+9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
module Gen.Hedgehog.Roundtrip.CBOR
44
( roundtrip_CBOR
5+
, roundtrip_CDDL_Tx
56
) where
67

78
import Cardano.Api
@@ -19,3 +20,11 @@ roundtrip_CBOR typeProxy gen =
1920
H.property $ do
2021
val <- H.forAll gen
2122
H.tripping val serialiseToCBOR (deserialiseFromCBOR typeProxy)
23+
24+
25+
roundtrip_CDDL_Tx
26+
:: IsCardanoEra era => CardanoEra era -> Gen (Tx era) -> Property
27+
roundtrip_CDDL_Tx era gen =
28+
H.property $ do
29+
val <- H.forAll gen
30+
H.tripping val serialiseTxLedgerCddl (deserialiseTxLedgerCddl era)

cardano-api/src/Cardano/Api/Error.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ data FileError e = FileError FilePath e
4848
-- ^ Temporary path
4949
Handle
5050
| FileIOError FilePath IOException
51-
deriving Show
51+
deriving (Show, Eq)
5252

5353
instance Error e => Error (FileError e) where
5454
displayError (FileErrorTempFile targetPath tempPath h)=

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ instance Error TextEnvelopeCddlError where
115115
<> List.intercalate ", " (map Text.unpack expTypes)
116116
<> " Actual: " <> Text.unpack actType
117117
displayError (TextEnvelopeCddlErrUnknownType unknownType) =
118-
"Unknown TextEnvelopeCddl type:" <> Text.unpack unknownType
118+
"Unknown TextEnvelopeCddl type: " <> Text.unpack unknownType
119119
displayError TextEnvelopeCddlErrByronKeyWitnessUnsupported =
120120
"TextEnvelopeCddl error: Byron key witnesses are currently unsupported."
121121

@@ -132,7 +132,6 @@ serialiseTxLedgerCddl tx =
132132
genType tx' = case getTxWitnesses tx' of
133133
[] -> "Unwitnessed " <> genTxType
134134
_ -> "Witnessed " <> genTxType
135-
136135
genTxType :: Text
137136
genTxType =
138137
case cardanoEra :: CardanoEra era of

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

+13
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ module Cardano.Api.Tx (
2929
toShelleySigningKey,
3030
signByronTransaction,
3131
signShelleyTransaction,
32+
3233
-- ** Incremental signing and separate witnesses
3334
makeSignedTransaction,
3435
KeyWitness(..),
@@ -57,7 +58,9 @@ import qualified Data.ByteString.Lazy as LBS
5758
import Data.Functor.Identity (Identity)
5859
import qualified Data.Map.Strict as Map
5960
import qualified Data.Set as Set
61+
import Data.Type.Equality (TestEquality (..), (:~:) (Refl))
6062
import qualified Data.Vector as Vector
63+
6164
--
6265
-- Common types, consensus, network
6366
--
@@ -127,6 +130,16 @@ data Tx era where
127130
-> Ledger.Tx (ShelleyLedgerEra era)
128131
-> Tx era
129132

133+
134+
instance Show (InAnyCardanoEra Tx) where
135+
show (InAnyCardanoEra _ tx) = show tx
136+
137+
instance Eq (InAnyCardanoEra Tx) where
138+
(==) (InAnyCardanoEra eraA txA) (InAnyCardanoEra eraB txB) =
139+
case testEquality eraA eraB of
140+
Nothing -> False
141+
Just Refl -> txA == txB
142+
130143
-- The GADT in the ShelleyTx case requires a custom instance
131144
instance Eq (Tx era) where
132145
(==) (ByronTx txA)

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ module Test.Cardano.Api.Typed.CBOR
88

99
import Cardano.Api
1010
import Cardano.Prelude
11-
import Data.String (IsString(..))
11+
import Data.String (IsString (..))
1212
import Gen.Cardano.Api.Typed
13-
import Gen.Hedgehog.Roundtrip.CBOR (roundtrip_CBOR)
13+
import Gen.Hedgehog.Roundtrip.CBOR (roundtrip_CBOR, roundtrip_CDDL_Tx)
1414
import Hedgehog (Property, forAll, property, success, tripping)
1515
import Test.Cardano.Api.Typed.Orphans ()
1616
import Test.Tasty (TestTree, testGroup)
@@ -24,14 +24,14 @@ import Test.Tasty.Hedgehog (testPropertyNamed)
2424
test_roundtrip_txbody_CBOR :: [TestTree]
2525
test_roundtrip_txbody_CBOR =
2626
[ testPropertyNamed (show era) (fromString (show era)) $
27-
roundtrip_CBOR (proxyToAsType Proxy) (genTxBody era)
28-
| AnyCardanoEra era <- [minBound..(AnyCardanoEra AlonzoEra)] -- TODO: Babbage era
27+
roundtrip_CDDL_Tx era (makeSignedTransaction [] <$> genTxBody era)
28+
| AnyCardanoEra era <- [minBound..(AnyCardanoEra BabbageEra)]
2929
]
3030

3131
test_roundtrip_tx_CBOR :: [TestTree]
3232
test_roundtrip_tx_CBOR =
3333
[ testPropertyNamed (show era) (fromString (show era)) $ roundtrip_CBOR (proxyToAsType Proxy) (genTx era)
34-
| AnyCardanoEra era <- [minBound..(AnyCardanoEra AlonzoEra)] -- TODO: Babbage era
34+
| AnyCardanoEra era <- [minBound..(AnyCardanoEra BabbageEra)]
3535
]
3636

3737
prop_roundtrip_witness_byron_CBOR :: Property

cardano-cli/src/Cardano/CLI/Shelley/Run/Transaction.hs

+21-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module Cardano.CLI.Shelley.Run.Transaction
1212
( ShelleyTxCmdError(..)
1313
, renderShelleyTxCmdError
1414
, runTransactionCmd
15+
, readCddlTx
1516
, readFileTx
1617
, readProtocolParametersSourceSpec
1718
, toTxOutInAnyEra
@@ -1276,7 +1277,7 @@ runTxSign txOrTxBody witSigningData mnw (TxFile outTxFile) = do
12761277
signedTx = makeSignedTransaction allKeyWits txbody
12771278

12781279
firstExceptT ShelleyTxCmdWriteFileError . newExceptT $
1279-
writeFileTextEnvelope outTxFile Nothing signedTx
1280+
writeTxFileTextEnvelopeCddl outTxFile signedTx
12801281

12811282
(InputTxBodyFile (TxBodyFile txbodyFile)) -> do
12821283
unwitnessed <- readFileTxBody txbodyFile
@@ -1828,10 +1829,29 @@ readFileWitness fp =
18281829
-- (respectively needs additional witnesses or totally unwitnessed)
18291830
-- while UnwitnessedCliFormattedTxBody is CLI formatted TxBody and
18301831
-- needs to be key witnessed.
1832+
18311833
data IncompleteTx
18321834
= UnwitnessedCliFormattedTxBody (InAnyCardanoEra TxBody)
18331835
| IncompleteCddlFormattedTx (InAnyCardanoEra Tx)
18341836

1837+
1838+
readCddlTx :: FilePath -> IO (Either (FileError TextEnvelopeCddlError) CddlTx)
1839+
readCddlTx fp = readFileTextEnvelopeCddlAnyOf teTypes fp
1840+
where
1841+
teTypes = [ FromCDDLTx "Witnessed Tx ByronEra" CddlTx
1842+
, FromCDDLTx "Witnessed Tx ShelleyEra" CddlTx
1843+
, FromCDDLTx "Witnessed Tx AllegraEra" CddlTx
1844+
, FromCDDLTx "Witnessed Tx MaryEra" CddlTx
1845+
, FromCDDLTx "Witnessed Tx AlonzoEra" CddlTx
1846+
, FromCDDLTx "Witnessed Tx BabbageEra" CddlTx
1847+
, FromCDDLTx "Unwitnessed Tx ByronEra" CddlTx
1848+
, FromCDDLTx "Unwitnessed Tx ShelleyEra" CddlTx
1849+
, FromCDDLTx "Unwitnessed Tx AllegraEra" CddlTx
1850+
, FromCDDLTx "Unwitnessed Tx MaryEra" CddlTx
1851+
, FromCDDLTx "Unwitnessed Tx AlonzoEra" CddlTx
1852+
, FromCDDLTx "Unwitnessed Tx BabbageEra" CddlTx
1853+
]
1854+
18351855
readFileTxBody :: FilePath -> ExceptT ShelleyTxCmdError IO IncompleteTx
18361856
readFileTxBody fp =
18371857
handleLeftT
@@ -1877,7 +1897,6 @@ readFileTx fp =
18771897
(\e -> unCddlTx <$> acceptTxCDDLSerialisation e)
18781898
(readFileInAnyCardanoEra AsTx fp)
18791899

1880-
18811900
readFileInAnyCardanoEra
18821901
:: ( HasTextEnvelope (thing ByronEra)
18831902
, HasTextEnvelope (thing ShelleyEra)

cardano-cli/src/Cardano/CLI/Types.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ data CBORObject = CBORBlockByron Byron.EpochSlots
7575
| CBORVoteByron
7676
deriving Show
7777

78-
newtype CddlTx = CddlTx {unCddlTx :: InAnyCardanoEra Tx}
78+
newtype CddlTx = CddlTx {unCddlTx :: InAnyCardanoEra Tx} deriving (Show, Eq)
7979

8080
-- Encompasses stake certificates, stake pool certificates,
8181
-- genesis delegate certificates and MIR certificates.

cardano-cli/test/Test/Golden/Shelley/TextEnvelope/Tx/Tx.hs

+10-20
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ module Test.Golden.Shelley.TextEnvelope.Tx.Tx
44
( golden_shelleyTx
55
) where
66

7-
import Cardano.Api (AsType (..), HasTextEnvelope (..))
87
import Cardano.Prelude
8+
99
import Hedgehog (Property)
1010
import Test.OptParse
1111

@@ -20,29 +20,21 @@ import qualified Hedgehog.Extras.Test.Base as H
2020
golden_shelleyTx :: Property
2121
golden_shelleyTx = propertyOnce . H.moduleWorkspace "tmp" $ \tempDir -> do
2222
-- Reference keys
23-
let referenceTx = "test/data/golden/shelley/tx/tx"
23+
let referenceTx = "test/data/golden/alonzo/tx"
2424

2525
-- Key filepaths
26-
paymentVerKey <- noteTempFile tempDir "payment-verification-key-file"
27-
paymentSignKey <- noteTempFile tempDir "payment-signing-key-file"
26+
paymentSignKey <- noteInputFile "test/data/golden/shelley/transaction-sign/utxo.skey"
2827
transactionFile <- noteTempFile tempDir "tx-file"
2928
transactionBodyFile <- noteTempFile tempDir "tx-body-file"
3029

31-
-- Generate payment signing key to sign transaction
32-
void $ execCardanoCLI
33-
[ "address","key-gen"
34-
, "--verification-key-file", paymentVerKey
35-
, "--signing-key-file", paymentSignKey
36-
]
37-
3830
-- Create transaction body
3931
void $ execCardanoCLI
4032
[ "transaction", "build-raw"
41-
, "--shelley-era"
42-
, "--tx-in", "91999ea21177b33ebe6b8690724a0c026d410a11ad7521caa350abdafa5394c3#0"
43-
, "--tx-out", "addr1v9wmu83pzajplrtpsq6tsqdgwr98x888trpmah2u0ezznsge7del3+100000000"
44-
, "--fee", "1000000"
45-
, "--invalid-hereafter", "500000"
33+
, "--alonzo-era"
34+
, "--tx-in", "f62cd7bc15d8c6d2c8519fb8d13c57c0157ab6bab50af62bc63706feb966393d#0"
35+
, "--tx-out", "addr_test1qpmxr8d8jcl25kyz2tz9a9sxv7jxglhddyf475045y8j3zxjcg9vquzkljyfn3rasfwwlkwu7hhm59gzxmsyxf3w9dps8832xh+1199989833223"
36+
, "--tx-out", "addr_test1vpqgspvmh6m2m5pwangvdg499srfzre2dd96qq57nlnw6yctpasy4+10000000"
37+
, "--fee", "166777"
4638
, "--out-file", transactionBodyFile
4739
]
4840

@@ -51,12 +43,10 @@ golden_shelleyTx = propertyOnce . H.moduleWorkspace "tmp" $ \tempDir -> do
5143
[ "transaction", "sign"
5244
, "--tx-body-file", transactionBodyFile
5345
, "--signing-key-file", paymentSignKey
54-
, "--mainnet"
46+
, "--testnet-magic", "42"
5547
, "--out-file", transactionFile
5648
]
5749

58-
let txType = textEnvelopeType (AsTx AsShelleyEra)
59-
6050
-- Check the newly created files have not deviated from the
6151
-- golden files
62-
checkTextEnvelopeFormat txType referenceTx transactionFile
52+
checkTxCddlFormat referenceTx transactionFile

cardano-cli/test/Test/Golden/Shelley/TextEnvelope/Tx/TxBody.hs

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ module Test.Golden.Shelley.TextEnvelope.Tx.TxBody
44
( golden_shelleyTxBody
55
) where
66

7-
import Cardano.Api (AsType (..), HasTextEnvelope (..))
87
import Cardano.Prelude
8+
99
import Hedgehog (Property)
1010
import Test.OptParse
1111

@@ -34,8 +34,7 @@ golden_shelleyTxBody = propertyOnce . H.moduleWorkspace "tmp" $ \tempDir -> do
3434
, "--out-file", transactionBodyFile
3535
]
3636

37-
let txBodyType = textEnvelopeType AsMaryTxBody
3837

3938
-- Check the newly created files have not deviated from the
4039
-- golden files
41-
checkTextEnvelopeFormat txBodyType referenceTxBody transactionBodyFile
40+
checkTxCddlFormat referenceTxBody transactionBodyFile

cardano-cli/test/Test/Golden/Shelley/Transaction/Build.hs

+5-5
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ golden_shelleyTransactionBuild =
4141
, "--tx-body-file", txBodyOutFile
4242
]
4343

44-
H.assertFileOccurences 1 "TxBodyMary" txBodyOutFile
44+
H.assertFileOccurences 1 "Tx MaryEra" txBodyOutFile
4545

4646
H.assertEndsWithSingleNewline txBodyOutFile
4747

@@ -64,7 +64,7 @@ golden_shelleyTransactionBuild_CertificateScriptWitnessed =
6464
, "--tx-body-file", txBodyOutFile
6565
]
6666

67-
H.assertFileOccurences 1 "TxBodyMary" txBodyOutFile
67+
H.assertFileOccurences 1 "Tx MaryEra" txBodyOutFile
6868

6969
H.assertEndsWithSingleNewline txBodyOutFile
7070

@@ -96,7 +96,7 @@ golden_shelleyTransactionBuild_Minting =
9696
, "--tx-body-file", txBodyOutFile
9797
]
9898

99-
H.assertFileOccurences 1 "TxBodyMary" txBodyOutFile
99+
H.assertFileOccurences 1 "Tx MaryEra" txBodyOutFile
100100

101101
H.assertEndsWithSingleNewline txBodyOutFile
102102

@@ -120,7 +120,7 @@ golden_shelleyTransactionBuild_WithdrawalScriptWitnessed =
120120
, "--tx-body-file", txBodyOutFile
121121
]
122122

123-
H.assertFileOccurences 1 "TxBodyMary" txBodyOutFile
123+
H.assertFileOccurences 1 "Tx MaryEra" txBodyOutFile
124124

125125
H.assertEndsWithSingleNewline txBodyOutFile
126126

@@ -140,6 +140,6 @@ golden_shelleyTransactionBuild_TxInScriptWitnessed =
140140
, "--tx-body-file", txBodyOutFile
141141
]
142142

143-
H.assertFileOccurences 1 "TxBodyMary" txBodyOutFile
143+
H.assertFileOccurences 1 "Tx MaryEra" txBodyOutFile
144144

145145
H.assertEndsWithSingleNewline txBodyOutFile

cardano-cli/test/Test/Golden/Shelley/Transaction/CreateWitness.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,5 @@ golden_shelleyTransactionSigningKeyWitness = propertyOnce $ H.moduleWorkspace "t
4747
, "--out-file", witnessOutFile
4848
]
4949

50-
H.assertFileOccurences 1 "TxWitnessShelley" witnessOutFile
50+
H.assertFileOccurences 1 "TxWitness ShelleyEra" witnessOutFile
5151
H.assertEndsWithSingleNewline txBodyOutFile

cardano-cli/test/Test/OptParse.hs

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module Test.OptParse
2-
( checkTextEnvelopeFormat
2+
( checkTxCddlFormat
3+
, checkTextEnvelopeFormat
34
, equivalence
45
, execCardanoCLI
56
, propertyOnce
@@ -17,6 +18,8 @@ import qualified GHC.Stack as GHC
1718

1819
import Cardano.Api
1920

21+
import Cardano.CLI.Shelley.Run.Transaction
22+
2023
import qualified Hedgehog as H
2124
import qualified Hedgehog.Extras.Test.Process as H
2225
import Hedgehog.Internal.Property (Diff, MonadTest, liftTest, mkTest)
@@ -64,6 +67,17 @@ checkTextEnvelopeFormat tve reference created = do
6467
equivalence refType createdType
6568
equivalence refTitle createdTitle
6669

70+
checkTxCddlFormat
71+
:: (MonadTest m, MonadIO m, HasCallStack)
72+
=> FilePath -- ^ Reference/golden file
73+
-> FilePath -- ^ Newly created file
74+
-> m ()
75+
checkTxCddlFormat reference created = do
76+
r <- liftIO $ readCddlTx reference
77+
c <- liftIO $ readCddlTx created
78+
r H.=== c
79+
80+
6781
--------------------------------------------------------------------------------
6882
-- Helpers, Error rendering & Clean up
6983
--------------------------------------------------------------------------------
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"type": "Witnessed Tx AlonzoEra",
3+
"description": "Ledger Cddl Format",
4+
"cborHex": "84a30081825820f62cd7bc15d8c6d2c8519fb8d13c57c0157ab6bab50af62bc63706feb966393d0001828258390076619da7963eaa588252c45e960667a4647eed69135f51f5a10f2888d2c20ac07056fc8899c47d825cefd9dcf5efba150236e043262e2b431b0000011764f7be0782581d604088059bbeb6add02eecd0c6a2a52c06910f2a6b4ba0029e9fe6ed131a00989680021a00028b79a100818258208dc60533b5dfa60a530955a696323a2ef4f14e8bc95a8f84cf6c441fea4234275840043220211a264209f6e61903e60e80093b7b3a08e8bc5fe8f8707635acd69b6e0589e61aea544b87729983955decded90a59f9701042bebe57f2afba7c94fc02f5f6"
5+
}
+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"type": "TxSignedShelley",
3-
"description": "",
4-
"cborHex": "83a4008182582091999ea21177b33ebe6b8690724a0c026d410a11ad7521caa350abdafa5394c300018182581d615dbe1e2117641f8d618034b801a870ca731ce758c3bedd5c7e4429c11a05f5e100021a000f4240031a0007a120a100818258202f5c56d5d354205130674e519c4abb18f14e36c86a03811c8a3b4ea4786702b3584068f460e722d1423c7d4ce286e9c1d43a3a70ac4f42ca4dd782fe03cb41ce23e91ec97b9b3d467ff435b8b4efc75c466a10fb9c2b76c42711f58a7a049416fb02f6"
2+
"type": "Witnessed Tx AlonzoEra",
3+
"description": "Ledger Cddl Format",
4+
"cborHex": "84a30081825820256fe89fc62e787e470c54e6d17390309ed6ceed27f11b9f873dd55802a6272f0001828258390076619da7963eaa588252c45e960667a4647eed69135f51f5a10f2888d2c20ac07056fc8899c47d825cefd9dcf5efba150236e043262e2b431b0000011764f7be0782581d604088059bbeb6add02eecd0c6a2a52c06910f2a6b4ba0029e9fe6ed131a00989680021a00028b79a100818258206cf237caaf3218032c28fe858aaeeb835a8b315f45ec273970fb64b3dd1cc7dc58402271384dc35dc926468747e901aba167d03f17d1f01374f8631210af32b2dfb601d4949bd9e03158473493425a31db653a3de8460510c5da92ff01022eecc801f5f6"
55
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"type": "TxBodyMary",
3-
"description": "",
4-
"cborHex": "83a4008182582091999ea21177b33ebe6b8690724a0c026d410a11ad7521caa350abdafa5394c300018182581d615dbe1e2117641f8d618034b801a870ca731ce758c3bedd5c7e4429c11a05f5e100021a000f4240031a0007a1209ffff6"
2+
"type": "Unwitnessed Tx MaryEra",
3+
"description": "Ledger Cddl Format",
4+
"cborHex": "83a4008182582091999ea21177b33ebe6b8690724a0c026d410a11ad7521caa350abdafa5394c300018182581d615dbe1e2117641f8d618034b801a870ca731ce758c3bedd5c7e4429c11a05f5e100021a000f4240031a0007a120a0f6"
55
}

cardano-cli/test/data/golden/shelley/witnesses/singleSigningKeyWitness

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"type": "TxWitness MaryEra",
33
"description": "",
44
"cborHex": "82008258208dc60533b5dfa60a530955a696323a2ef4f14e8bc95a8f84cf6c441fea42342758409f69aa4bf41acf78deaffb002c4f36157f3bad7c434a19b1ffe7e7df5f98f629e58503a762e83c1f60e54c9eb7eef9885b16c2b42ce1336914f2df9aa63b1407"
5-
}
5+
}

0 commit comments

Comments
 (0)