2.12 Receipt Ingestion and Settlement: Exit Evidence
Evidence · Step: 2.12 Receipt Ingestion and Settlement
| Step | 2.12 Receipt Ingestion and Settlement |
| Date | 2026-11-16 |
| Database | PostgreSQL 18.6 (postgres:18-alpine) in Docker Desktop on Windows 11; postgres:18-alpine on GitHub’s ubuntu-latest in CI |
| Decision | ADR-0019; protocol §8–§11 |
| Result | Passed |
Deliverables
Section titled “Deliverables”| Deliverable | Where |
|---|---|
| The receipt and its decoder | crates/offline/src/receipt.rs: canonical body, strict decode, digest, signing, the twelve Outcomes |
| The receipts table | Migration 0012: offline_receipts (primary key (device_id, counter)), offline_receipt_conflicts, offline_settings |
| The ingestion pipeline | crates/engine/src/offline_receipts.rs: ingest_receipt |
| The settlement entry | offline_receipt_settle.v1 (E17), engine-only |
| The endpoint | POST /v1/offline/receipts (a batch of up to 100), GET /v1/offline/vouchers/{id}/receipts |
| Operator tools | mezgeb-cli offline fee; the offline runbook, with a section on frozen devices |
| Test vectors | Two chained receipts in test-vectors/offline/v1.json |
The work arrived in nine pull requests:
- the receipt (#123), the schema (#124), the template (#125);
- the engine (#126), the API and the demo (#127), the CLI (#128);
- the Postman collection (#129), the documentation (#130);
- this evidence (#131).
One choice was made without the maintainer: the offline merchant fee.
Online, the client computes each fee (E07); offline, nobody can, because the
device does not know the client’s prices. So the fee is a client setting in
basis points (mezgeb-cli offline fee):
- the default is no fee;
- it is rounded down, so a fraction of a santim stays with the payee.
Implementation tasks
Section titled “Implementation tasks”| Task | Done |
|---|---|
receipts table with primary key (device_id, counter) |
offline_receipts. A receipt is stored the first time it arrives, whatever its outcome, with its signed bytes, signature, digest and link. CHECKs tie a settled outcome to its entry and to the parts that paid it. A trigger (MZ011) refuses any change to a settled receipt or a signed field |
| Decode | Receipt::decode is strict: the exact tag, every field, UUIDv7, a known currency, nothing after the last field. Each receipt has one encoding |
| Verify the voucher | The voucher must be the device’s, and counter ≥ counter_start (UNKNOWN_VOUCHER) |
| Verify the signature | With the device’s registered key, low-S, before anything is written (BAD_SIGNATURE) |
| Counter within the voucher range | counter ≥ counter_start. There is no upper bound to check: the escrow bounds what a voucher can pay (OVER_LIMIT) |
| Check the chain link | prev_digest must be the voucher’s digest for its first counter, else the previous receipt’s (CONFLICT). A receipt whose predecessor has not settled waits (WAITING_FOR_GAP) |
| Check the remaining escrow | The escrow’s balance; more is OVER_LIMIT and freezes the device. The posting rules also keep escrow from going below zero (INV-8, INV-19) |
| Settle in the same transaction as inserting the receipt | One transaction per receipt: the record, E17 and the outcome. Settling a receipt also settles, in order, every receipt that was waiting for it |
| Outcomes as in spec §5.5 | All twelve exist. Ten can occur now; SETTLED_LATE and EXPIRED need returned and ended vouchers, which step 2.13 builds |
POST /v1/offline/receipts batch endpoint |
One result per receipt, in upload order, with what each upload settled. Not one transaction, so repeating an upload is always safe |
The concepts.
- Idempotent on
(device_id, counter): the record decides betweenDUPLICATEandCONFLICT, and E17’s idempotency key isoffline:<device_id>:<counter>. - Cheap checks first: decode, then device, then signature. Nothing is written before the signature verifies.
- Same result whoever uploads: the checks use only the receipt and the ledger’s state.
Testing requirements
Section titled “Testing requirements”| Requirement | Test | Result |
|---|---|---|
The same receipt uploaded by payer and by payee: one settlement, then DUPLICATE |
payer_and_payee_upload_the_same_receipt_once; the demo, where the phone re-uploads what the merchants synced |
One entry; the second upload is DUPLICATE, naming the same entry |
| Reversed upload order: same final state | any_upload_order_gives_the_same_final_state: in order, reversed, and mixed |
Identical outcomes and balances. Reversed: three wait, then the first settles all four |
| A forged signature is rejected with no DB changes | a_forged_receipt_changes_nothing: another key, an altered amount, garbage |
BAD_SIGNATURE, BAD_SIGNATURE, BAD_FORMAT. Receipts, entries and audit events are unchanged, and the device stays active |
Overspending the voucher → OVER_LIMIT |
overspending_the_voucher_freezes_the_device |
OVER_LIMIT; the device and voucher freeze; what settled stays; the next receipt is FROZEN; one alert |
| 100 concurrent uploads of one receipt → one settlement | a_hundred_concurrent_uploads_settle_once |
1 SETTLED, 99 DUPLICATE, one entry (3 runs) |
Also tested:
- Conflicts:
- two receipts with one counter: the first stands, the second is kept once as evidence, and the device freezes;
- a receipt whose link does not match is a conflict too.
- Refusals record nothing: an unknown device, an unknown voucher, a counter before the voucher’s, and a payee of another client or not a wallet or merchant.
- A settled receipt never changes (
MZ011), even through the service’s role. - The decoder refuses every truncation, a trailing byte, another message’s tag, and a bad option flag.
Acceptance criteria
Section titled “Acceptance criteria”Exactly once, in property tests with random upload orderings. The roadmap names INV-9 and INV-10. It predates the numbering of docs/invariants.md, where the offline invariants are:
- INV-19: receipts never pay more than the escrow;
- INV-20: a
(device_id, counter)settles at most once; - INV-21: two different receipts with one counter never both settle.
random_upload_orders_settle_each_receipt_once uploads a chain of five
receipts in eight random orders, each with four repeats inserted. Every
time:
- the final state is identical (each receipt’s outcome and entry, and every balance);
- each receipt settles exactly once;
- the escrow check finds nothing.
The orders come from a fixed seed, so they are the same on every run and a failure reproduces.
Exit gate
Section titled “Exit gate”End-to-end demo: issue → offline receipts (simulated device) → sync →
balances correct. crates/api/tests/offline_demo.rs, through the API
on a real service:
Hana deposits 500.00 ETB; her wallet holds 500.00The phone gets a 200.00 ETB voucher: wallet 300.00, escrow 200.00 offline receipt 1: coffee, 60.00 ETB offline receipt 2: vegetables, 45.50 ETB offline receipt 3: tea, 12.00 ETBThe stall syncs receipt 2: "WAITING_FOR_GAP"The coffee shop syncs receipts 1 and 3: "SETTLED" (settling 2 receipts), "SETTLED"Hana's phone syncs all three: ["DUPLICATE", "DUPLICATE", "DUPLICATE"]Balances: wallet 300.00, escrow 82.50, Merkato Coffee 70.56, stall 44.59Fee revenue: 2.35Escrow check: every escrow holds its unspent voucher valueEvery balance is asserted. With a 2% fee:
- the escrow paid 117.50;
- Merkato Coffee got 72.00 less 1.44;
- the stall got 45.50 less 0.91;
- fee revenue is 2.35.
The demo runs in CI on every PR.
The whole suite. On main after #130, cargo test --workspace against
PostgreSQL ran 43 test binaries and 339 tests, with 0 failures.
Found and fixed during the step
Section titled “Found and fixed during the step”- A CI failure from splitting the work: the engine PR (#126) added an error the API did not yet map. The combined branch built, but the engine PR alone did not. Fixed in #126 before it merged. From now on, each split branch gets the workspace check on its own before it is pushed.
Not done in this step
Section titled “Not done in this step”- Gap timeouts (72 hours), voucher expiry, grace and return, late
receipts (
SETTLED_LATE,EXPIRED), the dispute workflow: step 2.13. A receipt for a returned voucher answers501until then. - Payee acknowledgements: optional evidence for disputes, not needed to settle; built with the dispute path.
- Uploads across clients: a receipt must name a device and a payee of the uploading client. Payments between two client platforms are not part of v1.
- The external security review and card attestation: still open, as recorded for step 2.11.