Skip to content

decdn fetch: stream validated bytes to disk incrementally + resume interrupted downloads #1120

Closed

Description

@alpergundogdu

Summary

decdn fetch buffers the entire blob in RAM and writes it to --output all-at-once at the end. If the fetch is interrupted, nothing usable lands on disk and re-running re-fetches (and re-pays for) the whole blob from byte 0. This tracks making the CLI fetch stream validated bytes to disk incrementally and resume an interrupted download, re-paying only for the un-fetched tail.

The cryptographic machinery for this already exists — this is contained to the CLI plus a small surface in client-pull; no protocol change.

Current behavior

  • fetch_blob returns the whole blob as a Vec<u8>; the client-pull receive loop accumulates every chunk into an in-memory BytesMut until StreamEnd (crates/client-pull/src/lib.rs:624, :647).
  • Verification runs on the complete buffer via bao (decode_verified_range, crates/client-pull/src/lib.rs:688).
  • write_blob_atomic then does one write_all → fsync → atomic rename into --output (crates/cli/src/commands/fetch.rs:571).
  • The CLI always passes byte_offset = 0 (crates/cli/src/commands/fetch.rs:369), so there is no resume.

Why it's tractable

Delivery uses bao verified-streaming: each 16 KiB chunk group is verified against the content root as it decodes, not a single whole-blob re-hash at the end. So:

  • Every validated chunk group is independently trustworthy → safe to flush to disk the moment it verifies (also drops peak memory from O(blob size) to O(chunk group)).
  • A range fetched at any byte_offset > 0 self-verifies against the root. client-pull already exposes byte_offset (stream_fetch_tracked, crates/client-pull/src/lib.rs:360/:367), and the node-side cache already imports BAO-verified partial blobs on the range-pull path (crates/cache/src/engine.rs:1681, import_bao_bytes, ADR 037/038).

Proposed work (CLI-side)

  1. Stream verified chunk groups to disk instead of buffering — write each bao-decoded Leaf to a <output>.partial temp file as it verifies, rather than appending to BytesMut.
  2. On resume, read the existing .partial length and pass it as byte_offset, re-paying only for the remainder.
  3. Promote .partial → final path on StreamEnd (keep the existing atomic rename).

Design decisions (settled)

  • Resume offset alignment: round the resume offset down to the last fully-verified 16 KiB chunk-group boundary; truncate .partial to that boundary before resuming.
  • Trusting a .partial from a prior process: re-verify the on-disk prefix by recomputing its bao outboard before resuming (option (a)). Safe, and still far cheaper than re-downloading. (bao makes the resumed tail self-verify against the root, but not the on-disk prefix — hence the re-verify.)
  • Node reachability on resume: re-run discovery / target-node resolution (crates/cli/src/commands/fetch.rs:291) and open/reuse a channel to whoever we land on. A different node can serve the tail since content is content-addressed.

Scope

Contained to crates/cli/src/commands/fetch.rs plus a small surface in crates/client-pull. No wire-protocol change.

Activity

  1. added
    kind:feature
    rust
    area:cli
    on Jul 12, 2026
  2. altanoruc commented on Jul 17, 2026

    @altanoruc
    Contributor

    Triage note for a later automated run: self-contained and mostly prescriptive (stream verified chunk groups to <output>.partial, resume via byte_offset, promote on StreamEnd), but one open correctness decision blocks decision-free dispatch. On resume, the newly-fetched range self-verifies against the root — but the existing .partial prefix already on disk is never re-verified against this hash. A stale, truncated, or wrong-hash .partial would be promoted to the final path as if valid. Pin the resume-safety rule before dispatch: re-verify the on-disk prefix against the root before trusting it, vs. a sidecar recording the (hash, verified-length) the .partial belongs to and refusing resume on mismatch.

  3. self-assigned this
    on Jul 17, 2026
  4. altanoruc commented on Jul 17, 2026

    @altanoruc
    Contributor

    Automated implementation triage found a remaining cryptographic decision, so no PR was opened. Recomputing a BAO outboard over an N-byte .partial authenticates an N-byte prefix tree, not the requested whole-blob root. The resumed BAO range independently authenticates only the tail, so the existing APIs cannot verify the retained prefix against the whole root before requesting only the tail. Please choose one safe design: persist a full-tree proof/outboard sidecar; re-fetch a proof-bearing prefix range; authenticate local prefix subtree hashes against commitments from the resumed-tail proof during receipt; or defer whole-root verification until prefix plus tail are complete. The latter two conflict with the current before-resuming wording.

  5. added this to the v0.1 milestone on Jul 28, 2026
  6. assigned and unassigned on Jul 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area:clikind:featurerust

Type

No type

Fields

Priority

None yet

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions