Skip to content

Commit

Permalink
Update tx golden test with the CDDL format
Browse files Browse the repository at this point in the history
  • Loading branch information
Jimbo4350 committed Sep 21, 2022
1 parent b32b6e5 commit 6207b62
Show file tree
Hide file tree
Showing 16 changed files with 96 additions and 48 deletions.
9 changes: 9 additions & 0 deletions cardano-api/gen/Gen/Hedgehog/Roundtrip/CBOR.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

module Gen.Hedgehog.Roundtrip.CBOR
( roundtrip_CBOR
, roundtrip_CDDL_Tx
) where

import Cardano.Api
Expand All @@ -19,3 +20,11 @@ roundtrip_CBOR typeProxy gen =
H.property $ do
val <- H.forAll gen
H.tripping val serialiseToCBOR (deserialiseFromCBOR typeProxy)


roundtrip_CDDL_Tx
:: IsCardanoEra era => CardanoEra era -> Gen (Tx era) -> Property
roundtrip_CDDL_Tx era gen =
H.property $ do
val <- H.forAll gen
H.tripping val serialiseTxLedgerCddl (deserialiseTxLedgerCddl era)
2 changes: 1 addition & 1 deletion cardano-api/src/Cardano/Api/Error.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ data FileError e = FileError FilePath e
-- ^ Temporary path
Handle
| FileIOError FilePath IOException
deriving Show
deriving (Show, Eq)

instance Error e => Error (FileError e) where
displayError (FileErrorTempFile targetPath tempPath h)=
Expand Down
3 changes: 1 addition & 2 deletions cardano-api/src/Cardano/Api/SerialiseLedgerCddl.hs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ instance Error TextEnvelopeCddlError where
<> List.intercalate ", " (map Text.unpack expTypes)
<> " Actual: " <> Text.unpack actType
displayError (TextEnvelopeCddlErrUnknownType unknownType) =
"Unknown TextEnvelopeCddl type:" <> Text.unpack unknownType
"Unknown TextEnvelopeCddl type: " <> Text.unpack unknownType
displayError TextEnvelopeCddlErrByronKeyWitnessUnsupported =
"TextEnvelopeCddl error: Byron key witnesses are currently unsupported."

Expand All @@ -132,7 +132,6 @@ serialiseTxLedgerCddl tx =
genType tx' = case getTxWitnesses tx' of
[] -> "Unwitnessed " <> genTxType
_ -> "Witnessed " <> genTxType

