Skip to content

Commit

Permalink
Merge pull request IntersectMBO#5552 from input-output-hk/newhoggy/tr…
Browse files Browse the repository at this point in the history
…ansaction-integration-test

Transaction integration test
  • Loading branch information
newhoggy authored Nov 29, 2023
2 parents 3384053 + c90d7f6 commit 1be7915
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cardano-testnet/cardano-testnet.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -182,6 +183,7 @@ test-suite cardano-testnet-test
, filepath
, hedgehog
, hedgehog-extras
, microlens
, process
, tasty
, text
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 1be7915

Please sign in to comment.