hashtree-cli 0.2.156

Hashtree daemon and CLI with content-addressed storage and adaptive blob routing
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
use anyhow::{Context, Result};
use futures::stream::{self, StreamExt};
use std::collections::HashSet;
use std::path::Path;
use std::sync::Arc;

use crate::config::ensure_keys_string;
use crate::fetch::{FetchConfig, Fetcher};
use crate::HashtreeStore;
use hashtree_core::{to_hex, Cid, HashTree, HashTreeConfig, Link, LinkType, Store, TreeNode};

const BLOSSOM_PUSH_CONCURRENCY: usize = 16;
const BLOSSOM_PUSH_PROGRESS_EVERY: usize = 512;

fn parse_root_cid(cid_str: &str) -> Result<Cid> {
    Cid::parse(cid_str).map_err(|e| anyhow::anyhow!("Invalid CID '{}': {}", cid_str, e))
}

fn child_cid(parent: &Cid, link: &Link) -> Cid {
    let inherits_parent_key = link
        .name
        .as_deref()
        .map(|name| {
            name.starts_with("_chunk_")
                || (name.starts_with('_') && name.chars().count() == 2 && link.link_type.is_tree())
        })
        .unwrap_or(false);

    Cid {
        hash: link.hash,
        key: link.key.or(if inherits_parent_key {
            parent.key
        } else {
            None
        }),
    }
}

async fn ensure_local_blob_for_push(
    store: &HashtreeStore,
    fetcher: Option<&Fetcher>,
    cid: &Cid,
) -> Result<()> {
    if store.get_blob(&cid.hash)?.is_some() {
        return Ok(());
    }

    if let Some(fetcher) = fetcher {
        let data = fetcher
            .fetch_chunk(&to_hex(&cid.hash))
            .await
            .with_context(|| format!("failed to hydrate missing local blob {}", cid))?;
        store
            .put_blob(&data)
            .with_context(|| format!("failed to persist hydrated blob {}", cid))?;
        if store.get_blob(&cid.hash)?.is_some() {
            return Ok(());
        }
    }

    anyhow::bail!("missing local blob while pushing DAG: {}", cid);
}

async fn node_for_push<S: Store>(
    tree: &HashTree<S>,
    cid: &Cid,
    blob_size: Option<u64>,
) -> Result<Option<TreeNode>> {
    if let Some(size) = blob_size {
        if let Some(data) = tree.get_blob(&cid.hash).await? {
            // Match the core file reader: a Blob's declared size describes its
            // plaintext leaf, regardless of the writer's chunk size. Authenticate
            // keyed bytes before treating tree-shaped file contents as a leaf.
            let plaintext_size = match cid.key.as_ref() {
                Some(key) => hashtree_core::crypto::decrypt_chk(&data, key)
                    .ok()
                    .map(|plaintext| plaintext.len() as u64),
                None => Some(data.len() as u64),
            };
            if plaintext_size == Some(size) {
                return Ok(None);
            }
        }
    }

    tree.get_node(cid)
        .await
        .map_err(|err| anyhow::anyhow!("Failed to inspect {cid}: {err}"))
}

pub(crate) async fn collect_cids_for_push(
    store: &HashtreeStore,
    root_cid: Cid,
    fetcher: Option<&Fetcher>,
) -> Result<Vec<Cid>> {
    let mut cids_to_push = Vec::new();
    // A shared CID can be both literal file bytes and a structural node.
    // Traverse each interpretation, but upload each block only once.
    let mut visited = HashSet::new();
    let mut collected = HashSet::new();
    let mut queue = vec![(root_cid, None)];
    let tree = HashTree::new(HashTreeConfig::new(store.store_arc()).public());

    while let Some((cid, blob_size)) = queue.pop() {
        if !visited.insert((cid.hash, blob_size)) {
            continue;
        }

        ensure_local_blob_for_push(store, fetcher, &cid).await?;
        if collected.insert(cid.hash) {
            cids_to_push.push(cid.clone());
        }

        let node = node_for_push(&tree, &cid, blob_size).await?;

        if let Some(node) = node {
            for link in &node.links {
                queue.push((
                    child_cid(&cid, link),
                    (link.link_type == LinkType::Blob).then_some(link.size),
                ));
            }
        }
    }

    Ok(cids_to_push)
}

