|
| 1 | +{-# LANGUAGE ScopedTypeVariables #-} |
| 2 | + |
1 | 3 | module Test.Cli.Transaction.Build where
|
2 | 4 |
|
| 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 |
3 | 29 | 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 |
4 | 33 | import System.Exit (ExitCode (..))
|
5 | 34 | import System.FilePath ((</>))
|
6 | 35 |
|
| 36 | +import Test.Gen.Cardano.Api.Typed (genShelleyHash) |
| 37 | + |
7 | 38 | import Test.Cardano.CLI.Util
|
8 | 39 |
|
9 | 40 | import Hedgehog
|
10 | 41 | 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 |
12 | 45 |
|
13 | 46 | inputDir :: FilePath
|
14 | 47 | inputDir = "test/cardano-cli-test/files/input/shelley/transaction"
|
@@ -93,3 +126,86 @@ hprop_conway_transaction_build_raw_negative_bits_positive_total_txout = watchdog
|
93 | 126 | , "--out-file"
|
94 | 127 | , outFile
|
95 | 128 | ]
|
| 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 |
0 commit comments