scionic-merkletree 2.5.6

Scionic Merkle trees: a folder of files under one root, with identical chunks, files and folders stored once, and any part of it shown and checked without the rest
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
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
//! Several threads build, take in and check what one thread builds, takes in
//! and checks.

mod support;

use scionic_merkletree::{
    build, export, fetch, import, read_file, read_range, record_of, refused_frame, verify_with,
    Builder, ChunkRef, Chunking, Error, ExtraData, Hash, Intake, MemoryStore, Refusal, Request,
    Responder, Result, Root, Store, Stored, VerifyOptions, BUNDLE_MAGIC, FRAME_CHUNK,
};
use std::collections::HashSet;
use std::io::{self, Read};
use std::panic::AssertUnwindSafe;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use std::thread::ThreadId;
use std::time::{Duration, Instant};
use support::{
    bytes, contents, sample_folder, small_chunks, small_options, write, TempDir, TestPeer,
};

#[test]
fn one_file_is_the_same_whatever_number_of_threads_keep_its_chunks() {
    // 6,250 chunks of 16 bytes: more than a builder hands on at a time.
    let data = bytes("threads-one-file", 100_000);
    let mut roots = Vec::new();
    let mut stores = Vec::new();
    for threads in [1, 2, 8, 0] {
        let store = MemoryStore::new();
        let mut builder = Builder::new(&store, small_chunks()).threads(threads);
        let streamed = builder.file(&mut data.as_slice()).unwrap();
        let in_place = builder.file_from_bytes(&data).unwrap();
        assert_eq!(streamed, in_place, "{threads} threads");
        roots.push(builder.finish(streamed, ExtraData::new()).unwrap());
        stores.push(contents(&store));
    }
    assert!(roots.windows(2).all(|pair| pair[0] == pair[1]));
    assert!(stores.windows(2).all(|pair| pair[0] == pair[1]));
}

#[test]
fn a_file_chunked_by_content_is_the_same_with_help() {
    let data = bytes("threads-by-content", 9 << 20);
    let mut roots = Vec::new();
    for threads in [1, 4] {
        let store = MemoryStore::new();
        let mut builder = Builder::new(&store, Chunking::FastCdcV1).threads(threads);
        let streamed = builder.file(&mut data.as_slice()).unwrap();
        assert_eq!(streamed, builder.file_from_bytes(&data).unwrap());
        assert!(store.count(Stored::Chunk) > 2, "several chunks");
        let root = builder.finish(streamed, ExtraData::new()).unwrap();
        for workers in [1, 4] {
            let options = VerifyOptions::new().workers(workers).rechunk(true);
            verify_with(&store, &root, options).unwrap();
        }
        roots.push(root);
    }
    assert_eq!(roots[0], roots[1]);
}

#[test]
fn a_folder_is_the_same_when_threads_outnumber_its_files() {
    let temp = TempDir::new("threads-folder").unwrap();
    let folder = sample_folder(temp.path(), "tree").unwrap();
    let mut roots = Vec::new();
    let mut stores = Vec::new();
    for workers in [1, 2, 64] {
        let store = MemoryStore::new();
        let root = build(&store, &folder, &small_options().workers(workers)).unwrap();
        verify_with(&store, &root, VerifyOptions::new().workers(workers)).unwrap();
        roots.push(root);
        stores.push(contents(&store));
    }
    assert!(roots.windows(2).all(|pair| pair[0] == pair[1]));
    assert!(stores.windows(2).all(|pair| pair[0] == pair[1]));
}

/// A store that will not keep one chunk.
struct Refusing {
    inner: MemoryStore,
    refused: Hash,
}

impl Store for Refusing {
    fn put(&self, kind: Stored, hash: &Hash, bytes: &[u8]) -> Result<()> {
        if kind == Stored::Chunk && *hash == self.refused {
            return Err(Error::Invariant {
                detail: "this store will not keep that chunk",
            });
        }
        self.inner.put(kind, hash, bytes)
    }

    fn get(&self, kind: Stored, hash: &Hash) -> Result<Option<Arc<[u8]>>> {
        self.inner.get(kind, hash)
    }

    fn remove(&self, kind: Stored, hash: &Hash) -> Result<()> {
        self.inner.remove(kind, hash)
    }
}

#[test]
fn a_chunk_that_cannot_be_kept_stops_the_file_whoever_was_keeping_it() {
    let data = bytes("threads-refused", 100_000);
    let refused = ChunkRef::of(&data[64_000..64_016]).unwrap().hash;
    for threads in [1, 8] {
        let store = Refusing {
            inner: MemoryStore::new(),
            refused,
        };
        let mut builder = Builder::new(&store, small_chunks()).threads(threads);
        for attempt in [
            builder.file(&mut data.as_slice()),
            builder.file_from_bytes(&data),
        ] {
            assert!(
                matches!(attempt, Err(Error::Invariant { .. })),
                "{threads} threads: {attempt:?}"
            );
        }
        // A file whose chunks are not all kept has no list.
        assert_eq!(store.inner.count(Stored::FileList), 0);
    }
}