fn matching_old_child<'a>(
    old_links: &'a [Link],
    new_index: usize,
    new_link: &Link,
) -> Option<&'a Link> {
    if let Some(name) = new_link.name.as_deref() {
        old_links
            .iter()
            .find(|old_link| old_link.name.as_deref() == Some(name))
    } else {
        old_links
            .get(new_index)
            .filter(|old_link| old_link.name.is_none())
    }
}

pub(crate) async fn collect_incremental_cids_for_push(
    store: &HashtreeStore,
    root_cid: Cid,
    previous_root_cid: Cid,
    fetcher: Option<&Fetcher>,
) -> Result<Vec<Cid>> {
    collect_incremental_cids(store, root_cid, previous_root_cid, fetcher, false).await
}

async fn collect_incremental_cids(
    store: &HashtreeStore,
    root_cid: Cid,
    previous_root_cid: Cid,
    fetcher: Option<&Fetcher>,
    require_previous_nodes: bool,
) -> Result<Vec<Cid>> {
    let mut cids_to_push = Vec::new();
    let mut visited_new = HashSet::new();
    let mut collected = HashSet::new();
    let mut queue = vec![(root_cid, Some(previous_root_cid), None)];
    let tree = HashTree::new(HashTreeConfig::new(store.store_arc()).public());

    while let Some((cid, old_cid, blob_size)) = queue.pop() {
        if old_cid.as_ref().is_some_and(|old| old.hash == cid.hash) {
            continue;
        }
        if !visited_new.insert((cid.hash, blob_size)) {
            continue;
        }

        ensure_local_blob_for_push(store, fetcher, &cid).await?;
        if collected.insert(cid.hash) {
            cids_to_push.push(cid.clone());
        }

        let node = node_for_push(&tree, &cid, blob_size).await?;
        let Some(node) = node else {
            continue;
        };

        if require_previous_nodes {
            if let Some(old_cid) = old_cid.as_ref() {
                ensure_local_blob_for_push(store, fetcher, old_cid)
                    .await
                    .context("previous DAG is unavailable for a bounded delta push")?;
            }
        }
        let old_node = match old_cid.as_ref() {
            Some(old_cid) => match node_for_push(&tree, old_cid, blob_size).await {
                Ok(old_node) => old_node,
                Err(err) if require_previous_nodes => {
                    return Err(err).context("previous DAG is unreadable for a bounded delta push");
                }
                Err(err) => {
                    tracing::warn!(
                        "Failed to inspect previous Blossom DAG node {}; uploading changed subtree: {}",
                        old_cid,
                        err
                    );
                    None
                }
            },
            None => None,
        };

        for (index, link) in node.links.iter().enumerate() {
            let child = child_cid(&cid, link);
            let old_child = old_node
                .as_ref()
                .and_then(|old_node| matching_old_child(&old_node.links, index, link))
                .filter(|old_link| {
                    (old_link.link_type == LinkType::Blob).then_some(old_link.size)
                        == (link.link_type == LinkType::Blob).then_some(link.size)
                })
                .map(|old_link| child_cid(old_cid.as_ref().expect("old node has cid"), old_link));

            if old_child
                .as_ref()
                .is_some_and(|old_child| old_child.hash == child.hash)
            {
                continue;
            }
            queue.push((
                child,
                old_child,
                (link.link_type == LinkType::Blob).then_some(link.size),
            ));
        }
    }

    Ok(cids_to_push)
}

