Skip to content

feat(client-pull): in-process voucher ledger for parallel pulls (#944) - #950

Merged
alpergundogdu merged 5 commits into
mainfrom
claude/funny-mcclintock-daf251
Jun 19, 2026
Merged

alpergundogdu merged 5 commits into
mainfrom
claude/funny-mcclintock-daf251

Conversation

@alpergundogdu

Copy link
Copy Markdown
Contributor

Summary

Fixes the voucher-nonce collision that made concurrent pulls over a single payment channel fail (#944). A channel's voucher state (nonce, cumulative bytes/amount) is a single serial ledger; today each concurrent pull stream snapshots ctx.prior_* and signs the next voucher at prior_nonce + 1, so N concurrent pulls collide and the node accepts only one.

This introduces a shared ChannelLedger that serializes voucher issuance — a tokio::sync::Mutex<Cumulative> whose issue() holds the lock across each voucher's compute → sign → send → await-ack → commit, so vouchers reach the node in strict nonce order while byte transfers stay parallel. Single-stream behavior is preserved; concurrent pulls sharing one ledger now all succeed with a monotonic channel.

Per the design discussion on #944, this is the in-process fix only — no cross-process daemon. Multi-instance parallelism is a key-management concern (per-context wallet derivation), not coordination.

Commits

  • next_voucher — pure cumulative voucher math (Cumulative), exhaustively unit-tested (nonce monotonicity, byte accumulation, ceil rounding incl. the sub-MiB boundary).
  • ChannelLedger — serialized issuance with I/O injected as a closure, so concurrency is unit-tested (50 racing issuers → nonces exactly 1..=50; failed-exchange-leaves-watermark-unmoved) without a live node.
  • Wiring — self_pay/fetch_inner/UpstreamPull route through the ledger; stream_fetch/stream_fetch_tracked signatures are unchanged (a one-shot ledger preserves single-stream behavior and the watermark-on-error contract); new stream_fetch_shared takes a caller-owned Arc<ChannelLedger> for concurrent pulls. The old "exactly one concurrent voucher accepted" loopback test is replaced by one asserting both concurrent pulls succeed with a monotonic channel.

Scope notes

  • Public API surface is minimal: only stream_fetch_shared is new. node_origin.rs / fetch.rs callers are untouched.
  • Deferred (optional, non-blocking): a dedicated tracked-path watermark-on-error regression test (the equivalent copy-back is already covered via the node_origin error tests); bundle-orchestration layer (a thin join! over stream_fetch_shared) is a future caller.

Test Plan

  • cargo nextest run -p decdn-client-pull — 7/7 (math + concurrency unit tests)
  • cargo nextest run -p decdn-node --test client_loopback — 24/24 incl. the flipped concurrent test
  • cargo nextest run -p decdn-node --test node_origin_pull — 25/25 (node-to-node pull regression)
  • cargo nextest run -p decdn-cli -p decdn-client-pull — 229/229
  • cargo clippy -p decdn-client-pull -p decdn-node -p decdn-cli --all-targets — clean

Closes #944.

🤖 Generated with Claude Code

alpergundogdu and others added 4 commits June 18, 2026 17:52
Adds `crates/client-pull/src/ledger.rs` with the `Cumulative` struct and
`next_voucher` function that computes per-voucher absolute totals
(nonce, bytes, amount with ceiling division) from channel state deltas.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Adds ChannelLedger (tokio::sync::Mutex over Cumulative) to ledger.rs, with
issue() holding the lock across the full compute→sign→send→ack→commit cycle
so concurrent streams on one channel produce strictly ordered nonces. Two
TDD tests verify monotonicity under 50-way concurrency and that a failed
exchange leaves the watermark unmoved.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Concurrent pulls on one payment channel previously each snapshotted
ctx.prior_* and collided on nonce prior_nonce+1, so only one voucher was
accepted and the rest were rejected as StaleNonce. Flow voucher state
through a ChannelLedger that serializes the sign->send->ack->commit cycle.

- self_pay issues through &ChannelLedger with a per-voucher delta instead
  of mutating a &mut VoucherProgress.
- fetch_inner takes &ChannelLedger; the receive loop is extracted into
  receive_and_pay and tracks bytes_since_voucher as the per-voucher delta.
- stream_fetch / stream_fetch_tracked keep byte-for-byte signatures: the
  tracked path runs a one-shot ledger and copies the acked watermark back
  into progress on every return path (Ok/Err/timeout), preserving the
  watermark-on-error contract. UpstreamPull keeps its public API via a
  one-shot ledger inside pay_one.
- New stream_fetch_shared takes a caller-owned &ChannelLedger so N
  concurrent callers coordinate voucher issuance.
- Loopback test flipped: client_concurrent_same_channel_both_succeed
  drives two concurrent pulls of two blobs through one shared
  Arc<ChannelLedger> and asserts both succeed with a monotonic channel.

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

@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 an in-process voucher ledger to coordinate parallel bundle pulls over a single payment channel, preventing voucher-nonce collisions and double-counted bytes. It adds a new ChannelLedger struct that serializes voucher issuance across concurrent streams using an internal mutex, and updates the stream_fetch entry points to utilize this shared ledger. Additionally, the integration tests have been updated to verify that concurrent pulls on the same channel now successfully coordinate and complete. As there are no review comments, I have no additional feedback to provide.

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 voucher-nonce collisions when multiple client pulls run concurrently over the same payment channel by introducing a per-channel in-process ledger (ChannelLedger) that serializes voucher issuance (compute → sign → send → ack → commit) while allowing byte transfer to remain parallel. It also updates loopback integration tests to assert that concurrent pulls sharing one ledger both succeed.

Changes:

  • Added crates/client-pull/src/ledger.rs with Cumulative + ChannelLedger + unit tests for cumulative math and concurrent issuance ordering.
  • Rewired crates/client-pull/src/lib.rs to issue vouchers via ChannelLedger, added new public entrypoint stream_fetch_shared, and refactored the receive loop into receive_and_pay.
  • Updated crates/node/tests/client_loopback.rs to replace the old “only one concurrent pull succeeds” assertion with a “both succeed” test and added a helper to seed two blobs.

Reviewed changes

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

File Description
docs/superpowers/plans/2026-06-18-in-process-voucher-ledger.md Added implementation plan documenting the in-process ledger approach and test strategy.
crates/client-pull/src/ledger.rs New per-channel ledger module with pure voucher math + serialized issuance and unit tests.
crates/client-pull/src/lib.rs Integrated ledger into pull/voucher flow; added stream_fetch_shared; refactored buffered receive loop.
crates/node/tests/client_loopback.rs Updated loopback test to validate concurrent shared-ledger pulls both succeed; added two-blob cache helper.

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

Comment thread crates/client-pull/src/ledger.rs Resolved
Comment thread crates/client-pull/src/lib.rs Resolved
@github-actions

github-actions Bot commented Jun 18, 2026 •

Copy link
Copy Markdown

Coverage report

Total: 84.27% (was 84.21%, ▲ +0.06%) — 38597/45800 lines

File Coverage Lines Δ
crates/client-pull/src/ledger.rs 100.00% 111/111 new
crates/client-pull/src/lib.rs 85.52% 496/580 ▲ +1.79%
History
Commit Total Δ vs base
d9a45f7 84.28% ▲ +0.06%
484311a (HEAD) 84.27% ▲ +0.06%

…oucher

Make the `ledger` module private (`mod` not `pub mod`); the public
entrypoints already re-export `ChannelLedger`/`Cumulative`, the only items
external callers need. Drop `pub` on `next_voucher` — it's used only inside
`ChannelLedger` and in-module tests.

Addresses Copilot review on #950.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@alpergundogdu
alpergundogdu merged commit 408eeb5 into main Jun 19, 2026
20 checks passed
@alpergundogdu
alpergundogdu deleted the claude/funny-mcclintock-daf251 branch June 19, 2026 16:00
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.

Client-side voucher issuance is not concurrency-safe per channel (blocks multi-blob fetch from one node)

2 participants