#[test]
fn the_failure_that_is_told_is_the_one_that_happened() {
    // A folder of many files, one of which holds a chunk the store refuses.
    // The threads that stop because of it have nothing of their own to tell.
    let temp = TempDir::new("threads-failure").unwrap();
    let folder = temp.path().join("tree");
    for number in 0..64 {
        let data = bytes(&format!("failure-{number}"), 64);
        write(folder.join(format!("file-{number:02}")), &data).unwrap();
    }
    let refused = ChunkRef::of(&bytes("failure-40", 64)[..16]).unwrap().hash;
    for workers in [1, 2, 8] {
        let store = Refusing {
            inner: MemoryStore::new(),
            refused,
        };
        match build(&store, &folder, &small_options().workers(workers)) {
            Err(Error::Invariant { detail }) => {
                assert_eq!(detail, "this store will not keep that chunk");
            }
            other => panic!("{workers} workers: {other:?}"),
        }
    }
}

/// A store that falls over whenever it is handed a chunk, as one would whose
/// own code has a fault.
struct Falling(MemoryStore);

impl Store for Falling {
    fn put(&self, kind: Stored, hash: &Hash, bytes: &[u8]) -> Result<()> {
        assert!(kind != Stored::Chunk, "this store falls over");
        self.0.put(kind, hash, bytes)
    }

    fn get(&self, kind: Stored, hash: &Hash) -> Result<Option<Arc<[u8]>>> {
        self.0.get(kind, hash)
    }

    fn remove(&self, kind: Stored, hash: &Hash) -> Result<()> {
        self.0.remove(kind, hash)
    }
}

#[test]
fn a_store_that_falls_over_does_not_leave_a_build_waiting() {
    // More files than the threads take at once: if every thread that chunks
    // is gone, the one that reads the folder must not wait for them.
    let temp = TempDir::new("threads-falling").unwrap();
    let folder = temp.path().join("tree");
    for number in 0..400 {
        let data = bytes(&format!("falling-{number}"), 16);
        write(
            folder.join(format!("dir-{}/file-{number:03}", number % 40)),
            &data,
        )
        .unwrap();
    }
    for workers in [1, 2, 8] {
        let folder = folder.clone();
        let (tell, hear) = std::sync::mpsc::channel();
        std::thread::spawn(move || {
            let store = Falling(MemoryStore::new());
            let options = small_options().workers(workers);
            let outcome = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
                build(&store, &folder, &options).map(|_| ())
            }));
            let _ = tell.send(outcome.is_err());
        });
        // The build ends, and it ends as the store did: by falling over.
        match hear.recv_timeout(std::time::Duration::from_secs(20)) {
            Ok(fell) => assert!(fell, "{workers} workers"),
            Err(_) => panic!("with {workers} workers the build was left waiting"),
        }
    }
}

/// A tree of one file chunked by content, long enough that its chunks are
/// taken in by several threads at once, and the frames of its bundle.
fn long_tree(seed: &str) -> (MemoryStore, Root, Vec<Vec<u8>>) {
    let store = MemoryStore::new();
    let mut builder = Builder::new(&store, Chunking::FastCdcV1);
    let top = builder.file_from_bytes(&bytes(seed, 12 << 20)).unwrap();
    let root = builder.finish(top, ExtraData::new()).unwrap();
    let mut bundle = Vec::new();
    export(&store, &root, &mut bundle).unwrap();
    (store, root, frames_of(&bundle))
}

/// The frames of a bundle.
fn frames_of(bundle: &[u8]) -> Vec<Vec<u8>> {
    let mut frames = Vec::new();
    let mut rest = &bundle[BUNDLE_MAGIC.len()..];
    while let Some((length, after)) = rest.split_first_chunk::<4>() {
        let (frame, after) = after.split_at(u32::from_be_bytes(*length) as usize);
        frames.push(frame.to_vec());
        rest = after;
    }
    frames
}

/// A bundle that holds these frames.
fn bundle_of(frames: &[Vec<u8>]) -> Vec<u8> {
    let mut bundle = BUNDLE_MAGIC.to_vec();
    for frame in frames {
        bundle.extend_from_slice(&(frame.len() as u32).to_be_bytes());
        bundle.extend_from_slice(frame);
    }
    bundle
}

/// Where the chunks' frames are among these frames.
fn chunk_places(frames: &[Vec<u8>]) -> Vec<usize> {
    (0..frames.len())
        .filter(|&place| frames[place][0] == FRAME_CHUNK)
        .collect()
}

#[test]
fn chunks_taken_in_by_several_threads_are_taken_in_as_by_one() {
    let (source, root, frames) = long_tree("threads-intake");
    let places = chunk_places(&frames);
    assert!(places.len() > 3, "several chunks");
    let imported = MemoryStore::new();
    import(&imported, &root, &mut bundle_of(&frames).as_slice()).unwrap();
    assert_eq!(contents(&imported), contents(&source));
    let fetched = MemoryStore::new();
    fetch(&fetched, &root, &mut TestPeer::new(&source)).unwrap();
    assert_eq!(contents(&fetched), contents(&source));

    // A chunk that comes twice in a row is kept once.
    let mut twice = frames.clone();
    twice.insert(places[1] + 1, frames[places[1]].clone());
    let store = MemoryStore::new();
    import(&store, &root, &mut bundle_of(&twice).as_slice()).unwrap();
    assert_eq!(contents(&store), contents(&source));
}

