Skip to content

fix(node): persist buyer voucher progress after pull-through (#852) - #872

Merged
thiras merged 2 commits into
mainfrom
fix/852-persist-buyer-voucher-progress
Jun 10, 2026
Merged

thiras merged 2 commits into
mainfrom
fix/852-persist-buyer-voucher-progress

Conversation

@thiras

@thiras thiras commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #852. On a cache-miss pull-through the node pays the upstream by signing cumulative payment vouchers, but BuyerChannelService::record_progress had zero production callers — stream_fetch discarded the signed (nonce, bytes_delivered, amount) and pull_from_candidate never persisted them. The buyer channel is reused per-provider (~90 days via try_reuse_live), so on reuse ChannelContext::for_buyer_channel rebuilt context from the open-time zeros and re-signed nonce=1 with a regressed cumulative bytes_delivered → the upstream rejected it (StaleNonce/BytesRegression) and delivery aborted at the first voucher boundary. The headline #834 channel-reuse feature worked exactly once per provider, then that provider was permanently unusable to this buyer until channel expiry. Same shape after a restart.

Approach

Thread a VoucherProgress acked-watermark out-param through the pull path, advanced only after each VoucherAck (matching the upstream's committed watermark — ADR 003: the seller persists before it acks), and persist it after every stream.

  • client_requester.rs — add VoucherProgress (encapsulated: private fields + seed/commit_ack/acked, so the "advance the quadruple atomically, only on ack" invariant is structural). self_pay/fetch_inner advance it post-ack. stream_fetch -> Result<Bytes> is kept unchanged for the ~25 simple callers via a thin wrapper; the new stream_fetch_tracked carries the out-param.
  • buyer_channel.rs — expose record_progress on the ChannelOpener seam.
  • node_origin.rs — pull_from_candidate persists progress on both Ok and Err paths when a voucher was acked (a mid-stream failure or a paid-but-corrupt/hash-mismatch delivery still advanced the upstream's watermark). A persist failure does not fail the already-paid pull — it warn!s and bumps a new node_pull_progress_persist_failures counter.
  • metrics.rs — new node_pull_progress_persist_failures counter.

Tests

crates/node/tests/node_origin_pull.rs (lightweight, no-chain harness):

  • extended StubOpener to model the real persist→reuse loop (seeds prior_* from recorded progress);
  • two-pull resume regression test (the direct guard against the bug: nonce 2→4, bytes 1.5→3 MiB);
  • persist-failure test (FailingRecordOpener): the pull still delivers the bytes and the counter increments;
  • exact-total assertions on the success (nonce 2 / 1,572,864 B / amount 15) and corruption (nonce 1 / 4096 B / amount 1) paths;
  • empty-log assertions on the three no-pull paths (the acked() guard).

Reviewed

Ran the multi-agent PR review (code / tests / silent-failures / type-design / comments). No critical correctness bugs; the four actionable findings (untested persist-failure branch, over-stated doc guarantee, a stale paid_amount doc reference, and VoucherProgress encapsulation) are all addressed in this PR.

Residuals (documented, out of scope for #852)

Verification

  • cargo clippy -p decdn-node --all-targets clean (default and --features anvil-e2e)
  • cargo fmt -- --check clean
  • cargo nextest run -p decdn-node → 566 passed

🤖 Generated with Claude Code

On a cache-miss pull-through the node pays the upstream by signing
cumulative vouchers, but `BuyerChannelService::record_progress` had zero
production callers: `stream_fetch` discarded the signed totals and
`pull_from_candidate` never persisted them. The buyer channel is reused
per-provider (~90 days), so on reuse `ChannelContext::for_buyer_channel`
rebuilt context from the open-time zeros and re-signed `nonce=1` with a
regressed cumulative `bytes_delivered`; the upstream rejected it
(`StaleNonce`/`BytesRegression`) and delivery aborted. The headline #834
channel-reuse feature worked exactly once per provider, then that
provider was permanently unusable until channel expiry.

Thread a `VoucherProgress` acked-watermark out-param through the pull
path, advanced only after each `VoucherAck`, and persist it after every
stream:

- client_requester: add `VoucherProgress` (encapsulated — `seed`,
  `commit_ack`, `acked`); `self_pay`/`fetch_inner` advance it post-ack;
  keep `stream_fetch -> Result<Bytes>` for the many simple callers via a
  thin wrapper, add `stream_fetch_tracked` carrying the out-param.
- buyer_channel: expose `record_progress` on the `ChannelOpener` seam.
- node_origin: `pull_from_candidate` persists progress on both Ok and Err
  paths when a voucher was acked (a mid-stream failure or paid-but-corrupt
  delivery still advanced the upstream's watermark). A persist failure
  does not fail the already-paid pull — it warns and bumps a new
  `node_pull_progress_persist_failures` counter.

Because the watermark only reflects *acked* vouchers, a lost ack after
the upstream commits under-records by one (self-corrects on rotation);
this residual is documented, not closed here.

Tests: extend `StubOpener` to model the persist→reuse loop; add a
two-pull resume regression test, a persist-failure test (pull still
delivers, counter increments), exact-total assertions on the
success/corruption paths, and empty-log assertions on the no-pull paths.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Copilot AI review requested due to automatic review settings June 10, 2026 17:48

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a mechanism to persist cumulative voucher totals (nonce, bytes delivered, and amount) paid on a buyer channel, addressing issue #852. This ensures that subsequent channel reuses or restarts resume from the correct watermark rather than re-signing stale vouchers. It introduces the VoucherProgress struct to track acked watermarks, updates the fetching logic to thread this progress, adds metrics to track persistence failures, and integrates progress recording into the node origin pull process. Comprehensive tests have also been added to verify correct progress persistence, channel reuse, and error handling. No review comments were provided, so there is no feedback to address.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a production bug in node-to-node pull-through where buyer-side payment voucher progress wasn’t persisted, causing reused buyer channels to re-sign stale cumulative vouchers (nonce/bytes regression) and get rejected by the upstream on subsequent pulls.

Changes:

  • Add a VoucherProgress out-param flow to track the last acked cumulative voucher totals during stream_fetch, and persist those totals after the pull.
  • Expose record_progress via the ChannelOpener seam and call it from pull_from_candidate on both success and failure paths when at least one voucher was acked.
  • Add a new metric (node_pull_progress_persist_failures) and expand tests to cover persistence, reuse/resume, and persist-failure behavior.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
crates/node/src/client_requester.rs Introduces VoucherProgress and stream_fetch_tracked to report acked voucher watermark to callers.
crates/node/src/node_origin.rs Persists acked voucher progress after pull attempts and emits a persist-failure metric.
crates/node/src/buyer_channel.rs Extends ChannelOpener with record_progress and wires it to BuyerChannelService.
crates/node/src/metrics.rs Adds the node_pull_progress_persist_failures counter and an increment helper.
crates/node/tests/node_origin_pull.rs Updates harness to record persisted progress, adds regression + persist-failure tests, and asserts persisted watermarks.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread crates/node/src/client_requester.rs Resolved
Comment thread crates/node/src/node_origin.rs Resolved
@github-actions

github-actions Bot commented Jun 10, 2026 •

Copy link
Copy Markdown

Coverage report

Total: 85.85% (was 85.85%, ● 0.00%) — 34232/39874 lines

File Coverage Lines Δ
crates/node/src/buyer_channel.rs 16.75% 64/382 ▼ 0.40%
crates/node/src/client_requester.rs 86.90% 252/290 ▲ +1.57%
crates/node/src/metrics.rs 84.19% 708/841 ▲ +0.06%
crates/node/src/node_origin.rs 89.53% 248/277 ▲ +0.52%
History
Commit Total Δ vs base
a61d35d 85.85% ● 0.00%
fb59584 (HEAD) 85.85% ● 0.00%

Address review: `record_progress` does a blocking fsync'd redb write, and
it ran before `started.elapsed()` was captured for the `Outcome::Delivered`
reputation signal — inflating the delivery-speed measurement with disk I/O
unrelated to the network pull. Capture `elapsed` immediately after
`stream_fetch_tracked` returns, before persistence.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@thiras
thiras merged commit 490d3fe into main Jun 10, 2026
20 checks passed
@thiras
thiras deleted the fix/852-persist-buyer-voucher-progress branch June 10, 2026 18:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants