Skip to content

Commit

Permalink
add test catching missing ada from cert deposit return
Browse files Browse the repository at this point in the history
  • Loading branch information
carbolymer committed Jan 14, 2025
1 parent 5de3cd1 commit 6577cbc
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
20 changes: 16 additions & 4 deletions cardano-api/internal/Cardano/Api/Fees.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
Expand Down Expand Up @@ -98,6 +99,7 @@ import Data.Set (Set)
import qualified Data.Set as Set
import Data.Text (Text)
import GHC.Exts (IsList (..))
import GHC.Stack
import Lens.Micro ((.~), (^.))

-- | Type synonym for logs returned by the ledger's @evalTxExUnitsWithLogs@ function.
Expand Down Expand Up @@ -190,7 +192,8 @@ instance Error (TxFeeEstimationError era) where
-- | Use when you do not have access to the UTxOs you intend to spend
estimateBalancedTxBody
:: forall era
. MaryEraOnwards era
. HasCallStack
=> MaryEraOnwards era
-> TxBodyContent BuildTx era
-> L.PParams (ShelleyLedgerEra era)
-> Set PoolId
Expand Down Expand Up @@ -1004,6 +1007,7 @@ data FeeEstimationMode era
makeTransactionBodyAutoBalance
:: forall era
. ()
=> HasCallStack
=> ShelleyBasedEra era
-> SystemStart
-> LedgerEpochInfo
Expand Down Expand Up @@ -1044,18 +1048,25 @@ makeTransactionBodyAutoBalance
-- 3. update tx with fees
-- 4. balance the transaction and update tx change output

let totalValueAtSpendableUTxO = fromLedgerValue sbe . calculateIncomingUTxOValue . Map.elems $ unUTxO utxo
let totalValueAtSpendableUTxO =
(fromLedgerValue sbe . calculateIncomingUTxOValue . Map.elems $ unUTxO utxo)
<> fromList [(AdaAssetId, Quantity 20_000_000)]
change =
monoidForEraInEon (toCardanoEra sbe) $ \w ->
toLedgerValue w $ calculateChangeValue sbe totalValueAtSpendableUTxO txbodycontent
changeTxOutValue = TxOutValueShelleyBased sbe change

-- check change balance, otherwsie the ledger code will throw a GHC exception in the pure code in
-- createTransactionBody
balanceCheck sbe pp changeaddr changeTxOutValue

txbody0 <-
first TxBodyError
$ createTransactionBody
sbe
$ txbodycontent
& modTxOuts
(<> [TxOut changeaddr (TxOutValueShelleyBased sbe change) TxOutDatumNone ReferenceScriptNone])
(<> [TxOut changeaddr changeTxOutValue TxOutDatumNone ReferenceScriptNone])
exUnitsMapWithLogs <-
first TxBodyErrorValidityInterval $
evaluateTransactionExecutionUnits
Expand Down Expand Up @@ -1578,7 +1589,8 @@ traverseScriptWitnesses =
traverse (\(item, eRes) -> eRes >>= (\res -> Right (item, res)))

calculateMinimumUTxO
:: ShelleyBasedEra era
:: HasCallStack
=> ShelleyBasedEra era
-> TxOut CtxTx era
-> Ledger.PParams (ShelleyLedgerEra era)
-> L.Coin
Expand Down
5 changes: 4 additions & 1 deletion cardano-api/internal/Cardano/Api/Tx/Body.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1890,6 +1890,7 @@ instance Error TxBodyError where

createTransactionBody
:: ()
=> HasCallStack
=> ShelleyBasedEra era
-> TxBodyContent BuildTx era
-> Either TxBodyError (TxBody era)
Expand Down Expand Up @@ -2633,7 +2634,8 @@ convTotalCollateral txTotalCollateral =