#[test]
fn a_chunk_refused_among_others_is_what_is_told_and_nothing_false_is_kept() {
    let (source, root, frames) = long_tree("threads-forged");
    let places = chunk_places(&frames);
    // The third chunk's last byte is changed: no list names what it is now.
    let mut forged = frames.clone();
    let last = forged[places[2]].len() - 1;
    forged[places[2]][last] ^= 1;
    let store = MemoryStore::new();
    let result = import(&store, &root, &mut bundle_of(&forged).as_slice());
    assert!(matches!(result, Err(Error::NotWanted { .. })), "{result:?}");
    // What was kept is the tree's, and so are the chunks that came before.
    for (kind, hash, kept) in contents(&store) {
        let held = source.get(kind, &hash).unwrap();
        assert_eq!(held.as_deref(), Some(kept.as_slice()));
    }
    for &place in &places[..2] {
        let hash = Hash::of(&frames[place][1..]);
        assert!(store.has(Stored::Chunk, &hash).unwrap());
    }
    // The tree is made whole from what is held.
    import(&store, &root, &mut bundle_of(&frames).as_slice()).unwrap();
    assert_eq!(contents(&store), contents(&source));
}

/// A store that will not keep one chunk the first time it is handed it.
struct RefusingOnce {
    inner: MemoryStore,
    refused: Hash,
    asked: AtomicBool,
}

impl RefusingOnce {
    fn new(refused: Hash) -> Self {
        Self {
            inner: MemoryStore::new(),
            refused,
            asked: AtomicBool::new(false),
        }
    }
}

impl Store for RefusingOnce {
    fn put(&self, kind: Stored, hash: &Hash, bytes: &[u8]) -> Result<()> {
        if kind == Stored::Chunk
            && *hash == self.refused
            && !self.asked.swap(true, Ordering::Relaxed)
        {
            return Err(Error::Invariant {
                detail: "this store will not keep that chunk yet",
            });
        }
        self.inner.put(kind, hash, bytes)
    }

    fn get(&self, kind: Stored, hash: &Hash) -> Result<Option<Arc<[u8]>>> {
        self.inner.get(kind, hash)
    }

    fn remove(&self, kind: Stored, hash: &Hash) -> Result<()> {
        self.inner.remove(kind, hash)
    }
}

#[test]
fn a_chunk_that_a_store_cannot_keep_is_wanted_still() {
    let (source, root, frames) = long_tree("threads-refused-once");
    let places = chunk_places(&frames);
    let refused = Hash::of(&frames[places[1]][1..]);
    let not_yet = |result: &Result<_>| {
        matches!(
            result,
            Err(Error::Invariant {
                detail: "this store will not keep that chunk yet"
            })
        )
    };

    // One frame at a time: the chunk is refused, and taken when it comes
    // again.
    let store = RefusingOnce::new(refused);
    let mut intake = Intake::new(&store, root).unwrap();
    for frame in &frames[..places[1]] {
        intake.accept(frame).unwrap();
    }
    assert!(not_yet(&intake.accept(&frames[places[1]]).map(|_| ())));
    assert!(intake.accept(&frames[places[1]]).unwrap().kept);
    for frame in &frames[places[1] + 1..] {
        intake.accept(frame).unwrap();
    }
    intake.finish().unwrap();
    assert_eq!(contents(&store.inner), contents(&source));

    // Many at once, from a bundle and from a peer: the tree is made whole
    // from what is held.
    let store = RefusingOnce::new(refused);
    let bundle = bundle_of(&frames);
    assert!(not_yet(
        &import(&store, &root, &mut bundle.as_slice()).map(|_| ())
    ));
    assert!(!store.inner.has(Stored::Chunk, &refused).unwrap());
    import(&store, &root, &mut bundle.as_slice()).unwrap();
    assert_eq!(contents(&store.inner), contents(&source));
    let store = RefusingOnce::new(refused);
    let fetched = fetch(&store, &root, &mut TestPeer::new(&source));
    assert!(not_yet(&fetched.map(|_| ())));
    fetch(&store, &root, &mut TestPeer::new(&source)).unwrap();
    assert_eq!(contents(&store.inner), contents(&source));
}

#[test]
fn a_store_that_falls_over_while_it_keeps_a_chunk_leaves_the_intake_done() {
    let (_source, root, frames) = long_tree("threads-falling-intake");
    let places = chunk_places(&frames);
    let store = Falling(MemoryStore::new());
    let mut intake = Intake::new(&store, root).unwrap();
    for frame in &frames[..places[0]] {
        intake.accept(frame).unwrap();
    }
    let fell = std::panic::catch_unwind(AssertUnwindSafe(|| intake.accept(&frames[places[0]])));
    assert!(fell.is_err());
    // The intake never holds the chunk as kept: it is done.
    let again = intake.accept(&frames[places[0]]);
    assert!(matches!(again, Err(Error::IntakeFailed)), "{again:?}");
    assert!(matches!(intake.finish(), Err(Error::IntakeFailed)));

    // Many at once: the fall comes through to the caller.
    let bundle = bundle_of(&frames);
    let fell = std::panic::catch_unwind(AssertUnwindSafe(|| {
        import(&Falling(MemoryStore::new()), &root, &mut bundle.as_slice())
    }));
    assert!(fell.is_err());
}

/// A store that takes a while to look for a thing, as one on disk does, and
/// cannot tell whether it holds the things it is told of.
struct Slow {
    inner: MemoryStore,
    unsure: Vec<(Hash, &'static str)>,
}

impl Store for Slow {
    fn put(&self, kind: Stored, hash: &Hash, bytes: &[u8]) -> Result<()> {
        self.inner.put(kind, hash, bytes)
    }

