|
| 1 | +{-# LANGUAGE NamedFieldPuns #-} |
| 2 | +{-# LANGUAGE OverloadedStrings #-} |
| 3 | +{-# LANGUAGE ScopedTypeVariables #-} |
| 4 | +{-# LANGUAGE TypeApplications #-} |
| 5 | + |
| 6 | +-- | Smoke tests for @transaction build@ with a @--withdrawal@ flag, |
| 7 | +-- covering both key-witnessed and Plutus-script-witnessed withdrawals. |
| 8 | +-- Loosely related to https://github.com/IntersectMBO/cardano-cli/issues/965. |
| 9 | +module Cardano.Testnet.Test.Cli.Transaction.WithdrawalReward |
| 10 | + ( hprop_tx_withdrawal_reward |
| 11 | + , hprop_tx_withdrawal_reward_plutus_v3 |
| 12 | + ) where |
| 13 | + |
| 14 | +import Cardano.Api as Api |
| 15 | +import qualified Cardano.Api.Ledger as L |
| 16 | + |
| 17 | +import Cardano.Testnet |
| 18 | + |
| 19 | +import Prelude |
| 20 | + |
| 21 | +import Control.Monad (void) |
| 22 | +import Data.Default.Class |
| 23 | +import qualified Data.Text as Text |
| 24 | +import System.Exit (ExitCode (..)) |
| 25 | +import System.FilePath ((</>)) |
| 26 | + |
| 27 | +import Testnet.Components.Configuration |
| 28 | +import Testnet.Components.Query |
| 29 | +import Testnet.Defaults (plutusV3Script) |
| 30 | +import Testnet.Process.Run (execCli, execCliAny, mkExecConfig) |
| 31 | +import Testnet.Property.Util (integrationRetryWorkspace) |
| 32 | +import Testnet.Start.Types |
| 33 | +import Testnet.Types |
| 34 | + |
| 35 | +import Hedgehog |
| 36 | +import qualified Hedgehog as H |
| 37 | +import qualified Hedgehog.Extras as H |
| 38 | + |
| 39 | + |
| 40 | +-- | Smoke test: @transaction build@ builds a transaction whose output |
| 41 | +-- exceeds the input, with a @--withdrawal@ covering the shortfall |
| 42 | +-- (@output = 2 * input@, @withdrawal = 2 * input@). The auto-balancer |
| 43 | +-- sums the withdrawal into consumed value, producing |
| 44 | +-- @change = inputs + withdrawal - outputs - fee = input - fee@ (positive). |
| 45 | +-- |
| 46 | +-- The withdrawal amount is fictional; the CLI does not validate it against |
| 47 | +-- on-chain reward balances at build time, and the test only exercises the |
| 48 | +-- build step. |
| 49 | +-- |
| 50 | +-- Execute me with: |
| 51 | +-- @DISABLE_RETRIES=1 cabal test cardano-testnet-test --test-options '-p "/transaction build with withdrawal/"'@ |
| 52 | +hprop_tx_withdrawal_reward :: Property |
| 53 | +hprop_tx_withdrawal_reward = integrationRetryWorkspace 2 "tx-withdrawal-reward" $ \tempAbsBasePath' -> H.runWithDefaultWatchdog_ $ do |
| 54 | + conf@Conf { tempAbsPath } <- mkConf tempAbsBasePath' |
| 55 | + let tempAbsPath' = unTmpAbsPath tempAbsPath |
| 56 | + tempBaseAbsPath = makeTmpBaseAbsPath tempAbsPath |
| 57 | + |
| 58 | + work <- H.createDirectoryIfMissing $ tempAbsPath' </> "work" |
| 59 | + |
| 60 | + let ceo = ConwayEraOnwardsConway |
| 61 | + sbe = convert ceo |
| 62 | + eraName = eraToString sbe |
| 63 | + fastTestnetOptions = def { cardanoNodeEra = AnyShelleyBasedEra sbe } |
| 64 | + shelleyOptions = def { genesisEpochLength = 100 } |
| 65 | + |
| 66 | + TestnetRuntime |
| 67 | + { testnetMagic |
| 68 | + , testnetNodes |
| 69 | + , wallets = wallet0:_ |
| 70 | + , configurationFile |
| 71 | + } |
| 72 | + <- createAndRunTestnet fastTestnetOptions shelleyOptions conf |
| 73 | + |
| 74 | + node <- H.headM testnetNodes |
| 75 | + poolSprocket1 <- H.noteShow $ nodeSprocket node |
| 76 | + execConfig <- mkExecConfig tempBaseAbsPath poolSprocket1 testnetMagic |
| 77 | + let socketPath = nodeSocketPath node |
| 78 | + |
| 79 | + epochStateView <- getEpochStateView configurationFile socketPath |
| 80 | + |
| 81 | + let delegatorStakeVKeyFp = tempAbsPath' </> "stake-delegators" </> "delegator1" </> "staking.vkey" |
| 82 | + |
| 83 | + delegatorStakeAddress <- filter (/= '\n') <$> |
| 84 | + execCli |
| 85 | + [ "latest", "stake-address", "build" |
| 86 | + , "--stake-verification-key-file", delegatorStakeVKeyFp |
| 87 | + , "--testnet-magic", show @Int testnetMagic |
| 88 | + ] |
| 89 | + H.note_ $ "Delegator stake address: " <> delegatorStakeAddress |
| 90 | + |
| 91 | + (txinForWithdrawal, TxOut _ txinValue _ _) <- H.nothingFailM $ |
| 92 | + findLargestUtxoWithAddress epochStateView sbe $ paymentKeyInfoAddr wallet0 |
| 93 | + let L.Coin inputLovelace = txOutValueToLovelace txinValue |
| 94 | + H.note_ $ "Input UTxO: " <> show txinForWithdrawal <> " = " <> show inputLovelace <> " lovelace" |
| 95 | + |
| 96 | + let withdrawalLovelace = inputLovelace * 2 |
| 97 | + outputLovelace = withdrawalLovelace |
| 98 | + |
| 99 | + let withdrawalTxBodyFp = work </> "withdrawal.txbody" |
| 100 | + |
| 101 | + (exitCode, _stdout, stderr) <- execCliAny execConfig |
| 102 | + [ eraName, "transaction", "build" |
| 103 | + , "--change-address", Text.unpack $ paymentKeyInfoAddr wallet0 |
| 104 | + , "--tx-in", Text.unpack $ renderTxIn txinForWithdrawal |
| 105 | + , "--tx-out", Text.unpack (paymentKeyInfoAddr wallet0) <> "+" <> show outputLovelace |
| 106 | + , "--withdrawal", delegatorStakeAddress <> "+" <> show withdrawalLovelace |
| 107 | + , "--witness-override", show @Int 2 |
| 108 | + , "--out-file", withdrawalTxBodyFp |
| 109 | + ] |
| 110 | + H.note_ stderr |
| 111 | + |
| 112 | + exitCode H.=== ExitSuccess |
| 113 | + |
| 114 | + -- Verify the transaction body was written and can be read back |
| 115 | + void $ H.readFile withdrawalTxBodyFp |
| 116 | + |
| 117 | +-- | Plutus-script-witnessed withdrawal variant of 'hprop_tx_withdrawal_reward'. |
| 118 | +-- A Plutus V3 always-succeeds script acts as the stake credential, exercising |
| 119 | +-- the script-witness branch of the auto-balancer. Same balance arithmetic |
| 120 | +-- (output exceeds input, withdrawal covers the difference), and the |
| 121 | +-- withdrawal amount is likewise fictional. |
| 122 | +-- |
| 123 | +-- Execute me with: |
| 124 | +-- @DISABLE_RETRIES=1 cabal test cardano-testnet-test --test-options '-p "/transaction build with plutus withdrawal/"'@ |
| 125 | +hprop_tx_withdrawal_reward_plutus_v3 :: Property |
| 126 | +hprop_tx_withdrawal_reward_plutus_v3 = integrationRetryWorkspace 2 "tx-withdrawal-reward-plutus-v3" $ \tempAbsBasePath' -> H.runWithDefaultWatchdog_ $ do |
| 127 | + conf@Conf { tempAbsPath } <- mkConf tempAbsBasePath' |
| 128 | + let tempAbsPath' = unTmpAbsPath tempAbsPath |
| 129 | + tempBaseAbsPath = makeTmpBaseAbsPath tempAbsPath |
| 130 | + |
| 131 | + work <- H.createDirectoryIfMissing $ tempAbsPath' </> "work" |
| 132 | + |
| 133 | + let ceo = ConwayEraOnwardsConway |
| 134 | + sbe = convert ceo |
| 135 | + eraName = eraToString sbe |
| 136 | + fastTestnetOptions = def { cardanoNodeEra = AnyShelleyBasedEra sbe } |
| 137 | + shelleyOptions = def { genesisEpochLength = 100 } |
| 138 | + |
| 139 | + TestnetRuntime |
| 140 | + { testnetMagic |
| 141 | + , testnetNodes |
| 142 | + , wallets = wallet0:wallet1:_ |
| 143 | + , configurationFile |
| 144 | + } |
| 145 | + <- createAndRunTestnet fastTestnetOptions shelleyOptions conf |
| 146 | + |
| 147 | + node <- H.headM testnetNodes |
| 148 | + poolSprocket1 <- H.noteShow $ nodeSprocket node |
| 149 | + execConfig <- mkExecConfig tempBaseAbsPath poolSprocket1 testnetMagic |
| 150 | + let socketPath = nodeSocketPath node |
| 151 | + |
| 152 | + epochStateView <- getEpochStateView configurationFile socketPath |
| 153 | + |
| 154 | + -- Write the always-succeeds Plutus V3 script used as stake credential |
| 155 | + plutusScriptFp <- H.note $ work </> "always-succeeds.plutusV3" |
| 156 | + H.writeFile plutusScriptFp $ Text.unpack plutusV3Script |
| 157 | + |
| 158 | + -- Build the bech32 stake address for the script credential |
| 159 | + scriptStakeAddr <- filter (/= '\n') <$> execCli |
| 160 | + [ eraName, "stake-address", "build" |
| 161 | + , "--stake-script-file", plutusScriptFp |
| 162 | + , "--testnet-magic", show @Int testnetMagic |
| 163 | + ] |
| 164 | + H.note_ $ "Script stake address: " <> scriptStakeAddr |
| 165 | + |
| 166 | + (txinForWithdrawal, TxOut _ txinValue _ _) <- H.nothingFailM $ |
| 167 | + findLargestUtxoWithAddress epochStateView sbe $ paymentKeyInfoAddr wallet0 |
| 168 | + let L.Coin inputLovelace = txOutValueToLovelace txinValue |
| 169 | + H.note_ $ "Input UTxO: " <> show txinForWithdrawal <> " = " <> show inputLovelace <> " lovelace" |
| 170 | + |
| 171 | + -- Plutus scripts require collateral |
| 172 | + txinCollateral <- findLargestUtxoForPaymentKey epochStateView sbe wallet1 |
| 173 | + |
| 174 | + let withdrawalLovelace = inputLovelace * 2 |
| 175 | + outputLovelace = withdrawalLovelace |
| 176 | + |
| 177 | + let withdrawalTxBodyFp = work </> "withdrawal.txbody" |
| 178 | + |
| 179 | + (exitCode, _stdout, stderr) <- execCliAny execConfig |
| 180 | + [ eraName, "transaction", "build" |
| 181 | + , "--change-address", Text.unpack $ paymentKeyInfoAddr wallet0 |
| 182 | + , "--tx-in-collateral", Text.unpack $ renderTxIn txinCollateral |
| 183 | + , "--tx-in", Text.unpack $ renderTxIn txinForWithdrawal |
| 184 | + , "--tx-out", Text.unpack (paymentKeyInfoAddr wallet0) <> "+" <> show outputLovelace |
| 185 | + , "--withdrawal", scriptStakeAddr <> "+" <> show withdrawalLovelace |
| 186 | + , "--withdrawal-script-file", plutusScriptFp |
| 187 | + , "--withdrawal-redeemer-value", "0" |
| 188 | + , "--witness-override", show @Int 2 |
| 189 | + , "--out-file", withdrawalTxBodyFp |
| 190 | + ] |
| 191 | + H.note_ stderr |
| 192 | + |
| 193 | + exitCode H.=== ExitSuccess |
| 194 | + |
| 195 | + -- Verify the transaction body was written and can be read back |
| 196 | + void $ H.readFile withdrawalTxBodyFp |
0 commit comments