Part II · Checkpoint 2 CP 2 · 40 min · ProveIt/Checkpoints/Cp2.lean

Checkpoint 2: Crash-Safe WAL Ledger

Repair a write-ahead log replay double-credit bug, strengthen the ledger invariant, and prove conservation across arbitrary crash loops.

Checkpoint Bug · Crash Between Balance Update and ACK
structure WalLedger where
  balance   : Nat
  appliedTx : List Nat -- committed tx IDs persisted in WAL
-- Buggy recovery re-runs `balance := balance + tx.amount` without checking `tx.id ∈ appliedTx`!
-- After 1 crash-restart, a $500 deposit credits $1,000.
Socratic Gate · Commit to a Prediction Unanswered · Locks on commit

After adding the deduplication guard `if txId ∈ s.appliedTx then s else ...`, what inductive invariant links `s.appliedTx` to replaying `txId` after `k` crashes?

Crash-Recovery Verification Pattern

Storage engines, Kafka consumers, and Stripe-style payment ledgers achieve effectively-once state updates over at-least-once delivery by persisting transaction IDs inside the same atomic WAL batch as the balance mutation. Proving idempotence + monotonic appliedTx membership guarantees zero double-credits across arbitrary crash-restart traces.

⊢ ProveIt/Checkpoints/Cp2.lean · Idempotent WAL Recovery
Step 1 / 1
Lean Infoview Open Goal
Hands-On Exercise (ProveIt/Checkpoints/Cp2.lean):

Complete `ProveIt/Checkpoints/Cp2.lean` locally and run `lake exe check cp2`.

Finished the Socratic gate, Infoview trace, and playground run?
Record completion in local browser storage to track your 20-unit progress.