Skip to content

Commit 47dd9d1

Browse files
committed
tetsest
1 parent ae0c896 commit 47dd9d1

3 files changed

Lines changed: 37 additions & 13 deletions

File tree

  • cardano-api
    • src/Cardano/Api/Experimental
    • test/cardano-api-test/Test/Cardano/Api/Experimental

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ import Cardano.Crypto.Hash qualified as Hash
250250
import Cardano.Ledger.Alonzo.Core qualified as Ledger
251251
import Cardano.Ledger.Api qualified as L
252252
import Cardano.Ledger.Binary qualified as Ledger
253-
import Cardano.Ledger.Credential as Ledger (Credential)
253+
import Cardano.Ledger.Credential qualified as Ledger (Credential)
254254
import Cardano.Ledger.Hashes qualified as L hiding (Hash)
255255

256256
import Control.Exception (displayException)

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
{-# LANGUAGE StandaloneDeriving #-}
1212
{-# LANGUAGE TupleSections #-}
1313
{-# LANGUAGE TypeApplications #-}
14-
{-# LANGUAGE UndecidableInstances #-}
1514

1615
module Cardano.Api.Experimental.Tx.Internal.Fee
1716
( FeeCalculationError (..)
@@ -537,7 +536,7 @@ calculateMinimumUTxO pp (TxOut txout) =
537536
in txOutWithMinCoin ^. L.coinTxOutL
538537

539538
-- | Result of evaluating a signed transaction against the current ledger state.
540-
data TxEvaluationResult era = TxEvaluationResult
539+
data TxEvaluationResult era = Show (L.Value era) => TxEvaluationResult
541540
{ txEvalFee :: L.Coin
542541
-- ^ Computed minimum fee for the transaction
543542
, txEvalExecutionUnits
@@ -547,7 +546,7 @@ data TxEvaluationResult era = TxEvaluationResult
547546
-- ^ Remaining balance (consumed - produced); mempty when balanced
548547
}
549548

550-
deriving instance Show (L.Value era) => Show (TxEvaluationResult era)
549+
deriving instance Show (TxEvaluationResult era)
551550

552551
-- | Evaluate a signed transaction: run all scripts, compute the minimum fee,
553552
-- and check the balance. This is a read-only diagnostic - it does not modify
@@ -587,7 +586,7 @@ evaluateTransaction systemStart epochInfo protocolParams poolIds stakeDelegDepos
587586
& L.witsTxL . L.rdmrsTxWitsL
588587
%~ \redeemers ->
589588
L.Redeemers
590-
$ Map.mapWithKey
589+
. Map.mapWithKey
591590
( \purpose (datum, oldExUnits) ->
592591
(datum, Map.findWithDefault oldExUnits purpose evaluatedExUnitsMap)
593592
)
@@ -610,7 +609,7 @@ evaluateTransaction systemStart epochInfo protocolParams poolIds stakeDelegDepos
610609
}
611610
where
612611
isRegPool :: Ledger.KeyHash Ledger.StakePool -> Bool
613-
isRegPool kh = Api.StakePoolKeyHash kh `Set.member` poolIds
612+
isRegPool keyHash = Api.StakePoolKeyHash keyHash `Set.member` poolIds
614613

615614
lookupDelegDeposit
616615
:: Ledger.Credential Ledger.Staking -> Maybe L.Coin

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

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ tests =
116116
, testProperty
117117
"script-free tx returns empty execution units map"
118118
prop_evaluateTransaction_no_scripts_empty_exunits
119+
, testProperty
120+
"balanced tx has mempty balance"
121+
prop_evaluateTransaction_balanced_mempty
119122
]
120123
]
121124

@@ -745,10 +748,16 @@ prop_makeTransactionBodyAutoBalance_balance_negative = H.property $ do
745748
H.annotate "Expected TxBodyErrorBalanceNegative but tx balanced successfully"
746749
>> H.failure
747750

748-
-- | A well-funded transaction returns a positive fee from 'evaluateTransaction'.
749-
prop_evaluateTransaction_positive_fee :: Property
750-
prop_evaluateTransaction_positive_fee = H.property $ do
751-
(unsignedTx, utxo, _changeAddr) <- H.forAll $ genFundedSimpleTx Exp.ConwayEra
751+
-- | Evaluate a simple signed transaction, returning the result and UTxO.
752+
evalSimpleTx
753+
:: Gen
754+
( Exp.TxEvaluationResult (Exp.LedgerEra Exp.ConwayEra)
755+
, L.UTxO (Exp.LedgerEra Exp.ConwayEra)
756+
, Exp.UnsignedTx (Exp.LedgerEra Exp.ConwayEra)
757+
, L.Addr
758+
)
759+
evalSimpleTx = do
760+
(unsignedTx, utxo, changeAddr) <- genFundedSimpleTx Exp.ConwayEra
752761
let signedTx = Exp.signTx Exp.ConwayEra [] [] unsignedTx
753762
systemStart = Api.SystemStart $ Time.posixSecondsToUTCTime 0
754763
epochInfo =
@@ -764,13 +773,29 @@ prop_evaluateTransaction_positive_fee = H.property $ do
764773
mempty
765774
utxo
766775
signedTx
776+
pure (result, utxo, unsignedTx, changeAddr)
777+
778+
-- | A well-funded transaction returns a positive fee from 'evaluateTransaction'.
779+
prop_evaluateTransaction_positive_fee :: Property
780+
prop_evaluateTransaction_positive_fee = H.property $ do
781+
(result, _utxo, _unsignedTx, _changeAddr) <- H.forAll evalSimpleTx
767782
H.assertWith (Exp.txEvalFee result) (> L.Coin 0)
768783

769784
-- | A script-free transaction returns an empty execution units map.
770785
prop_evaluateTransaction_no_scripts_empty_exunits :: Property
771786
prop_evaluateTransaction_no_scripts_empty_exunits = H.property $ do
772-
(unsignedTx, utxo, _changeAddr) <- H.forAll $ genFundedSimpleTx Exp.ConwayEra
773-
let signedTx = Exp.signTx Exp.ConwayEra [] [] unsignedTx
787+
(result, _utxo, _unsignedTx, _changeAddr) <- H.forAll evalSimpleTx
788+
H.assertWith (Exp.txEvalExecutionUnits result) Map.null
789+
790+
-- | A transaction balanced by 'calcMinFeeRecursive' has mempty balance
791+
-- when checked by 'evaluateSignedTx'.
792+
prop_evaluateTransaction_balanced_mempty :: Property
793+
prop_evaluateTransaction_balanced_mempty = H.property $ do
794+
(unsignedTx, utxo, changeAddr) <- H.forAll $ genFundedSimpleTx Exp.ConwayEra
795+
Exp.UnsignedTx balancedLedgerTx <-
796+
H.leftFail $
797+
Exp.calcMinFeeRecursive changeAddr unsignedTx utxo exampleProtocolParams mempty mempty mempty 0
798+
let signedTx = Exp.SignedTx balancedLedgerTx
774799
systemStart = Api.SystemStart $ Time.posixSecondsToUTCTime 0
775800
epochInfo =
776801
Api.LedgerEpochInfo $
@@ -785,7 +810,7 @@ prop_evaluateTransaction_no_scripts_empty_exunits = H.property $ do
785810
mempty
786811
utxo
787812
signedTx
788-
H.assertWith (Exp.txEvalExecutionUnits result) Map.null
813+
Exp.txEvalBalance result H.=== mempty
789814

790815
-- | Regression test for the bug where 'mapScriptWitnessesCertificates' silently
791816
-- dropped certs stored with a @Nothing@ witness (e.g. shelley stake registration

0 commit comments

Comments
 (0)