    fn get(&self, kind: Stored, hash: &Hash) -> Result<Option<Arc<[u8]>>> {
        self.inner.get(kind, hash)
    }

    fn remove(&self, kind: Stored, hash: &Hash) -> Result<()> {
        self.inner.remove(kind, hash)
    }

    fn size(&self, kind: Stored, hash: &Hash) -> Result<Option<u64>> {
        let started = Instant::now();
        while started.elapsed() < Duration::from_micros(20) {
            std::hint::spin_loop();
        }
        match self.unsure.iter().find(|(unsure, _)| unsure == hash) {
            Some(&(_, detail)) => Err(Error::Invariant { detail }),
            None => self.inner.size(kind, hash),
        }
    }
}

/// A tree of one file of a thousand short chunks, enough that whether a
/// store holds each is asked on several threads at once, and its chunks.
fn many_chunks(seed: u64) -> (MemoryStore, Root, Vec<Vec<u8>>) {
    let data = long_bytes(seed, 16 * 1000);
    let store = MemoryStore::new();
    let mut builder = Builder::new(&store, Chunking::fixed(16).unwrap());
    let top = builder.file_from_bytes(&data).unwrap();
    let root = builder.finish(top, ExtraData::new()).unwrap();
    (store, root, data.chunks(16).map(<[u8]>::to_vec).collect())
}

/// A store that holds what `source` holds, but for its chunks.
fn lists_of(source: &MemoryStore) -> MemoryStore {
    let store = MemoryStore::new();
    for (kind, hash, bytes) in contents(source) {
        if kind != Stored::Chunk {
            store.put(kind, &hash, &bytes).unwrap();
        }
    }
    store
}

#[test]
fn what_a_store_holds_of_a_long_list_is_found_as_one_thread_finds_it() {
    let (source, root, chunks) = many_chunks(11);
    // The store holds the record, the list and two chunks in three, and one
    // of those it holds cut short: damaged.
    let store = Slow {
        inner: lists_of(&source),
        unsure: Vec::new(),
    };
    let mut lacking = 0;
    for (number, chunk) in chunks.iter().enumerate() {
        let hash = Hash::of(chunk);
        if number % 3 == 0 {
            lacking += 1;
        } else if number == 500 {
            store.inner.put(Stored::Chunk, &hash, &chunk[1..]).unwrap();
        } else {
            store.inner.put(Stored::Chunk, &hash, chunk).unwrap();
        }
    }
    let intake = Intake::new(&store, root).unwrap();
    assert_eq!(intake.outstanding(), lacking + 1);
    // What it wants is what it lacks: once that is fetched, the tree is whole.
    fetch(&store, &root, &mut TestPeer::new(&source)).unwrap();
    assert_eq!(contents(&store.inner), contents(&source));
}

#[test]
fn the_failure_told_of_a_long_list_is_that_of_its_first_chunk_that_fails() {
    let (source, root, chunks) = many_chunks(13);
    let store = Slow {
        inner: lists_of(&source),
        unsure: vec![
            (Hash::of(&chunks[900]), "the second chunk it cannot tell of"),
            (Hash::of(&chunks[100]), "the first chunk it cannot tell of"),
        ],
    };
    let result = Intake::new(&store, root).map(|_| ());
    assert!(
        matches!(
            result,
            Err(Error::Invariant {
                detail: "the first chunk it cannot tell of"
            })
        ),
        "{result:?}"
    );
}

/// A store that notes every thread that asks it anything.
#[derive(Default)]
struct Watched {
    inner: MemoryStore,
    threads: Mutex<HashSet<ThreadId>>,
}

impl Watched {
    fn note(&self) {
        let thread = std::thread::current().id();
        self.threads.lock().unwrap().insert(thread);
    }
}

impl Store for Watched {
    fn put(&self, kind: Stored, hash: &Hash, bytes: &[u8]) -> Result<()> {
        self.note();
        self.inner.put(kind, hash, bytes)
    }

    fn get(&self, kind: Stored, hash: &Hash) -> Result<Option<Arc<[u8]>>> {
        self.note();
        self.inner.get(kind, hash)
    }

    fn remove(&self, kind: Stored, hash: &Hash) -> Result<()> {
        self.note();
        self.inner.remove(kind, hash)
    }

