From c90d7f6831f32cde42e5911394c2455c40ea8f75 Mon Sep 17 00:00:00 2001 From: John Ky Date: Wed, 29 Nov 2023 00:43:56 +1100 Subject: [PATCH] Transaction integration test --- cardano-testnet/cardano-testnet.cabal | 2 + .../Testnet/Test/Cli/Babbage/Transaction.hs | 137 ++++++++++++++++++ .../cardano-testnet-test.hs | 2 + 3 files changed, 141 insertions(+) create mode 100644 cardano-testnet/test/cardano-testnet-test/Cardano/Testnet/Test/Cli/Babbage/Transaction.hs diff --git a/cardano-testnet/cardano-testnet.cabal b/cardano-testnet/cardano-testnet.cabal index 0c63020eacb..c1a6bcd0acc 100644 --- a/cardano-testnet/cardano-testnet.cabal +++ b/cardano-testnet/cardano-testnet.cabal @@ -160,6 +160,7 @@ test-suite cardano-testnet-test other-modules: Cardano.Testnet.Test.Cli.Alonzo.LeadershipSchedule Cardano.Testnet.Test.Cli.Babbage.LeadershipSchedule Cardano.Testnet.Test.Cli.Babbage.StakeSnapshot + Cardano.Testnet.Test.Cli.Babbage.Transaction Cardano.Testnet.Test.Cli.Conway.StakeSnapshot Cardano.Testnet.Test.Cli.KesPeriodInfo Cardano.Testnet.Test.Cli.QuerySlotNumber @@ -182,6 +183,7 @@ test-suite cardano-testnet-test , filepath , hedgehog , hedgehog-extras + , microlens , process , tasty , text diff --git a/cardano-testnet/test/cardano-testnet-test/Cardano/Testnet/Test/Cli/Babbage/Transaction.hs b/cardano-testnet/test/cardano-testnet-test/Cardano/Testnet/Test/Cli/Babbage/Transaction.hs new file mode 100644 index 00000000000..a672f5f4a27 --- /dev/null +++ b/cardano-testnet/test/cardano-testnet-test/Cardano/Testnet/Test/Cli/Babbage/Transaction.hs @@ -0,0 +1,137 @@ +{-# LANGUAGE DisambiguateRecordFields #-} +{-# LANGUAGE NamedFieldPuns #-} +{-# LANGUAGE NumericUnderscores #-} +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE ScopedTypeVariables #-} +{-# LANGUAGE TypeApplications #-} + +{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-} +{-# LANGUAGE LambdaCase #-} + +{- HLINT ignore "Redundant id" -} +{- HLINT ignore "Redundant return" -} +{- HLINT ignore "Use head" -} +{- HLINT ignore "Use let" -} + +module Cardano.Testnet.Test.Cli.Babbage.Transaction + ( hprop_transaction + ) where + +import Cardano.Api + +import Cardano.Testnet + +import Prelude + +import Control.Monad (void) +import qualified Data.Text as Text +import System.FilePath (()) +import qualified System.Info as SYS + +import Hedgehog (Property) +import qualified Hedgehog as H +import qualified Hedgehog.Extras.Test.Base as H +import qualified Hedgehog.Extras.Test.File as H + +import qualified Cardano.Api.Ledger.Lens as A +import qualified Data.Map as Map +import Testnet.Components.SPO +import qualified Testnet.Process.Run as H +import Testnet.Process.Run +import qualified Testnet.Property.Utils as H +import Testnet.Runtime + +import qualified Cardano.Api.Ledger as L +import qualified Data.List as List +import Lens.Micro + +hprop_transaction :: Property +hprop_transaction = H.integrationRetryWorkspace 0 "babbage-transaction" $ \tempAbsBasePath' -> do + H.note_ SYS.os + conf@Conf { tempAbsPath } <- H.noteShowM $ mkConf tempAbsBasePath' + let tempAbsPath' = unTmpAbsPath tempAbsPath + work <- H.createDirectoryIfMissing $ tempAbsPath' "work" + + let + sbe = ShelleyBasedEraBabbage + era = toCardanoEra sbe + tempBaseAbsPath = makeTmpBaseAbsPath $ TmpAbsolutePath tempAbsPath' + options = cardanoDefaultTestnetOptions + { cardanoNodes = cardanoDefaultTestnetNodeOptions + , cardanoSlotLength = 0.1 + , cardanoNodeEra = AnyCardanoEra era -- TODO: We should only support the latest era and the upcoming era + } + + TestnetRuntime + { testnetMagic + , poolNodes + , wallets + } <- cardanoTestnet options conf + + poolNode1 <- H.headM poolNodes + + poolSprocket1 <- H.noteShow $ nodeSprocket $ poolRuntime poolNode1 + + execConfig <- H.mkExecConfig tempBaseAbsPath poolSprocket1 + + + txbodyFp <- H.note $ work "tx.body" + txbodySignedFp <- H.note $ work "tx.body.signed" + + void $ execCli' execConfig + [ "babbage", "query", "utxo" + , "--address", Text.unpack $ paymentKeyInfoAddr $ head wallets + , "--cardano-mode" + , "--testnet-magic", show @Int testnetMagic + , "--out-file", work "utxo-1.json" + ] + + utxo1Json <- H.leftFailM . H.readJsonFile $ work "utxo-1.json" + UTxO utxo1 <- H.noteShowM $ H.noteShowM $ decodeEraUTxO sbe utxo1Json + txin1 <- H.noteShow =<< H.headM (Map.keys utxo1) + + void $ execCli' execConfig + [ "babbage", "transaction", "build" + , "--testnet-magic", show @Int testnetMagic + , "--change-address", Text.unpack $ paymentKeyInfoAddr $ head wallets + , "--tx-in", Text.unpack $ renderTxIn txin1 + , "--tx-out", Text.unpack (paymentKeyInfoAddr (head wallets)) <> "+" <> show @Int 5_000_001 + , "--out-file", txbodyFp + ] + + void $ execCli' execConfig + [ "babbage", "transaction", "sign" + , "--testnet-magic", show @Int testnetMagic + , "--tx-body-file", txbodyFp + , "--signing-key-file", paymentSKey $ paymentKeyInfoPair $ wallets !! 0 + , "--out-file", txbodySignedFp + ] + + void $ execCli' execConfig + [ "babbage", "transaction", "submit" + , "--testnet-magic", show @Int testnetMagic + , "--tx-file", txbodySignedFp + ] + + H.byDurationM 1 10 "Expected UTxO found" $ do + void $ execCli' execConfig + [ "babbage", "query", "utxo" + , "--address", Text.unpack $ paymentKeyInfoAddr $ head wallets + , "--cardano-mode" + , "--testnet-magic", show @Int testnetMagic + , "--out-file", work "utxo-2.json" + ] + + utxo2Json <- H.leftFailM . H.readJsonFile $ work "utxo-2.json" + UTxO utxo2 <- H.noteShowM $ H.noteShowM $ decodeEraUTxO sbe utxo2Json + txouts2 <- H.noteShow $ L.unCoin . txOutValueLovelace . txOutValue . snd <$> Map.toList utxo2 + + H.assert $ 5_000_001 `List.elem` txouts2 + +txOutValue :: TxOut ctx era -> TxOutValue era +txOutValue (TxOut _ v _ _) = v + +txOutValueLovelace ::TxOutValue era -> L.Coin +txOutValueLovelace = \case + TxOutValueShelleyBased sbe v -> v ^. A.adaAssetL sbe + TxOutValueByron (Lovelace v) -> L.Coin v diff --git a/cardano-testnet/test/cardano-testnet-test/cardano-testnet-test.hs b/cardano-testnet/test/cardano-testnet-test/cardano-testnet-test.hs index 86eda6adebf..7b234f26d71 100644 --- a/cardano-testnet/test/cardano-testnet-test/cardano-testnet-test.hs +++ b/cardano-testnet/test/cardano-testnet-test/cardano-testnet-test.hs @@ -7,6 +7,7 @@ module Main import qualified Cardano.Crypto.Init as Crypto import qualified Cardano.Testnet.Test.Cli.Babbage.LeadershipSchedule import qualified Cardano.Testnet.Test.Cli.Babbage.StakeSnapshot +import qualified Cardano.Testnet.Test.Cli.Babbage.Transaction import qualified Cardano.Testnet.Test.Cli.KesPeriodInfo import qualified Cardano.Testnet.Test.Cli.QuerySlotNumber import qualified Cardano.Testnet.Test.FoldBlocks @@ -37,6 +38,7 @@ tests = pure $ T.testGroup "test/Spec.hs" -- this should remedy. Double check and make sure we have re-enabled it and remove this comment. [ H.ignoreOnMacAndWindows "leadership-schedule" Cardano.Testnet.Test.Cli.Babbage.LeadershipSchedule.hprop_leadershipSchedule -- FAILS , H.ignoreOnWindows "stake-snapshot" Cardano.Testnet.Test.Cli.Babbage.StakeSnapshot.hprop_stakeSnapshot + , H.ignoreOnWindows "transaction" Cardano.Testnet.Test.Cli.Babbage.Transaction.hprop_transaction ] -- TODO: Conway - Re-enable when create-staked is working in conway again --, T.testGroup "Conway"