Skip to content

Commit 885e91f

Browse files
committed
Add offline test for calculate-plutus-script-cost command
1 parent 1e6ed26 commit 885e91f

File tree

5 files changed

+836
-1
lines changed

5 files changed

+836
-1
lines changed

cardano-cli/cardano-cli.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,8 @@ test-suite cardano-cli-test
353353
filepath,
354354
hedgehog,
355355
hedgehog-extras,
356+
lens,
357+
lens-aeson,
356358
mmorph,
357359
monad-control,
358360
parsec,

cardano-cli/test/cardano-cli-test/Test/Cli/Transaction/Build.hs

Lines changed: 117 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,47 @@
1+
{-# LANGUAGE ScopedTypeVariables #-}
2+
13
module Test.Cli.Transaction.Build where
24

5+
import Cardano.Api.Ledger (hashToBytes)
6+
import Cardano.Api.Shelley
7+
( AddressAny (AddressShelley)
8+
, AddressInEra (AddressInEra)
9+
, AddressTypeInEra (ShelleyAddressInEra)
10+
, AsType (AsAddressAny)
11+
, ReferenceScript (ReferenceScriptNone)
12+
, ShelleyBasedEra (ShelleyBasedEraConway)
13+
, TxId (TxId)
14+
, TxIn (TxIn)
15+
, TxIx (TxIx)
16+
, TxOut (TxOut)
17+
, TxOutDatum (TxOutDatumNone)
18+
, UTxO (UTxO)
19+
, deserialiseAddress
20+
, liftIO
21+
, lovelaceToTxOutValue
22+
, writeFileJSON
23+
)
24+
25+
import Control.Lens ((^?))
26+
import Data.Aeson qualified as Aeson
27+
import Data.Aeson.Lens qualified as Aeson
28+
import Data.ByteString.Base16 qualified as Base16
329
import Data.List (isInfixOf)
30+
import Data.Map qualified as Map
31+
import Data.Text qualified as Text
32+
import Data.Text.Encoding qualified as Text
433
import System.Exit (ExitCode (..))
534
import System.FilePath ((</>))
635

36+
import Test.Gen.Cardano.Api.Typed (genShelleyHash)
37+
738
import Test.Cardano.CLI.Util
839

940
import Hedgehog
1041
import Hedgehog qualified as H
11-
import Hedgehog.Extras.Test.Base qualified as H
42+
import Hedgehog.Extras qualified as H
43+
import Hedgehog.Gen qualified as Gen
44+
import Hedgehog.Range qualified as Range
1245

1346
inputDir :: FilePath
1447
inputDir = "test/cardano-cli-test/files/input/shelley/transaction"
@@ -93,3 +126,86 @@ hprop_conway_transaction_build_raw_negative_bits_positive_total_txout = watchdog
93126
, "--out-file"
94127
, outFile
95128
]
129+
130+
-- | This tests calculating the cost of a plutus script offline.
131+
-- @cabal test cardano-cli-test --test-options '-p "/conway calculate plutus script cost offline/"'@
132+
hprop_conway_calculate_plutus_script_cost_offline :: Property
133+
hprop_conway_calculate_plutus_script_cost_offline = propertyOnce $ H.moduleWorkspace "tmp" $ \tempDir -> do
134+
randomTxIdHash <- forAll genShelleyHash
135+
txIx <- forAll $ Gen.integral (Range.linear 0 10)
136+
AddressShelley scriptAddr <-
137+
H.evalMaybe $
138+
deserialiseAddress AsAddressAny "addr_test1wqvxuvh64q9zdqgrjt76d42eclk5wgdxtnsun4808cwg0dqxy2mj0"
139+
140+
unsignedTx <- H.noteTempFile tempDir "unsigned.tx"
141+
142+
H.noteShowM_ $
143+
execCardanoCLI
144+
[ "conway"
145+
, "transaction"
146+
, "build-raw"
147+
, "--tx-in"
148+
, Text.unpack (Text.decodeLatin1 (Base16.encode (hashToBytes randomTxIdHash))) <> "#" <> show txIx
149+
, "--tx-in-script-file"
150+
, "test/cardano-cli-test/files/input/plutus/v3-always-succeeds.plutus"
151+
, "--tx-in-redeemer-value"
152+
, "42"
153+
, "--tx-in-execution-units"
154+
, "(30,40)"
155+
, "--tx-out"
156+
, "addr_test1wqvxuvh64q9zdqgrjt76d42eclk5wgdxtnsun4808cwg0dqxy2mj0+1000000"
157+
, "--fee"
158+
, "200000"
159+
, "--out-file"
160+
, unsignedTx
161+
]
162+
163+
costOutFile <- H.noteTempFile tempDir "cost-calculation.json"
164+
165+
utxoFile <- H.noteTempFile tempDir "utxo.json"
166+
H.evalEitherM $
167+
liftIO $
168+
writeFileJSON
169+
utxoFile
170+
( UTxO $
171+
Map.singleton
172+
(TxIn (TxId randomTxIdHash) (TxIx txIx))
173+
( TxOut
174+
(AddressInEra (ShelleyAddressInEra ShelleyBasedEraConway) scriptAddr)
175+
(lovelaceToTxOutValue ShelleyBasedEraConway 1200000)
176+
TxOutDatumNone
177+
ReferenceScriptNone
178+
)
179+
)
180+
181+
H.noteShowM_ $
182+
execCardanoCLI
183+
[ "conway"
184+
, "transaction"
185+
, "calculate-plutus-script-cost"
186+
, "--start-time-posix"
187+
, "1666656000"
188+
, "--protocol-params-file"
189+
, "test/cardano-cli-test/files/input/protocol-params-preview.json"
190+
, "--utxo-file"
191+
, utxoFile
192+
, "--unsafe-extend-safe-zone"
193+
, "--era-history-file"
194+
, "test/cardano-cli-test/files/input/preview-era-history.json"
195+
, "--tx-file"
196+
, unsignedTx
197+
, "--out-file"
198+
, costOutFile
199+
]
200+
201+
json :: Aeson.Value <- H.evalEitherM $ H.readJsonFile costOutFile
202+
203+
lovelaceCost <- H.evalMaybe $ json ^? Aeson.nth 0 . Aeson.key "lovelaceCost" . Aeson._Number
204+
lovelaceCost === 34
205+
206+
executionUnits <- H.evalMaybe $ json ^? Aeson.nth 0 . Aeson.key "executionUnits"
207+
memory <- H.evalMaybe $ executionUnits ^? Aeson.key "memory" . Aeson._Number
208+
memory === 500
209+
210+
steps <- H.evalMaybe $ executionUnits ^? Aeson.key "steps" . Aeson._Number
211+
steps === 64100
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"type": "PlutusScriptV3",
3+
"description": "Always succeeds",
4+
"cborHex": "46450101002499"
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"type": "EraHistory",
3+
"description": "",
4+
"cborHex": "9f838300000083000000841910e0194e2083001903608100190360838300000083000000841a000151801903e883001965408100196540838300000083000000841a000151801903e883001965408100196540838300000083000000841a000151801903e8830019654081001965408383000000831b0398dd06d5c800001a0003f48003841a000151801903e88300196540810019654083831b0398dd06d5c800001a0003f4800383c2490306949515279000001a0353a900190286841a000151801903e8830019654081001965408383c2490306949515279000001a0353a90019028683c249040992bf0147d000001a04706d0019035e841a000151801903e883001965408100196540ff"
5+
}

0 commit comments

Comments
 (0)