    fn size(&self, kind: Stored, hash: &Hash) -> Result<Option<u64>> {
        self.note();
        self.inner.size(kind, hash)
    }
}

#[test]
fn a_build_and_a_check_asked_for_one_thread_use_one_thread() {
    // A long list, whose chunks would be looked for on several threads.
    let data = long_bytes(19, 16 * 1000);
    let store = Watched::default();
    let mut builder = Builder::new(&store, Chunking::fixed(16).unwrap()).threads(1);
    let top = builder.file_from_bytes(&data).unwrap();
    let root = builder.finish(top, ExtraData::new()).unwrap();
    verify_with(&store, &root, VerifyOptions::new().workers(1)).unwrap();
    let here = std::thread::current().id();
    assert_eq!(*store.threads.lock().unwrap(), HashSet::from([here]));
}

/// A store that holds what `source` holds, with the chunk `hash` damaged:
/// its first byte changed.
fn damaged_copy(source: &MemoryStore, hash: &Hash) -> MemoryStore {
    let store = MemoryStore::new();
    for (kind, held, mut bytes) in contents(source) {
        if kind == Stored::Chunk && held == *hash {
            bytes[0] ^= 1;
        }
        store.put(kind, &held, &bytes).unwrap();
    }
    store
}

#[test]
fn a_bundle_ends_at_the_first_chunk_that_is_damaged_or_missing() {
    let (source, root, frames) = long_tree("threads-export");
    let places = chunk_places(&frames);
    // The chunks are read and checked ahead, several at once, and written
    // in order: what comes out is the bundle up to the bad chunk.
    for bad in [places[0], places[2], places[places.len() - 1]] {
        let hash = Hash::of(&frames[bad][1..]);
        let before = bundle_of(&frames[..bad]);

        let damaged = damaged_copy(&source, &hash);
        let mut out = Vec::new();
        let result = export(&damaged, &root, &mut out);
        assert!(matches!(result, Err(Error::Corrupt { .. })), "{result:?}");
        assert!(out == before, "damaged chunk at frame {bad}");

        let missing = damaged_copy(&source, &hash);
        missing.remove(Stored::Chunk, &hash).unwrap();
        let mut out = Vec::new();
        let result = export(&missing, &root, &mut out);
        assert!(matches!(result, Err(Error::Missing { .. })), "{result:?}");
        assert!(out == before, "missing chunk at frame {bad}");
    }
}

#[test]
fn a_long_file_is_read_back_whole_and_stops_at_a_damaged_chunk() {
    let (source, root, frames) = long_tree("threads-read");
    let places = chunk_places(&frames);
    let data: Vec<u8> = places
        .iter()
        .flat_map(|&place| frames[place][1..].iter().copied())
        .collect();
    let file = record_of(&source, &root).unwrap().top();
    let mut out = Vec::new();
    read_file(&source, &file, &mut out).unwrap();
    assert!(out == data);
    // A stretch over several chunks.
    let (offset, length) = (1_000_003, 7 << 20);
    let mut out = Vec::new();
    read_range(&source, &file, offset, length, &mut out).unwrap();
    assert!(out[..] == data[offset as usize..(offset + length) as usize]);

    // Read and checked ahead, written in order: the bytes before the damaged
    // chunk come out, and nothing from it on.
    let damaged = damaged_copy(&source, &Hash::of(&frames[places[2]][1..]));
    let mut out = Vec::new();
    let result = read_file(&damaged, &file, &mut out);
    assert!(matches!(result, Err(Error::Corrupt { .. })), "{result:?}");
    let before: usize = places[..2]
        .iter()
        .map(|&place| frames[place].len() - 1)
        .sum();
    assert!(out[..] == data[..before]);
}

/// Bytes that look random, made quickly: a long file for a test.
fn long_bytes(seed: u64, length: usize) -> Vec<u8> {
    let mut state = seed | 1;
    let mut out = Vec::with_capacity(length + 8);
    while out.len() < length {
        state ^= state << 13;
        state ^= state >> 7;
        state ^= state << 17;
        out.extend_from_slice(&state.to_le_bytes());
    }
    out.truncate(length);
    out
}

/// A reader that gives at most `step` bytes at a time, and fails once it has
/// given `fails_at`.
struct Stutter<'a> {
    data: &'a [u8],
    at: usize,
    step: usize,
    fails_at: usize,
}

impl<'a> Stutter<'a> {
    fn new(data: &'a [u8], step: usize) -> Self {
        Self {
            data,
            at: 0,
            step,
            fails_at: usize::MAX,
        }
    }
}

impl Read for Stutter<'_> {
    fn read(&mut self, buffer: &mut [u8]) -> io::Result<usize> {
        if self.at >= self.fails_at {
            return Err(io::Error::other("the disk went away"));
        }
        let count = buffer.len().min(self.step).min(self.data.len() - self.at);
        buffer[..count].copy_from_slice(&self.data[self.at..self.at + count]);
        self.at += count;
        Ok(count)
    }
}

#[test]
fn a_long_file_that_is_read_is_the_file_held_in_memory() {
    // Longer than a window of a builder that works alone, and than the two
    // windows of one that has help.
    let data = long_bytes(7, 72 << 20);
    for chunking in [Chunking::FastCdcV1, Chunking::fixed(3 << 20).unwrap()] {
        let mut first: Option<(scionic_merkletree::Target, _)> = None;
        for threads in [1, 3] {
            let store = MemoryStore::new();
            let mut builder = Builder::new(&store, chunking).threads(threads);
            let in_memory = builder.file_from_bytes(&data).unwrap();
            // Reads that end anywhere, and a window filled in many reads.
            for step in [(1 << 20) + 17, 5_000_011] {
                let read = builder.file(&mut Stutter::new(&data, step)).unwrap();
                assert_eq!(
                    read, in_memory,
                    "{chunking:?}, {threads} threads, step {step}"
                );
            }
            let kept = contents(&store);
            match &first {
                None => first = Some((in_memory, kept)),
                Some((target, held)) => {
                    assert_eq!(&in_memory, target, "{chunking:?}, {threads} threads");
                    assert!(&kept == held, "{chunking:?}, {threads} threads");
                }
            }
        }
    }
}

