This repository has been archived on 2026-08-30. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
varde/varde-daemon/tests/store_ops.rs
T
bl ad69f7d884 Migrate to iroh 1.0 and iroh-blobs 0.103
The 0.35 pin's rationale ("the post-0.35 rewrite is not yet production
quality") expired when iroh hit 1.0 in June 2026: the rewrite line
(0.103) is now the production line and the only one receiving fixes.

The rewrite replaced wrappable store traits with an irpc-based API, so
the seams moved:

- shaped.rs: the 440-line ShapedStore trait wrapper becomes a provider
  event handler. Trust gating (untrusted peers see only openly-served
  hashes) now intercepts requests before any bytes move; upload rate
  limiting rides the provider's Throttle hook; upload progress events
  come from per-request update streams. The TokenBucket is unchanged.
- store.rs: FsStore's API handle replaces the store traits, and the
  LocalPool machinery for non-Send futures is gone. GC is now the
  store's built-in periodic mark-and-sweep, fed by a pin-roots snapshot
  via the protect callback (new config knob gc_interval_secs, default
  300); the on-demand Gc request answers Unimplemented, and the gc
  conformance test polls the sweep instead.
- transfer.rs: BlobsProtocol + Router replace handle_connection, the
  new multi-provider Downloader replaces the old queue, and mdns
  discovery moved to the iroh-mdns-address-lookup crate (it left iroh
  core in 1.0). Ticket-embedded provider addresses feed a MemoryLookup
  address book. Endpoint presets: Minimal (LAN-only default) or N0
  (wan_upload), preserving the old relay posture.
- Node* became Endpoint* throughout; announcement signatures use iroh's
  own Signature type (ed25519-dalek dep dropped); iroh-io dropped.

Known regression: max_download_bytes_per_sec is currently not enforced
— download shaping rode the old store's batch writer and 0.103's
downloader has no equivalent seam yet.

Announcement wire format note: EndpointAddr serializes differently than
NodeAddr, so pre- and post-migration daemons won't parse each other's
LAN announcements. Announcements are live-only, nothing stored breaks.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-16 14:03:15 +02:00

280 lines
9.3 KiB
Rust

