Repository navigation
Description
Context
Split out of the closed #1652 (its Decision 1), which bundled async persistence with PayWord (#1670). This is the durability / crash-recovery contract for the seller-side lane watermark.
Current posture (main) — three regimes, only one synchronous
| Store | Durability today | Crash cost |
|---|---|---|
Voucher anti-replay watermark — node/src/channel_store.rs PersistentChannelStateStore |
redb Durability::Immediate — fsync per accepted voucher, before the receipt |
re-serving already-paid bytes (replay) → guarded |
Chain-watcher checkpoint — payment_settlement.rs KeyedCheckpointStore |
debounced — flush ≥ 512 blocks or ≥ 30 s (CHECKPOINT_FLUSH_BLOCKS / _INTERVAL) |
crash re-scans the lagging span (idempotent) |
Receipt log — receipt_log.rs |
best-effort write + flush, no fsync, off hot path via background writer (#803) | audit/export only, not anti-replay |
The codebase already debounces the two stores where a crash is cheap. Only the voucher watermark pays a synchronous fsync per voucher — the ~3.18 ms/voucher cost, ~313 MiB/s lane ceiling.
The key distinction: frontier vs redeemed watermark
Lane state has two parts with different durability needs:
- Un-redeemed frontier — how far the client has paid since the last on-chain redemption. Safe to lose on a crash:
- an honest reconnecting client resumes forward (its state is authoritative; its next voucher is past the node's stale watermark, so
max(local, client)just advances — nothing re-served); - a malicious replay of un-redeemed vouchers costs nothing — they are still redeemable on-chain, so re-serving and banking them pays the node. No loss.
- an honest reconnecting client resumes forward (its state is authoritative; its next voucher is past the node's stale watermark, so
- Redeemed watermark — the cumulative already settled on-chain. Losing this is a loss: the node could re-serve already-redeemed bytes for free against a below-watermark replay, undetected until its next redemption reverts to
paid = 0.
Proposed change
- Frontier: in-memory working state + async batched local writes, on a configurable timer. Advance lane state in memory on verify; flush the frontier to a buffered local store on a configurable interval (
persist_flush_interval, default ~5–30 s), batched (one redb txn per interval covers all pending lanes) and run off the delivery path in a background writer — the codebase already does this for the receipt log (spawn_receipt_writer, node: audit-only download-receipt write is awaited on the paid-delivery hot path #803), so the ~3 ms fsync is a background task holding a write txn briefly, not a wire stall. Worst case on an unclean restart: lose one interval of frontier — which is safe to lose (above). - Redeemed watermark: durable at redemption — outside the knob. Flush local
≥ redeemedsynchronously as part of each on-chain redemption (rare — once per epoch/close — and the node is already paying gas, so the local fsync is negligible). This is a correctness rule, not a performance one: it guarantees post-crashlocal ≥ redeemed, so the node never re-serves already-redeemed bytes for free. It is not governed bypersist_flush_interval; without it, a crash in the unflushed window right after a redemption could re-serve a whole epoch of already-redeemed bytes. - Persist failure = metric, not wire rejection. In-memory state has advanced and the payment is honoured; retry would be wrong. Record + continue; a store that stays down widens the (frontier-only) replay surface for monitoring to catch.
Flush cost & the risk-appetite knob
The fsync is rounding error at any sane interval, and because frontier loss is safe you can push the interval out for more nines without real risk. persist_flush_interval trades throughput-nines against seconds-of-harmless-frontier-replay:
| Interval | fsync overhead | Frontier lost on crash |
|---|---|---|
| 5 s | ~0.06% (3½ nines) | ≤ 5 s |
| 30 s | ~0.01% (4 nines) | ≤ 30 s |
| ~50 min | ~0.0001% (six nines) | ≤ 50 min |
Both ends are cheap — the flush is negligible and the loss is harmless — so a generous default (5–30 s) with an operator override is right. The redeem-time flush (rule 2) is unaffected by this setting.
Dropped: the per-lane eth_call floor
#1652 Decision 1 seeded an unknown lane's floor from watermark[poolId][signer][provider] via one eth_call on first contact. Dropped, because it is a bad trade:
- Returns 0 for the common case. The on-chain watermark is non-zero only for lanes already redeemed on-chain, and redemption is rare — so on the overwhelmingly common fresh-contact path the call returns 0 and buys nothing (the ADR itself notes a never-redeemed lane "floors at zero").
- Real RPC cost. Providers bill per call; at client scale an eth_call per fresh lane is a recurring bill.
- Unpriced abuse surface. Lane creation is ~free, so an attacker opening N throwaway lanes forces N eth_calls of the node's RPC spend even if they never pay.
- Redundant. Its safety is already provided for free: the redeemed-watermark durable flush (rule 2) floors the local state, and
redeemreadswatermark[...]on-chain inside the tx and paysclaimed − paid, so a stale/replayed claim collects 0 — the node can never double-collect. The only residual is re-serve, capped by rule 2 (never belowredeemed) and the credit window per pause.
So the chain still floors the catastrophic case — consulted when the node is already paying gas to redeem, not on every connection.
Risk tradeoff
Worst-case loss on an unclean restart is one flush interval of frontier per lane, which is safe to lose (honest resume forward, or attacker pays for re-delivery), plus zero already-redeemed replay (rule 2). Far cheaper than the per-voucher fsync ceiling, and a node crash / data-loss event is itself the larger risk.
Scope
- Implementation seam: a
PoolStateStoreincrates/incentive, redb-backed impl incrates/node(relaxesPersistentChannelStateStorefromImmediateto a buffered configurable-interval frontier flush + synchronous redeemed-watermark flush at redemption). - ADR 003 §persistence text update — replace the durable-commit-before-ack ordering rule with the frontier/redeemed split above.
- The protocol fixes only the frontier/redeemed durability split; backend/trait shape is an implementation detail.
Metadata
Metadata
Assignees
Labels
Type
Fields
Priority
Projects
- StatusShow more project fieldsDone
Activity
feat: credit slow-start — ramped credit window (start at floor, ramp …
perf(payments): in-memory vouchers with periodic + pre-redeem durable…
Merge origin/main into pool floor-credit bound