#[test]
fn a_long_file_that_cannot_be_read_or_kept_whole_has_no_list() {
    let data = long_bytes(11, 48 << 20);
    for threads in [1, 3] {
        // The reader fails in the second window, while the first is kept.
        let store = MemoryStore::new();
        let mut builder = Builder::new(&store, Chunking::FastCdcV1).threads(threads);
        let mut reader = Stutter::new(&data, 1 << 20);
        reader.fails_at = 40 << 20;
        let result = builder.file(&mut reader);
        assert!(matches!(result, Err(Error::Stream { .. })), "{result:?}");
        assert_eq!(store.count(Stored::FileList), 0);

        // The store will not keep a chunk of the first window.
        let chunks = Chunking::FastCdcV1.cut(&data);
        let refused = ChunkRef::of(chunks[2]).unwrap().hash;
        let store = Refusing {
            inner: MemoryStore::new(),
            refused,
        };
        let mut builder = Builder::new(&store, Chunking::FastCdcV1).threads(threads);
        let result = builder.file(&mut Stutter::new(&data, 1 << 20));
        assert!(matches!(result, Err(Error::Invariant { .. })), "{result:?}");
        assert_eq!(store.inner.count(Stored::FileList), 0);
    }
}

/// A tree of one file of many short chunks, and the frames of its bundle.
/// Its chunks are taken in in several batches, each while the next comes.
fn many_chunks_tree() -> (MemoryStore, Root, Vec<Vec<u8>>) {
    // 4,100 chunks of 64 bytes: four batches of 1,024, and a few more.
    let store = MemoryStore::new();
    let mut builder = Builder::new(&store, Chunking::fixed(64).unwrap());
    let top = builder.file_from_bytes(&long_bytes(13, 64 * 4100)).unwrap();
    let root = builder.finish(top, ExtraData::new()).unwrap();
    let mut bundle = Vec::new();
    export(&store, &root, &mut bundle).unwrap();
    (store, root, frames_of(&bundle))
}

/// Changes the last byte of a chunk's frame: no list names what it is then.
fn damage(frame: &mut [u8]) {
    let last = frame.len() - 1;
    frame[last] ^= 1;
}

/// A peer that answers from `store`, with the chunk `bad` damaged, and with
/// a frame longer than was asked for after the chunk that starts the
/// request for chunks from `overlong_at` on, when that is given.
fn bad_peer(store: &MemoryStore, bad: Option<Hash>, overlong_at: Option<u64>) -> TestPeer<'_> {
    TestPeer::tampering(store, move |request, mut answer| {
        for frame in &mut answer {
            if frame[0] == FRAME_CHUNK && Some(Hash::of(&frame[1..])) == bad {
                damage(frame);
            }
        }
        if let Ok(Request::Chunks { start, .. }) = Request::decode(request) {
            if Some(start) == overlong_at {
                answer.insert(1, vec![FRAME_CHUNK; 4096]);
            }
        }
        answer
    })
}

#[test]
fn chunks_taken_in_while_more_come_are_taken_in_as_by_one() {
    let (source, root, frames) = many_chunks_tree();
    assert!(chunk_places(&frames).len() > 4 * 1024, "several batches");
    let imported = MemoryStore::new();
    import(&imported, &root, &mut bundle_of(&frames).as_slice()).unwrap();
    assert!(contents(&imported) == contents(&source));
    let fetched = MemoryStore::new();
    fetch(&fetched, &root, &mut TestPeer::new(&source)).unwrap();
    assert!(contents(&fetched) == contents(&source));
}

#[test]
fn a_bad_chunk_taken_in_while_more_come_is_what_is_told() {
    let (source, root, frames) = many_chunks_tree();
    let places = chunk_places(&frames);
    // In the first batch, which is taken in while the second comes; in the
    // second; and the last chunk, in the last batch.
    for bad in [100, 2_000, places.len() - 1] {
        let hash = Hash::of(&frames[places[bad]][1..]);
        let mut forged = frames.clone();
        damage(&mut forged[places[bad]]);
        let imported = MemoryStore::new();
        let result = import(&imported, &root, &mut bundle_of(&forged).as_slice());
        assert!(
            matches!(result, Err(Error::NotWanted { .. })),
            "chunk {bad}: {result:?}"
        );
        let fetched = MemoryStore::new();
        let result = fetch(&fetched, &root, &mut bad_peer(&source, Some(hash), None));
        assert!(
            matches!(result, Err(Error::NotWanted { .. })),
            "chunk {bad}: {result:?}"
        );
        for store in [&imported, &fetched] {
            // What was kept is the tree's, and so are the chunks before the
            // bad one.
            for (kind, hash, kept) in contents(store) {
                assert!(source.get(kind, &hash).unwrap().as_deref() == Some(kept.as_slice()));
            }
            for &place in &places[..bad] {
                let hash = Hash::of(&frames[place][1..]);
                assert!(store.has(Stored::Chunk, &hash).unwrap(), "chunk {bad}");
            }
            assert!(!store.has(Stored::Chunk, &hash).unwrap());
        }
    }
}

