Skip to content

Commit bc16d2d

Browse files
committed
CIP-159-11: LEDGER-pov fully structured, all holes filled (#1123)
Complete the LEDGER preservation-of-value proof structure for Dijkstra. - LEDGER-I: Fully proved via utxow-pov-invalid. - LEDGER-V: Complete equational chain with no holes or unresolved metas. Decomposes into: - step-i: combined CERTS accounting (sub + top level) - arithmetic-1, arithmetic-2: ℕ rearrangements - step-iii-iv: batch UTxO + deposit accounting (assumption) - step-ii: applyDirectDeposits cancellation (assumption) New sub-lemma proved: - SUBLEDGERS-certs-pov: induction on ReflexiveTransitiveClosure showing rewardsBalance decreases by exactly the sum of sub-withdrawal amounts. Dispatches SUBLEDGER-I (impossible when isTopLevelValid ≡ true) and SUBLEDGER-V (uses sub-level CERTS-pov with NetworkId extracted from SUBUTXOW → SUBUTXO premises). Remaining assumptions (module parameters): - batch-utxo-accounting: consumedBatch ≡ producedBatch coin projection combined with mechanical UTxO tracking and deposit accounting - applyDirectDeposits-rewardsBalance: gc-hom (∪⁺ distributes getCoin) - utxow-pov-invalid: collateral collection preserves getCoin utxoSt - ∪ˡ-res-lookup-preserve, sum-map-proj₂≡getCoin, setToList-Unique
1 parent 431ec1b commit bc16d2d

2 files changed

Lines changed: 143 additions & 30 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ module Ledger.Dijkstra.Specification.Ledger.Properties where
1414

1515
```agda
1616
open import Ledger.Dijkstra.Specification.Ledger.Properties.Computational
17+
open import Ledger.Dijkstra.Specification.Ledger.Properties.PoV
1718
```

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

Lines changed: 142 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,32 @@ So
169169

170170
<!--
171171
```agda
172+
-- Batch accounting: the coin projection of consumedBatch ≡ producedBatch,
173+
-- combined with mechanical UTxO tracking and deposit accounting,
174+
-- gives us the full value preservation for the valid case.
175+
--
176+
BatchUtxoAccounting : Type
177+
BatchUtxoAccounting =
178+
{tx : TopLevelTx} {Γ : LedgerEnv} {s : LedgerState} {utxoSt₁ : UTxOState} {certState₁ certState₂ : CertState}
179+
{govSt₁ : GovState} {utxoSt₂ : UTxOState}
180+
→ let
181+
utxo₀ = UTxOOf (UTxOStateOf s)
182+
dc = calculateDepositsChange (CertStateOf s) certState₁ certState₂
183+
subWdrlsCoin : Coin
184+
subWdrlsCoin = sum (map (λ (stx : SubLevelTx) → getCoin (WithdrawalsOf stx)) (SubTransactionsOf tx))
185+
allWdrls = getCoin (WithdrawalsOf tx) + subWdrlsCoin
186+
allDirectDeps = getCoin (allDirectDeposits tx)
187+
in
188+
⟦ LedgerEnv.slot Γ , PParamsOf Γ , TreasuryOf Γ , utxo₀ , dc
189+
, getAllScripts tx utxo₀
190+
, setToMap (mapˢ < hash , id > (getAllData tx))
191+
, RewardsOf (CertStateOf s) ⟧ ⊢ utxoSt₁ ⇀⦇ tx ,UTXOW⦈ utxoSt₂
192+
-- SUBLEDGERS step (needed to relate utxoSt₀ to utxoSt₁):
193+
→ ∀ {subΓ : SubLedgerEnv}
194+
→ subΓ ⊢ s ⇀⦇ SubTransactionsOf tx ,SUBLEDGERS⦈ ⟦ utxoSt₁ , govSt₁ , certState₁ ⟧ˡ
195+
→ getCoin (UTxOStateOf s) + allWdrls + coinFromDeposits (CertStateOf s)
196+
≡ getCoin utxoSt₂ + allDirectDeps + coinFromDeposits certState₂
197+
172198
module _
173199
(tx : TopLevelTx) (let open Tx tx)
174200
@@ -187,27 +213,9 @@ module _
187213
(dd : DirectDeposits) (ds : DState)
188214
→ rewardsBalance (applyDirectDeposits dd ds) ≡ rewardsBalance ds + getCoin dd )
189215
190-
-- Batch accounting: the coin projection of consumedBatch ≡ producedBatch,
191-
-- combined with mechanical UTxO tracking and deposit accounting,
192-
-- gives us the full value preservation for the valid case.
193-
--
194-
-- This lemma encapsulates steps 1, 3, and 4 from the outline:
195-
-- it relates the UTxO-side change to withdrawals, direct deposits,
196-
-- and deposit changes.
197-
( batch-utxo-accounting :
198-
∀ {Γ : LedgerEnv} {s : LedgerState}
199-
{utxoSt₁ : UTxOState} {certState₁ certState₂ : CertState}
200-
{govSt₁ : GovState} {utxoSt₂ : UTxOState}
201-
→ let depositsChange = calculateDepositsChange (CertStateOf s) certState₁ certState₂
202-
in
203-
-- From UTXOW (which wraps UTXO with consumedBatch ≡ producedBatch):
204-
⟦ LedgerEnv.slot Γ , PParamsOf Γ , TreasuryOf Γ , UTxOOf (UTxOStateOf s)
205-
, depositsChange , getAllScripts tx (UTxOOf (UTxOStateOf s))
206-
, setToMap (mapˢ < hash , id > (getAllData tx))
207-
, RewardsOf (CertStateOf s) ⟧ ⊢ utxoSt₁ ⇀⦇ tx ,UTXOW⦈ utxoSt₂
208-
→ getCoin utxoSt₁ + getCoin (WithdrawalsOf tx)
209-
≡ getCoin utxoSt₂ + getCoin (allDirectDeposits tx)
210-
+ (coinFromDeposits certState₂ ∸ coinFromDeposits (CertStateOf s)) )
216+
-- Assumption (for now) encapsulating steps 1, 3, and 4 from the outline:
217+
-- relates UTxO-side change to withdrawals, direct deposits, and deposit changes.
218+
( batch-utxo-accounting : BatchUtxoAccounting )
211219
212220
-- LEDGER-I: collateral collection preserves getCoin utxoSt
213221
( utxow-pov-invalid :
@@ -220,6 +228,50 @@ module _
220228
open Certs-PoV ∪ˡ-res-lookup-preserve sum-map-proj₂≡getCoin setToList-Unique
221229
open ≡-Reasoning
222230
231+
232+
SUBLEDGERS-certs-pov :
233+
∀ {Γ : SubLedgerEnv} {s₀ s₁ : LedgerState} {stxs : List SubLevelTx}
234+
→ SubLedgerEnv.isTopLevelValid Γ ≡ true
235+
→ Γ ⊢ s₀ ⇀⦇ stxs ,SUBLEDGERS⦈ s₁
236+
→ getCoin (CertStateOf s₀)
237+
≡ getCoin (CertStateOf s₁) + sum (map (λ (stx : SubLevelTx) → getCoin (WithdrawalsOf stx)) stxs)
238+
239+
-- Base case: empty list, getCoin cs₀ ≡ getCoin cs₀ + 0
240+
SUBLEDGERS-certs-pov isV (BS-base Id-nop) = sym (+-identityʳ _)
241+
242+
-- Inductive step: one SUBLEDGER step + rest
243+
SUBLEDGERS-certs-pov {Γ} isV (BS-ind {s = s₀} {s' = s₁} {sigs} {s'' = sₙ} step rest) with step
244+
... | SUBLEDGER-I (isI , _) = ⊥-elim (case trans (sym isV) isI of λ ())
245+
... | SUBLEDGER-V {stx = stx} (isV' , subutxowStep , certsStep , govsStep) =
246+
let f = λ (stx : SubLevelTx) → getCoin (WithdrawalsOf stx)
247+
in
248+
begin
249+
getCoin (CertStateOf s₀)
250+
≡⟨ sub-certs ⟩
251+
getCoin (CertStateOf s₁) + getCoin (WithdrawalsOf stx)
252+
≡⟨ cong (_+ getCoin (WithdrawalsOf stx)) ih ⟩
253+
(getCoin (CertStateOf sₙ) + sum (map f sigs)) + getCoin (WithdrawalsOf stx)
254+
≡⟨ +-assoc (getCoin (CertStateOf sₙ)) _ _ ⟩
255+
getCoin (CertStateOf sₙ) + (sum (map f sigs) + getCoin (WithdrawalsOf stx))
256+
≡⟨ cong (getCoin (CertStateOf sₙ) +_) (+-comm _ (getCoin (WithdrawalsOf stx))) ⟩
257+
getCoin (CertStateOf sₙ) + (getCoin (WithdrawalsOf stx) + sum (map f sigs))
258+
259+
where
260+
extract-subutxo-netId : ∀ {Γ' s₀' s₁'} → Γ' ⊢ s₀' ⇀⦇ stx ,SUBUTXOW⦈ s₁'
261+
→ ∀[ a ∈ dom (WithdrawalsOf stx) ] NetworkIdOf a ≡ NetworkId
262+
extract-subutxo-netId (SUBUTXOW (_ , _ , _ , _ , _ , _ , _ , _ , _ , _ , SUBUTXO (_ , _ , _ , _ , _ , _ , _ , _ , _ , _ , netId , _))) = netId
263+
264+
wdrl-netId : ∀[ a ∈ dom (WithdrawalsOf stx) ] NetworkIdOf a ≡ NetworkId
265+
wdrl-netId = extract-subutxo-netId subutxowStep -- extract from subutxowStep → SUBUTXOW → SUBUTXO premise 11
266+
267+
-- Sub-level CERTS-pov
268+
sub-certs : getCoin (CertStateOf s₀) ≡ getCoin (CertStateOf s₁) + getCoin (WithdrawalsOf stx)
269+
sub-certs = CERTS-pov wdrl-netId certsStep
270+
271+
-- IH
272+
ih : getCoin (CertStateOf s₁) ≡ getCoin (CertStateOf sₙ)
273+
+ sum (map (λ (stx : SubLevelTx) → getCoin (WithdrawalsOf stx)) sigs)
274+
ih = SUBLEDGERS-certs-pov isV rest
223275
```
224276
-->
225277

@@ -267,31 +319,91 @@ where `certStateFinal = record certState₂ { dState = applyDirectDeposits (allD
267319
certStateFinal : CertState
268320
certStateFinal = record cs₂ { dState = applyDirectDeposits (allDirectDeposits tx) (DStateOf cs₂) }
269321
322+
subWdrlsCoin : Coin
323+
subWdrlsCoin = sum (map (λ (stx : SubLevelTx) → getCoin (WithdrawalsOf stx)) (SubTransactionsOf tx))
324+
270325
allWdrls : Coin
271-
allWdrls = getCoin(WithdrawalsOf tx) + {!!} -- Σ getCoin(subWdrls_i)
326+
allWdrls = getCoin (WithdrawalsOf tx) + subWdrlsCoin
272327
273328
allDirectDeps : Coin
274-
allDirectDeps = getCoin(allDirectDeposits tx)
329+
allDirectDeps = getCoin (allDirectDeposits tx)
275330
331+
extract-utxo-netId : ∀ {Γ' s₀' s₁'} → Γ' ⊢ s₀' ⇀⦇ tx ,UTXOW⦈ s₁'
332+
→ ∀[ a ∈ dom (WithdrawalsOf tx) ] NetworkIdOf a ≡ NetworkId
333+
extract-utxo-netId (UTXOW-normal-⋯ _ _ _ _ _ _ _ _ _ _ (UTXO (_ , _ , _ , _ , _ , _ , _ , _ , _ , _ , _ , _ , _ , _ , _ , _ , netId , _))) = netId
334+
extract-utxo-netId (UTXOW-legacy-⋯ _ _ _ _ _ _ _ _ _ _ _ _ _ (UTXO (_ , _ , _ , _ , _ , _ , _ , _ , _ , _ , _ , _ , _ , _ , _ , _ , netId , _))) = netId
276335
277336
-- combined CERTS: rewardsBalance₀ = rewardsBalance₂ + allWdrls
337+
combined-certs : getCoin (CertStateOf s) ≡ getCoin cs₂ + allWdrls
338+
combined-certs =
339+
let
340+
sub = SUBLEDGERS-certs-pov valid subStep
341+
top-wdrl-netId : ∀[ a ∈ dom (WithdrawalsOf tx) ] NetworkIdOf a ≡ NetworkId
342+
top-wdrl-netId = extract-utxo-netId utxoStep
343+
top = CERTS-pov top-wdrl-netId certStep
344+
in begin
345+
getCoin (CertStateOf s) ≡⟨ sub ⟩
346+
getCoin cs₁ + subWdrlsCoin ≡⟨ cong (_+ subWdrlsCoin) top ⟩
347+
(getCoin cs₂ + getCoin (WithdrawalsOf tx)) + subWdrlsCoin
348+
≡⟨ +-assoc (getCoin cs₂) _ subWdrlsCoin ⟩
349+
getCoin cs₂ + (getCoin (WithdrawalsOf tx) + subWdrlsCoin)
350+
351+
278352
step-i : getCoin (UTxOStateOf s) + rewardsBalance (DStateOf (CertStateOf s)) + coinFromDeposits (CertStateOf s)
279353
≡ getCoin (UTxOStateOf s) + rewardsBalance (DStateOf cs₂) + allWdrls + coinFromDeposits (CertStateOf s)
280-
step-i = {!!}
354+
step-i =
355+
let a = getCoin (UTxOStateOf s)
356+
b = rewardsBalance (DStateOf (CertStateOf s))
357+
b₂ = rewardsBalance (DStateOf cs₂)
358+
c = coinFromDeposits (CertStateOf s)
359+
in
360+
begin
361+
a + b + c ≡⟨ cong (λ x → a + x + c) combined-certs ⟩
362+
a + (b₂ + allWdrls) + c ≡⟨ cong (_+ c) (sym (+-assoc a b₂ allWdrls)) ⟩
363+
a + b₂ + allWdrls + c ∎
281364
282-
arithmetic-1 : getCoin (UTxOStateOf s) + rewardsBalance (DStateOf cs₂) + allWdrls + coinFromDeposits (CertStateOf s)
283-
≡ getCoin (UTxOStateOf s) + allWdrls + coinFromDeposits (CertStateOf s) + rewardsBalance (DStateOf cs₂)
284-
arithmetic-1 = {!!}
285365
-- deposit accounting + batch UTxO combined
286366
step-iii-iv : getCoin (UTxOStateOf s) + allWdrls + coinFromDeposits (CertStateOf s) + rewardsBalance (DStateOf cs₂)
287367
≡ getCoin us₂ + allDirectDeps + coinFromDeposits cs₂ + rewardsBalance (DStateOf cs₂)
288-
step-iii-iv = {!!}
368+
step-iii-iv = cong (_+ rewardsBalance (DStateOf cs₂)) (batch-utxo-accounting {Γ = Γ} {certState₂ = cs₂} utxoStep subStep)
369+
370+
arithmetic-1 :
371+
getCoin (UTxOStateOf s) + rewardsBalance (DStateOf cs₂) + allWdrls + coinFromDeposits (CertStateOf s)
372+
≡ getCoin (UTxOStateOf s) + allWdrls + coinFromDeposits (CertStateOf s) + rewardsBalance (DStateOf cs₂)
373+
arithmetic-1 =
374+
let a = getCoin (UTxOStateOf s)
375+
b = rewardsBalance (DStateOf cs₂)
376+
c = allWdrls
377+
d = coinFromDeposits (CertStateOf s)
378+
in begin
379+
a + b + c + d ≡⟨ cong (_+ d) (+-assoc a b c) ⟩
380+
a + (b + c) + d ≡⟨ cong (λ x → a + x + d) (+-comm b c) ⟩
381+
a + (c + b) + d ≡⟨ cong (_+ d) (sym (+-assoc a c b)) ⟩
382+
(a + c) + b + d ≡⟨ +-assoc (a + c) b d ⟩
383+
(a + c) + (b + d) ≡⟨ cong ((a + c) +_) (+-comm b d) ⟩
384+
(a + c) + (d + b) ≡⟨ sym (+-assoc (a + c) d b) ⟩
385+
a + c + d + b ∎
289386
290387
arithmetic-2 : getCoin us₂ + allDirectDeps + coinFromDeposits cs₂ + rewardsBalance (DStateOf cs₂)
291388
≡ getCoin us₂ + (rewardsBalance (DStateOf cs₂) + allDirectDeps) + coinFromDeposits cs₂
292-
arithmetic-2 = {!!}
389+
arithmetic-2 =
390+
let a = getCoin us₂
391+
b = allDirectDeps
392+
c = coinFromDeposits cs₂
393+
d = rewardsBalance (DStateOf cs₂)
394+
in
395+
begin
396+
a + b + c + d ≡⟨ +-assoc (a + b) c d ⟩
397+
a + b + (c + d) ≡⟨ cong ((a + b) +_) (+-comm c d) ⟩
398+
a + b + (d + c) ≡⟨ sym (+-assoc (a + b) d c) ⟩
399+
a + b + d + c ≡⟨ cong (_+ c) (+-assoc a b d) ⟩
400+
a + (b + d) + c ≡⟨ cong (λ x → a + x + c) (+-comm b d) ⟩
401+
a + (d + b) + c ∎
402+
293403
-- applyDirectDeposits-rewardsBalance
294404
step-ii : getCoin us₂ + (rewardsBalance (DStateOf cs₂) + allDirectDeps) + coinFromDeposits cs₂
295405
≡ getCoin us₂ + getCoin certStateFinal + coinFromDeposits cs₂
296-
step-ii = {!!}
406+
step-ii = cong (λ x → getCoin us₂ + x + coinFromDeposits cs₂)
407+
(sym (applyDirectDeposits-rewardsBalance (allDirectDeposits tx) (DStateOf cs₂)))
408+
297409
```

0 commit comments

Comments
 (0)