genTxType :: Text
genTxType =
case cardanoEra :: CardanoEra era of
Expand Down
13 changes: 13 additions & 0 deletions cardano-api/src/Cardano/Api/Tx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module Cardano.Api.Tx (
toShelleySigningKey,
signByronTransaction,
signShelleyTransaction,

-- ** Incremental signing and separate witnesses
makeSignedTransaction,
KeyWitness(..),
Expand Down Expand Up @@ -57,7 +58,9 @@ import qualified Data.ByteString.Lazy as LBS
import Data.Functor.Identity (Identity)
import qualified Data.Map.Strict as Map
import qualified Data.Set as Set
import Data.Type.Equality (TestEquality (..), (:~:) (Refl))
import qualified Data.Vector as Vector

--
-- Common types, consensus, network
--
Expand Down Expand Up @@ -127,6 +130,16 @@ data Tx era where
-> Ledger.Tx (ShelleyLedgerEra era)
-> Tx era


instance Show (InAnyCardanoEra Tx) where
show (InAnyCardanoEra _ tx) = show tx

instance Eq (InAnyCardanoEra Tx) where
(==) (InAnyCardanoEra eraA txA) (InAnyCardanoEra eraB txB) =
case testEquality eraA eraB of
Nothing -> False
Just Refl -> txA == txB

-- The GADT in the ShelleyTx case requires a custom instance
instance Eq (Tx era) where
(==) (ByronTx txA)
Expand Down
10 changes: 5 additions & 5 deletions cardano-api/test/Test/Cardano/Api/Typed/CBOR.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ module Test.Cardano.Api.Typed.CBOR

import Cardano.Api
import Cardano.Prelude
import Data.String (IsString(..))
import Data.String (IsString (..))
import Gen.Cardano.Api.Typed
import Gen.Hedgehog.Roundtrip.CBOR (roundtrip_CBOR)
import Gen.Hedgehog.Roundtrip.CBOR (roundtrip_CBOR, roundtrip_CDDL_Tx)
import Hedgehog (Property, forAll, property, success, tripping)
import Test.Cardano.Api.Typed.Orphans ()
import Test.Tasty (TestTree, testGroup)
Expand All @@ -24,14 +24,14 @@ import Test.Tasty.Hedgehog (testPropertyNamed)
test_roundtrip_txbody_CBOR :: [TestTree]
test_roundtrip_txbody_CBOR =
[ testPropertyNamed (show era) (fromString (show era)) $
roundtrip_CBOR (proxyToAsType Proxy) (genTxBody era)
| AnyCardanoEra era <- [minBound..(AnyCardanoEra AlonzoEra)] -- TODO: Babbage era
roundtrip_CDDL_Tx era (makeSignedTransaction [] <$> genTxBody era)
| AnyCardanoEra era <- [minBound..(AnyCardanoEra BabbageEra)]
]

test_roundtrip_tx_CBOR :: [TestTree]
test_roundtrip_tx_CBOR =
[ testPropertyNamed (show era) (fromString (show era)) $ roundtrip_CBOR (proxyToAsType Proxy) (genTx era)
| AnyCardanoEra era <- [minBound..(AnyCardanoEra AlonzoEra)] -- TODO: Babbage era
| AnyCardanoEra era <- [minBound..(AnyCardanoEra BabbageEra)]
]

prop_roundtrip_witness_byron_CBOR :: Property
Expand Down
23 changes: 21 additions & 2 deletions cardano-cli/src/Cardano/CLI/Shelley/Run/Transaction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module Cardano.CLI.Shelley.Run.Transaction
( ShelleyTxCmdError(..)
, renderShelleyTxCmdError
, runTransactionCmd
, readCddlTx
, readFileTx
, readProtocolParametersSourceSpec
, toTxOutInAnyEra
Expand Down Expand Up @@ -1274,7 +1275,7 @@ runTxSign txOrTxBody witSigningData mnw (TxFile outTxFile) = do
signedTx = makeSignedTransaction allKeyWits txbody

firstExceptT ShelleyTxCmdWriteFileError . newExceptT $
writeFileTextEnvelope outTxFile Nothing signedTx
writeTxFileTextEnvelopeCddl outTxFile signedTx

(InputTxBodyFile (TxBodyFile txbodyFile)) -> do
unwitnessed <- readFileTxBody txbodyFile
Expand Down Expand Up @@ -1826,10 +1827,29 @@ readFileWitness fp =
-- (respectively needs additional witnesses or totally unwitnessed)
-- while UnwitnessedCliFormattedTxBody is CLI formatted TxBody and
-- needs to be key witnessed.

data IncompleteTx
= UnwitnessedCliFormattedTxBody (InAnyCardanoEra TxBody)
| IncompleteCddlFormattedTx (InAnyCardanoEra Tx)


readCddlTx :: FilePath -> IO (Either (FileError TextEnvelopeCddlError) CddlTx)
readCddlTx = readFileTextEnvelopeCddlAnyOf teTypes
where
teTypes = [ FromCDDLTx "Witnessed Tx ByronEra" CddlTx
, FromCDDLTx "Witnessed Tx ShelleyEra" CddlTx
, FromCDDLTx "Witnessed Tx AllegraEra" CddlTx
, FromCDDLTx "Witnessed Tx MaryEra" CddlTx
, FromCDDLTx "Witnessed Tx AlonzoEra" CddlTx
, FromCDDLTx "Witnessed Tx BabbageEra" CddlTx
, FromCDDLTx "Unwitnessed Tx ByronEra" CddlTx
, FromCDDLTx "Unwitnessed Tx ShelleyEra" CddlTx
, FromCDDLTx "Unwitnessed Tx AllegraEra" CddlTx
, FromCDDLTx "Unwitnessed Tx MaryEra" CddlTx
, FromCDDLTx "Unwitnessed Tx AlonzoEra" CddlTx
, FromCDDLTx "Unwitnessed Tx BabbageEra" CddlTx
]

readFileTxBody :: FilePath -> ExceptT ShelleyTxCmdError IO IncompleteTx
readFileTxBody fp =
handleLeftT
Expand Down Expand Up @@ -1875,7 +1895,6 @@ readFileTx fp =
(\e -> unCddlTx <$> acceptTxCDDLSerialisation e)
(readFileInAnyCardanoEra AsTx fp)


readFileInAnyCardanoEra
:: ( HasTextEnvelope (thing ByronEra)
, HasTextEnvelope (thing ShelleyEra)
Expand Down
2 changes: 1 addition & 1 deletion cardano-cli/src/Cardano/CLI/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ data CBORObject = CBORBlockByron Byron.EpochSlots
| CBORVoteByron
deriving Show

newtype CddlTx = CddlTx {unCddlTx :: InAnyCardanoEra Tx}
newtype CddlTx = CddlTx {unCddlTx :: InAnyCardanoEra Tx} deriving (Show, Eq)

-- Encompasses stake certificates, stake pool certificates,
-- genesis delegate certificates and MIR certificates.
Expand Down
30 changes: 10 additions & 20 deletions cardano-cli/test/Test/Golden/Shelley/TextEnvelope/Tx/Tx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ module Test.Golden.Shelley.TextEnvelope.Tx.Tx
( golden_shelleyTx
) where

import Cardano.Api (AsType (..), HasTextEnvelope (..))
import Cardano.Prelude

import Hedgehog (Property)
import Test.OptParse

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

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

-- Generate payment signing key to sign transaction
void $ execCardanoCLI
[ "address","key-gen"
, "--verification-key-file", paymentVerKey
, "--signing-key-file", paymentSignKey
]

-- Create transaction body
void $ execCardanoCLI
[ "transaction", "build-raw"
, "--shelley-era"
, "--tx-in", "91999ea21177b33ebe6b8690724a0c026d410a11ad7521caa350abdafa5394c3#0"
, "--tx-out", "addr1v9wmu83pzajplrtpsq6tsqdgwr98x888trpmah2u0ezznsge7del3+100000000"
, "--fee", "1000000"
, "--invalid-hereafter", "500000"
, "--alonzo-era"
, "--tx-in", "f62cd7bc15d8c6d2c8519fb8d13c57c0157ab6bab50af62bc63706feb966393d#0"
, "--tx-out", "addr_test1qpmxr8d8jcl25kyz2tz9a9sxv7jxglhddyf475045y8j3zxjcg9vquzkljyfn3rasfwwlkwu7hhm59gzxmsyxf3w9dps8832xh+1199989833223"
, "--tx-out", "addr_test1vpqgspvmh6m2m5pwangvdg499srfzre2dd96qq57nlnw6yctpasy4+10000000"
, "--fee", "166777"
, "--out-file", transactionBodyFile
]

Expand All @@ -51,12 +43,10 @@ golden_shelleyTx = propertyOnce . H.moduleWorkspace "tmp" $ \tempDir -> do
[ "transaction", "sign"
, "--tx-body-file", transactionBodyFile
, "--signing-key-file", paymentSignKey
, "--mainnet"
, "--testnet-magic", "42"
, "--out-file", transactionFile
]

let txType = textEnvelopeType (AsTx AsShelleyEra)

-- Check the newly created files have not deviated from the
-- golden files
checkTextEnvelopeFormat txType referenceTx transactionFile
checkTxCddlFormat referenceTx transactionFile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ module Test.Golden.Shelley.TextEnvelope.Tx.TxBody
( golden_shelleyTxBody
) where

import Cardano.Api (AsType (..), HasTextEnvelope (..))
import Cardano.Prelude

import Hedgehog (Property)
import Test.OptParse

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

let txBodyType = textEnvelopeType AsMaryTxBody

-- Check the newly created files have not deviated from the
-- golden files
checkTextEnvelopeFormat txBodyType referenceTxBody transactionBodyFile
checkTxCddlFormat referenceTxBody transactionBodyFile
10 changes: 5 additions & 5 deletions cardano-cli/test/Test/Golden/Shelley/Transaction/Build.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ golden_shelleyTransactionBuild =
, "--tx-body-file", txBodyOutFile
]

H.assertFileOccurences 1 "TxBodyMary" txBodyOutFile
H.assertFileOccurences 1 "Tx MaryEra" txBodyOutFile

H.assertEndsWithSingleNewline txBodyOutFile

Expand All @@ -64,7 +64,7 @@ golden_shelleyTransactionBuild_CertificateScriptWitnessed =
, "--tx-body-file", txBodyOutFile
]

H.assertFileOccurences 1 "TxBodyMary" txBodyOutFile
H.assertFileOccurences 1 "Tx MaryEra" txBodyOutFile

H.assertEndsWithSingleNewline txBodyOutFile

Expand Down Expand Up @@ -96,7 +96,7 @@ golden_shelleyTransactionBuild_Minting =
, "--tx-body-file", txBodyOutFile
]

