Skip to content

Add evaluateTransaction to Cardano.Api.Experimental#1205

Open
carbolymer wants to merge 1 commit into
masterfrom
mgalazyn/refactor/move-evaltx-to-api
Open

Add evaluateTransaction to Cardano.Api.Experimental#1205
carbolymer wants to merge 1 commit into
masterfrom
mgalazyn/refactor/move-evaltx-to-api

Conversation

@carbolymer
Copy link
Copy Markdown
Contributor

@carbolymer carbolymer commented May 14, 2026

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 evalTxMethod filled this gap by inlining the pipeline with ledger internals directly. This PR extracts that logic into a reusable cardano-api function.

Changes

  • cardano-api: Add evaluateTransaction and evaluateSignedTx to Cardano.Api.Experimental, composing script evaluation, fee computation via setMinFeeTxUtxo, and balance checking into a single pure function. Add TxEvaluationResult result type with fee, per-redeemer execution units, and balance fields.
  • cardano-rpc: Refactor evalTxMethod to call Exp.evaluateTransaction instead of inlining the pipeline (-46 lines).
  • Tests: Three new property tests for evaluateTransaction - positive fee, empty execution units for script-free transactions, and mempty balance for transactions balanced by calcMinFeeRecursive.

How to trust this PR

The core function (evaluateTransaction in Fee.hs) is a direct extraction of the logic previously in Eval.hs lines 83-137. The pipeline is:

  1. evaluateTransactionExecutionUnits - run scripts
  2. Substitute evaluated ExUnits into the redeemer map (failed redeemers keep client-supplied units)
  3. setMinFeeTxUtxo - compute fee for the concrete signed tx with UTxO context
  4. evalBalanceTxBody - check the balance

Checklist

  • Commit sequence broadly makes sense and commits have useful messages
  • New tests are added if needed and existing tests are updated
  • Self-reviewed the diff
  • Changelog fragments added in .changes/

@carbolymer carbolymer self-assigned this May 14, 2026
@carbolymer carbolymer force-pushed the mgalazyn/refactor/move-evaltx-to-api branch 2 times, most recently from 47dd9d1 to 10cf9be Compare May 15, 2026 18:01
@carbolymer carbolymer changed the title Refactor: move evaltx to api Add evaluateTransaction to Cardano.Api.Experimental May 15, 2026
@carbolymer carbolymer marked this pull request as ready for review May 15, 2026 18:02
Copilot AI review requested due to automatic review settings May 15, 2026 18:02
…rimental`, composing script evaluation, fee computation, and balance checking into a single pure function for signed transactions.
@carbolymer carbolymer force-pushed the mgalazyn/refactor/move-evaltx-to-api branch from 10cf9be to 91d7750 Compare May 15, 2026 18:02
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 / TxEvaluationResult to Cardano.Api.Experimental.Tx.Internal.Fee and re-export through Cardano.Api.Experimental.Tx and Cardano.Api.Experimental under a new "Transaction evaluation" section.
  • Refactor evalTxMethod in cardano-rpc to 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 (feature for cardano-api, refactoring for 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 Jimbo4350 self-requested a review May 15, 2026 18:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Extract transaction evaluation pipeline from cardano-rpc into cardano-api

3 participants