Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,4 @@ if impl (ghc >= 9.12)
-- IMPORTANT
-- Do NOT add more source-repository-package stanzas here unless they are strictly
-- temporary! Please read the section in CONTRIBUTING about updating dependencies.

4 changes: 4 additions & 0 deletions cardano-testnet/cardano-testnet.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,12 @@ test-suite cardano-testnet-test
Cardano.Testnet.Test.Cli.QuerySlotNumber
Cardano.Testnet.Test.Cli.Plutus.Scripts
Cardano.Testnet.Test.Cli.Plutus.CostCalculation
Cardano.Testnet.Test.Cli.Plutus.MultiAssetReturnCollateral
Cardano.Testnet.Test.Cli.Scripts.Simple.CostCalculation
Cardano.Testnet.Test.Cli.Scripts.Simple.Mint
Cardano.Testnet.Test.Cli.StakeSnapshot
Cardano.Testnet.Test.Cli.Transaction
Cardano.Testnet.Test.Cli.Transaction.BuildEstimate
Cardano.Testnet.Test.Cli.Transaction.RegisterDeregisterStakeAddress
Cardano.Testnet.Test.DumpConfig
Cardano.Testnet.Test.FoldEpochState
Expand Down
23 changes: 22 additions & 1 deletion cardano-testnet/src/Testnet/Components/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ module Testnet.Components.Query
, findAllUtxos
, findUtxosWithAddress
, findLargestUtxoWithAddress
, findLargestMultiAssetUtxoWithAddress
, findLargestUtxoForPaymentKey

, checkDRepsNumber
Expand All @@ -47,7 +48,6 @@ import Cardano.Api as Api hiding (txId)
import Cardano.Api.Ledger (Credential, DRepState, EpochInterval (..), KeyRole (DRepRole))
import qualified Cardano.Api.Ledger as L
import qualified Cardano.Api.UTxO as Utxo

import Cardano.Ledger.Api (ConwayGovState)
import qualified Cardano.Ledger.Api as L
import qualified Cardano.Ledger.Api.State.Query as SQ
Expand Down Expand Up @@ -350,6 +350,27 @@ findLargestUtxoWithAddress epochStateView sbe address = withFrozenCallStack $ do
. listToMaybe
$ sortOn (\(_, TxOut _ txOutValue _ _) -> Down $ txOutValueToLovelace txOutValue) utxos

-- | Retrieve the largest utxo with a multi-asset
findLargestMultiAssetUtxoWithAddress
:: HasCallStack
=> MonadAssertion m
=> MonadIO m
=> MonadTest m
=> EpochStateView
-> ShelleyBasedEra era
-> Text -- ^ Address
-> m (Maybe (TxIn, TxOut CtxUTxO era))
findLargestMultiAssetUtxoWithAddress epochStateView sbe address = withFrozenCallStack $ do
utxos <- toList <$> findUtxosWithAddress epochStateView sbe address
let sortedUTxOs = sortOn (\(_, TxOut _ txOutValue _ _) -> Down $ txOutValueToLovelace txOutValue) utxos
utxosWithMas = filter (\(_,TxOut _ txOutValue _ _) -> isMultiAssetPresent txOutValue) sortedUTxOs
pure $ listToMaybe utxosWithMas

isMultiAssetPresent :: TxOutValue era -> Bool
isMultiAssetPresent v =
Map.size (valueToPolicyAssets $ txOutValueToValue v) > 0


-- | Retrieve a largest UTxO for a payment key info - a convenience wrapper for
-- 'findLargestUtxoWithAddress'.
findLargestUtxoForPaymentKey
Expand Down
13 changes: 13 additions & 0 deletions cardano-testnet/src/Testnet/Defaults.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ module Testnet.Defaults
, defaultYamlHardforkViaConfig
, defaultMainnetTopology
, defaultUtxoKeys
, plutusV2Script
, plutusV3Script
, plutusV3SupplementalDatumScript
, plutusV2StakeScript
, simpleScript
) where

import Cardano.Api (AnyShelleyBasedEra (..), CardanoEra (..), File (..),
Expand Down Expand Up @@ -592,6 +594,17 @@ defaultUtxoKeys n =
, signingKey = File $ "utxo-keys" </> "utxo" <> show n </> "utxo.skey"
}


simpleScript :: Text -> Text
simpleScript signerRequired =
"{ \"scripts\": [ { \"keyHash\": \"" <> signerRequired <> "\", \"type\": \"sig\" } ], \"type\": \"all\" }"


plutusV2Script :: Text
plutusV2Script =
"{ \"type\": \"PlutusScriptV2\", \"description\": \"\", \"cborHex\": \"5822582001000022325333573466e1ccde5251333792945200000100111200116375a005\" }"


-- | Default plutus script that always succeeds
plutusV3Script :: Text
plutusV3Script =
Expand Down
9 changes: 8 additions & 1 deletion cardano-testnet/src/Testnet/Property/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ module Testnet.Property.Util
, integrationRetryWorkspace
, integrationWorkspace
, isLinux


, aesonObjectLookUp
, decodeEraUTxO
) where

import Cardano.Api

import qualified Data.Aeson as Aeson
import qualified Data.Aeson.KeyMap as Aeson
import Data.String
import qualified Data.Text as Text
import GHC.Stack
import qualified System.Environment as IO
import System.Info (os)
Expand Down Expand Up @@ -60,3 +64,6 @@ isLinux = os == "linux"
decodeEraUTxO :: (IsShelleyBasedEra era, MonadTest m) => ShelleyBasedEra era -> Aeson.Value -> m (UTxO era)
decodeEraUTxO _ = H.jsonErrorFail . Aeson.fromJSON

aesonObjectLookUp :: MonadTest m => Aeson.Value -> Text -> m (Maybe Aeson.Value)
aesonObjectLookUp (Aeson.Object o) k = return $ Aeson.lookup (fromString $ Text.unpack k) o
aesonObjectLookUp v _ = H.failMessage callStack $ "Expected an Aeson Object but got: " <> show v
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ hprop_kes_period_info = integrationRetryWorkspace 2 "kes-period-info" $ \tempAbs
, "--out-file", work </> "utxo-1.json"
]

utxo1Json <- H.leftFailM . H.readJsonFile $ work </> "utxo-1.json"
utxo1Json <- H.readJsonFileOk $ work </> "utxo-1.json"
UTxO utxo1 <- H.noteShowM $ decodeEraUTxO sbe utxo1Json
txin1 <- H.noteShow =<< H.headM (Map.keys utxo1)

Expand Down Expand Up @@ -174,7 +174,7 @@ hprop_kes_period_info = integrationRetryWorkspace 2 "kes-period-info" $ \tempAbs

H.cat $ work </> "utxo-2.json"

utxo2Json <- H.leftFailM . H.readJsonFile $ work </> "utxo-2.json"
utxo2Json <- H.readJsonFileOk $ work </> "utxo-2.json"
UTxO utxo2 <- H.noteShowM $ decodeEraUTxO sbe utxo2Json
txin2 <- H.noteShow =<< H.headM (Map.keys utxo2)

Expand Down Expand Up @@ -309,7 +309,7 @@ hprop_kes_period_info = integrationRetryWorkspace 2 "kes-period-info" $ \tempAbs
, "--out-file", work </> "current-tip.json"
]

tipJSON <- H.leftFailM . H.readJsonFile $ work </> "current-tip.json"
tipJSON <- H.readJsonFileOk $ work </> "current-tip.json"
tip <- H.noteShowM $ H.jsonErrorFail $ J.fromJSON @QueryTipLocalStateOutput tipJSON
currEpoch <-
case mEpoch tip of
Expand Down Expand Up @@ -337,7 +337,7 @@ hprop_kes_period_info = integrationRetryWorkspace 2 "kes-period-info" $ \tempAbs
, "--out-file", work </> "current-tip-2.json"
]

tip2JSON <- H.leftFailM . H.readJsonFile $ work </> "current-tip-2.json"
tip2JSON <- H.readJsonFileOk $ work </> "current-tip-2.json"
tip2 <- H.noteShowM $ H.jsonErrorFail $ J.fromJSON @QueryTipLocalStateOutput tip2JSON

currEpoch2 <-
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ hprop_leadershipSchedule = integrationRetryWorkspace 2 "leadership-schedule" $ \
, "--out-file", work </> "utxo-1.json"
]

