Skip to content

Commit a994f03

Browse files
committed
Address PR review: simplify calcMinFeeRecursive and remove redundant constraints
- Remove getMultiAssets helper and inline the MaryValue extraction, since EraCommonConstraints guarantees Value ~ MaryValue for all eras including Dijkstra - Remove unnecessary parameters from go's recursive loop — utxo, pparams, poolids, stakeDelegDeposits, drepDelegDeposits, and nExtraWitnesses are never modified and are already in scope from the enclosing function - Remove redundant obtainCommonConstraints calls in test generators where constraints are already in scope from the outer call
1 parent c53efcd commit a994f03

2 files changed

Lines changed: 12 additions & 36 deletions

File tree

  • cardano-api

cardano-api/src/Cardano/Api/Experimental/Tx/Internal/Fee.hs

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -760,18 +760,13 @@ calcMinFeeRecursive changeAddr unsignedTx utxo pparams poolids stakeDelegDeposit
760760
| multiAssetIsNegative =
761761
Left $ NonAdaAssetsUnbalanced multiAssets
762762
| otherwise =
763-
go
764-
maxIterations
765-
unsignedTx
766-
utxo
767-
pparams
768-
poolids
769-
stakeDelegDeposits
770-
drepDelegDeposits
771-
nExtraWitnesses
763+
go maxIterations unsignedTx
772764
where
773765
initialBalance = evaluateTransactionBalance pparams poolids stakeDelegDeposits drepDelegDeposits utxo unsignedTx
774-
multiAssets = getMultiAssets (useEra @era) initialBalance
766+
multiAssets =
767+
obtainCommonConstraints (useEra @era) $
768+
let L.MaryValue _ ma = initialBalance
769+
in ma
775770
-- Check whether any native token quantity is negative.
776771
-- ADA is zeroed out so it doesn't influence the check.
777772
multiAssetIsNegative =
@@ -783,34 +778,28 @@ calcMinFeeRecursive changeAddr unsignedTx utxo pparams poolids stakeDelegDeposit
783778
go
784779
:: Int
785780
-> UnsignedTx (LedgerEra era)
786-
-> L.UTxO (LedgerEra era)
787-
-> L.PParams (LedgerEra era)
788-
-> Set PoolId
789-
-> Map StakeCredential L.Coin
790-
-> Map (Ledger.Credential Ledger.DRepRole) L.Coin
791-
-> Int
792781
-> Either FeeCalculationError (UnsignedTx (LedgerEra era))
793-
go 0 _ _ _ _ _ _ _ = Left FeeCalculationDidNotConverge
794-
go n unSignTx@(UnsignedTx ledgerTx) utxo' pparams' poolids' stakeDelegDeposits' drepDelegDeposits' nExtraWitnesses'
782+
go 0 _ = Left FeeCalculationDidNotConverge
783+
go n unSignTx@(UnsignedTx ledgerTx)
795784
| minFee == txBodyFee && L.isZero txBalanceValue = do
796785
-- Case 1
797786
let outs = toList $ ledgerTx ^. L.bodyTxL . L.outputsTxBodyL
798-
mapM_ (checkOutputMinUTxO pparams') outs
787+
mapM_ (checkOutputMinUTxO pparams) outs
799788
return unSignTx
800789
| minFee == txBodyFee = do
801790
-- Case 2
802791
balancedOuts <- balanceTxOuts @era changeAddr txBalanceValue unSignTx
803792
let updatedTx = UnsignedTx (ledgerTx & L.bodyTxL . L.outputsTxBodyL .~ balancedOuts)
804-
go (n - 1) updatedTx utxo' pparams' poolids' stakeDelegDeposits' drepDelegDeposits' nExtraWitnesses'
793+
go (n - 1) updatedTx
805794
| otherwise =
806795
-- Case 3
807796
let newTx = UnsignedTx (ledgerTx & L.bodyTxL . L.feeTxBodyL .~ minFee)
808-
in go (n - 1) newTx utxo' pparams' poolids' stakeDelegDeposits' drepDelegDeposits' nExtraWitnesses'
797+
in go (n - 1) newTx
809798
where
810-
minFee = obtainCommonConstraints (useEra @era) $ L.calcMinFeeTx utxo' pparams' ledgerTx nExtraWitnesses'
799+
minFee = obtainCommonConstraints (useEra @era) $ L.calcMinFeeTx utxo pparams ledgerTx nExtraWitnesses
811800
txBodyFee = ledgerTx ^. L.bodyTxL . L.feeTxBodyL
812801
txBalanceValue =
813-
evaluateTransactionBalance pparams' poolids' stakeDelegDeposits' drepDelegDeposits' utxo' unSignTx
802+
evaluateTransactionBalance pparams poolids stakeDelegDeposits drepDelegDeposits utxo unSignTx
814803

815804
checkOutputMinUTxO
816805
:: forall era
@@ -826,12 +815,6 @@ checkOutputMinUTxO pp out =
826815
Left (TxOut offending, minRequired) ->
827816
Left $ MinUTxONotMet (offending ^. L.coinTxOutL) minRequired
828817

829-
getMultiAssets :: Era era -> L.Value (LedgerEra era) -> L.MultiAsset
830-
getMultiAssets era val = case era of
831-
DijkstraEra -> mempty
832-
ConwayEra ->
833-
let L.MaryValue _ ma = val
834-
in ma
835818

836819
balanceTxOuts
837820
:: forall era

cardano-api/test/cardano-api-test/Test/Cardano/Api/Experimental.hs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,6 @@ genFundedSimpleTx era = do
606606
sendTxOut =
607607
Exp.obtainCommonConstraints era $
608608
Exp.TxOut $
609-
Exp.obtainCommonConstraints era $
610609
Ledger.mkBasicTxOut addr (L.MaryValue sendCoin mempty)
611610
txBodyContent =
612611
Exp.defaultTxBodyContent
@@ -644,7 +643,6 @@ genFundedMultiAssetTx era = do
644643
sendTxOut =
645644
Exp.obtainCommonConstraints era $
646645
Exp.TxOut $
647-
Exp.obtainCommonConstraints era $
648646
Ledger.mkBasicTxOut addr (L.MaryValue sendCoin multiAsset)
649647
txBodyContent =
650648
Exp.defaultTxBodyContent
@@ -678,7 +676,6 @@ genUnderfundedTx era = do
678676
sendTxOut =
679677
Exp.obtainCommonConstraints era $
680678
Exp.TxOut $
681-
Exp.obtainCommonConstraints era $
682679
Ledger.mkBasicTxOut addr (L.MaryValue sendCoin mempty)
683680
txBodyContent =
684681
Exp.defaultTxBodyContent
@@ -787,7 +784,6 @@ genNonAdaUnbalancedTx era = do
787784
sendTxOut =
788785
Exp.obtainCommonConstraints era $
789786
Exp.TxOut $
790-
Exp.obtainCommonConstraints era $
791787
Ledger.mkBasicTxOut addr sendValue
792788
txBodyContent =
793789
Exp.defaultTxBodyContent
@@ -827,13 +823,11 @@ genMinUTxOViolatingTx era = do
827823
sendTxOut1 =
828824
Exp.obtainCommonConstraints era $
829825
Exp.TxOut $
830-
Exp.obtainCommonConstraints era $
831826
Ledger.mkBasicTxOut addr (L.MaryValue (L.Coin 1_000_000) mempty)
832827
-- Output 2: tokens with tiny ADA (below min UTxO)
833828
sendTxOut2 =
834829
Exp.obtainCommonConstraints era $
835830
Exp.TxOut $
836-
Exp.obtainCommonConstraints era $
837831
Ledger.mkBasicTxOut addr (L.MaryValue (L.Coin 1_000) multiAsset)
838832
txBodyContent =
839833
Exp.defaultTxBodyContent
@@ -955,7 +949,6 @@ genTinySurplusTx era = do
955949
sendTxOut =
956950
Exp.obtainCommonConstraints era $
957951
Exp.TxOut $
958-
Exp.obtainCommonConstraints era $
959952
Ledger.mkBasicTxOut addr (L.MaryValue sendCoin mempty)
960953
txBodyContent =
961954
Exp.defaultTxBodyContent

0 commit comments

Comments
 (0)