async fn upload_cids_with_client(
    store: Arc<HashtreeStore>,
    fetcher: Option<Arc<Fetcher>>,
    client: hashtree_blossom::BlossomClient,
    cids_to_push: Vec<Cid>,
    force_upload: bool,
) -> Result<(usize, usize)> {
    let total = cids_to_push.len();
    let mut total_uploaded = 0usize;
    let mut total_skipped = 0usize;
    let mut total_errors = 0usize;
    let mut last_error = None;
    let mut processed = 0usize;

    let mut uploads = stream::iter(cids_to_push.into_iter().map(|cid| {
        let store = Arc::clone(&store);
        let fetcher = fetcher.clone();
        let client = client.clone();
        async move {
            ensure_local_blob_for_push(store.as_ref(), fetcher.as_deref(), &cid).await?;
            let data = store
                .get_blob(&cid.hash)?
                .ok_or_else(|| anyhow::anyhow!("missing local blob while uploading {}", cid))?;
            if force_upload {
                client
                    .upload_to_selected_servers(&data, client.write_servers())
                    .await
                    .map(|(hash, _successes)| (hash, true))
                    .map_err(|error| anyhow::anyhow!(error.to_string()))
            } else {
                client
                    .upload_if_missing(&data)
                    .await
                    .map_err(|error| anyhow::anyhow!(error.to_string()))
            }
        }
    }))
    .buffer_unordered(BLOSSOM_PUSH_CONCURRENCY);

    while let Some(result) = uploads.next().await {
        processed += 1;
        match result {
            Ok((_hash, was_uploaded)) => {
                if was_uploaded {
                    total_uploaded += 1;
                } else {
                    total_skipped += 1;
                }
            }
            Err(error) => {
                tracing::warn!("Blossom upload failed: {}", error);
                total_errors += 1;
                last_error = Some(error.to_string());
            }
        }

        if processed.is_multiple_of(BLOSSOM_PUSH_PROGRESS_EVERY) || processed == total {
            println!(
                "  file servers: {processed}/{total} processed ({total_uploaded} uploaded, {total_skipped} already exist, {total_errors} failed)",
            );
        }
    }

    if total_errors > 0 {
        let detail = last_error
            .as_deref()
            .map(|err| format!(" (last error: {err})"))
            .unwrap_or_default();
        anyhow::bail!(
            "failed to upload {} blob(s) to configured file servers{}",
            total_errors,
            detail
        );
    }

    Ok((total_uploaded, total_skipped))
}

/// Push content to Blossom servers.
pub async fn push_to_blossom(
    data_dir: &Path,
    cid_str: &str,
    server_override: Option<String>,
    force_upload: bool,
    shallow: bool,
    previous_root: Option<&str>,
) -> Result<()> {
    use hashtree_blossom::BlossomClient;
    use nostr::Keys;

    if shallow && previous_root.is_some() {
        anyhow::bail!("a DAG delta cannot be combined with a shallow push");
    }

    let (nsec_str, _) = ensure_keys_string()?;
    let keys = Keys::parse(&nsec_str).context("Failed to parse nsec")?;

    let client = if let Some(server) = server_override {
        BlossomClient::new(keys).with_write_servers(vec![server])
    } else {
        BlossomClient::new(keys)
    };

    if client.write_servers().is_empty() {
        anyhow::bail!(
            "No file servers configured. Use --server or add write_servers to config.toml"
        );
    }

    let store = Arc::new(HashtreeStore::new(data_dir)?);
    let fetcher = Arc::new(Fetcher::new(FetchConfig::default()));

    let root_cid = parse_root_cid(cid_str)?;
    let cids_to_push = if shallow {
        ensure_local_blob_for_push(store.as_ref(), Some(fetcher.as_ref()), &root_cid).await?;
        vec![root_cid]
    } else if let Some(previous_root) = previous_root {
        let previous = parse_root_cid(previous_root)?;
        ensure_local_blob_for_push(store.as_ref(), Some(fetcher.as_ref()), &previous).await?;
        println!("Collecting blocks changed since the retained previous root...");
        // The caller must already retain the previous DAG on this server. A
        // collection error aborts this push rather than restarting a full scan.
        collect_incremental_cids(
            store.as_ref(),
            root_cid,
            previous,
            Some(fetcher.as_ref()),
            true,
        )
        .await?
    } else {
        println!("Collecting blocks...");
        collect_cids_for_push(store.as_ref(), root_cid, Some(fetcher.as_ref())).await?
    };

    println!("Found {} blocks to push", cids_to_push.len());
    let (uploaded, skipped) =
        upload_cids_with_client(store, Some(fetcher), client, cids_to_push, force_upload).await?;

    println!("\nUploaded: {uploaded}, Skipped: {skipped}, Errors: 0");
    println!("Done!");
    Ok(())
}

