Skip to content

Commit bff46ee

Browse files
committed
cleanup and explain two utxo arguments
1 parent d480c57 commit bff46ee

File tree

3 files changed

+147
-58
lines changed

3 files changed

+147
-58
lines changed

src/Ledger/Dijkstra/Specification/Script/Validation.lagda.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ indexedRdmrs tx sp = maybe (λ x → lookupᵐ? txRedeemers x) nothing (rdptr tx
4747
4848
-- Datum lookup for spent outputs (Spend txin). Uses initial UTxO snapshot, utxoSpend₀.
4949
getDatum : Tx ℓ → UTxO → ScriptPurpose → Maybe Datum
50-
getDatum tx utxoSpend₀ (Spend txin) =
51-
do (_ , _ , just d , _) ← lookupᵐ? utxoSpend₀ txin where
50+
getDatum tx utxo₀ (Spend txin) =
51+
do (_ , _ , just d , _) ← lookupᵐ? utxo₀ txin where
5252
(_ , _ , nothing , _) → nothing
5353
case d of λ where
5454
(inj₁ d) → just d
5555
(inj₂ h) → lookupᵐ? (setToMap (mapˢ < hash , id > (DataOf tx))) h
56-
getDatum tx utxoSpend₀ _ = nothing
56+
getDatum tx utxo₀ _ = nothing
5757
```
5858
-->
5959

@@ -114,7 +114,7 @@ credsNeededMinusCollateral : {ℓ : TxLevel} → TxBody ℓ → ℙ (ScriptPurpo
114114
credsNeededMinusCollateral txb =
115115
mapˢ (λ a → (Rwrd a , CredentialOf a)) (dom ∣ WithdrawalsOf txb ∣)
116116
∪ mapPartial (λ c → (Cert c ,_) <$> cwitness c) (fromList (DCertsOf txb))
117-
∪ mapˢ (λ x → (Mint x , ScriptObj x)) (policies (ValueOf txb))
117+
∪ mapˢ (λ x → (Mint x , ScriptObj x)) (policies (MintedValueOf txb))
118118
∪ mapPartial (λ v → if isGovVoterCredential v then (λ {c} → just (Vote v , c)) else nothing)
119119
(fromList (map GovVoterOf (GovVotesOf txb)))
120120
∪ mapPartial (λ p → if PolicyOf p then (λ {sh} → just (Propose p , ScriptObj sh)) else nothing)
@@ -133,9 +133,9 @@ txOutToDataHash : TxOut → Maybe DataHash
133133
txOutToDataHash (_ , _ , d , _) = d >>= isInj₂
134134
135135
txOutToP2Script : UTxO → UTxO → (Tx ℓ) → TxOut → Maybe P2Script
136-
txOutToP2Script utxoSpend₀ utxoRefView tx (a , _) =
136+
txOutToP2Script utxo₀ utxoRefView tx (a , _) =
137137
do sh ← isScriptObj (payCred a)
138-
s ← lookupScriptHash sh tx utxoSpend₀ utxoRefView
138+
s ← lookupScriptHash sh tx utxo₀ utxoRefView
139139
toP2Script s
140140
141141
opaque

src/Ledger/Dijkstra/Specification/Transaction.lagda.md

Lines changed: 134 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -283,38 +283,47 @@ could be either of them.
283283

284284
<!--
285285
```agda
286+
-- Level-Dependent Type Classes --
286287
record HasTxBody {txLevel} {a} (A : Type a) : Type a where
287288
field TxBodyOf : A → TxBody txLevel
288289
open HasTxBody ⦃...⦄ public
289290
290-
record HasTxId {a} (A : Type a) : Type a where
291-
field TxIdOf : A → TxId
292-
open HasTxId ⦃...⦄ public
293-
294-
record HasFees? {a} (A : Type a) : Type a where
295-
field FeesOf? : A → Maybe Fees
296-
open HasFees? ⦃...⦄ public
297-
298-
record HasDCerts {a} (A : Type a) : Type a where
299-
field DCertsOf : A → List DCert
300-
open HasDCerts ⦃...⦄ public
301-
302-
record HasData {a} (A : Type a) : Type a where
303-
field DataOf : A → ℙ Datum
304-
open HasData ⦃...⦄ public
305-
306291
record HasTxWitnesses {txLevel} {a} (A : Type a) : Type a where
307292
field TxWitnessesOf : A → TxWitnesses txLevel
308293
open HasTxWitnesses ⦃...⦄ public
309294
310-
record HasValue {a} (A : Type a) : Type a where
311-
field ValueOf : A → Value
312-
open HasValue ⦃...⦄ public
295+
record HasRedeemers {txLevel} {a} (A : Type a) : Type a where
296+
field RedeemersOf : A → RedeemerPtr txLevel ⇀ Redeemer × ExUnits
297+
open HasRedeemers ⦃...⦄ public
298+
299+
-- (top-level only) --
300+
record HasCollateralInputs {txLevel} {a} (A : Type a) : Type a where
301+
field CollateralInputsOf : A → InTopLevel txLevel (ℙ TxIn)
302+
open HasCollateralInputs ⦃...⦄ public
303+
304+
record HasTxFees {txLevel} {a} (A : Type a) : Type a where
305+
field TxFeesOf : A → InTopLevel txLevel Fees
306+
open HasTxFees ⦃...⦄ public
313307
314308
record HasSubTransactions {txLevel} {a} (A : Type a) : Type a where
315309
field SubTransactionsOf : A → InTopLevel txLevel (List (Tx TxLevelSub))
316310
open HasSubTransactions ⦃...⦄ public
317311
312+
-- (sub-level only) --
313+
record HasTopLevelGuards {txLevel} {a} (A : Type a) : Type a where
314+
field TopLevelGuardsOf : A → InSubLevel txLevel (ℙ (Credential × Maybe Datum))
315+
open HasTopLevelGuards ⦃...⦄ public
316+
317+
318+
-- Level-Independent Type Classes --
319+
record HasTxId {a} (A : Type a) : Type a where
320+
field TxIdOf : A → TxId
321+
open HasTxId ⦃...⦄ public
322+
323+
record HasSize {a} (A : Type a) : Type a where
324+
field SizeOf : A → ℕ
325+
open HasSize ⦃...⦄ public
326+
318327
record HasSpendInputs {a} (A : Type a) : Type a where
319328
field SpendInputsOf : A → ℙ TxIn
320329
open HasSpendInputs ⦃...⦄ public
@@ -323,6 +332,26 @@ could be either of them.
323332
field ReferenceInputsOf : A → ℙ TxIn
324333
open HasReferenceInputs ⦃...⦄ public
325334
335+
record HasIndexedOutputs {a} (A : Type a) : Type a where
336+
field IndexedOutputsOf : A → Ix ⇀ TxOut
337+
open HasIndexedOutputs ⦃...⦄ public
338+
339+
record HasMintedValue {a} (A : Type a) : Type a where
340+
field MintedValueOf : A → Value
341+
open HasMintedValue ⦃...⦄ public
342+
343+
record HasFees? {a} (A : Type a) : Type a where
344+
field FeesOf? : A → Maybe Fees
345+
open HasFees? ⦃...⦄ public
346+
347+
record HasDCerts {a} (A : Type a) : Type a where
348+
field DCertsOf : A → List DCert
349+
open HasDCerts ⦃...⦄ public
350+
351+
record HasData {a} (A : Type a) : Type a where
352+
field DataOf : A → ℙ Datum
353+
open HasData ⦃...⦄ public
354+
326355
record HasGovProposals {a} (A : Type a) : Type a where
327356
field GovProposalsOf : A → List GovProposal
328357
open HasGovProposals ⦃...⦄ public
@@ -331,16 +360,45 @@ could be either of them.
331360
field GovVotesOf : A → List GovVote
332361
open HasGovVotes ⦃...⦄ public
333362
363+
record HasGuards {a} (A : Type a) : Type a where
364+
field GuardsOf : A → ℙ Credential
365+
open HasGuards ⦃...⦄ public
366+
367+
record HasScripts {a} (A : Type a) : Type a where
368+
field ScriptsOf : A → ℙ Script
369+
open HasScripts ⦃...⦄ public
370+
334371
instance
335372
HasTxBody-Tx : HasTxBody (Tx txLevel)
336373
HasTxBody-Tx .TxBodyOf = Tx.txBody
337374
338375
HasTxWitnesses-Tx : HasTxWitnesses (Tx txLevel)
339376
HasTxWitnesses-Tx .TxWitnessesOf = Tx.txWitnesses
340377
378+
HasRedeemers-TxWitnesses : HasRedeemers (TxWitnesses txLevel)
379+
HasRedeemers-TxWitnesses .RedeemersOf = TxWitnesses.txRedeemers
380+
381+
HasRedeemers-Tx : HasRedeemers (Tx txLevel)
382+
HasRedeemers-Tx .RedeemersOf = RedeemersOf ∘ TxWitnessesOf
383+
384+
HasCollateralInputs-TopLevelTx : HasCollateralInputs TopLevelTx
385+
HasCollateralInputs-TopLevelTx .CollateralInputsOf = TxBody.collateralInputs ∘ TxBodyOf
386+
387+
HasTxFees-TopLevelTx : HasTxFees TopLevelTx
388+
HasTxFees-TopLevelTx .TxFeesOf = TxBody.txFee ∘ TxBodyOf
389+
390+
HasSubTransactions-TopLevelTx : HasSubTransactions TopLevelTx
391+
HasSubTransactions-TopLevelTx .SubTransactionsOf = TxBody.txSubTransactions ∘ TxBodyOf
392+
393+
HasTopLevelGuards-SubLevelTx : HasTopLevelGuards SubLevelTx
394+
HasTopLevelGuards-SubLevelTx .TopLevelGuardsOf = TxBody.txRequiredTopLevelGuards ∘ TxBodyOf
395+
341396
HasDCerts-TxBody : HasDCerts (TxBody txLevel)
342397
HasDCerts-TxBody .DCertsOf = TxBody.txCerts
343398
399+
HasDCerts-Tx : HasDCerts (Tx txLevel)
400+
HasDCerts-Tx .DCertsOf = DCertsOf ∘ TxBodyOf
401+
344402
HasWithdrawals-TxBody : HasWithdrawals (TxBody txLevel)
345403
HasWithdrawals-TxBody .WithdrawalsOf = TxBody.txWithdrawals
346404
@@ -359,8 +417,17 @@ could be either of them.
359417
HasReferenceInputs-Tx : HasReferenceInputs (Tx txLevel)
360418
HasReferenceInputs-Tx .ReferenceInputsOf = ReferenceInputsOf ∘ TxBodyOf
361419
362-
HasValue-TxBody : HasValue (TxBody txLevel)
363-
HasValue-TxBody .ValueOf = TxBody.mint
420+
HasIndexedOutputs-TxBody : HasIndexedOutputs (TxBody txLevel)
421+
HasIndexedOutputs-TxBody .IndexedOutputsOf = TxBody.txOuts
422+
423+
HasIndexedOutputs-Tx : HasIndexedOutputs (Tx txLevel)
424+
HasIndexedOutputs-Tx .IndexedOutputsOf = IndexedOutputsOf ∘ TxBodyOf
425+
426+
HasMintedValue-TxBody : HasMintedValue (TxBody txLevel)
427+
HasMintedValue-TxBody .MintedValueOf = TxBody.mint
428+
429+
HasMintedValue-Tx : HasMintedValue (Tx txLevel)
430+
HasMintedValue-Tx .MintedValueOf = MintedValueOf ∘ TxBodyOf
364431
365432
HasGovVotes-TxBody : HasGovVotes (TxBody txLevel)
366433
HasGovVotes-TxBody .GovVotesOf = TxBody.txGovVotes
@@ -381,11 +448,17 @@ could be either of them.
381448
HasFees?-Tx : HasFees? (Tx txLevel)
382449
HasFees?-Tx .FeesOf? = FeesOf? ∘ TxBodyOf
383450
384-
HasSubTransactions-TopLevelTx : HasSubTransactions TopLevelTx
385-
HasSubTransactions-TopLevelTx .SubTransactionsOf = TxBody.txSubTransactions ∘ TxBodyOf
451+
HasTxId-TxBody : HasTxId (TxBody txLevel)
452+
HasTxId-TxBody .TxIdOf = TxBody.txId
386453
387454
HasTxId-Tx : HasTxId (Tx txLevel)
388-
HasTxId-Tx .TxIdOf = TxBody.txId ∘ TxBodyOf
455+
HasTxId-Tx .TxIdOf = TxIdOf ∘ TxBodyOf
456+
457+
HasDonations-TxBody : HasDonations (TxBody txLevel)
458+
HasDonations-TxBody .DonationsOf = TxBody.txDonation
459+
460+
HasDonations-Tx : HasDonations (Tx txLevel)
461+
HasDonations-Tx .DonationsOf = DonationsOf ∘ TxBodyOf
389462
390463
HasCoin-TxOut : HasCoin TxOut
391464
HasCoin-TxOut .getCoin = coin ∘ proj₁ ∘ proj₂
@@ -395,6 +468,18 @@ could be either of them.
395468
396469
HasData-Tx : HasData (Tx txLevel)
397470
HasData-Tx .DataOf = DataOf ∘ TxWitnessesOf
471+
472+
HasGuards-TxBody : HasGuards (TxBody txLevel)
473+
HasGuards-TxBody .GuardsOf = TxBody.txGuards
474+
475+
HasGuards-Tx : HasGuards (Tx txLevel)
476+
HasGuards-Tx .GuardsOf = GuardsOf ∘ TxBodyOf
477+
478+
HasScripts-TxWitnesses : HasScripts (TxWitnesses txLevel)
479+
HasScripts-TxWitnesses .ScriptsOf = TxWitnesses.scripts
480+
481+
HasScripts-Tx : HasScripts (Tx txLevel)
482+
HasScripts-Tx .ScriptsOf = ScriptsOf ∘ TxWitnessesOf
398483
```
399484
-->
400485

@@ -426,47 +511,46 @@ This section collects some unimportant but useful helper and accessor functions.
426511
```
427512

428513
In the Dijkstra era, *spending* inputs must exist in the initial UTxO snapshot, while
429-
*reference* inputs may see earlier outputs, so we need two UTxO views:
514+
*reference* inputs may come from earlier outputs, so we will need two to keep track
515+
of two UTxOs; we'll denote these as follows:
430516

431-
+ `utxoSpend`{.AgdaBound}, the initial snapshot, used for `txIns`{.AgdaField},
432-
+ `utxoRefView`{.AgdaBound}, the evolving view, used for `refInputs`{.AgdaField}.
517+
+ `utxo`{.AgdaBound}, the initial UTxO snapshot, used for `txIns`{.AgdaField};
518+
+ `utxoRef`{.AgdaBound}, the evolving UTxO, used for reference input lookups.
433519

434-
Thus functions like `refScripts`{.AgdaFunction}, `txscripts`{.AgdaFunction},
435-
and `lookupScriptHash`{.AgdaFunction} (defined below) will take *two* UTxO arguments.
520+
We now define some functions for scripts. Some of these will take two UTxO
521+
arguments, denoting the initial UTxO snapshot and an evolving UTxO, which evolves as
522+
a batch of subtransactions is processed. (Later, when the functions below are used,
523+
the two UTxO arguments may come from the UTxO environment and an evolving UTxO state;
524+
types for these are defined in the `Utxo`{.AgdaModule} module, which depends
525+
on the present module; thus, we cannot bind the UTxO arguments to a particular
526+
UTxO environment and state at this point.)
436527

437528
```agda
438529
refScripts : Tx txLevel → UTxO → UTxO → List Script
439-
refScripts tx utxoSpend₀ utxoRefView =
440-
mapMaybe (proj₂ ∘ proj₂ ∘ proj₂) ( setToList (range (utxoSpend₀ ∣ txIns))
441-
++ setToList (range (utxoRefView ∣ refInputs))
442-
)
443-
where open Tx tx; open TxBody (TxBodyOf tx)
530+
refScripts tx utxo₀ utxoRef =
531+
mapMaybe (proj₂ ∘ proj₂ ∘ proj₂)
532+
( setToList (range (utxo₀ ∣ SpendInputsOf tx))
533+
++ setToList (range (utxoRef ∣ ReferenceInputsOf tx)) )
444534
445535
txscripts : Tx txLevel → UTxO → UTxO → ℙ Script
446-
txscripts tx utxoSpend₀ utxoRefView =
447-
scripts (tx .txWitnesses) ∪ fromList (refScripts tx utxoSpend₀ utxoRefView)
448-
where open Tx; open TxWitnesses
536+
txscripts tx utxo₀ utxoRef = ScriptsOf tx ∪ fromList (refScripts tx utxo₀ utxoRef)
449537
450538
lookupScriptHash : ScriptHash → Tx txLevel → UTxO → UTxO → Maybe Script
451-
lookupScriptHash sh tx utxoSpend₀ utxoRefView =
539+
lookupScriptHash sh tx utxo₀ utxoRef =
452540
if sh ∈ mapˢ proj₁ (m ˢ) then just (lookupᵐ m sh) else nothing
453-
where m = setToMap (mapˢ < hash , id > (txscripts tx utxoSpend₀ utxoRefView))
454-
455-
-- TODO: implement the actual evolving `utxoRefView` (issue #1006)
541+
where m = setToMap (mapˢ < hash , id > (txscripts tx utxo₀ utxoRef))
456542
457543
getSubTxScripts : SubLevelTx → ℙ (TxId × ScriptHash)
458544
getSubTxScripts subtx = mapˢ (λ hash → (TxIdOf subtx , hash)) (ScriptHashes subtx)
459545
where
460546
ScriptHashes : Tx TxLevelSub → ℙ ScriptHash
461-
ScriptHashes tx = -- `txRequiredTopLevelGuards`
462-
mapPartial (isScriptObj ∘ proj₁) -- has key creds too, but only
463-
(TxBody.txRequiredTopLevelGuards (TxBodyOf tx)) -- `ScriptObj hash` contributes
464-
-- a phase-2 script hash.
547+
ScriptHashes tx = mapPartial (isScriptObj ∘ proj₁) (TopLevelGuardsOf tx)
548+
-- `txRequiredTopLevelGuards` has key creds too, but only
549+
-- `ScriptObj hash` contributes a phase-2 script hash.
465550
466551
getTxScripts : {ℓ : TxLevel} → Tx ℓ → ℙ (TxId × ScriptHash)
467552
getTxScripts {TxLevelSub} = getSubTxScripts
468-
getTxScripts {TxLevelTop} =
469-
concatMapˢ getSubTxScripts ∘ fromList ∘ TxBody.txSubTransactions ∘ TxBodyOf
553+
getTxScripts {TxLevelTop} = concatMapˢ getSubTxScripts ∘ fromList ∘ SubTransactionsOf
470554
```
471555

472556
CIP-0118 models "required top-level guards" as a list of requirements coming
@@ -524,17 +608,16 @@ remaining helper functions of this section.
524608
525609
subTxTaggedGuards : SubLevelTx → ℙ TaggedTopLevelGuard
526610
subTxTaggedGuards subtx =
527-
mapˢ (λ (cred , md) → (TxIdOf subtx , cred , md))
528-
(TxBody.txRequiredTopLevelGuards (TxBodyOf subtx))
611+
mapˢ (λ (cred , md) → (TxIdOf subtx , cred , md)) (TopLevelGuardsOf subtx)
529612
530613
-- Turn a subTx body's `txRequiredTopLevelGuards` into a set of guard credentials.
531614
subTxGuardCredentials : SubLevelTx → ℙ Credential
532-
subTxGuardCredentials = (mapˢ proj₁) ∘ (TxBody.txRequiredTopLevelGuards ∘ TxBodyOf)
615+
subTxGuardCredentials = (mapˢ proj₁) ∘ TopLevelGuardsOf
533616
534617
-- Phase-1 condition (CIP-0118):
535618
-- every credential required by a subTx body must appear in the top-level txGuards set.
536619
requiredTopLevelGuardsSatisfied : TopLevelTx → List SubLevelTx → Type
537-
requiredTopLevelGuardsSatisfied topTx subTxs = requiredCreds ⊆ TxBody.txGuards (TxBodyOf topTx)
620+
requiredTopLevelGuardsSatisfied topTx subTxs = requiredCreds ⊆ GuardsOf topTx
538621
where
539622
concatMapˡ : {A B : Type} → (A → ℙ B) → List A → ℙ B
540623
concatMapˡ f as = proj₁ $ unions (fromList (map f as))

src/Ledger/Dijkstra/Specification/Utxo.lagda.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ open import Ledger.Dijkstra.Specification.Script.Validation txs abs
3333

3434
## Environments and states
3535

36-
(copied shape from Conway; semantics TBD)
3736

3837
```agda
3938
record UTxOEnv : Type where
@@ -42,7 +41,13 @@ record UTxOEnv : Type where
4241
pparams : PParams
4342
treasury : Treasury
4443
utxo₀ : UTxO
44+
```
45+
46+
Note that the `utxo`{.AgdaField} field of `UTxOEnv`{.AgdaRecord} is renamed in the
47+
Dijkstra era as `utxo₀`{.AgdaField} to emphasize the fact that it denotes the
48+
*snapshot* UTxO before any of the batch transactions are applied.
4549

50+
```agda
4651
record UTxOState : Type where
4752
field
4853
utxo : UTxO
@@ -51,6 +56,7 @@ record UTxOState : Type where
5156
donations : Donations
5257
```
5358

59+
5460
<!--
5561
```agda
5662
instance

0 commit comments

Comments
 (0)