#[test]
fn a_failure_after_a_bad_chunk_that_is_being_taken_in_is_not_what_is_told() {
    let (source, root, frames) = many_chunks_tree();
    let places = chunk_places(&frames);
    // Chunk 100 is in the first batch, which is taken in while the second
    // comes, and the failure comes in the second, at chunk 1,500.
    let bad = Hash::of(&frames[places[100]][1..]);
    let mut forged = frames.clone();
    damage(&mut forged[places[100]]);
    let fails_at = bundle_of(&frames[..places[1_500]]).len();
    for (frames, damaged) in [(&forged, true), (&frames, false)] {
        let bundle = bundle_of(frames);
        let mut reader = Stutter::new(&bundle, 1_000);
        reader.fails_at = fails_at;
        let result = import(&MemoryStore::new(), &root, &mut reader);
        if damaged {
            assert!(matches!(result, Err(Error::NotWanted { .. })), "{result:?}");
        } else {
            assert!(matches!(result, Err(Error::Stream { .. })), "{result:?}");
        }
    }
    // A peer that sends a frame longer than was asked for in the answer for
    // chunks 1,472 to 1,535.
    for (bad, damaged) in [(Some(bad), true), (None, false)] {
        let mut peer = bad_peer(&source, bad, Some(1_472));
        let result = fetch(&MemoryStore::new(), &root, &mut peer);
        if damaged {
            assert!(matches!(result, Err(Error::NotWanted { .. })), "{result:?}");
        } else {
            assert!(
                matches!(result, Err(Error::LimitExceeded { .. })),
                "{result:?}"
            );
        }
    }
}

#[test]
fn a_store_that_falls_over_while_more_chunks_come_does_not_leave_a_fetch_waiting() {
    let (source, root, frames) = many_chunks_tree();
    let bundle = bundle_of(&frames);
    let fell = std::panic::catch_unwind(AssertUnwindSafe(|| {
        import(&Falling(MemoryStore::new()), &root, &mut bundle.as_slice())
    }));
    assert!(fell.is_err());
    let fell = std::panic::catch_unwind(AssertUnwindSafe(|| {
        fetch(
            &Falling(MemoryStore::new()),
            &root,
            &mut TestPeer::new(&source),
        )
    }));
    assert!(fell.is_err());
}

/// The frames of a responder's answer to a request for chunks.
fn chunks_answer(responder: &Responder, file: &Hash, start: u64) -> Vec<Vec<u8>> {
    let request = Request::Chunks {
        file: *file,
        start,
        count: 256,
    }
    .encode();
    let mut frames = Vec::new();
    responder
        .answer(&request, &mut |frame| {
            frames.push(frame.to_vec());
            Ok(())
        })
        .unwrap();
    frames
}

#[test]
fn a_responder_that_reads_chunks_ahead_answers_as_one_that_does_not() {
    let (source, root, frames) = long_tree("threads-responder");
    let chunks: Vec<Vec<u8>> = chunk_places(&frames)
        .into_iter()
        .map(|place| frames[place].clone())
        .collect();
    assert!(chunks.len() > 3, "several chunks");
    let file = record_of(&source, &root).unwrap().top().hash;
    let not_held = refused_frame(Refusal::NotHeld);
    // The third chunk is not held: the answer is the two before it and a
    // refusal, and nothing after.
    let missing = damaged_copy(&source, &Hash::of(&chunks[2][1..]));
    missing
        .remove(Stored::Chunk, &Hash::of(&chunks[2][1..]))
        .unwrap();
    // The third chunk is held with another length than its list gives: it
    // cannot be the chunk, and the answer is the same.
    let third = &chunks[2][1..];
    let shortened = damaged_copy(&source, &Hash::of(third));
    shortened.remove(Stored::Chunk, &Hash::of(third)).unwrap();
    shortened
        .put(Stored::Chunk, &Hash::of(third), &third[..third.len() - 1])
        .unwrap();
    let mut cut_short = chunks[..2].to_vec();
    cut_short.push(not_held.clone());
    for threads in [1, 2, 0] {
        let responder = Responder::new(&source).threads(threads);
        assert!(
            chunks_answer(&responder, &file, 0) == chunks,
            "{threads} threads"
        );
        assert!(
            chunks_answer(&responder, &file, 1) == chunks[1..],
            "{threads} threads"
        );
        let responder = Responder::new(&missing).threads(threads);
        assert!(
            chunks_answer(&responder, &file, 0) == cut_short,
            "{threads} threads"
        );
        assert!(
            chunks_answer(&responder, &file, 2) == [not_held.clone()],
            "{threads} threads"
        );
        let responder = Responder::new(&shortened).threads(threads);
        assert!(
            chunks_answer(&responder, &file, 0) == cut_short,
            "{threads} threads"
        );
    }
    // Several threads asking one responder at once are each answered whole.
    let responder = Responder::new(&source);
    std::thread::scope(|scope| {
        for start in 0..4 {
            let (responder, chunks) = (&responder, &chunks);
            scope.spawn(move || {
                for _ in 0..3 {
                    let answer = chunks_answer(responder, &file, start);
                    assert!(answer == chunks[start as usize..], "from chunk {start}");
                }
            });
        }
    });
}

/// A store that reads things into the caller's memory itself, as a store on
/// disk does. It can hand out one chunk damaged, or say it lacks one that
/// it holds.
struct ReadingInto {
    inner: MemoryStore,
    damaged: Option<Hash>,
    lost: Option<Hash>,
    reads: std::sync::atomic::AtomicUsize,
}