/// Push tree to Blossom servers using BlossomClient.
pub async fn background_blossom_push(
    data_dir: &Path,
    cid_str: &str,
    servers: &[String],
) -> Result<()> {
    let store = Arc::new(HashtreeStore::new(data_dir)?);
    background_blossom_push_with_store(store, cid_str, servers).await
}

pub async fn background_blossom_push_with_store(
    store: Arc<HashtreeStore>,
    cid_str: &str,
    servers: &[String],
) -> Result<()> {
    let root_cid = parse_root_cid(cid_str)?;
    background_blossom_push_incremental_with_store(store, root_cid, None, servers).await
}

pub async fn background_blossom_push_incremental_with_store(
    store: Arc<HashtreeStore>,
    root_cid: Cid,
    previous_root_cid: Option<Cid>,
    servers: &[String],
) -> Result<()> {
    use hashtree_blossom::BlossomClient;
    use nostr::Keys;

    let (nsec_str, _) = ensure_keys_string()?;
    let keys = Keys::parse(&nsec_str).context("Failed to parse nsec")?;

    let fetcher = Arc::new(Fetcher::new(FetchConfig::default()));
    let cids_to_push = if let Some(previous_root_cid) = previous_root_cid.as_ref() {
        println!("Collecting bounded DAG diff for file-server push...");
        match collect_incremental_cids_for_push(
            store.as_ref(),
            root_cid.clone(),
            previous_root_cid.clone(),
            Some(fetcher.as_ref()),
        )
        .await
        {
            Ok(cids) => cids,
            Err(err) => {
                tracing::warn!(
                    "Blossom DAG diff failed; falling back to full push: {}",
                    err
                );
                collect_cids_for_push(store.as_ref(), root_cid, Some(fetcher.as_ref())).await?
            }
        }
    } else {
        println!("Collecting DAG for file-server push...");
        collect_cids_for_push(store.as_ref(), root_cid, Some(fetcher.as_ref())).await?
    };

    if cids_to_push.is_empty() {
        return Ok(());
    }

    let client = if servers.is_empty() {
        BlossomClient::new(keys)
    } else {
        BlossomClient::new(keys).with_write_servers(servers.to_vec())
    };
    upload_cids_with_client(store, Some(fetcher), client, cids_to_push, false).await?;
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::{collect_cids_for_push, collect_incremental_cids_for_push};
    use crate::HashtreeStore;
    use futures::executor::block_on as sync_block_on;
    use hashtree_core::{DirEntry, HashTree, HashTreeConfig, LinkType};
    use tempfile::tempdir;

    #[tokio::test]
    async fn push_collectors_accept_tree_shaped_raw_blobs() {
        let tmp = tempdir().expect("tempdir");
        let store = HashtreeStore::with_options(tmp.path(), None, 32 * 1024 * 1024).expect("store");
        let payload = [0x82, 0xa1, b'l', 0x90, 0xa1, b't', 0x40];

        for encrypted in [false, true] {
            let mut config = HashTreeConfig::new(store.store_arc());
            if !encrypted {
                config = config.public();
            }
            let tree = HashTree::new(config);
            let old_root = tree.put_directory(vec![]).await.expect("old root");
            let (blob, size) = tree.put(&payload).await.expect("raw blob");
            let root = tree
                .put_directory(vec![
                    DirEntry::from_cid("raw.msgpack", &blob).with_size(size)
                ])
                .await
                .expect("root");

            for previous in [None, Some(old_root)] {
                let cids = match previous {
                    Some(previous) => {
                        collect_incremental_cids_for_push(&store, root.clone(), previous, None)
                            .await
                    }
                    None => collect_cids_for_push(&store, root.clone(), None).await,
                }
                .expect("raw bytes must not be decoded as a structural node");
                let hashes = cids
                    .iter()
                    .map(|cid| cid.hash)
                    .collect::<std::collections::HashSet<_>>();
                assert_eq!(
                    hashes,
                    std::collections::HashSet::from([root.hash, blob.hash])
                );
            }
        }
    }

    #[tokio::test]
    async fn push_collectors_walk_mixed_chunk_sizes_and_reject_missing_chunks() {
        let tmp = tempdir().expect("tempdir");
        let store = HashtreeStore::with_options(tmp.path(), None, 32 * 1024 * 1024).expect("store");
        let tree = HashTree::new(HashTreeConfig::new(store.store_arc()));
        let old_root = tree.put_directory(vec![]).await.expect("old root");
        let mut entries = Vec::new();
        let mut required_hashes = std::collections::HashSet::new();
        let mut missing_chunk = None;

        for chunk_size in [64 * 1024, hashtree_core::DEFAULT_CHUNK_SIZE] {
            let chunk_tree = HashTree::new(
                HashTreeConfig::new(store.store_arc())
                    .with_chunk_size(chunk_size)
                    .with_max_links(2),
            );
            let data = [
                vec![1; chunk_size],
                vec![2; chunk_size],
                vec![3; chunk_size],
                vec![4; 7],
            ]
            .concat();
            let (file, size) = chunk_tree.put(&data).await.expect("chunked file");
            required_hashes.insert(file.hash);
            for chunk in data.chunks(chunk_size) {
                let (leaf, _) = chunk_tree.put(chunk).await.expect("leaf");
                required_hashes.insert(leaf.hash);
                missing_chunk.get_or_insert(leaf.hash);
            }
            entries.push(DirEntry::from_cid(chunk_size.to_string(), &file).with_size(size));
        }
        let root = tree.put_directory(entries).await.expect("mixed root");
        required_hashes.insert(root.hash);

        for previous in [None, Some(old_root.clone())] {
            let cids = match previous {
                Some(previous) => {
                    collect_incremental_cids_for_push(&store, root.clone(), previous, None).await
                }
                None => collect_cids_for_push(&store, root.clone(), None).await,
            }
            .expect("all chunk sizes must be traversed");
            let hashes = cids
                .iter()
                .map(|cid| cid.hash)
                .collect::<std::collections::HashSet<_>>();
            assert!(required_hashes.is_subset(&hashes));
        }

        store
            .router()
            .delete_local_only(&missing_chunk.expect("leaf"))
            .expect("delete chunk");
        for previous in [None, Some(old_root)] {
            let result = match previous {
                Some(previous) => {
                    collect_incremental_cids_for_push(&store, root.clone(), previous, None).await
                }
                None => collect_cids_for_push(&store, root.clone(), None).await,
            };
            assert!(result
                .expect_err("missing chunk must fail")
                .to_string()
                .contains("missing local blob"));
        }
    }

    #[tokio::test]
    async fn push_collectors_reject_invalid_structural_nodes() {
        let tmp = tempdir().expect("tempdir");
        let store = HashtreeStore::with_options(tmp.path(), None, 32 * 1024 * 1024).expect("store");
        let tree = HashTree::new(HashTreeConfig::new(store.store_arc()));
        let old_root = tree.put_directory(vec![]).await.expect("old root");
        let (invalid, size) = tree
            .put(&[0x82, 0xa1, b'l', 0x90, 0xa1, b't', 0x40])
            .await
            .expect("invalid node");

        for kind in [LinkType::Dir, LinkType::File, LinkType::Fanout] {
            let root = tree
                .put_directory(vec![DirEntry::from_cid("invalid", &invalid)
                    .with_size(size)
                    .with_link_type(kind)])
                .await
                .expect("root");
            for previous in [None, Some(old_root.clone())] {
                let result = match previous {
                    Some(previous) => {
                        collect_incremental_cids_for_push(&store, root.clone(), previous, None)
                            .await
                    }
                    None => collect_cids_for_push(&store, root.clone(), None).await,
                };
                assert!(result
                    .expect_err("invalid structural node must fail")
                    .to_string()
                    .contains("Invalid node type: 64"));
            }
        }
    }

    #[tokio::test]
    async fn push_collectors_expand_nodes_also_referenced_as_raw_blobs() {
        let tmp = tempdir().expect("tempdir");
        let store = HashtreeStore::with_options(tmp.path(), None, 32 * 1024 * 1024).expect("store");
        for encrypted in [false, true] {
            let mut config = HashTreeConfig::new(store.store_arc());
            if !encrypted {
                config = config.public();
            }
            let chunk_tree = HashTree::new(HashTreeConfig {
                chunk_size: 4,
                encrypted,
                ..HashTreeConfig::new(store.store_arc())
            });
            let tree = HashTree::new(config);
            let (file, size) = chunk_tree.put(b"multiple chunks").await.expect("file");
            let file_node = tree
                .get_node(&file)
                .await
                .expect("file node")
                .expect("node");
            let encoded = tree
                .get_blob(&file.hash)
                .await
                .expect("encoded file")
                .expect("bytes");
            let encoded = match file.key.as_ref() {
                Some(key) => {
                    hashtree_core::crypto::decrypt_chk(&encoded, key).expect("decrypt file")
                }
                None => encoded,
            };
            let (raw, raw_size) = tree.put(&encoded).await.expect("raw encoded node");
            assert_eq!(file, raw, "both interpretations must share the same CID");
            let old_root = tree.put_directory(vec![]).await.expect("old root");
            let raw_only_root = tree
                .put_directory(vec![
                    DirEntry::from_cid("a-file", &raw).with_size(raw_size),
                    DirEntry::from_cid("z-raw", &raw).with_size(raw_size),
                ])
                .await
                .expect("raw-only root");
            let root = tree
                .put_directory(vec![
                    DirEntry::from_cid("a-file", &file)
                        .with_size(size)
                        .with_link_type(LinkType::File),
                    DirEntry::from_cid("z-raw", &raw).with_size(raw_size),
                ])
                .await
                .expect("root");
            let mut expected = std::collections::HashSet::from([root.hash, file.hash]);
            expected.extend(file_node.links.iter().map(|link| link.hash));

            for previous in [None, Some(old_root), Some(raw_only_root)] {
                let cids = match previous {
                    Some(previous) => {
                        collect_incremental_cids_for_push(&store, root.clone(), previous, None)
                            .await
                    }
                    None => collect_cids_for_push(&store, root.clone(), None).await,
                }
                .expect("structural reference must still expand after raw reference");
                let hashes = cids
                    .iter()
                    .map(|cid| cid.hash)
                    .collect::<std::collections::HashSet<_>>();
                assert_eq!(hashes, expected);
                assert_eq!(
                    cids.len(),
                    hashes.len(),
                    "upload each shared block only once"
                );
            }
        }
    }

    #[tokio::test]
    async fn incremental_push_does_not_prune_children_of_old_raw_blobs() {
        let tmp = tempdir().expect("tempdir");
        let store = HashtreeStore::with_options(tmp.path(), None, 32 * 1024 * 1024).expect("store");
        for encrypted in [false, true] {
            let config = || HashTreeConfig {
                encrypted,
                ..HashTreeConfig::new(store.store_arc())
            };
            let tree = HashTree::new(config());
            let chunk_tree = HashTree::new(config().with_chunk_size(4));
            let (shared, _) = chunk_tree.put(b"abcd").await.expect("shared leaf");
            let (file, _) = chunk_tree.put(b"abcdabcd").await.expect("file node");
            let encoded = tree
                .get_blob(&file.hash)
                .await
                .expect("encoded node")
                .expect("bytes");
            let encoded = match file.key.as_ref() {
                Some(key) => {
                    hashtree_core::crypto::decrypt_chk(&encoded, key).expect("decrypt node")
                }
                None => encoded,
            };
            let (raw, size) = tree.put(&encoded).await.expect("literal node bytes");
            let old_root = tree
                .put_directory(vec![DirEntry::from_cid("file", &raw).with_size(size)])
                .await
                .expect("old root");
            let old_cids = collect_cids_for_push(&store, old_root.clone(), None)
                .await
                .expect("raw-only closure");
            assert!(!old_cids.iter().any(|cid| cid.hash == shared.hash));

            let mut data = vec![b'x'; size as usize];
            data[..4].copy_from_slice(b"abcd");
            let (new_file, new_size) = chunk_tree.put(&data).await.expect("new chunked file");
            assert_eq!(new_size, size);
            assert_ne!(new_file.hash, raw.hash);
            let new_root = tree
                .put_directory(vec![
                    DirEntry::from_cid("file", &new_file).with_size(new_size)
                ])
                .await
                .expect("new root");
            let cids = collect_incremental_cids_for_push(&store, new_root, old_root, None)
                .await
                .expect("changed closure");
            assert!(
                cids.iter().any(|cid| cid.hash == shared.hash),
                "old raw bytes provide no descendant coverage"
            );
        }
    }

    #[tokio::test]
    async fn failed_http_uploads_return_an_error() {
        let tmp = tempdir().expect("tempdir");
        let store = std::sync::Arc::new(
            HashtreeStore::with_options(tmp.path(), None, 32 * 1024 * 1024).expect("store"),
        );
        let tree = HashTree::new(HashTreeConfig::new(store.store_arc()));
        let (cid, _) = tree.put(b"upload rejection fixture").await.expect("blob");
        let requests = std::sync::Arc::new(std::sync::atomic::AtomicUsize::new(0));
        let observed_requests = requests.clone();
        let app = axum::Router::new().route(
            "/upload",
            axum::routing::put(move |body: bytes::Bytes| async move {
                assert!(!body.is_empty());
                observed_requests.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
                axum::http::StatusCode::BAD_REQUEST
            }),
        );
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
            .await
            .expect("bind");
        let address = listener.local_addr().expect("address");
        let server = tokio::spawn(async move { axum::serve(listener, app).await.expect("serve") });
        let client = hashtree_blossom::BlossomClient::new_empty(nostr::Keys::generate())
            .with_write_servers(vec![format!("http://{address}")]);

        for force_upload in [false, true] {
            let result = super::upload_cids_with_client(
                store.clone(),
                None,
                client.clone(),
                vec![cid.clone()],
                force_upload,
            )
            .await;
            assert!(result
                .expect_err("failed uploads must not report success")
                .to_string()
                .contains("failed to upload 1 blob(s)"));
        }
        assert_eq!(requests.load(std::sync::atomic::Ordering::Relaxed), 2);
        server.abort();
        let _ = server.await;
    }

    #[tokio::test]
    async fn collect_cids_for_push_fails_on_missing_descendant_blob() {
        let tmp = tempdir().expect("tempdir");
        let store = HashtreeStore::with_options(tmp.path(), None, 32 * 1024 * 1024).expect("store");
        let tree = HashTree::new(HashTreeConfig::new(store.store_arc()).public());

        let root = sync_block_on(async {
            let (file_cid, _size) = tree.put_file(b"hello").await.expect("file");
            tree.put_directory(vec![hashtree_core::DirEntry::from_cid(
                "greeting.txt",
                &file_cid,
            )])
            .await
            .expect("dir")
        });

        let entries = store
            .get_tree_node(&root.hash)
            .expect("root node")
            .expect("root node present")
            .links;
        let child_hash = entries[0].hash;
        store
            .router()
            .delete_local_only(&child_hash)
            .expect("delete child locally");

        let err = collect_cids_for_push(&store, root, None)
            .await
            .expect_err("missing child should fail");
        assert!(err
            .to_string()
            .contains("missing local blob while pushing DAG"));
    }

    #[tokio::test]
    async fn explicit_delta_fails_when_a_previous_comparison_subtree_is_missing() {
        let tmp = tempdir().expect("tempdir");
        let store = HashtreeStore::with_options(tmp.path(), None, 32 * 1024 * 1024).expect("store");
        let tree = HashTree::new(HashTreeConfig::new(store.store_arc()).public());
        let old_child = tree.put_directory(vec![]).await.expect("old child");
        let old_root = tree
            .put_directory(vec![
                DirEntry::from_cid("subtree", &old_child).with_link_type(LinkType::Dir)
            ])
            .await
            .expect("old root");
        let leaf = tree.put_blob(b"new event").await.expect("leaf");
        let new_child = tree
            .put_directory(vec![DirEntry::new("event", leaf).with_size(9)])
            .await
            .expect("new child");
        let new_root = tree
            .put_directory(vec![
                DirEntry::from_cid("subtree", &new_child).with_link_type(LinkType::Dir)
            ])
            .await
            .expect("new root");
        store
            .router()
            .delete_local_only(&old_child.hash)
            .expect("remove old subtree");

        let error =
            super::collect_incremental_cids(&store, new_root.clone(), old_root.clone(), None, true)
                .await
                .expect_err("explicit deltas must not silently expand to a full traversal");
        assert!(error.to_string().contains("previous DAG is unavailable"));
        let fallback = collect_incremental_cids_for_push(&store, new_root, old_root, None)
            .await
            .expect("existing background behavior remains available");
        assert_eq!(fallback.len(), 3);
    }

    #[tokio::test]
    async fn incremental_push_collects_only_changed_named_subtrees() {
        let tmp = tempdir().expect("tempdir");
        let store = HashtreeStore::with_options(tmp.path(), None, 32 * 1024 * 1024).expect("store");
        let tree = HashTree::new(HashTreeConfig::new(store.store_arc()).public());

        let stable_file = tree.put_blob(b"stable").await.expect("stable file");
        let old_changed_file = tree.put_blob(b"old").await.expect("old file");
        let old_subdir = tree
            .put_directory(vec![
                DirEntry::new("changed.txt", old_changed_file).with_size(3),
                DirEntry::new("stable.txt", stable_file).with_size(6),
            ])
            .await
            .expect("old subdir");
        let old_root = tree
            .put_directory(vec![
                DirEntry::new("subdir", old_subdir.hash).with_link_type(LinkType::Dir),
                DirEntry::new("stable-root.txt", stable_file).with_size(6),
            ])
            .await
            .expect("old root");

        let new_changed_file = tree.put_blob(b"new").await.expect("new file");
        let new_subdir = tree
            .put_directory(vec![
                DirEntry::new("stable.txt", stable_file).with_size(6),
                DirEntry::new("changed.txt", new_changed_file).with_size(3),
            ])
            .await
            .expect("new subdir");
        let new_root = tree
            .put_directory(vec![
                DirEntry::new("stable-root.txt", stable_file).with_size(6),
                DirEntry::new("subdir", new_subdir.hash).with_link_type(LinkType::Dir),
            ])
            .await
            .expect("new root");

        let cids = collect_incremental_cids_for_push(&store, new_root.clone(), old_root, None)
            .await
            .expect("incremental cids");
        let hashes = cids.iter().map(|cid| cid.hash).collect::<Vec<_>>();

        assert_eq!(hashes.len(), 3);
        assert!(hashes.contains(&new_root.hash));
        assert!(hashes.contains(&new_subdir.hash));
        assert!(hashes.contains(&new_changed_file));
        assert!(!hashes.contains(&stable_file));
    }
}