H.assertFileOccurences 1 "TxBodyMary" txBodyOutFile
H.assertFileOccurences 1 "Tx MaryEra" txBodyOutFile

H.assertEndsWithSingleNewline txBodyOutFile

Expand All @@ -120,7 +120,7 @@ golden_shelleyTransactionBuild_WithdrawalScriptWitnessed =
, "--tx-body-file", txBodyOutFile
]

H.assertFileOccurences 1 "TxBodyMary" txBodyOutFile
H.assertFileOccurences 1 "Tx MaryEra" txBodyOutFile

H.assertEndsWithSingleNewline txBodyOutFile

Expand All @@ -140,6 +140,6 @@ golden_shelleyTransactionBuild_TxInScriptWitnessed =
, "--tx-body-file", txBodyOutFile
]

H.assertFileOccurences 1 "TxBodyMary" txBodyOutFile
H.assertFileOccurences 1 "Tx MaryEra" txBodyOutFile

H.assertEndsWithSingleNewline txBodyOutFile
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ golden_shelleyTransactionSigningKeyWitness = propertyOnce $ H.moduleWorkspace "t
, "--out-file", witnessOutFile
]

H.assertFileOccurences 1 "TxWitnessShelley" witnessOutFile
H.assertFileOccurences 1 "TxWitness ShelleyEra" witnessOutFile
H.assertEndsWithSingleNewline txBodyOutFile
16 changes: 15 additions & 1 deletion cardano-cli/test/Test/OptParse.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Test.OptParse
( checkTextEnvelopeFormat
( checkTxCddlFormat
, checkTextEnvelopeFormat
, equivalence
, execCardanoCLI
, propertyOnce
Expand All @@ -17,6 +18,8 @@ import qualified GHC.Stack as GHC

import Cardano.Api

import Cardano.CLI.Shelley.Run.Transaction

import qualified Hedgehog as H
import qualified Hedgehog.Extras.Test.Process as H
import Hedgehog.Internal.Property (Diff, MonadTest, liftTest, mkTest)
Expand Down Expand Up @@ -64,6 +67,17 @@ checkTextEnvelopeFormat tve reference created = do
equivalence refType createdType
equivalence refTitle createdTitle

checkTxCddlFormat
:: (MonadTest m, MonadIO m, HasCallStack)
=> FilePath -- ^ Reference/golden file
-> FilePath -- ^ Newly created file
-> m ()
checkTxCddlFormat reference created = do
r <- liftIO $ readCddlTx reference
c <- liftIO $ readCddlTx created
r H.=== c


--------------------------------------------------------------------------------
-- Helpers, Error rendering & Clean up
--------------------------------------------------------------------------------
Expand Down
5 changes: 5 additions & 0 deletions cardano-cli/test/data/golden/alonzo/tx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "Witnessed Tx AlonzoEra",
"description": "Ledger Cddl Format",
"cborHex": "84a30081825820f62cd7bc15d8c6d2c8519fb8d13c57c0157ab6bab50af62bc63706feb966393d0001828258390076619da7963eaa588252c45e960667a4647eed69135f51f5a10f2888d2c20ac07056fc8899c47d825cefd9dcf5efba150236e043262e2b431b0000011764f7be0782581d604088059bbeb6add02eecd0c6a2a52c06910f2a6b4ba0029e9fe6ed131a00989680021a00028b79a100818258208dc60533b5dfa60a530955a696323a2ef4f14e8bc95a8f84cf6c441fea4234275840043220211a264209f6e61903e60e80093b7b3a08e8bc5fe8f8707635acd69b6e0589e61aea544b87729983955decded90a59f9701042bebe57f2afba7c94fc02f5f6"
}
6 changes: 3 additions & 3 deletions cardano-cli/test/data/golden/shelley/tx/tx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"type": "TxSignedShelley",
"description": "",
"cborHex": "83a4008182582091999ea21177b33ebe6b8690724a0c026d410a11ad7521caa350abdafa5394c300018182581d615dbe1e2117641f8d618034b801a870ca731ce758c3bedd5c7e4429c11a05f5e100021a000f4240031a0007a120a100818258202f5c56d5d354205130674e519c4abb18f14e36c86a03811c8a3b4ea4786702b3584068f460e722d1423c7d4ce286e9c1d43a3a70ac4f42ca4dd782fe03cb41ce23e91ec97b9b3d467ff435b8b4efc75c466a10fb9c2b76c42711f58a7a049416fb02f6"
"type": "Witnessed Tx AlonzoEra",
"description": "Ledger Cddl Format",
"cborHex": "84a30081825820256fe89fc62e787e470c54e6d17390309ed6ceed27f11b9f873dd55802a6272f0001828258390076619da7963eaa588252c45e960667a4647eed69135f51f5a10f2888d2c20ac07056fc8899c47d825cefd9dcf5efba150236e043262e2b431b0000011764f7be0782581d604088059bbeb6add02eecd0c6a2a52c06910f2a6b4ba0029e9fe6ed131a00989680021a00028b79a100818258206cf237caaf3218032c28fe858aaeeb835a8b315f45ec273970fb64b3dd1cc7dc58402271384dc35dc926468747e901aba167d03f17d1f01374f8631210af32b2dfb601d4949bd9e03158473493425a31db653a3de8460510c5da92ff01022eecc801f5f6"
}
6 changes: 3 additions & 3 deletions cardano-cli/test/data/golden/shelley/tx/txbody
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"type": "TxBodyMary",
"description": "",
"cborHex": "83a4008182582091999ea21177b33ebe6b8690724a0c026d410a11ad7521caa350abdafa5394c300018182581d615dbe1e2117641f8d618034b801a870ca731ce758c3bedd5c7e4429c11a05f5e100021a000f4240031a0007a1209ffff6"
"type": "Unwitnessed Tx MaryEra",
"description": "Ledger Cddl Format",
"cborHex": "83a4008182582091999ea21177b33ebe6b8690724a0c026d410a11ad7521caa350abdafa5394c300018182581d615dbe1e2117641f8d618034b801a870ca731ce758c3bedd5c7e4429c11a05f5e100021a000f4240031a0007a120a0f6"
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
"type": "TxWitness MaryEra",
"description": "",
"cborHex": "82008258208dc60533b5dfa60a530955a696323a2ef4f14e8bc95a8f84cf6c441fea42342758409f69aa4bf41acf78deaffb002c4f36157f3bad7c434a19b1ffe7e7df5f98f629e58503a762e83c1f60e54c9eb7eef9885b16c2b42ce1336914f2df9aa63b1407"
}
}

0 comments on commit 6207b62

Please sign in to comment.