Skip to content

Conversation

@williamdemeo
Copy link
Member

@williamdemeo williamdemeo commented Dec 22, 2025

Description

Stacked PR. (This PR should be rebased on and target master if PR #1014 is merged first.)

This PR closes issue #1006 by starting the Dijkstra-era refactor needed for batch-wide phase-2 evaluation.

This PR handles the collector refactor; the single call site is implemented in PR #1021.

Key change: collectP2ScriptsWithContext now takes two UTxO views:

  • utxo₀ = the snapshot UTxO "before any of the batch transactions are applied" (this is explicitly a CIP-0118 requirement).
  • utxoRef = the view used for reference-input lookup, which is allowed to include outputs from earlier subtransactions in the batch (also explicitly CIP-0118).

It also fixes context construction to use Dijkstra's TxLevel-indexed txInfo via txInfoForPurpose, rather than Conway's txInfo (language p2s) ... pattern.

Motivation

Dijkstra batches need to:

  • build script inputs/contexts across subTxs + top-level,
  • run a single batch-wide phase-2 evaluation at UTXOS,
  • keep spending inputs pinned to the initial UTxO snapshot (mempool safety),
  • allow reference lookup to use an evolving prefix-applied view (see design note in PR [Dijkstra] resolve ambiguity: visibility of reference inputs #1014).

This PR is an incremental step: it makes P2 collection ready to be called per-subTx with the correct UTxO parameters.

Changes

  • Add {ℓ : TxLevel} and (utxo₀ utxoRef : UTxO) arguments to collectP2ScriptsWithContext.
  • Use credsNeeded ℓ utxo₀ ... so spend-side input inspection remains snapshot-based.
  • Thread both views through lookupScriptHash and txOutToP2Script.
  • Build validation context using txInfoForPurpose ℓ utxo₀ tx sp (TxLevel-indexed).

Follow-ups (next commits / next PR)

  • Define the batch-level collector that computes utxoRef per subTx prefix.
  • Move to exactly one batch-wide evalP2Scripts call site at UTXOS.
  • Specify failure behavior: no batch effects applied; collateral collected (top-level only).

Copilot-generated Description

This pull request introduces significant refactoring and abstraction improvements to the Dijkstra specification, focusing on transaction structure, script validation, and the handling of scripts and guards. The changes add new type classes for modular access to transaction components, refactor script and UTxO handling for clarity and extensibility, and move or abstract several definitions to improve code organization. These updates lay the groundwork for more robust, maintainable, and extensible formalizations of transaction and script validation logic.

Major abstraction and type class improvements:

  • Introduced a suite of new type classes (e.g., HasTxBody, HasTxWitnesses, HasRedeemers, HasCollateralInputs, HasTxFees, HasSubTransactions, HasTopLevelGuards, HasIndexedOutputs, HasMintedValue, HasGuards, HasScripts) to provide modular, level-dependent and level-independent accessors for transaction components, replacing direct record field access throughout the codebase. [1] [2] [3] [4] [5] [6] [7]

  • Added corresponding type class instances for Tx, TxBody, and TxWitnesses, ensuring seamless and consistent access to transaction data at both top-level and sub-level contexts. [1] [2] [3] [4]

Script and UTxO handling refactor:

  • Refactored script and UTxO handling functions (e.g., refScripts, txscripts, lookupScriptHash, getSubTxScripts, getTxScripts) to use the new type classes and clarified the distinction between initial and evolving UTxO snapshots (utxo₀ and utxoRef), improving readability and correctness.

  • Updated documentation and function signatures to reflect the new UTxO handling conventions and to clarify the use of two UTxO arguments for reference and spending inputs.

Script validation and context improvements:

  • Moved the definition of ScriptPurpose and TxInfo to a new module (ScriptPurpose.lagda.md), and updated imports accordingly, centralizing script purpose logic. [1] [2] [3]

  • Added the valContext function to the AbstractFunctions record, and used it in script validation to provide context data to scripts, improving the expressiveness and correctness of script evaluation. [1] [2]

Correctness and consistency fixes:

  • Updated the credsNeededMinusCollateral function to use the new MintedValueOf accessor instead of the removed ValueOf, ensuring correct calculation of credentials needed for minting.

  • Standardized variable naming and usage (e.g., utxo₀, utxoRef) across script validation and helper functions for consistency. [1] [2] [3] [4]

Guard and credential handling improvements:

  • Refactored functions related to guard credentials and top-level guard satisfaction to use the new GuardsOf and TopLevelGuardsOf accessors, streamlining the logic for CIP-0118 compliance.

Checklist

  • Commit sequence broadly makes sense and commits have useful messages
  • Any semantic changes to the specifications are documented in CHANGELOG.md
  • Code is formatted according to CONTRIBUTING.md
  • Self-reviewed the diff

@williamdemeo williamdemeo self-assigned this Dec 22, 2025
@williamdemeo williamdemeo moved this to In Progress in Dijkstra formal spec Dec 22, 2025
@carlostome carlostome mentioned this pull request Jan 5, 2026
4 tasks
@williamdemeo williamdemeo changed the base branch from master to 1005-dijkstra-cip-0118-ambiguity-visibility-of-reference-inputs January 6, 2026 03:58
@williamdemeo williamdemeo force-pushed the 1005-dijkstra-cip-0118-ambiguity-visibility-of-reference-inputs branch from d2e681c to 0e6ef2c Compare January 7, 2026 00:41
@williamdemeo williamdemeo force-pushed the 1006-dijkstra-batch-aware-collectp2scriptswithcontext branch from 6ee6860 to 90a7192 Compare January 7, 2026 00:46
@williamdemeo williamdemeo force-pushed the 1005-dijkstra-cip-0118-ambiguity-visibility-of-reference-inputs branch from 0e6ef2c to 20498dc Compare January 7, 2026 02:36
@williamdemeo williamdemeo force-pushed the 1006-dijkstra-batch-aware-collectp2scriptswithcontext branch from 90a7192 to bb90078 Compare January 7, 2026 02:46
@williamdemeo williamdemeo force-pushed the 1005-dijkstra-cip-0118-ambiguity-visibility-of-reference-inputs branch from 20498dc to 0eb3c68 Compare January 7, 2026 13:46
@williamdemeo williamdemeo force-pushed the 1006-dijkstra-batch-aware-collectp2scriptswithcontext branch from bb90078 to 66ac197 Compare January 7, 2026 13:50
@williamdemeo williamdemeo force-pushed the 1005-dijkstra-cip-0118-ambiguity-visibility-of-reference-inputs branch from 0eb3c68 to 58fc737 Compare January 7, 2026 14:04
@williamdemeo williamdemeo force-pushed the 1006-dijkstra-batch-aware-collectp2scriptswithcontext branch from 66ac197 to 1134bff Compare January 7, 2026 14:05
@williamdemeo williamdemeo force-pushed the 1005-dijkstra-cip-0118-ambiguity-visibility-of-reference-inputs branch from 58fc737 to d9fd7b5 Compare January 7, 2026 14:18
@williamdemeo williamdemeo force-pushed the 1006-dijkstra-batch-aware-collectp2scriptswithcontext branch from 1134bff to 4a690a6 Compare January 7, 2026 14:19
@williamdemeo williamdemeo marked this pull request as ready for review January 8, 2026 06:26
@williamdemeo williamdemeo requested a review from Copilot January 8, 2026 06:42
Copy link
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

This PR refactors the Dijkstra-era script validation infrastructure to support batch-wide phase-2 evaluation. The key change is making collectP2ScriptsWithContext batch-aware by introducing dual UTxO views: one for spending input inspection (pinned to the initial snapshot) and one for reference script/datum lookup (which will eventually use an evolving view).

  • Introduced separate ScriptPurpose.lagda.md module to house ScriptPurpose, TxInfo, and SubTxInfo types
  • Added txInfoForPurpose function that conditionally includes sub-transaction context for guard scripts at the top level
  • Updated collectP2ScriptsWithContext to accept two UTxO parameters and thread them appropriately through script lookup and context construction

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
src/Ledger/Dijkstra/Specification/Script/ScriptPurpose.lagda.md New module defining ScriptPurpose data type and TxInfo record with txInfoSubTxs field for guard scripts
src/Ledger/Dijkstra/Specification/Abstract.lagda.md Imports ScriptPurpose module and adds valContext function to AbstractFunctions interface
src/Ledger/Dijkstra/Specification/Script/Validation.lagda.md Removes inline type definitions (moved to ScriptPurpose), updates collectP2ScriptsWithContext to use dual UTxO views, adds txInfoForPurpose for purpose-specific context construction

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@williamdemeo williamdemeo force-pushed the 1005-dijkstra-cip-0118-ambiguity-visibility-of-reference-inputs branch from d9fd7b5 to f2f9003 Compare January 9, 2026 02:52
@williamdemeo williamdemeo force-pushed the 1006-dijkstra-batch-aware-collectp2scriptswithcontext branch 2 times, most recently from b0da3a6 to a60af70 Compare January 9, 2026 05:52
@williamdemeo williamdemeo force-pushed the 1005-dijkstra-cip-0118-ambiguity-visibility-of-reference-inputs branch 3 times, most recently from c070f60 to ce58826 Compare January 9, 2026 15:00
@williamdemeo williamdemeo force-pushed the 1006-dijkstra-batch-aware-collectp2scriptswithcontext branch from a60af70 to e5e912f Compare January 9, 2026 15:01
@williamdemeo williamdemeo force-pushed the 1005-dijkstra-cip-0118-ambiguity-visibility-of-reference-inputs branch from ce58826 to 8418eed Compare January 9, 2026 15:05
@williamdemeo williamdemeo force-pushed the 1006-dijkstra-batch-aware-collectp2scriptswithcontext branch from e5e912f to 1f7ea3b Compare January 9, 2026 15:05
@williamdemeo williamdemeo force-pushed the 1006-dijkstra-batch-aware-collectp2scriptswithcontext branch from 1f7ea3b to 4c2836d Compare January 10, 2026 00:14
Copy link
Collaborator

@carlostome carlostome left a comment

Choose a reason for hiding this comment

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

LGTM with a minor comment (TB discussed)

: ScriptPurpose → ScriptHash
→ Maybe (P2Script × List Data × ExUnits × CostModel)
toScriptInput sp sh =
do s ← lookupScriptHash sh tx utxoSpend₀ utxoRefView
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not particularly happy about the namings utxoSpend₀ utxoRefView.
In utxoSpend₀ the subscript 0 suggests (to me) is a snapshot of the original utxo while the suffix RefView in utxoRefView suggest the same.
For simplicity I'd suggest keeping utxo for the utxo in the state and maybe utxo₀ for the snapshot before subtransactions are applied.

Copy link
Member Author

Choose a reason for hiding this comment

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

Okay, that sounds good. The idea behind the current names is that the subscript 0 refers to the origin utxo, while RefView is the evolving one that the reference scripts see... but I'm fine with your proposed naming scheme, too.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll rename utxoSpend₀utxo₀ (pre-batch snapshot used for spending-input inspection + realizedInputs) and keep utxoRefView (ref-input lookup view, potentially evolving to include earlier-prefix outputs).

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't see an obvious way to avoid having two utxo arguments in the script functions defined near the bottom of the Transactions module. I agree it would be nicer if we could bind these utxos to a UTxOEnv and a UTxOState, but the functions are defined in the Transaction module and used in the Scripts.Validation module, before the UTxOEnv and UTxOState types are even defined.

@williamdemeo williamdemeo force-pushed the 1005-dijkstra-cip-0118-ambiguity-visibility-of-reference-inputs branch from a636905 to 9b4798b Compare January 12, 2026 16:25
Base automatically changed from 1005-dijkstra-cip-0118-ambiguity-visibility-of-reference-inputs to master January 12, 2026 16:27
+  Remove contradiction between "ref inputs may refer to earlier tx outputs in the batch" vs "all inputs must exist before applying any tx in the batch." The new text punts the exact constraint to the UTxO rules (where it belongs).
+  Fix Plutus bullet (old "nor earlier versions" reads like "no Plutus at all").
+  Align fees with current Agda (`txFee : InTopLevel …`), but leave room for later CIP-driven updates.
1. Introduce subTx info type (using an alias for `TxInfo` for now).
2. Extend `TxInfo` with field `txInfoSubTxs : Maybe (List SubTxInfo)`.
3. Define a purpose-built builder:

   + Top-level Guard scripts ⇒ `txInfoSubTxs = just (...)`
   + Everything else ⇒ `txInfoSubTxs = nothing`
   + SubTx scripts ⇒ always `nothing` (even for `Guard` at sub level)
…split UTxO views

-  Update collectP2ScriptsWithContext to take both utxoSpend₀ (initial snapshot) and utxoRefView (for reference lookups), preparing for batch-aware collection and single phase-2 eval.
-  Use txInfoForPurpose to construct validation context (TxLevel-indexed) instead of Conway's language-indexed txInfo.
   Dijkstra txInfo is indexed by TxLevel, not by Plutus language.
@williamdemeo williamdemeo force-pushed the 1006-dijkstra-batch-aware-collectp2scriptswithcontext branch 2 times, most recently from 01f1a15 to bff46ee Compare January 13, 2026 05:41
@williamdemeo williamdemeo force-pushed the 1006-dijkstra-batch-aware-collectp2scriptswithcontext branch from bff46ee to ca47549 Compare January 13, 2026 05:47
@williamdemeo williamdemeo merged commit b978ae2 into master Jan 13, 2026
2 checks passed
@williamdemeo williamdemeo deleted the 1006-dijkstra-batch-aware-collectp2scriptswithcontext branch January 13, 2026 05:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Dijkstra] batch-aware collectP2ScriptsWithContext + single evalP2Scripts at end

3 participants