convTxOuts
:: forall ctx era ledgerera
. ShelleyLedgerEra era ~ ledgerera
. HasCallStack
=> ShelleyLedgerEra era ~ ledgerera
=> ShelleyBasedEra era
-> [TxOut ctx era]
-> Seq.StrictSeq (Ledger.TxOut ledgerera)
Expand Down Expand Up @@ -2816,6 +2818,7 @@ guardShelleyTxInsOverflow txIns = do
-- all eras
mkCommonTxBody
:: ()
=> HasCallStack
=> ShelleyBasedEra era
-> TxIns BuildTx era
-> [TxOut ctx era]
Expand Down
4 changes: 3 additions & 1 deletion cardano-api/internal/Cardano/Api/Tx/Compatible.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import Data.Maybe.Strict
import Data.Monoid
import qualified Data.Sequence.Strict as Seq
import GHC.Exts (IsList (..))
import GHC.Stack
import Lens.Micro hiding (ix)

data AnyProtocolUpdate era where
Expand Down Expand Up @@ -205,7 +206,8 @@ createCompatibleSignedTx sbe ins outs witnesses txFee' anyProtocolUpdate anyVote
.~ shelleyBootstrapWitnesses

createCommonTxBody
:: ShelleyBasedEra era
:: HasCallStack
=> ShelleyBasedEra era
-> [TxIn]
-> [TxOut ctx era]
-> Lovelace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeApplications #-}

{- HLINT ignore "Use camelCase" -}

Expand Down Expand Up @@ -60,8 +59,7 @@ prop_make_transaction_body_autobalance_return_correct_fee_for_multi_asset = H.pr
beo = convert ceo
meo = convert beo
sbe = convert ceo
era = toCardanoEra sbe
aeo <- H.nothingFail $ forEraMaybeEon @AlonzoEraOnwards era
aeo = convert beo

systemStart <-
fmap SystemStart . H.evalIO $
Expand Down Expand Up @@ -144,8 +142,7 @@ prop_make_transaction_body_autobalance_when_deregistering_certs = H.propertyOnce
beo = convert ceo
sbe = convert beo
meo = convert beo
era = toCardanoEra sbe
aeo <- H.nothingFail $ forEraMaybeEon @AlonzoEraOnwards era
aeo = convert beo

systemStart <-
fmap SystemStart . H.evalIO $
Expand All @@ -169,7 +166,7 @@ prop_make_transaction_body_autobalance_when_deregistering_certs = H.propertyOnce
meo
[(policyId', [("eeee", 1, BuildTxWith plutusWitness)])]
stakeCred <- forAll genStakeCredential
let deregDeposit = L.Coin 200_000
let deregDeposit = L.Coin 20_000_000
let certs =
[ ConwayCertificate ceo $
L.ConwayTxCertDeleg (L.ConwayUnRegCert (toShelleyStakeCredential stakeCred) (L.SJust deregDeposit))
Expand All @@ -179,13 +176,13 @@ prop_make_transaction_body_autobalance_when_deregistering_certs = H.propertyOnce
defaultTxBodyContent sbe
& setTxIns txInputs
& setTxInsCollateral txInputsCollateral
& setTxOuts (mkTxOutput beo address (L.Coin 2_000_000) Nothing)
& setTxOuts (mkTxOutput beo address (L.Coin 20_800_000) Nothing)
& setTxMintValue txMint
& setTxProtocolParams (pure $ pure pparams)
& setTxCertificates (TxCertificates sbe certs (BuildTxWith []))

-- autobalanced body has assets and ADA in the change txout
(BalancedTxBody balancedContent _ txOut fee) <-
(BalancedTxBody balancedContent _ changeOut fee) <-
H.leftFail $
makeTransactionBodyAutoBalance
sbe
Expand All @@ -202,7 +199,7 @@ prop_make_transaction_body_autobalance_when_deregistering_certs = H.propertyOnce

-- 335_475 === fee
H.noteShow fee
H.noteShowPretty txOut
H.noteShowPretty changeOut

TxReturnCollateral _ (TxOut _ txOutValue _ _) <-
H.noteShowPretty $ txReturnCollateral balancedContent
Expand All @@ -219,7 +216,7 @@ prop_make_transaction_body_autobalance_multi_asset_collateral = H.propertyOnce $
sbe = convert beo
meo = convert beo
era = toCardanoEra sbe
aeo <- H.nothingFail $ forEraMaybeEon @AlonzoEraOnwards era
aeo = convert beo

systemStart <-
fmap SystemStart . H.evalIO $
Expand Down

0 comments on commit 6577cbc

Please sign in to comment.