//! Milestone 2: Add, List, Materialize, Gc against a real daemon.
mod support;
use std::path::Path;
use varde_proto::{ErrorCode, MaterializeMode, PinPolicy, Request, ResponseData};
fn add(client: &mut support::Client, path: &Path, recursive: bool) -> (String, u64) {
let reply = client.request(&Request::Add {
path: path.display().to_string(),
recursive,
});
assert!(reply.ok, "add failed: {:?}", reply.error);
match reply.data {
Some(ResponseData::Added { hash, bytes }) => (hash, bytes),
other => panic!("unexpected add reply: {other:?}"),
}
}
fn materialize(
client: &mut support::Client,
hash: &str,
dest: &Path,
mode: MaterializeMode,
) -> Result<(u64, bool), ErrorCode> {
let reply = client.request(&Request::Materialize {
hash: hash.to_string(),
dest: dest.display().to_string(),
mode,
});
if !reply.ok {
return Err(reply.error.expect("error body").code);
}
match reply.data {
Some(ResponseData::Materialized { bytes, reflinked }) => Ok((bytes, reflinked)),
other => panic!("unexpected materialize reply: {other:?}"),
}
}
#[test]
fn file_round_trips_byte_identical() {
let daemon = support::spawn_daemon();
let mut client = support::Client::connect(&daemon.socket);
let dir = tempfile::tempdir().unwrap();
let src = dir.path().join("input.bin");
let payload: Vec<u8> = (0..100_000u32).flat_map(|i| i.to_le_bytes()).collect();
std::fs::write(&src, &payload).unwrap();
let (hash, bytes) = add(&mut client, &src, false);
assert_eq!(bytes, payload.len() as u64);
let dest = dir.path().join("output.bin");
let (out_bytes, _) = materialize(&mut client, &hash, &dest, MaterializeMode::Copy).unwrap();
assert_eq!(out_bytes, payload.len() as u64);
assert_eq!(
std::fs::read(&dest).unwrap(),
payload,
"bytes differ after round trip"
);
}
#[test]
fn directory_tree_round_trips() {
let daemon = support::spawn_daemon();
let mut client = support::Client::connect(&daemon.socket);
let dir = tempfile::tempdir().unwrap();
let src = dir.path().join("tree");
std::fs::create_dir_all(src.join("sub/deeper")).unwrap();
std::fs::write(src.join("a.txt"), b"alpha").unwrap();
std::fs::write(src.join("sub/b.bin"), vec![0u8; 20_000]).unwrap();
std::fs::write(src.join("sub/deeper/c"), b"").unwrap();
let (hash, bytes) = add(&mut client, &src, true);
assert_eq!(bytes, 5 + 20_000);
let dest = dir.path().join("out");
let (out_bytes, _) =
materialize(&mut client, &hash, &dest, MaterializeMode::ReflinkOrCopy).unwrap();
assert_eq!(out_bytes, bytes);
for rel in ["a.txt", "sub/b.bin", "sub/deeper/c"] {
let original = std::fs::read(src.join(rel)).unwrap();
let copy = std::fs::read(dest.join(rel)).unwrap();
assert_eq!(original, copy, "{rel} differs after round trip");
}
}
#[test]
fn directory_without_recursive_is_rejected() {
let daemon = support::spawn_daemon();
let mut client = support::Client::connect(&daemon.socket);
let dir = tempfile::tempdir().unwrap();
std::fs::write(dir.path().join("f"), b"x").unwrap();
let reply = client.request(&Request::Add {
path: dir.path().display().to_string(),
recursive: false,
});
assert!(!reply.ok);
assert_eq!(reply.error.unwrap().code, ErrorCode::InvalidArgument);
}
#[test]
fn materialize_unknown_hash_is_not_found() {
let daemon = support::spawn_daemon();
let mut client = support::Client::connect(&daemon.socket);
let dir = tempfile::tempdir().unwrap();
let missing = "0".repeat(64);
let err = materialize(
&mut client,
&missing,
&dir.path().join("out"),
MaterializeMode::Copy,
)
.unwrap_err();
assert_eq!(err, ErrorCode::NotFound);
}
#[test]
fn gc_drops_unpinned_and_keeps_pinned() {
// Since iroh-blobs 0.103 gc is a periodic loop inside the store, not
// an on-demand request; run it fast so the sweep happens in-test.
let daemon_dir = tempfile::tempdir().unwrap();
let daemon =
support::spawn_daemon_with_env(daemon_dir.path(), &[("VARDE_GC_INTERVAL", "1")]);
let mut client = support::Client::connect(&daemon.socket);
let dir = tempfile::tempdir().unwrap();
// A pinned directory tree (root + children + meta must all survive)
// and an unpinned lone file (must be collected).
let tree = dir.path().join("tree");
std::fs::create_dir_all(&tree).unwrap();
std::fs::write(tree.join("keep.bin"), vec![7u8; 50_000]).unwrap();
let (kept_root, _) = add(&mut client, &tree, true);
let loose = dir.path().join("loose.bin");
std::fs::write(&loose, vec![9u8; 50_000]).unwrap();
let (doomed, _) = add(&mut client, &loose, false);
let reply = client.request(&Request::Pin {
hash: kept_root.clone(),
policy: PinPolicy::default(),
});
assert!(reply.ok, "pin failed: {:?}", reply.error);
// The on-demand request reports the new reality honestly.
let reply = client.request(&Request::Gc {});
assert!(!reply.ok, "on-demand gc should be unimplemented");
// Wait for the periodic sweep to collect the loose blob.
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(20);
loop {
let err = materialize(
&mut client,
&doomed,
&dir.path().join("probe"),
MaterializeMode::Copy,
);
if err.is_err() {
break;
}
assert!(
std::time::Instant::now() < deadline,
"unpinned blob survived gc for 20s"
);
std::thread::sleep(std::time::Duration::from_millis(500));
}
// Pinned tree still materializes; loose blob is gone.
materialize(
&mut client,
&kept_root,
&dir.path().join("out"),
MaterializeMode::Copy,
)
.expect("pinned content must survive gc");
let err = materialize(
&mut client,
&doomed,
&dir.path().join("out2"),
MaterializeMode::Copy,
)
.unwrap_err();
assert_eq!(err, ErrorCode::NotFound);
// Unpinning lets the next sweep drop the tree too.
let reply = client.request(&Request::Unpin {
hash: kept_root.clone(),
});
assert!(reply.ok);
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(20);
loop {
let err = materialize(
&mut client,
&kept_root,
&dir.path().join("out3"),
MaterializeMode::Copy,
);
if let Err(code) = err {
assert_eq!(code, ErrorCode::NotFound);
break;
}
assert!(
std::time::Instant::now() < deadline,
"unpinned tree survived gc for 20s"
);
std::thread::sleep(std::time::Duration::from_millis(500));
}
}
#[test]
fn list_reports_pin_completeness() {
let daemon = support::spawn_daemon();
let mut client = support::Client::connect(&daemon.socket);
let dir = tempfile::tempdir().unwrap();
let src = dir.path().join("f.bin");
std::fs::write(&src, vec![1u8; 10_000]).unwrap();
let (hash, _) = add(&mut client, &src, false);
let reply = client.request(&Request::Pin {
hash: hash.clone(),
policy: PinPolicy { open_lan: true },
});
assert!(reply.ok);
let reply = client.request(&Request::List {});
assert!(reply.ok);
match reply.data {
Some(ResponseData::Pins { pins }) => {
assert_eq!(pins.len(), 1);
assert_eq!(pins[0].hash, hash);
assert!(pins[0].complete);
assert!(pins[0].policy.open_lan);
assert_eq!(pins[0].total_bytes, Some(10_000));
}
other => panic!("unexpected list reply: {other:?}"),
}
}
/// Reflink needs store and destination on one filesystem that supports
/// FICLONE. `CARGO_TARGET_TMPDIR` lives under `target/`, so this exercises
/// the real filesystem the repo sits on; on filesystems without reflink
/// the test degrades into asserting the copy fallback works.
#[test]
fn reflink_when_supported() {
let base = Path::new(env!("CARGO_TARGET_TMPDIR")).join("reflink-test");
let _ = std::fs::remove_dir_all(&base);
std::fs::create_dir_all(&base).unwrap();
let daemon = support::spawn_daemon_at(&base);
let mut client = support::Client::connect(&daemon.socket);
let src = base.join("big.bin");
// Comfortably above the inline threshold so the blob lands as a
// real file in the store's data dir.
std::fs::write(&src, vec![42u8; 1_000_000]).unwrap();
let (hash, _) = add(&mut client, &src, false);
let dest = base.join("out.bin");
let (bytes, reflinked) =
materialize(&mut client, &hash, &dest, MaterializeMode::ReflinkOrCopy).unwrap();
assert_eq!(bytes, 1_000_000);
assert_eq!(std::fs::read(&dest).unwrap(), vec![42u8; 1_000_000]);
if !reflinked {
eprintln!("SKIP: filesystem does not support reflink; copy fallback verified instead");
return;
}
// Forced copy mode must not reflink.
let dest2 = base.join("out2.bin");
let (_, reflinked2) = materialize(&mut client, &hash, &dest2, MaterializeMode::Copy).unwrap();
assert!(!reflinked2, "copy mode must never reflink");
drop(daemon);
let _ = std::fs::remove_dir_all(&base);
}