utxo1Json <- H.leftFailM . H.readJsonFile $ work </> "utxo-1.json"
utxo1Json <- H.readJsonFileOk $ work </> "utxo-1.json"
UTxO utxo1 <- H.noteShowM $ decodeEraUTxO sbe utxo1Json
txin1 <- H.noteShow =<< H.headM (Map.keys utxo1)
let node1SocketPath = Api.File $ IO.sprocketSystemName node1sprocket
Expand Down Expand Up @@ -179,7 +179,7 @@ hprop_leadershipSchedule = integrationRetryWorkspace 2 "leadership-schedule" $ \

H.cat $ work </> "utxo-2.json"

utxo2Json <- H.leftFailM . H.readJsonFile $ work </> "utxo-2.json"
utxo2Json <- H.readJsonFileOk $ work </> "utxo-2.json"
UTxO utxo2 <- H.noteShowM $ decodeEraUTxO sbe utxo2Json
txin2 <- H.noteShow =<< H.headM (Map.keys utxo2)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ module Cardano.Testnet.Test.Cli.Plutus.CostCalculation
, hprop_included_plutus_cost_calculation
, hprop_included_simple_script_cost_calculation
-- | Execute tests in this module with:
-- @DISABLE_RETRIES=1 cabal run cardano-testnet-test -- -p "/Spec.hs.Spec.Ledger Events.Plutus.Cost Calc/"@
-- @DISABLE_RETRIES=1 cabal test cardano-testnet-test -- -p "/Spec.hs.Spec.Ledger Events.Plutus.Cost Calc/"@
)
where

import Cardano.Api hiding (Value)
import Cardano.Api.Experimental (Some (Some))
import Cardano.Api.Ledger (EpochInterval (..))

import Cardano.Testnet

import Prelude
import Testnet.Types

import Control.Monad (void)
import Data.Aeson (Value, encodeFile)
Expand All @@ -36,14 +36,13 @@ import qualified System.Info as SYS

