Skip to content

1.14 Holds and Two-Phase Operations: Exit Evidence

Evidence · Step: 1.14 Holds and Two-Phase Operations

Step 1.14 Holds and Two-Phase Operations
Date 2026-11-13
Database PostgreSQL 18.6 (postgres:18-alpine, 1 GB shared memory) in Docker Desktop on Windows 11
Result Passed
Deliverable File
Hold model crates/core/src/hold.rs (Hold, HoldStatus); hold fingerprints in crates/core/src/fingerprint.rs
Holds in the reference ledger MemLedger::place_hold, capture_hold, release_hold, expire_hold; INV-16, INV-17 and INV-18 in check_invariants
Holds trait crates/core/src/model/api.rs, implemented by MemLedger and PgStore
Holds on PostgreSQL crates/engine/src/holds.rs: place_hold, capture_hold, release_hold, expire_hold, all_holds
Expiry worker expire_due_holds in crates/engine/src/holds.rs; PgStore::expire_due_holds
Database migrations/0005_holds.{up,down}.sql: holds table, holds_guard trigger (MZ006), holds_due_idx
Verification INV-18 check in crates/engine/src/verification.rs (Finding::HoldBalance)
Tests crates/core/src/model/tests.rs (holds and the state machine), crates/engine/tests/holds.rs, crates/engine/tests/expiry.rs, and random hold operations in crates/engine/tests/differential.rs
Documentation db-schema.md, Holds, spec.md 4.3

The code arrived in three pull requests: the core and reference ledger (#32), PostgreSQL (#33), and the expiry worker (#34).

Task Done
holds table (id, account, held account, amount, status active|captured|released|expired, expires_at, entry ids) Yes, plus currency, captured, and one held account per hold
place_hold (an entry from wallet to held) Yes
capture_hold (full or partial, from held to destination, the remainder released) Yes: the capture and the return of the remainder are one entry
release_hold Yes, and expire_hold once expires_at has passed
An expiry worker that claims due holds with FOR UPDATE SKIP LOCKED and releases them idempotently Yes: one hold per transaction, under the key hold-expire:<hold id>
Status transitions enforced in the engine and the DB Yes: the engine refuses HoldNotActive; the holds_guard trigger raises MZ006 for any change to an ended hold, or any change other than ending it

Each hold has its own held account (such as 2020:wd-0001), and only the hold’s own entries may post to it: ordinary postings, adjustments and reversals that touch it are refused with AccountIsHeld. That is what makes INV-18 checkable account by account.

Requirement Test Result
Hold, then spend: available is reduced a_hold_reduces_what_can_be_spent (core); a_withdrawal_is_a_hold_that_is_captured (PostgreSQL) Pass: spending more than what is left is InsufficientFunds
Capturing twice: the second is rejected a_hold_is_captured_once_and_a_partial_capture_returns_the_rest; a_withdrawal_is_a_hold_that_is_captured Pass: HoldNotActive { status: Captured }
Capture racing expiry concurrently: exactly one wins capture_racing_expiry_has_exactly_one_winner (tests/expiry.rs) Pass: see the exit gate
Partial capture plus release equals the original a_partial_capture_returns_the_rest Pass: captured plus returned equals the hold
The worker crashes mid-batch and is restarted: no hold is released twice a_worker_killed_mid_batch_expires_no_hold_twice Pass: see below
Property test: the held account balance always equals SUM(active holds) invariants_hold_after_every_operation (core state machine, with random hold operations); the differential test Pass

The worker killed mid-batch. 240 holds are due. A worker starts expiring them on its own connection; once 20 have expired, the connection is terminated with pg_terminate_backend, as when the process dies. The worker returns an error, part-way through. A new worker then runs to the end. Afterwards:

  • all 240 holds are expired, and a third run finds nothing due
  • no hold has more than one closing entry
  • exactly 240 hold-expire: idempotency keys exist, one per hold
  • every held account is at zero, and every wallet is back to its original balance

Also tested. Hold operations are idempotent: a repeat returns the same hold, and the same key with a different request is IdempotencyKeyReused. With the engine bypassed, the database refuses:

  • reopening an ended hold (MZ006)
  • changing an active hold’s amount (MZ006)
  • capturing more than the hold (23514)
  • deleting a hold (MZ001)
  • changing a hold’s amount as the service role (42501)

Verification reports a held account that drifted:

INV-18 hold 00000000-0000-7000-8000-000000001389: held account 00000000-0000-7000-8000-000000000004 holds 299, not 300

The differential test runs random sequences of postings, reversals, adjustments and hold operations (place, capture, release, expire, time passing) on the reference ledger and on PostgreSQL. Results, errors, accounts, history and holds are identical in all 64 sequences.

“Held balance = sum of active holds” holds in the oracle and in PG under concurrency.

  • Oracle: check_invariants checks INV-18 after every operation of the property-based state machine.
  • PostgreSQL: held_balance_equals_active_holds_under_concurrency runs 200 holds over 10 wallets, placed, captured (fully or partly), released or left to expire, all at once. Meanwhile two expiry workers run against an advancing clock. A monitor on its own connection compares, in one snapshot, the balance of every held account with the sum of the active holds. They were equal in all 640 snapshots taken, and every held account was at zero once every hold had ended. The full verification at the end of the test was clean.

Pass.

Gate: the race test between capture and expiry passes 50 consecutive runs.

Each run of capture_racing_expiry_has_exactly_one_winner is itself 50 rounds. In each round, 8 holds become due at the same instant. Each has a capture one microsecond before its expiry, and two expiry workers run at its expiry, all released at once by a barrier. Every hold must end exactly once:

  • captured if its capture succeeded, never also expired
  • expired by a worker if its capture failed with HoldNotActive { status: Expired }
  • never expired twice

At the end, no hold has two closing entries, held balances are zero, and the wallet has lost exactly what was captured.

50 consecutive runs, one after another, on 2026-11-13:

Runs passed 50 of 50, none failed
Holds raced 20,000 (50 runs × 50 rounds × 8 holds)
Won by the capture 14,715 (294 to 296 per run)
Won by an expiry worker 5,285
Holds ended twice, or not at all 0
Time per run 34.2 s to 51.4 s, 33.9 minutes in total

Both sides won thousands of times, so the race was real, not one side always arriving first. The worker skips a hold that a capture has locked (SKIP LOCKED), which is why captures win most rounds.

Result: passed.

  • The monitor needs its own connection. At first, the INV-18 monitor took its connection from the same 12-connection pool as the workload. It queued behind 200 tasks and took only 9 snapshots. On its own connection it took 640, so the check now really runs under concurrency.
  • Capture, release and expiry share one code path. The worker expires a hold through the same code as expire_hold, so an expiry posts the same entry whichever way it runs.