Skip to content

fix(client-pull): one receive loop — verify every chunk group as it lands on every path - #1994

Merged
alpergundogdu merged 2 commits into
mainfrom
claude/github-issue-1985-6e752e
Sep 11, 2026
Merged

alpergundogdu merged 2 commits into
mainfrom
claude/github-issue-1985-6e752e

Conversation

@yigitdot

@yigitdot yigitdot commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

Closes #1985.

Problem

crates/client-pull had two receivers for cdn/client/v1 paid pulls. The buffered one (stream_fetch* → fetch_inner_once → receive_and_pay → decode_verified_range) drained the whole wire stream into a buffer, releasing a 1 MiB reveal per chunk as bytes landed, and only ran the bao decoder after StreamEnd. A provider that signed an inflated total_bytes and streamed framed garbage was paid min(claim, max_blob_size_mb-wire) × rate before a single byte verified. The progressive one (open_progressive_pull → UpstreamPull → PullReader → ResponseDecoder) verified each 16 KiB group as it arrived. Exposure depended on which receiver a caller used — and two comments in lib.rs claimed the buffered path had production callers and verified before paying; neither was true.

Change

  • Delete the buffered receiver. fetch_inner_once, receive_and_pay, decode_verified_range, TerminalAfterWrite are gone. The stream_fetch* family (zero production callers; used by crates/e2e and the node integration suites) survives only as test-util-gated in-memory wrappers over the one receive loop: fetch_in_memory_once opens a progressive pull, feeds it through PullReader into a new decode_to_vec (same loop shape as ClientRangedStore::ingest_stream), and finishes. The reseed retry loop is kept. A corrupt group now aborts at that group on every path (ADR 038 AC 2); what a lying peer can bill is the bytes that arrived before its first unverifiable group.
  • UpstreamPull::next_chunk absorbs the two guards only the buffered loop had: the received-byte ceiling (Node-to-node size-claim gate lets a holder centralize traffic (refuse-on-claim → refuse-on-received-bytes) #1895) as a wire bound checked before the crossing chunk is paid — open_progressive_pull takes max_blob_size_bytes (its doc already claimed next_chunk enforced it; now it does) and all five callers pass their ceiling — and the voucher-write recovery that prefers a terminal StreamEnd/StreamError over the opaque write failure a node's end-of-stream STOP_SENDING(0) produces. The production leg gets both.
  • bao_encoded_size gains an O(log groups) closed form for the group-aligned contiguous ranges align_range produces, property-tested byte-for-byte against the encoder walk across every tree shape up to nine groups and deep offsets in multi-GiB trees. Found while verifying: the walk is O(groups) — 67M groups for the CLI's default 1 TiB --max-blob-mb — and stalled the CLI fetch past the node's voucher wait before the first byte (cli_fetch_resume e2e). The same walk already ran on every open against the peer-claimed total_bytes; that e2e now completes in 7 s vs 39 s on main.
  • crates/e2e's normal decdn-client-pull dep enables test-util (its lib fixture calls stream_fetch_tracked); ProgressCallback is one unit everywhere (content bytes); stale prose across client-pull, node, cli, e2e, and CLAUDE.md is rewritten to describe the one loop.

Tests

  • client_pays_nothing_for_an_inflated_garbage_claim: 32 MiB signed claim, 64 KiB garbage frames → HashMismatch, nothing paid (first group fails before a metering interval accrues).
  • client_spend_on_a_garbage_stream_is_bounded_by_one_frame: same lie with a 3 MiB first frame → paid ≤ that frame, ≪ the claim — pins the bound so the pay-then-verify shape cannot return.
  • client_rejects_an_empty_claim_for_a_non_empty_hash (cdn/client/v1: empty (0-byte) blob delivery errors on the bao path #1054 guard, now on the live path).
  • client_rejects_a_server_that_oversends_past_the_promised_length accepts both over-delivery messages: the decoder consumes exactly the promised wire, so the extra frame now surfaces at finish (node_origin's is_bao_corruption already classifies both as one).
  • decode_verified_range_* unit tests ported onto decode_to_vec; two new bao-range property tests for the closed form.

Verification

cargo clippy --workspace --all-targets -- -D warnings, cargo clippy -p decdn-client-pull / -p decdn-cli (non-test-util builds), cargo fmt --check, RUSTDOCFLAGS=-D warnings cargo doc --workspace --no-deps --document-private-items, cargo nextest run --workspace --no-fail-fast (3406 passed), check_crate_edges.py, cargo tree -p decdn-cli -e normal -i iroh-blobs (empty). Anvil-gated: anvil_settlement_e2e (4/4) and decdn-e2e smoke, cli_fetch_resume, cli_fetch_multi_source, cli_fetch_topup, cli_bundle_pull_shared_chunk, node_pull_topup, coalesce_two_clients, origin_stream_while_store (all pass).

Review round (second commit)

  • driver::drive proves a total_bytes == 0 claim against the empty root before the empty store can be finalized — the gap-driven path had no verify site for it, so a holder could complete decdn fetch <non-empty hash> with an empty output file (pre-existing; found by the coverage review).
  • Voucher-write recovery: the hash-chain rollover contradiction is tagged LocalPullFault so it can never be rescued into a clean completion; the recovery's own observation (peer kept streaming / read failed / timed out) rides along as context on the write error; the resume-offset-past-ceiling check is stated directly.
  • More tests: honest-prefix-then-garbage exposure bound (≥ 2 MiB paid for the verified prefix, ≤ corrupt offset + one frame), mid-stream straddling overrun guard, gap-scoped leg ceiling anchored at its own offset via open_progressive_pull directly, multi-group exact-ceiling blob, driver empty-claim guard both ways. window_pull_through_oversized_upstream_aborts_on_received_bytes now bounds spend by the ceiling's own wire (no PULL_WINDOW_FLOOR slack).
  • Follow-up filed: client-pull: pin the voucher-write terminal recovery on the gap-driven drive path #1995 (pin the write recovery's interaction with payment-based completion on the drive path).

Out of scope

  • Issue test ADR 002: Specify BLAKE3→keccak256 Merkle proof scheme for on-chain slashing #4 (seller redeems signed vouchers after a failed-delivery verify) — an anvil e2e documenting accepted irreversibility, not part of the fix.
  • Recommendation 4 (max_blob_size_mb: 0 = unlimited warning wording) — docs, separate PR.
  • PullDeadlines::hard_cap / capped() are now consumed only by the test-util wrappers (the CLI still builds capped deadlines) — separate cleanup.

🤖 Generated with Claude Code

…ands on every path

The buffered `stream_fetch*` receiver drained the whole wire stream into a
buffer, releasing a 1 MiB reveal per chunk as bytes landed, and only ran the
bao decoder after `StreamEnd`. A provider that signed an inflated
`total_bytes` and streamed framed garbage was therefore paid for the whole
claim (bounded only by `max_blob_size_mb`) before a single byte verified. The
progressive path verified each group as it arrived, so exposure depended on
which receiver a caller happened to use.

Delete the buffered receiver (`fetch_inner_once`, `receive_and_pay`,
`decode_verified_range`). The `stream_fetch*` family survives only as
`test-util` in-memory wrappers over the one receive loop
(`open_progressive_pull` → `UpstreamPull` → `PullReader` → the bao
`ResponseDecoder`), so a corrupt group aborts the pull at that group on every
path; what a lying peer can bill is the bytes that arrived before its first
unverifiable group, never its claim.

`UpstreamPull::next_chunk` absorbs the two guards only the buffered loop had:
the received-byte ceiling (#1895) as a wire bound checked before the crossing
chunk is paid (`open_progressive_pull` takes `max_blob_size_bytes`; every
caller passes its ceiling), and the voucher-write recovery that prefers a
terminal `StreamEnd` / `StreamError` over the opaque write failure a node's
end-of-stream `STOP_SENDING(0)` produces.

`bao_encoded_size` gains an `O(log groups)` closed form for the group-aligned
contiguous ranges `align_range` produces, property-tested against the encoder
walk. The walk is `O(groups)` — 67M groups for the CLI's default 1 TiB ceiling
or for any peer-claimed `total_bytes` in that range — and stalled the pull
past the node's voucher wait before the first byte.

Tests: two loopback tests pin the exposure bound against an inflated-garbage
provider (zero paid below one metering interval; ≤ one frame above it), one
pins the empty-claim-for-a-non-empty-hash rejection, and the
`decode_verified_range` unit tests port onto the shared in-memory decoder.
Stale prose naming the buffered path, `stream_fetch` as the production
requester, or wire-byte progress units is rewritten to describe the one loop.

Closes #1985

Co-Authored-By: Claude Opus 5 <[email protected]>
@github-actions

github-actions Bot commented Sep 11, 2026 •

Copy link
Copy Markdown

Coverage report

Total: 82.68% (was 82.67%, ▲ +0.01%) — 76508/92538 lines

File Coverage Lines Δ
crates/bao-range/src/lib.rs 95.95% 237/247 ▲ +2.07%
crates/cli/src/commands/fetch.rs 41.98% 927/2208 ▼ 0.04%
crates/cli/src/commands/pull_progress.rs 76.17% 294/386 ● 0.00%
crates/client-pull/src/driver.rs 92.74% 1150/1240 ▲ +0.10%
crates/client-pull/src/ledger.rs 97.06% 793/817 ● 0.00%
crates/client-pull/src/lib.rs 89.42% 1690/1890 ▼ 0.42%
crates/client-pull/src/ranged_store.rs 91.04% 1037/1139 ● 0.00%
crates/client-pull/src/sink.rs 98.00% 147/150 ▲ +1.33%
crates/client-pull/src/source.rs 93.00% 319/343 ▲ +0.02%
crates/e2e/src/client.rs 0.00% 0/454 ● 0.00%
crates/node/src/buyer_ledgers.rs 100.00% 109/109 ● 0.00%
crates/node/src/dht/node_address.rs 83.78% 93/111 ● 0.00%
crates/node/src/node_origin/mod.rs 92.92% 1010/1087 ▲ +0.01%
crates/node/src/node_origin/pull_leg.rs 83.83% 679/810 ▲ +0.02%
History
Commit Total Δ vs base
90715f0 82.67% ● 0.00%
cb14c1c (HEAD) 82.68% ▲ +0.01%

…guard, exposure tests

Tag the hash-chain rollover contradiction as a local fault so the voucher-write
recovery can never rescue it into a clean completion; carry what the recovery
read saw into the write error's context; state the resume-offset-past-ceiling
check directly instead of mapping every alignment error to `BlobTooLarge`.

`driver::drive` proves a `total_bytes == 0` claim against the empty root before
the empty store can be finalized — the one verify site the gap-driven path had
none of, so a holder could complete `decdn fetch <non-empty hash>` with an
empty file.

Tests: honest-prefix-then-garbage exposure bound (payment for verified groups
coexists with the cut-off), the mid-stream straddling overrun guard, the
received-byte ceiling on a gap-scoped leg anchored at its own offset, a
multi-group exact-ceiling blob, the driver's empty-claim guard both ways; the
window pull-through ceiling bound tightens to the ceiling's own wire; the
progress test pins content bytes as the unit.

Prose: the CLI progress bar and `ProgressCallback` consumers describe content
bytes; `HashMismatch` names its actual returners; stale `stream_fetch` /
buffered-path references in tests, `sink.rs`, and ADR 038 §Receive side
describe the one receive loop.

Co-Authored-By: Claude Opus 5 <[email protected]>
@alpergundogdu
alpergundogdu merged commit 0b94efe into main Sep 11, 2026
27 checks passed
@alpergundogdu
alpergundogdu deleted the claude/github-issue-1985-6e752e branch September 11, 2026 08:29
alpergundogdu added a commit that referenced this pull request Sep 11, 2026
…e pull

Copilot review of #1996: `ClientFetchArgs::validate()` still errored with "the
hard cap bounds the whole exchange," contradicting the updated docs that the
delivery pull is uncapped. The contradiction ran wider than the one message —
the `--timeout-ms` field help, the `hard_cap()` getter doc, and several
validate-related test docs/names were all premised on `--timeout-ms` capping
the blob fetch, which it has not since the pull path stopped consulting
`PullDeadlines::hard_cap` (#1134/#1994).

Bring all of them to the current truth: `--timeout-ms` bounds the CapacityBond
registry read (discovery, `discovery_cap`); the delivery pull carries no overall
wall-clock cap and is bounded only by `--stall-timeout-ms` and
`--min-throughput-bps`, which together catch a dead or drip-feeding provider
(#1797). The `validate()` check (`timeout > 2 × stall`) is preserved exactly —
only its rationale is restated as a misconfiguration guard rather than a
stall-detection one. Test assertions are unchanged; two tests are renamed off
the old "hard cap disables stall detection" framing.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
alpergundogdu added a commit that referenced this pull request Sep 11, 2026
…d pull cap (#1996)

* refactor(client-pull): dedup empty-claim guard; drop the CLI's ignored pull cap

Two review follow-ups from #1994.

Share the empty-claim guard. The `total_bytes == 0 && hash != empty-root ->
HashMismatch` check (#1054) lived hand-copied in `driver::drive` and the
in-memory `fetch_in_memory_once` path. Extract one crate-private
`reject_empty_claim_for_nonempty_root` both call, so the two verify sites for
the trivial-empty-range bypass cannot drift.

Drop the CLI's ignored pull hard-cap. Since #1994 the production pull path
(`open_progressive_pull` / `driver::drive`) never consults `PullDeadlines`'
`hard_cap`: a progressing pull is bounded only by the open bound and the stall
window (#1134). The CLI's `fetch` and `bundle pull` still built `capped`
deadlines carrying a `hard_cap` the pull ignores, and re-ran the cap-outlasts-
stages validation `common.validate()` already performs at parse time. Switch
both to `PullDeadlines::new` (functionally inert — the pull was never
hard-capped) and rewrite the now-stale comments plus `ClientFetchArgs::validate`
docs to state the truth: `--timeout-ms` bounds discovery, not the pull.

`PullDeadlines::capped` / `hard_cap` / `whole_transfer` / `with_hard_cap` stay:
the test-util in-memory wrappers still cap the whole exchange through them.
Fully retiring `--timeout-ms` as a pull cap (and thus `capped`) is a separate
design question, left out here.

Co-Authored-By: Claude Opus 4.8 <[email protected]>

* docs(common): correct --timeout-ms docs — it bounds discovery, not the pull

Copilot review of #1996: `ClientFetchArgs::validate()` still errored with "the
hard cap bounds the whole exchange," contradicting the updated docs that the
delivery pull is uncapped. The contradiction ran wider than the one message —
the `--timeout-ms` field help, the `hard_cap()` getter doc, and several
validate-related test docs/names were all premised on `--timeout-ms` capping
the blob fetch, which it has not since the pull path stopped consulting
`PullDeadlines::hard_cap` (#1134/#1994).

Bring all of them to the current truth: `--timeout-ms` bounds the CapacityBond
registry read (discovery, `discovery_cap`); the delivery pull carries no overall
wall-clock cap and is bounded only by `--stall-timeout-ms` and
`--min-throughput-bps`, which together catch a dead or drip-feeding provider
(#1797). The `validate()` check (`timeout > 2 × stall`) is preserved exactly —
only its rationale is restated as a misconfiguration guard rather than a
stall-detection one. Test assertions are unchanged; two tests are renamed off
the old "hard cap disables stall detection" framing.

Co-Authored-By: Claude Opus 4.8 <[email protected]>

---------

Co-authored-by: Claude Opus 4.8 <[email protected]>
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-pull: hostile provider bills the full buffered stream before any byte verifies when total_bytes is inflated

2 participants