Repository navigation
feat(client-pull): in-process voucher ledger for parallel pulls (#944) - #950
Conversation
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]>
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.rswithCumulative+ChannelLedger+ unit tests for cumulative math and concurrent issuance ordering. - Rewired
crates/client-pull/src/lib.rsto issue vouchers viaChannelLedger, added new public entrypointstream_fetch_shared, and refactored the receive loop intoreceive_and_pay. - Updated
crates/node/tests/client_loopback.rsto 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.
Coverage reportTotal: 84.27% (was 84.21%, ▲ +0.06%) — 38597/45800 lines
History
|
…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]>
Summary
Fixes the voucher-nonce collision that made concurrent pulls over a single payment channel fail (#944). A channel's voucher state (
nonce, cumulativebytes/amount) is a single serial ledger; today each concurrent pull stream snapshotsctx.prior_*and signs the next voucher atprior_nonce + 1, so N concurrent pulls collide and the node accepts only one.This introduces a shared
ChannelLedgerthat serializes voucher issuance — atokio::sync::Mutex<Cumulative>whoseissue()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.self_pay/fetch_inner/UpstreamPullroute through the ledger;stream_fetch/stream_fetch_trackedsignatures are unchanged (a one-shot ledger preserves single-stream behavior and the watermark-on-error contract); newstream_fetch_sharedtakes a caller-ownedArc<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
stream_fetch_sharedis new.node_origin.rs/fetch.rscallers are untouched.node_originerror tests); bundle-orchestration layer (a thinjoin!overstream_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 testcargo 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/229cargo clippy -p decdn-client-pull -p decdn-node -p decdn-cli --all-targets— cleanCloses #944.
🤖 Generated with Claude Code