-
Notifications
You must be signed in to change notification settings - Fork 721
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test the
calculate-plutus-script-cost
command against a reference s…
…cript
- Loading branch information
Showing
5 changed files
with
166 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 118 additions & 13 deletions
131
cardano-testnet/test/cardano-testnet-test/Cardano/Testnet/Test/Cli/PlutusCostCalculation.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,162 @@ | ||
{-# LANGUAGE NamedFieldPuns #-} | ||
{-# LANGUAGE NumericUnderscores #-} | ||
|
||
module Cardano.Testnet.Test.Cli.PlutusCostCalculation ( | ||
hprop_plutus_cost_calculation, | ||
-- | Execute tests in this module with: | ||
-- @DISABLE_RETRIES=1 cabal run cardano-testnet-test -- -p "/Spec.hs.Spec.CLI.included plutus/"@ | ||
-- @DISABLE_RETRIES=1 cabal run cardano-testnet-test -- -p "/Spec.hs.Spec.CLI.plutus cost calc/"@ | ||
) where | ||
|
||
import Cardano.Api | ||
import Cardano.Api (AnyCardanoEra (AnyCardanoEra), | ||
AnyShelleyBasedEra (AnyShelleyBasedEra), File (File), MonadIO (liftIO), | ||
ShelleyBasedEra (ShelleyBasedEraConway), ToCardanoEra (toCardanoEra), renderTxIn, | ||
unFile) | ||
import Cardano.Api.Experimental (Some (Some)) | ||
import Cardano.Api.Ledger (EpochInterval (EpochInterval), unCoin) | ||
|
||
import Cardano.Testnet | ||
|
||
import Prelude | ||
|
||
import Data.Default.Class | ||
import Control.Monad (void) | ||
import Data.Default.Class (Default (def)) | ||
import qualified Data.Text as Text | ||
import System.Directory (makeAbsolute) | ||
import System.FilePath ((</>)) | ||
import qualified System.Info as SYS | ||
|
||
import Testnet.Components.Query (getEpochStateView) | ||
import Testnet.Process.Run (mkExecConfig) | ||
import Testnet.Components.Query (findLargestUtxoForPaymentKey, getEpochStateView, getTxIx, | ||
watchEpochStateUpdate) | ||
import Testnet.Process.Cli.Transaction (TxOutAddress (..), mkSpendOutputsOnlyTx, | ||
retrieveTransactionId, signTx, submitTx) | ||
import Testnet.Process.Run (execCli', mkExecConfig) | ||
import Testnet.Property.Util (integrationRetryWorkspace) | ||
import Testnet.Start.Types (eraToString) | ||
import Testnet.Types (PaymentKeyInfo (paymentKeyInfoAddr), paymentKeyInfoPair) | ||
|
||
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 Hedgehog.Extras.Test.Golden as H | ||
import qualified Hedgehog.Extras.Test.TestWatchdog as H | ||
|
||
hprop_plutus_cost_calculation :: Property | ||
hprop_plutus_cost_calculation = integrationRetryWorkspace 2 "included plutus" $ \tempAbsBasePath' -> H.runWithDefaultWatchdog_ $ do | ||
hprop_plutus_cost_calculation = integrationRetryWorkspace 2 "reference plutus script" $ \tempAbsBasePath' -> H.runWithDefaultWatchdog_ $ do | ||
H.note_ SYS.os | ||
conf@Conf{tempAbsPath} <- mkConf tempAbsBasePath' | ||
let tempAbsPath' = unTmpAbsPath tempAbsPath | ||
_work <- H.createDirectoryIfMissing $ tempAbsPath' </> "work" | ||
work <- H.createDirectoryIfMissing $ tempAbsPath' </> "work" | ||
|
||
let | ||
sbe = ShelleyBasedEraConway | ||
_txEra = AsConwayEra | ||
era = toCardanoEra sbe | ||
_cEra = AnyCardanoEra era | ||
cEra = AnyCardanoEra era | ||
eraName = eraToString era | ||
tempBaseAbsPath = makeTmpBaseAbsPath $ TmpAbsolutePath tempAbsPath' | ||
options = def{cardanoNodeEra = AnyShelleyBasedEra sbe} | ||
|
||
TestnetRuntime | ||
{ configurationFile | ||
, testnetMagic | ||
, testnetNodes | ||
, wallets = _wallet0 : _ | ||
, wallets = wallet0 : wallet1 : _ | ||
} <- | ||
cardanoTestnetDefault options def conf | ||
|
||
poolNode1 <- H.headM testnetNodes | ||
poolSprocket1 <- H.noteShow $ nodeSprocket poolNode1 | ||
_execConfig <- mkExecConfig tempBaseAbsPath poolSprocket1 testnetMagic | ||
_epochStateView <- getEpochStateView configurationFile (nodeSocketPath poolNode1) | ||
execConfig <- mkExecConfig tempBaseAbsPath poolSprocket1 testnetMagic | ||
epochStateView <- getEpochStateView configurationFile (nodeSocketPath poolNode1) | ||
|
||
H.failure | ||
refScriptSizeWork <- H.createDirectoryIfMissing $ work </> "ref-script-publish" | ||
plutusV3Script <- File <$> liftIO (makeAbsolute "test/cardano-testnet-test/files/plutus/v3/always-succeeds.plutus") | ||
|
||
let scriptPublishUTxOAmount = 10_000_000 | ||
|
||
-- Submit a transaction to publish the reference script | ||
txBodyPublishRefScript <- | ||
mkSpendOutputsOnlyTx | ||
execConfig | ||
epochStateView | ||
sbe | ||
refScriptSizeWork | ||
"tx-body" | ||
wallet0 | ||
[(ReferenceScriptAddress plutusV3Script, scriptPublishUTxOAmount)] | ||
signedTxPublishRefScript <- signTx execConfig cEra refScriptSizeWork "signed-tx" txBodyPublishRefScript [Some $ paymentKeyInfoPair wallet0] | ||
submitTx execConfig cEra signedTxPublishRefScript | ||
|
||
-- Wait until transaction is on chain and obtain transaction identifier | ||
txIdPublishRefScript <- retrieveTransactionId execConfig signedTxPublishRefScript | ||
txIxPublishRefScript <- H.evalMaybeM $ watchEpochStateUpdate epochStateView (EpochInterval 2) (getTxIx sbe txIdPublishRefScript scriptPublishUTxOAmount) | ||
|
||
-- Submit a transaction to lock money in the reference script | ||
refScriptLock <- H.createDirectoryIfMissing $ work </> "ref-script-lock" | ||
|
||
let transferAmount = 20_000_000 | ||
enoughAmountForFees = 2_000_000 -- Needs to be more than min ada | ||
txBodyLock <- | ||
mkSpendOutputsOnlyTx | ||
execConfig | ||
epochStateView | ||
sbe | ||
refScriptLock | ||
"tx-body" | ||
wallet0 | ||
[(ReferenceScriptAddress plutusV3Script, transferAmount)] | ||
signedTxLock <- signTx execConfig cEra refScriptLock "signed-tx" txBodyLock [Some $ paymentKeyInfoPair wallet0] | ||
submitTx execConfig cEra signedTxLock | ||
|
||
-- Wait until transaction is on chain and obtain transaction identifier | ||
txIdLock <- retrieveTransactionId execConfig signedTxLock | ||
txIxLock <- H.evalMaybeM $ watchEpochStateUpdate epochStateView (EpochInterval 2) (getTxIx sbe txIdLock transferAmount) | ||
|
||
-- Create transaction that uses reference script | ||
refScriptUnlock <- H.createDirectoryIfMissing $ work </> "ref-script-unlock" | ||
let unsignedUnlockTx = File $ refScriptUnlock </> "unsigned-tx.tx" | ||
largestUTxO <- findLargestUtxoForPaymentKey epochStateView sbe wallet1 | ||
|
||
void $ | ||
execCli' | ||
execConfig | ||
[ eraName | ||
, "transaction" | ||
, "build" | ||
, "--change-address" | ||
, Text.unpack $ paymentKeyInfoAddr wallet1 | ||
, "--tx-in" | ||
, txIdLock <> "#" <> show txIxLock | ||
, "--spending-reference-tx-in-inline-datum-present" | ||
, "--spending-tx-in-reference" | ||
, txIdPublishRefScript <> "#" <> show txIxPublishRefScript | ||
, "--spending-plutus-script-v3" | ||
, "--spending-reference-tx-in-redeemer-value" | ||
, "42" | ||
, "--tx-in-collateral" | ||
, Text.unpack $ renderTxIn largestUTxO | ||
, "--tx-out" | ||
, Text.unpack (paymentKeyInfoAddr wallet1) <> "+" <> show (unCoin (transferAmount - enoughAmountForFees)) | ||
, "--out-file" | ||
, unFile unsignedUnlockTx | ||
] | ||
|
||
signedUnlockTx <- signTx execConfig cEra refScriptUnlock "signed-tx" unsignedUnlockTx [Some $ paymentKeyInfoPair wallet1] | ||
|
||
submitTx execConfig cEra signedUnlockTx | ||
|
||
-- Calculate cost of the transaction | ||
let txCostOutput = File $ refScriptUnlock </> "unsigned-tx.tx" | ||
H.noteM_ $ | ||
execCli' | ||
execConfig | ||
[ eraName | ||
, "transaction" | ||
, "calculate-plutus-script-cost" | ||
, "--tx-file" | ||
, unFile signedUnlockTx | ||
, "--out-file" | ||
, unFile txCostOutput | ||
] | ||
|
||
H.diffFileVsGoldenFile (unFile txCostOutput) "test/cardano-testnet-test/files/calculatePlutusScriptCost1.json" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
cardano-testnet/test/cardano-testnet-test/files/calculatePlutusScriptCost1.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[ | ||
{ | ||
"executionUnits": { | ||
"memory": 500, | ||
"steps": 64100 | ||
}, | ||
"lovelaceCost": 34, | ||
"scriptHash": "186e32faa80a26810392fda6d559c7ed4721a65ce1c9d4ef3e1c87b4" | ||
} | ||
] |
6 changes: 3 additions & 3 deletions
6
cardano-testnet/test/cardano-testnet-test/files/plutus/v3/always-succeeds.plutus
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"type": "PlutusScriptV3", | ||
"description": "", | ||
"cborHex": "484701010022280001" | ||
} | ||
"description": "Always succeeds", | ||
"cborHex": "46450101002499" | ||
} |