Skip to content

1.13 Balance Verification and Integrity Checking: Exit Evidence

Evidence · Step: 1.13 Balance Verification and Integrity Checking

Step 1.13 Balance Verification and Integrity Checking
Date 2026-11-12
Database PostgreSQL 18.6 (postgres:18-alpine, 1 GB shared memory) in Docker Desktop on Windows 11
Result Passed
Deliverable File
Verifier crates/engine/src/verification.rs (verify, VerifyOptions, Scope, Finding, VerificationReport)
CLI crates/cli/src/main.rs: mezgeb-cli verify [--full] [--settle-seconds N] [--no-record] [--database-url URL]; exit 0 clean, 1 dirty, 2 could not run
Tables migrations/0004_verification.{up,down}.sql: verification_runs, verification_checkpoints, both append-only
Role db/init/02-verifier.sql: mezgeb_verifier (reads the ledger, records runs, nothing else), and transaction_timeout = 30s on mezgeb_app
Corruption suite crates/engine/tests/verification.rs (10 tests)
Benchmark crates/engine/examples/verify_bench.rs
Every test verified TestDb::drop in crates/engine/tests/support/mod.rs runs a full verification before dropping each test database
CI .github/workflows/ci.yml: creates every role, and verifies a ledger written by the engine with mezgeb-cli verify
Documentation db-schema.md, Verification
Task Done
Each entry balances per currency Yes (INV-1)
Global debits = credits per currency Yes (INV-2)
Account totals = SUM(lines) Yes (INV-5): each account’s checkpoint plus every line after it, for every account, in every run
The balance_after chain is continuous per account Yes (INV-6): reported at the first line where an account’s chain breaks
No orphan entries or lines Yes (INV-4): entries with fewer than two lines; lines whose entry is missing
verification_runs table (scope, from/to checkpoint, result, duration) Yes, plus lines checked and findings
Incremental mode Yes: only entries after the last clean checkpoint, from per-account checkpoint totals

No entry is skipped. A run checkpoints only entries recorded at least 60 seconds earlier, and posting transactions are capped at 30 seconds, so an entry below a checkpoint can never commit later. See db-schema.md.

Corruption tests. Each corruption was planted as the superuser with triggers and foreign keys switched off, and each was detected, with the exact account and entry:

Corruption Findings
A line’s amount changed (300 → 301) INV-1 entry …3ea: ETB debits 301 ≠ credits 300; INV-6 account …002: line 0 of entry …3ea stores totals (300, 1000), the chain gives (301, 1000); INV-5 account …002: stores totals (300, 1000), its lines give (301, 1000)
An account total changed (+5) INV-5 account …003: stores totals (0, 805), its lines give (0, 800); INV-2 ETB: all debits 1800 ≠ all credits 1805
A line deleted INV-1 entry …3eb: ETB debits 500 ≠ credits 0; INV-4 entry …3eb: 1 line(s), at least 2 required; INV-5 account …003: stores totals (0, 800), its lines give (0, 300)
An entry deleted, its lines left INV-4 line 0 of entry …3ea: the entry does not exist (and line 1); INV-6 at the next line of the affected account; INV-5 for both accounts
An entry with no lines inserted INV-4 entry …3f1: 0 line(s), at least 2 required
A new line tampered after a checkpoint Found by the incremental run (INV-1, INV-6, INV-5); the dirty run does not move the checkpoint
An untouched account’s total tampered after a checkpoint Found by the incremental run (INV-5)

One limit, tested and documented: an incremental run does not re-check lines it has already verified. Tampering with an old line is missed by an incremental run and found by a full run (what_incremental_runs_do_and_do_not_recheck). Full runs should be scheduled regularly; the hash chain (step 2.09) makes such tampering cheap to detect.

Also tested: entries newer than the settle time wait for the next run; runs and checkpoints are recorded and append-only (MZ001); the verifier cannot change accounts or delete runs (42501).

Verification on 1M lines. verify_bench bulk-loads valid entries (each debits a bank account and credits one of 1,000 wallets, with correct running totals), then times verification. PostgreSQL 18.6 in Docker Desktop on a Windows 11 development machine:

Run From entry Lines checked Time Result
Full, first batch 0 1,000,000 35.6 s clean
Incremental, second batch only 500,000 1,000,000 40.2 s clean
Incremental, nothing new 1,000,000 0 3.2 s clean
Full, both batches 0 2,000,000 55.8 s clean

The first version took 49.6 s (full) and 59.4 s (incremental), because each check joined the million lines to their entries again and sorted with 4 MB of memory. Gathering the lines in scope once into a temporary table and giving the run 256 MB of sort memory brought it to the figures above. That in turn needed Docker’s shared memory raised from 64 MB to 1 GB (shm_size in compose.yaml, --shm-size in CI), because PostgreSQL’s parallel workers share their memory through it.

Criterion Result
100% of the planted corruptions are detected Pass: 7 of 7, each with the exact account and entry
An incremental run on 1M lines finishes in under 1 minute on dev hardware (record the actual figure) Pass: 40.2 s for 1,000,000 new lines on top of 1,000,000 already verified

Gate: verify runs clean after every test suite in CI.

It runs after every test, not only every suite: every database test ends with TestDb::drop, which runs a full verification of the ledger the test leaves behind and fails the test if it is not clean. Only the corruption tests, which break the ledger on purpose, skip it (drop_unverified). All 46 database tests pass this way, as do the 109 others in the workspace.

CI also runs the operator’s command on a ledger written by the engine and interrupted mid-posting:

crash_writer setup …; timeout -s KILL 10 crash_writer write … || true
mezgeb-cli verify --full --settle-seconds 0 --database-url …
full verification of entries 1..=479: 1437 line(s) in 210 ms: clean
recorded as verification run 2

A dirty result exits with status 1 and fails the CI run. Result: passed.

mezgeb-cli verify refused to migrate the local mezgeb database because migration 0002’s recorded checksum matched neither the file nor a CRLF copy of it. The local scratch database was recreated. The underlying risk was real, though: Git on Windows (core.autocrlf = true) can check out migrations with CRLF line endings, which changes their checksum, and SQLx would then refuse to migrate a production database. A new .gitattributes pins every text file, and migrations in particular, to LF. All files in the repository were already stored with LF.