impl ReadingInto {
    fn new(inner: MemoryStore) -> Self {
        Self {
            inner,
            damaged: None,
            lost: None,
            reads: std::sync::atomic::AtomicUsize::new(0),
        }
    }
}

impl Store for ReadingInto {
    fn put(&self, kind: Stored, hash: &Hash, bytes: &[u8]) -> Result<()> {
        self.inner.put(kind, hash, bytes)
    }
    fn get(&self, kind: Stored, hash: &Hash) -> Result<Option<Arc<[u8]>>> {
        self.inner.get(kind, hash)
    }
    fn read_into(&self, kind: Stored, hash: &Hash, into: &mut [u8]) -> Result<Option<u64>> {
        self.reads.fetch_add(1, Ordering::Relaxed);
        if Some(*hash) == self.lost {
            return Ok(None);
        }
        let Some(bytes) = self.inner.get(kind, hash)? else {
            return Ok(None);
        };
        if bytes.len() == into.len() {
            into.copy_from_slice(&bytes);
            if Some(*hash) == self.damaged {
                into[0] ^= 1;
            }
        }
        Ok(Some(bytes.len() as u64))
    }
    fn remove(&self, kind: Stored, hash: &Hash) -> Result<()> {
        self.inner.remove(kind, hash)
    }
}

#[test]
fn what_a_store_reads_into_memory_of_its_own_is_checked_as_what_it_lends() {
    // By default, a thing is read into memory as long as it is, and only
    // its length is told for memory of another length.
    let store = MemoryStore::new();
    store
        .put(Stored::Chunk, &Hash::of(b"held"), b"held")
        .unwrap();
    let mut into = *b"....";
    let length = store.read_into(Stored::Chunk, &Hash::of(b"held"), &mut into);
    assert_eq!(length.unwrap(), Some(4));
    assert_eq!(&into, b"held");
    let mut longer = *b".....";
    let length = store.read_into(Stored::Chunk, &Hash::of(b"held"), &mut longer);
    assert_eq!(length.unwrap(), Some(4));
    assert_eq!(&longer, b".....");
    let length = store.read_into(Stored::Chunk, &Hash::of(b"not held"), &mut into);
    assert_eq!(length.unwrap(), None);

    let (source, root, frames) = long_tree("threads-read-into");
    let places = chunk_places(&frames);
    let copy = || {
        let store = MemoryStore::new();
        for (kind, hash, bytes) in contents(&source) {
            store.put(kind, &hash, &bytes).unwrap();
        }
        ReadingInto::new(store)
    };
    let file = record_of(&source, &root).unwrap().top();
    let whole = bundle_of(&frames);

    // A sound store is read through its own way, and gives what the store in
    // memory gives.
    let reading = copy();
    let mut out = Vec::new();
    export(&reading, &root, &mut out).unwrap();
    assert!(out == whole);
    assert!(reading.reads.load(Ordering::Relaxed) >= places.len());
    let fetched = MemoryStore::new();
    fetch(&fetched, &root, &mut Responder::new(&reading)).unwrap();
    assert!(contents(&fetched) == contents(&source));

    // A chunk it reads damaged is found, whoever reads it.
    let bad = Hash::of(&frames[places[2]][1..]);
    let mut damaged = copy();
    damaged.damaged = Some(bad);
    let mut out = Vec::new();
    let result = export(&damaged, &root, &mut out);
    assert!(matches!(result, Err(Error::Corrupt { .. })), "{result:?}");
    assert!(out == bundle_of(&frames[..places[2]]));
    let result = read_file(&damaged, &file, &mut Vec::new());
    assert!(matches!(result, Err(Error::Corrupt { .. })), "{result:?}");
    let result = fetch(&MemoryStore::new(), &root, &mut Responder::new(&damaged));
    assert!(matches!(result, Err(Error::NotWanted { .. })), "{result:?}");

    // A chunk it cannot find is missing.
    let mut lost = copy();
    lost.lost = Some(bad);
    let result = export(&lost, &root, &mut Vec::new());
    assert!(matches!(result, Err(Error::Missing { .. })), "{result:?}");
    let result = read_file(&lost, &file, &mut Vec::new());
    assert!(matches!(result, Err(Error::Missing { .. })), "{result:?}");

    // A chunk it holds with another length than its list gives is not read
    // into the memory laid out for it, by it or by default. Read to be
    // checked, it is damaged; to be sent, it is not held. A file that is
    // read out has the lengths of its chunks looked at before any of it is
    // written.
    let shortened = copy();
    let chunk = &frames[places[2]][1..];
    shortened.inner.remove(Stored::Chunk, &bad).unwrap();
    shortened
        .inner
        .put(Stored::Chunk, &bad, &chunk[..chunk.len() - 1])
        .unwrap();
    for store in [&shortened as &dyn Store, &shortened.inner] {
        let mut out = Vec::new();
        let result = export(store, &root, &mut out);
        assert!(matches!(result, Err(Error::Corrupt { .. })), "{result:?}");
        assert!(out == bundle_of(&frames[..places[2]]));
        let mut out = Vec::new();
        let result = read_file(store, &file, &mut out);
        assert!(
            matches!(result, Err(Error::SizeMismatch { .. })),
            "{result:?}"
        );
        assert!(out.is_empty());
        let result = fetch(&MemoryStore::new(), &root, &mut Responder::new(store));
        assert!(matches!(result, Err(Error::Peer { .. })), "{result:?}");
    }
}