Skip to content

Commit 4b99a0d

Browse files
committed
[Dijkstra] CIP-159-10: Apply batch-wide direct deposits in LEDGER rule (#1122)
After all sub-rule transitions (`SUBLEDGERS`, `CERTS`, `GOVS`, `UTXOW`), apply batch-wide direct deposits to the final CertState via `applyDirectDeposits` and `allDirectDeposits`. `Ledger.lagda.md`: + Update `LEDGER-V` output: compute `certStateFinal` by applying `allDirectDeposits` to `certState₂`, use `certStateFinal` in the output `LedgerState` and in `rmOrphanDRepVotes`; + `LEDGER-I` unchanged (invalid batches don't apply deposits); + Document direct deposit application ordering and phantom asset prevention rationale. `Ledger/Properties/Computational.lagda.md`: + Update `computeProof` valid branch to compute `certStateFinal` and use it in the output `LedgerState`.
1 parent 1f14032 commit 4b99a0d

3 files changed

Lines changed: 32 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
### WIP
66

7+
- Apply batch-wide direct deposits to `CertState` in `LEDGER-V` output (CIP-159).
78
- Forbid CIP-159 fields (`txDirectDeposits`, `txBalanceIntervals`) in legacy mode via explicit `UTXOW-legacy` premises.
8-
- Allow partial withdrawals in `PRE-CERT` rule; define `applyWithdrawals` and `_≤ᵐ_` (CIP-159).
9+
- Allow partial withdrawals in `PRE-CERT` rule; define `applyWithdrawals` (CIP-159).
910
- Extend `TxInfo` with `txDirectDeposits` and `txBalanceIntervals` fields (CIP-159).
1011
- Remove `allBalanceIntervals` batch-level aggregation helper; balance interval
1112
assertions will be checked per-(sub)transaction in `UTXO`/`SUBUTXO` rules (see #1117).

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,31 @@ data _⊢_⇀⦇_,SUBLEDGER⦈_ : SubLedgerEnv → LedgerState → SubLevelTx
305305
306306
_⊢_⇀⦇_,SUBLEDGERS⦈_ : SubLedgerEnv → LedgerState → List SubLevelTx → LedgerState → Type
307307
_⊢_⇀⦇_,SUBLEDGERS⦈_ = ReflexiveTransitiveClosure {sts = _⊢_⇀⦇_,SUBLEDGER⦈_}
308+
```
309+
310+
**Direct Deposit Application (CIP-159)**. After all sub-rule transitions
311+
(`SUBLEDGERS`{.AgdaDatatype}, `CERTS`{.AgdaDatatype}, `GOVS`{.AgdaDatatype},
312+
`UTXOW`{.AgdaDatatype}), batch-wide direct deposits are applied to the final
313+
`CertState`{.AgdaRecord}. The function `applyDirectDeposits`{.AgdaFunction} (from
314+
the `Certs`{.AgdaModule} module) adds deposit amounts to account balances via `∪⁺`,
315+
and `allDirectDeposits`{.AgdaFunction} (from the `Transaction`{.AgdaModule} module)
316+
aggregates direct deposits across the top-level transaction and all sub-transactions.
317+
318+
Direct deposits are applied *after* withdrawal processing (in `CERTS`{.AgdaDatatype})
319+
to ensure that withdrawals are checked against pre-batch balances. This prevents
320+
phantom asset attacks where a deposit from one sub-transaction inflates the balance
321+
available for withdrawal by another sub-transaction in the same batch.
322+
323+
`depositsChange`{.AgdaFunction} is computed from `certStateᵢ` (`i ∈ {0,1,2}`)
324+
(not `certStateFinal`) since it represents net deposit change across the batch
325+
(not direct deposit value transfers) and reflects registration/deregistration.
308326

327+
`rmOrphanDRepVotes` uses `certStateFinal` (not `certState₂`) so it sees
328+
the post-deposit `DRep` state. (In practice, `applyDirectDeposits`{.AgdaFunction}
329+
only modifies rewards, so `rmOrphanDRepVotes` would produce the same result either
330+
way, but using `certStateFinal` is semantically correct.)
309331

332+
```agda
310333
data _⊢_⇀⦇_,LEDGER⦈_ : LedgerEnv → LedgerState → TopLevelTx → LedgerState → Type where
311334
312335
LEDGER-V :
@@ -321,15 +344,17 @@ data _⊢_⇀⦇_,LEDGER⦈_ : LedgerEnv → LedgerState → TopLevelTx → Ledg
321344
322345
depositsChange : DepositsChange
323346
depositsChange = calculateDepositsChange certState₀ certState₁ certState₂
347+
348+
certStateFinal : CertState
349+
certStateFinal = record certState₂ { dState = applyDirectDeposits (allDirectDeposits tx) (DStateOf certState₂) }
324350
in
325351
∙ IsValidFlagOf tx ≡ true
326352
∙ ⟦ slot , ppolicy , pp , enactState , treasury , utxo₀ , IsValidFlagOf tx , allScripts , allData , RewardsOf certState₀ ⟧ ⊢ ⟦ utxoState₀ , govState₀ , certState₀ ⟧ ⇀⦇ SubTransactionsOf tx ,SUBLEDGERS⦈ ⟦ utxoState₁ , govState₁ , certState₁ ⟧
327353
∙ ⟦ epoch slot , pp , ListOfGovVotesOf tx , WithdrawalsOf tx , allColdCreds govState₁ enactState ⟧ ⊢ certState₁ ⇀⦇ DCertsOf tx ,CERTS⦈ certState₂
328354
∙ ⟦ TxIdOf tx , epoch slot , pp , ppolicy , enactState , certState₂ , dom (RewardsOf certState₂) ⟧ ⊢ govState₁ ⇀⦇ GovProposals+Votes tx ,GOVS⦈ govState₂
329355
∙ ⟦ slot , pp , treasury , utxo₀ , depositsChange , allScripts , allData , RewardsOf certState₀ ⟧ ⊢ utxoState₁ ⇀⦇ tx ,UTXOW⦈ utxoState₂
330356
────────────────────────────────
331-
⟦ slot , ppolicy , pp , enactState , treasury ⟧ ⊢ ⟦ utxoState₀ , govState₀ , certState₀ ⟧ ⇀⦇ tx ,LEDGER⦈ ⟦ utxoState₂ , rmOrphanDRepVotes certState₂ govState₂ , certState₂ ⟧
332-
357+
⟦ slot , ppolicy , pp , enactState , treasury ⟧ ⊢ ⟦ utxoState₀ , govState₀ , certState₀ ⟧ ⇀⦇ tx ,LEDGER⦈ ⟦ utxoState₂ , rmOrphanDRepVotes certStateFinal govState₂ , certStateFinal ⟧
333358
334359
LEDGER-I :
335360
let utxo₀ : UTxO

src/Ledger/Dijkstra/Specification/Ledger/Properties/Computational.lagda.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,9 +330,10 @@ instance
330330
-- UTXOW must run from the post-SUBLEDGERS UTxOState (utxoSt₁)
331331
computeUtxow utxoΓ utxoSt₁ txTop >>= λ where
332332
(utxoSt₂ , utxoStep) →
333-
let finalGov = rmOrphanDRepVotes certSt₂ govSt₂
333+
let certStateFinal = record certSt₂ { dState = applyDirectDeposits (allDirectDeposits txTop) (DStateOf certSt₂) }
334+
finalGov = rmOrphanDRepVotes certStateFinal govSt₂
334335
in
335-
success ( ⟦ utxoSt₂ , finalGov , certSt₂ ⟧ˡ
336+
success ( ⟦ utxoSt₂ , finalGov , certStateFinal ⟧ˡ
336337
, LEDGER-V (isV , subStep , certStep , govStep , utxoStep))
337338
338339
(no ¬isV) →

0 commit comments

Comments
 (0)