Skip to content

feat(payments): group-commit voucher fsyncs, ack after commit (#1483) - #1490

Merged
alpergundogdu merged 1 commit into
mainfrom
claude/group-commit-vouchers-1483
Jul 28, 2026
Merged

alpergundogdu merged 1 commit into
mainfrom
claude/group-commit-vouchers-1483

Conversation

@alpergundogdu

@alpergundogdu alpergundogdu commented Jul 28, 2026 •

Copy link
Copy Markdown
Contributor

Supersedes #1487 (auto-closed when its base branch claude/upbeat-babbage-dd8661 was deleted by #1485's merge). Now rebased directly onto main (which contains #1477 via #1485). No content change from #1487's approved state.

What

Amortise the per-voucher fsynced redb commit (measured 3.18 ms/voucher on local SSD, Durability::Immediate) across a batch of vouchers instead of one fsync each. Today a single channel caps at ~313 MiB/s at a 1 MiB interval because vouchers serialize on the per-channel mutex and each pays one fsync.

Plan: group commit, ack AFTER the commit returns (Option A). Nothing is ever acked before it is durable, so ADR 003 §Off-chain voucher state persistence stands verbatim — no ADR amendment, no new replay exposure. Builds on #1477's credit window, which takes the delayed ack off the delivery critical path.

How

  • crates/incentive — ChannelState::stage_voucher validates + advances a candidate without persisting; apply_voucher refactored to stage → record → swap. Because vouchers are cumulative, the final staged state supersedes every intermediate one, so one store.record commits the whole batch (one fsync) — no new ChannelStateStore method needed.
  • crates/node handlers/client — collect_voucher_batch gathers up to credit_window / interval vouchers (first read blocking to preserve park-awaiting-voucher; the rest under a bounded commit interval via a cancellation-safe BufferedVoucherReader), verifies each against the advancing candidate, commits the prefix once, then acks each. A failed commit fails the WHOLE batch → RetryLater, no acks, state unchanged (ADR 003 §355). Mid-batch verify rejection commits the valid prefix first. Both the buffered (deliver) and window pull-through (window_forward_loop) loops share the batched recoup, gating the ADR 011 takedown check on collected_any so the two read symmetrically; stop-and-wait (window = one interval) collapses to one voucher per commit exactly as before.
  • crates/protocol — parse_frame (sync, non-consuming) backs the cancellation-safe reader that read_frame's read_exact cannot survive a gather timeout.
  • config — payment.voucher_commit_interval_ms (default 5 ms), documented with the sizing constraint credit_window >= throughput * (RTT + commit_interval).

Durability::Immediate is kept on the batched commit.

Tests (TDD)

  • group_commit_amortises_the_fsync_across_a_batch — 8 cumulative vouchers commit with exactly 1 record; verified to FAIL under forced stop-and-wait.
  • group_commit_failure_rejects_whole_batch_with_retry_later — failed commit → one RetryLater, zero acks, watermark unchanged.
  • incentive stage_voucher staging tests; protocol parse_frame tests.

Closes #1483

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features

    • Added configurable voucher commit intervals, including immediate-commit mode.
    • Added group committing to persist multiple voucher updates in a single operation.
    • Added safe buffering for voucher frames during interrupted or timed-out reads.
    • Added frame parsing support for incomplete and oversized messages.
  • Bug Fixes

    • Voucher batches now reject atomically when persistence fails, preventing partial acknowledgments or state updates.
    • Improved delivery and accounting consistency during batched voucher processing.

@coderabbitai

coderabbitai Bot commented Jul 28, 2026 •

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@alpergundogdu, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 57 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Free

Run ID: 5bb2a25c-6d3f-431e-9f54-87c03ae2445f

📥 Commits

Reviewing files that changed from the base of the PR and between ec5910a and f74776b.

⛔ Files ignored due to path filters (1)
  • crates/protocol/tests/snapshots/public_api__public_api.snap is excluded by !**/*.snap, !**/*.snap
📒 Files selected for processing (15)
  • crates/cli/tests/config_validate.rs
  • crates/common/src/config/mod.rs
  • crates/common/src/config/resolved.rs
  • crates/common/src/config/types.rs
  • crates/incentive/src/channel.rs
  • crates/node/src/handlers/client/delivery.rs
  • crates/node/src/handlers/client/mod.rs
  • crates/node/src/handlers/client/voucher.rs
  • crates/node/src/handlers/client/window.rs
  • crates/node/src/runtime/mod.rs
  • crates/node/src/runtime/reload.rs
  • crates/node/tests/anvil_bringup_shutdown_e2e.rs
  • crates/node/tests/client_loopback.rs
  • crates/node/tests/sighup_signal.rs
  • crates/protocol/src/framing.rs

Walkthrough

Adds a configurable voucher group-commit interval, stages voucher state in memory before durable persistence, buffers cancellable frame reads, batches delivery recoup, and validates successful and failed batch commits through configuration, unit, and loopback tests.

Changes

Voucher group-commit flow

Layer / File(s) Summary
Commit interval configuration
crates/common/src/config/*, crates/cli/tests/config_validate.rs
Adds voucher_commit_interval_ms to payment configuration and resolved payment data, applies the 5 ms default, preserves explicit values including 0, and updates fixtures and resolver tests.
Pure voucher staging
crates/incentive/src/channel.rs
Separates voucher validation and state advancement from persistence, allowing multiple vouchers to be staged before one store record.
Buffered voucher frame ingestion
crates/protocol/src/framing.rs, crates/node/src/handlers/client/mod.rs
Adds synchronous frame parsing and a persistent buffered reader that retains bytes across cancelled reads.
Batch verification and durable commit
crates/node/src/handlers/client/voucher.rs
Collects and verifies voucher batches, records the final candidate once, advances state after durability succeeds, and emits acknowledgements for committed vouchers.
Batched delivery recoup and runtime wiring
crates/node/src/handlers/client/{delivery,window}.rs, crates/node/src/runtime/*
Wires the commit interval into handlers and processes bounded recoup batches while preserving pending deltas and updated stop conditions.
Group-commit integration coverage
crates/node/tests/client_loopback.rs
Tests one durable record for a successful voucher burst and whole-batch RetryLater behavior when persistence fails.

Estimated code review effort: 5 (Critical) | ~120 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant ClientHandler
  participant ChannelState
  participant ChannelStateStore
  Client->>ClientHandler: send cumulative voucher burst
  ClientHandler->>ChannelState: stage each voucher
  ClientHandler->>ChannelStateStore: record final staged state
  ChannelStateStore-->>ClientHandler: commit success or failure
  ClientHandler-->>Client: acknowledgements or RetryLater rejection
Loading

Note

🎁 Summarized by CodeRabbit Free

Your organization is on the Free plan. CodeRabbit will generate a high-level summary and a walkthrough for each pull request. For a comprehensive line-by-line review, please upgrade your subscription to CodeRabbit Pro by visiting https://app.coderabbit.ai/login.

Comment @coderabbitai help to get the list of available commands.

Amortise the per-voucher fsynced redb commit (~3.18 ms/voucher on local SSD,
Durability::Immediate) across a batch of vouchers instead of one fsync each.
Before this, a single channel caps at ~313 MiB/s at a 1 MiB interval because
vouchers serialize on the per-channel mutex and each pays one fsync.

Group commit with the ack sent AFTER the commit returns (Option A). Nothing is
ever acked before it is durable, so ADR 003 §Off-chain voucher state persistence
stands verbatim — no ADR amendment, no new replay exposure. Builds on #1477's
credit window, which takes the delayed ack off the delivery critical path.

Mechanism (stacked on #1477's seam):
- crates/incentive: `ChannelState::stage_voucher` validates + advances a
  candidate WITHOUT persisting; `apply_voucher` is refactored to stage + record
  + swap. Because vouchers are cumulative, the final staged state supersedes
  every intermediate one, so one `store.record` commits the whole batch (one
  fsync) — no new store method is needed.
- crates/node handlers/client: `collect_voucher_batch` gathers up to
  `credit_window / interval` vouchers (first read blocking to preserve the
  park-awaiting-voucher semantics; the rest under a bounded commit interval via
  a cancellation-safe `BufferedVoucherReader`), verifies each against the
  advancing candidate, commits the prefix once, then writes one VoucherAck per
  voucher. A failed commit fails the WHOLE batch → RetryLater, no acks, state
  unchanged (ADR 003 §355). A mid-batch verify rejection commits the valid
  prefix first, then rejects. Both the buffered (`deliver`) and window
  pull-through (`window_forward_loop`) loops use the shared batched recoup,
  gating the ADR 011 takedown check on `collected_any` so the two read
  symmetrically; stop-and-wait (window = one interval) collapses to one voucher
  per commit exactly as before.
- crates/protocol: `parse_frame` (sync, non-consuming) backs the
  cancellation-safe buffered reader that `read_frame`'s `read_exact` cannot.
- config: `payment.voucher_commit_interval_ms` (default 5 ms) is the governable
  commit interval, documented with the sizing constraint
  `credit_window >= throughput * (RTT + commit_interval)`.

Keeps Durability::Immediate on the batched commit. TDD: batched-throughput test
(8 vouchers → 1 record, proven to fail without batching) and a crash/rollback
test (failed commit → RetryLater, zero acks, watermark unchanged), plus
incentive-level staging tests.

Closes #1483

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@alpergundogdu
alpergundogdu force-pushed the claude/group-commit-vouchers-1483 branch from ec5910a to f74776b Compare July 28, 2026 16:14
@github-actions

Copy link
Copy Markdown

Coverage report

Total: 81.29% (was 81.24%, ▲ +0.05%) — 53206/65450 lines

File Coverage Lines Δ
crates/common/src/config/mod.rs 97.06% 6855/7063 ▲ +0.01%
crates/common/src/config/resolved.rs 100.00% 33/33 ● 0.00%
crates/incentive/src/channel.rs 96.51% 498/516 ▲ +0.18%
crates/node/src/handlers/client/delivery.rs 97.12% 101/104 ▲ +0.64%
crates/node/src/handlers/client/mod.rs 89.58% 679/758 ● 0.00%
crates/node/src/handlers/client/voucher.rs 83.15% 232/279 ▲ +1.69%
crates/node/src/handlers/client/window.rs 85.07% 245/288 ▲ +0.05%
crates/node/src/runtime/mod.rs 46.95% 1085/2311 ▼ 0.02%
crates/node/src/runtime/reload.rs 96.58% 1639/1697 ▲ +0.01%
crates/protocol/src/framing.rs 99.23% 385/388 ▼ 0.16%
History
Commit Total Δ vs base
f74776b (HEAD) 81.29% ▲ +0.05%

@alpergundogdu
alpergundogdu merged commit 5ec81b6 into main Jul 28, 2026
24 checks passed
@alpergundogdu
alpergundogdu deleted the claude/group-commit-vouchers-1483 branch July 28, 2026 16:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Group-commit voucher fsyncs (ack after commit): amortise the 3.18 ms per-voucher commit

1 participant