Add evaluateTransaction to Cardano.Api.Experimental#1205
Open
carbolymer wants to merge 1 commit into
Open
Conversation
47dd9d1 to
10cf9be
Compare
…rimental`, composing script evaluation, fee computation, and balance checking into a single pure function for signed transactions.
10cf9be to
91d7750
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
Extracts the "evaluate execution units → compute min fee → check balance" pipeline that was inlined in cardano-rpc's evalTxMethod into a reusable Cardano.Api.Experimental function (evaluateTransaction, plus a SignedTx-flavoured wrapper evaluateSignedTx), introduces a TxEvaluationResult record, and refactors evalTxMethod to call the new function. Adds three property tests covering the new function.
Changes:
- Add
evaluateTransaction/evaluateSignedTx/TxEvaluationResulttoCardano.Api.Experimental.Tx.Internal.Feeand re-export throughCardano.Api.Experimental.TxandCardano.Api.Experimentalunder a new "Transaction evaluation" section. - Refactor
evalTxMethodincardano-rpcto delegate the pipeline to the new function (−46 lines). - Add property tests for fee positivity, empty ExUnits for script-free txs, and mempty balance for balanced txs; add two Herald changelog fragments (
featurefor cardano-api,refactoringfor cardano-rpc).
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| cardano-api/src/Cardano/Api/Experimental/Tx/Internal/Fee.hs | Adds TxEvaluationResult data type and evaluateTransaction orchestration function. |
| cardano-api/src/Cardano/Api/Experimental/Tx.hs | Adds evaluateSignedTx wrapper and re-exports new symbols under a "Transaction evaluation" section. |
| cardano-api/src/Cardano/Api/Experimental.hs | Re-exports new evaluation API under a new sub-section. |
| cardano-api/test/cardano-api-test/Test/Cardano/Api/Experimental/Fee.hs | Three new property tests for the new evaluation function. |
| cardano-rpc/src/Cardano/Rpc/Server/Internal/UtxoRpc/Eval.hs | Refactor: replace inlined pipeline with a call to Exp.evaluateTransaction. |
| .changes/20260515_cardano_api_evaluate_transaction.yml | Herald changelog fragment (feature) for cardano-api. |
| .changes/20260515_cardano_rpc_use_evaluate_transaction.yml | Herald changelog fragment (refactoring) for cardano-rpc. |
Comment on lines
+552
to
+571
| evaluateTransaction | ||
| :: forall era | ||
| . IsEra era | ||
| => SystemStart | ||
| -- ^ Start time of the blockchain | ||
| -> LedgerEpochInfo | ||
| -- ^ Epoch info for slot/time conversions | ||
| -> L.PParams (LedgerEra era) | ||
| -- ^ Protocol parameters | ||
| -> Set PoolId | ||
| -- ^ Registered stake pools | ||
| -> Map StakeCredential L.Coin | ||
| -- ^ Stake delegation deposits | ||
| -> Map (Ledger.Credential Ledger.DRepRole) L.Coin | ||
| -- ^ DRep delegation deposits | ||
| -> L.UTxO (LedgerEra era) | ||
| -- ^ UTxO set for the transaction inputs | ||
| -> L.Tx L.TopTx (LedgerEra era) | ||
| -- ^ Signed transaction to evaluate | ||
| -> TxEvaluationResult (LedgerEra era) |
Comment on lines
+539
to
+549
| data TxEvaluationResult era = Show (L.Value era) => TxEvaluationResult | ||
| { txEvalFee :: L.Coin | ||
| -- ^ Computed minimum fee for the transaction | ||
| , txEvalExecutionUnits | ||
| :: Map ScriptWitnessIndex (Either ScriptExecutionError (EvalTxExecutionUnitsLog, ExecutionUnits)) | ||
| -- ^ Per-redeemer execution units or script errors | ||
| , txEvalBalance :: L.Value era | ||
| -- ^ Remaining balance (consumed - produced); mempty when balanced | ||
| } | ||
|
|
||
| deriving instance Show (TxEvaluationResult era) |
Comment on lines
+360
to
+369
| obtainCommonConstraints (useEra @era) $ | ||
| evaluateTransaction | ||
| systemStart | ||
| epochInfo | ||
| protocolParams | ||
| poolIds | ||
| stakeDelegDeposits | ||
| drepDelegDeposits | ||
| utxo | ||
| tx |
Comment on lines
+763
to
+764
| -- | A transaction balanced by 'calcMinFeeRecursive' has mempty balance | ||
| -- when checked by 'evaluateSignedTx'. |
Comment on lines
+768
to
+786
| Exp.UnsignedTx balancedLedgerTx <- | ||
| H.leftFail $ | ||
| Exp.calcMinFeeRecursive changeAddr unsignedTx utxo exampleProtocolParams mempty mempty mempty 0 | ||
| let signedTx = Exp.SignedTx balancedLedgerTx | ||
| systemStart = Api.SystemStart $ Time.posixSecondsToUTCTime 0 | ||
| epochInfo = | ||
| Api.LedgerEpochInfo $ | ||
| Slotting.fixedEpochInfo (Slotting.EpochSize 100) (Slotting.mkSlotLength 1000) | ||
| result = | ||
| Exp.evaluateSignedTx | ||
| systemStart | ||
| epochInfo | ||
| exampleProtocolParams | ||
| mempty | ||
| mempty | ||
| mempty | ||
| utxo | ||
| signedTx | ||
| Exp.txEvalBalance result H.=== mempty |
Jimbo4350
approved these changes
May 15, 2026
Jimbo4350
approved these changes
May 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
Closes #1204
cardano-api has individual building blocks for transaction evaluation (
evaluateTransactionExecutionUnits,evaluateTransactionFee,evaluateTransactionBalance) but no single function that runs the full evaluate execution units, compute fee, check balance pipeline on a signed transaction.cardano-rpc's
evalTxMethodfilled this gap by inlining the pipeline with ledger internals directly. This PR extracts that logic into a reusable cardano-api function.Changes
evaluateTransactionandevaluateSignedTxtoCardano.Api.Experimental, composing script evaluation, fee computation viasetMinFeeTxUtxo, and balance checking into a single pure function. AddTxEvaluationResultresult type with fee, per-redeemer execution units, and balance fields.evalTxMethodto callExp.evaluateTransactioninstead of inlining the pipeline (-46 lines).evaluateTransaction- positive fee, empty execution units for script-free transactions, and mempty balance for transactions balanced bycalcMinFeeRecursive.How to trust this PR
The core function (
evaluateTransactioninFee.hs) is a direct extraction of the logic previously inEval.hslines 83-137. The pipeline is:evaluateTransactionExecutionUnits- run scriptssetMinFeeTxUtxo- compute fee for the concrete signed tx with UTxO contextevalBalanceTxBody- check the balanceChecklist
.changes/