import Testnet.Components.Query (findLargestUtxoForPaymentKey, getEpochStateView, getTxIx,
watchEpochStateUpdate)
import qualified Testnet.Defaults as Defaults
import Testnet.Process.Cli.Transaction (TxOutAddress (..), mkSpendOutputsOnlyTx,
retrieveTransactionId, signTx, submitTx)
import Testnet.Process.Run (execCli', mkExecConfig)
import Testnet.Process.RunIO (liftIOAnnotated)
import Testnet.Property.Util (integrationRetryWorkspace)
import Testnet.Start.Types (eraToString)
import Testnet.Types (PaymentKeyInfo (paymentKeyInfoAddr), paymentKeyInfoPair,
verificationKey)

import Hedgehog (Property)
import qualified Hedgehog as H
Expand All @@ -52,7 +51,7 @@ import qualified Hedgehog.Extras.Test.File as H
import qualified Hedgehog.Extras.Test.Golden as H
import qualified Hedgehog.Extras.Test.TestWatchdog as H

-- @DISABLE_RETRIES=1 cabal run cardano-testnet-test -- -p "/Spec.hs.Spec.Ledger Events.Plutus.Cost Calc.Ref Script/"@
-- @DISABLE_RETRIES=1 cabal test cardano-testnet-test --test-options '-p "/Ref Script/"'@
hprop_ref_plutus_cost_calculation :: Property
hprop_ref_plutus_cost_calculation = integrationRetryWorkspace 2 "ref-plutus-script" $ \tempAbsBasePath' -> H.runWithDefaultWatchdog_ $ do
H.note_ SYS.os
Expand Down Expand Up @@ -144,7 +143,16 @@ hprop_ref_plutus_cost_calculation = integrationRetryWorkspace 2 "ref-plutus-scri
refScriptUnlock <- H.createDirectoryIfMissing $ work </> "ref-script-unlock"
let unsignedUnlockTx = File $ refScriptUnlock </> "unsigned-tx.tx"
largestUTxO <- findLargestUtxoForPaymentKey epochStateView sbe wallet1

refScriptHash <- execCli' execConfig [ eraName, "transaction", "policyid", "--script-file", unFile plutusV3Script]
H.note_ $ "Reference script hash: " <> refScriptHash

void $ execCli' execConfig
[ eraName, "query", "utxo"
, "--whole-utxo"
, "--cardano-mode"
, "--out-file", work </> "utxo-1.json"
]
H.cat $ work </> "utxo-1.json"
void $
execCli'
execConfig
Expand Down Expand Up @@ -200,7 +208,7 @@ hprop_ref_plutus_cost_calculation = integrationRetryWorkspace 2 "ref-plutus-scri

H.diffVsGoldenFile output "test/cardano-testnet-test/files/calculatePlutusScriptCost.json"

-- @DISABLE_RETRIES=1 cabal run cardano-testnet-test -- -p "/Spec.hs.Spec.Ledger Events.Plutus.Cost Calc.Normal Script/"@
-- @DISABLE_RETRIES=1 cabal test cardano-testnet-test --test-options '-p "/Spec.hs.Spec.Ledger Events.Plutus.Cost Calc.Normal Script/"'@
hprop_included_plutus_cost_calculation :: Property
hprop_included_plutus_cost_calculation = integrationRetryWorkspace 2 "included-plutus-script" $ \tempAbsBasePath' -> H.runWithDefaultWatchdog_ $ do
H.note_ SYS.os
Expand Down Expand Up @@ -230,8 +238,12 @@ hprop_included_plutus_cost_calculation = integrationRetryWorkspace 2 "included-p
epochStateView <- getEpochStateView configurationFile (nodeSocketPath poolNode1)

includedScriptLockWork <- H.createDirectoryIfMissing $ work </> "included-script-lock"
plutusV3Script <-
File <$> liftIOAnnotated (makeAbsolute "test/cardano-testnet-test/files/plutus/v3/always-succeeds.plutus")

plutusScriptFp <- H.note $ work </> "always-succeeds-script.plutusV3"
H.writeFile plutusScriptFp $ Text.unpack Defaults.plutusV3Script

--_plutusV3Script <-
-- File <$> liftIOAnnotated (makeAbsolute "test/cardano-testnet-test/files/plutus/v3/always-succeeds.plutus")

let includedScriptLockAmount = 10_000_000
enoughAmountForFees = 2_000_000 -- Needs to be more than min ada
Expand All @@ -245,7 +257,7 @@ hprop_included_plutus_cost_calculation = integrationRetryWorkspace 2 "included-p
includedScriptLockWork
"tx-body"
wallet0
[(ScriptAddress plutusV3Script, includedScriptLockAmount, Nothing)]
[(ScriptAddress $ File plutusScriptFp, includedScriptLockAmount, Nothing)]
signedTxIncludedScriptLock <-
signTx
execConfig
Expand All @@ -269,15 +281,16 @@ hprop_included_plutus_cost_calculation = integrationRetryWorkspace 2 "included-p
includedScriptUnlock <- H.createDirectoryIfMissing $ work </> "included-script-unlock"
let unsignedIncludedScript = File $ includedScriptUnlock </> "unsigned-tx.tx"
newLargestUTxO <- findLargestUtxoForPaymentKey epochStateView sbe wallet1

scriptHash <- execCli' execConfig [ eraName, "transaction", "policyid", "--script-file", plutusScriptFp]
H.note_ $ "Script hash: " <> scriptHash
void $
execCli'
execConfig
[ eraName
, "transaction", "build"
, "--change-address", Text.unpack $ paymentKeyInfoAddr wallet1
, "--tx-in", prettyShow (TxIn txIdIncludedScriptLock txIxIncludedScriptLock)
, "--tx-in-script-file", unFile plutusV3Script
, "--tx-in-script-file", plutusScriptFp
, "--tx-in-redeemer-value", "42"
, "--tx-in-collateral", prettyShow newLargestUTxO
, "--tx-out", Text.unpack (paymentKeyInfoAddr wallet1) <> "+" <> show (unCoin (includedScriptLockAmount - enoughAmountForFees))
Expand Down Expand Up @@ -310,7 +323,7 @@ hprop_included_plutus_cost_calculation = integrationRetryWorkspace 2 "included-p
(unFile includedScriptCostOutput)
"test/cardano-testnet-test/files/calculatePlutusScriptCost.json"

-- @DISABLE_RETRIES=1 cabal run cardano-testnet-test -- -p "/Spec.hs.Spec.Ledger Events.Plutus.Cost Calc.Simple Script/"@
-- @DISABLE_RETRIES=1 cabal test cardano-testnet-test --test-options '-p "/Spec.hs.Spec.Ledger Events.Plutus.Cost Calc.Simple Script/"'@
hprop_included_simple_script_cost_calculation :: Property
hprop_included_simple_script_cost_calculation = integrationRetryWorkspace 2 "included-simple-script" $ \tempAbsBasePath' -> H.runWithDefaultWatchdog_ $ do
H.note_ SYS.os
Expand Down
Loading
Loading