Skip to content

feat: auto-open/track/reuse payment channels for decdn fetch (#940) - #941

Merged
alpergundogdu merged 8 commits into
mainfrom
claude/client-channel-mgmt-940
Jun 17, 2026
Merged

alpergundogdu merged 8 commits into
mainfrom
claude/client-channel-mgmt-940

Conversation

@alpergundogdu

Copy link
Copy Markdown
Contributor

What

Makes decdn fetch turnkey: instead of requiring a pre-opened, unused --channel-id, it auto-opens a PaymentChannel with --provider-address, persists it, and reuses it (resuming the voucher watermark) on the next fetch. Implements #940; builds on the client-fetch foundation from #938.

Prioritising already-open channels is built in: reuse is the fast path; a new channel is opened only when none is live. (The discovery side, #936, will rank candidates by this store — see that issue.)

Commits (reviewable in order)

  1. feat(client-pull) — extract the duplication-prone buyer-channel open kernel (openChannel tx + authoritative ChannelOpened-from-receipt decode + state/ctx build) and ensure_allowance into decdn-client-pull.
  2. refactor(node) — node's BuyerChannelService::open_and_persist now delegates to that kernel (drops its duplicate copy + dead approval_floor/Erc20). Behaviour-identical: buyer/loopback/reconcile/origin suites pass unchanged.
  3. feat(incentive) — a buyer-only redb BuyerChannelStore behind a new redb feature (off by default; the node keeps its combined seller+buyer store). Same durable-fsync + versioned-encoding + transactional advance/deposit/CAS semantics as the node's buyer table.
  4. feat(cli) — fetch composes it all: reuse-or-open → stream_fetch_tracked → persist watermark. Chain coordinates resolve flag > [blockchain]/[identity] config > default (reusing chain_ctx + the main config schema), so a configured client needs no chain flags. --channel-id/--token dropped (derived).

Verification

  • cargo metadata --all-features, cargo build --workspace, clippy --workspace --all-targets -- -D warnings, fmt --check, cargo doc, cargo deny — all clean (via pre-commit on every commit).
  • #578 holds — CLI stays iroh-blobs-free (redb ≠ iroh-blobs).
  • Node delegation: 35/35 buyer/loopback/reconcile/origin tests pass.
  • New unit tests: open kernel; redb store (reuse-across-reopen, advance regression/mismatch/CAS guards); fetch flag>config>default resolution.

Scope / deferred

🤖 Generated with Claude Code

alpergundogdu and others added 4 commits June 17, 2026 16:55
Extract the duplication-prone buyer-channel open logic — the openChannel
tx, the authoritative ChannelOpened-from-receipt decode, and the
BuyerChannelState/ChannelContext construction — plus the one-time USDC
`ensure_allowance` into decdn-client-pull, so both the node service
(#744) and the CLI (decdn fetch, #940) share one copy. Channel reuse and
watermark recording stay thin compositions over decdn_incentive's
BuyerChannelStore that each caller does directly; a one-shot CLI fetch
needs neither the node service's per-provider concurrency guard nor its
background reclaim/reconcile machinery, so only the kernel is shared.

Additive — node still uses its own copy; delegating it to this kernel is
the next step.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
BuyerChannelService::open_and_persist now calls
decdn_client_pull::buyer_channel::open_channel for the openChannel tx +
ChannelOpened decode + state/ctx build, and bootstrap calls the kernel's
ensure_allowance — dropping node's duplicate copies (and the now-dead
approval_floor + Erc20 import). The escrowed-but-untracked error posture
on a local persist failure stays node-side. Behavior-identical: the
buyer/loopback/reconcile/origin suites pass unchanged.

Refs #940

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…ature (#940)

A client (decdn fetch) needs a PERSISTENT buyer-channel store to reuse
channels across one-shot invocations (resuming the voucher watermark
rather than opening a fresh channel every run). The node's redb store is
deliberately a combined seller+buyer file, so this is a buyer-only store:
one redb file, one table, the same Durability::Immediate fsync-on-commit
discipline and the same versioned StoredBuyerChannelState encoding +
transactional advance/deposit/CAS semantics as the node's buyer table.

Gated behind a new `redb` feature (off by default) so the
contracts/voucher consumers of decdn-incentive don't pull redb/postcard;
verified redb stays out of the default dep tree. Tests cover
reuse-across-reopen, advance regression/mismatch guards, and empty-store
outcomes.

Refs #940

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
#940)

decdn fetch is now turnkey: it opens-or-reuses a PaymentChannel with
--provider-address instead of requiring a pre-opened --channel-id. A live
channel in the persistent RedbBuyerChannelStore is reused (resuming its
voucher watermark via stream_fetch_tracked); otherwise one is opened
on-chain (ensure_allowance + the shared open_channel kernel) and
recorded. The acked watermark is persisted after the pull (even on a
paid-but-failed delivery) so the next reuse resumes correctly.

Chain coordinates (rpc_url, PaymentChannel/SlashJudge addresses, chain
id, keystore, data dir, deposit) resolve flag > [blockchain]/[identity]
config > default, reusing chain_ctx::{build_provider, parse_address} and
the main config schema. So a configured client runs
`decdn fetch --node-id … --addr … --provider-address … --hash … -o …`
with no chain flags. Drops --channel-id and --token (both now derived).

Enables decdn-incentive's redb feature for the CLI. Tests cover the
flag>config>default resolution precedence; the on-chain open+fetch path
is exercised by the anvil e2e (the requester itself is covered by node's
loopback suite).

Refs #391, #940

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

@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 persistent buyer-channel management and auto-opening/reusing of payment channels for the decdn fetch CLI command. It extracts the core PaymentChannel opening logic into a shared buyer_channel module in crates/client-pull and implements a client-side RedbBuyerChannelStore in crates/incentive backed by redb. The decdn fetch command is updated to automatically resolve chain configuration, reuse active channels, and persist voucher progress. Feedback on the changes suggests refactoring an unidiomatic early return in fetch.rs to use a cleaner .map_err chain with the ? operator.

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.

Comment thread crates/cli/src/commands/fetch.rs Resolved
…review)

Replace the awkward `return Err(anyhow::Error::new(e)).map_err(...)` with a
direct `store.record(...).map_err(...)?` chain — same escrowed-but-untracked
error context, cleaner control flow.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>

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 makes decdn fetch turnkey by automatically opening, tracking, and reusing buyer PaymentChannels across CLI invocations, while reducing duplication by extracting the buyer-channel “open kernel” into the shared decdn-client-pull crate. It also introduces a buyer-only redb-backed store in decdn-incentive (feature-gated) so the CLI can persist and resume voucher watermarks.

Changes:

  • Extract shared buyer-channel open/approval logic into crates/client-pull and delegate node buyer-channel opening to it.
  • Add a feature-gated buyer-only redb store (buyer_channel_redb) to persist buyer channel state for CLI reuse.
  • Update decdn fetch CLI args/config resolution and wire fetch to reuse-or-open + tracked watermark persistence.

Reviewed changes

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

Show a summary per file
File Description
crates/node/src/buyer_channel.rs Removes duplicated open/approve logic and delegates channel opening to the shared kernel.
crates/incentive/src/lib.rs Exposes the new buyer-only redb store behind the redb feature gate.
crates/incentive/src/buyer_channel_redb.rs Implements buyer-only redb persistence for channel state + watermark resume.
crates/incentive/Cargo.toml Adds redb feature and optional redb/postcard deps.
crates/common/src/cli/fetch.rs Updates fetch CLI args to remove --channel-id/--token and add chain/config inputs.
crates/client-pull/src/lib.rs Exposes the new shared buyer-channel kernel module.
crates/client-pull/src/buyer_channel.rs Adds shared ensure_allowance + open_channel kernel for node + CLI.
crates/client-pull/Cargo.toml Adds tracing dep for the new buyer-channel kernel logging.
crates/cli/src/commands/fetch.rs Implements reuse-or-open flow, tracked fetch, and watermark persistence.
crates/cli/Cargo.toml Enables decdn-incentive’s redb feature for CLI channel reuse.
Cargo.lock Locks new deps (redb, postcard, tracing usage).

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

Comment thread crates/cli/src/commands/fetch.rs Resolved
Comment thread crates/cli/src/commands/fetch.rs Resolved
Comment thread crates/client-pull/src/buyer_channel.rs Resolved
@github-actions

github-actions Bot commented Jun 17, 2026 •

Copy link
Copy Markdown

Coverage report

Total: 84.99% (was 85.11%, ▼ 0.12%) — 38177/44921 lines

File Coverage Lines Δ
crates/cli/src/commands/fetch.rs 45.99% 149/324 ▲ +16.53%
crates/client-pull/src/buyer_channel.rs 0.00% 0/73 new
crates/client-pull/src/lib.rs 83.73% 427/510 ● 0.00%
crates/incentive/src/buyer_channel_redb.rs 86.82% 415/478 new
crates/node/src/buyer_channel.rs 35.48% 231/651 ▲ +2.86%
History
Commit Total Δ vs base
5b30330 84.72% ▼ 0.39%
8c8fd9f 84.99% ▼ 0.12%
86ed68b (HEAD) 84.99% ▼ 0.12%

alpergundogdu and others added 3 commits June 17, 2026 17:46
… zero-length guard)

- fetch: handle the full AdvanceOutcome from advance_progress, not just
  backend errors — a non-Advanced outcome (unknown provider / channel
  replaced / regression) also means the watermark didn't move, so warn
  rather than silently risk a stale-nonce reuse.
- fetch: clamp the new-channel deposit up to the on-chain
  PaymentChannel.minDeposit() (matches the --deposit-micro-usdc help and
  the node's buyer path) so openChannel can't revert for under-funding on
  a higher-floor network.
- RedbBuyerChannelStore::open: reject a zero-length db file (redb's
  Database::create treats empty-but-present as 'create fresh', silently
  wiping the watermark) — mirrors the node store's guard. New test covers it.
- client-pull: drop the stale node::buyer_channel::approval_floor doc
  reference (that helper moved into this kernel).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Add tests for the previously-uncovered money-path store branches:
load_all (multiple records), forget (remove + absent no-op),
forget_if_channel (CAS: stale id does not delete, matching id does), and
add_deposit (Added / ChannelMismatch / UnknownProvider). These are the
transactional/CAS guards most likely to hide a funds bug.

The open kernel in decdn-client-pull (open_channel/ensure_allowance)
stays at unit-0%: it is async on-chain work whose meaningful coverage
needs a live chain (anvil e2e), not a mock — deferred to a fetch anvil
e2e rather than padded with hollow tests.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
… review)

stream_fetch_tracked was called with max_blob_size_bytes = 0 (unlimited),
so a provider that over-claims total_bytes could drive the buffered
delivery toward client OOM (bounded only by the channel deposit, which
can be large at a low rate). Add --max-blob-mb (default 1024 MiB, the
node's default serve ceiling) and pass it as the ceiling, mirroring the
node's buyer-pull guard. Self-review finding.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@alpergundogdu
alpergundogdu merged commit 351ad72 into main Jun 17, 2026
22 checks passed
@alpergundogdu
alpergundogdu deleted the claude/client-channel-mgmt-940 branch June 17, 2026 17:15
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.

2 participants