heddle-object-model 0.24.3

Heddle's content-addressed object model and stable codecs.
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
//! Immutable source-target bindings. Forks share a root; updates copy one trie
//! route, never the map or its referencing annotations. Addresses are ordinary
//! blob hashes. A failed update may leave unreachable immutable nodes, but
//! never returns a replacement root for the caller to install.
use super::ContentHash;

const MAGIC: &[u8; 5] = b"HDTM\x01";
const LEAF_ENTRIES: usize = 8;
const LEVELS: usize = 52;
/// Largest canonical node: header, depth, bitmap and 32 (hash, count) children.
pub const MAX_NODE_BYTES: usize = 5 + 1 + 1 + 4 + 32 * 40;

/// Storage must bound a read before allocating/fetching more than `max_bytes`.
/// Writes install immutable bytes at their ordinary blob address.
pub trait SourceTargetMapStore {
    type Error;
    fn read(&mut self, hash: ContentHash, max_bytes: usize)
    -> Result<Option<Vec<u8>>, Self::Error>;
    fn write(&mut self, hash: ContentHash, bytes: Vec<u8>) -> Result<(), Self::Error>;
}

/// Remaining work allowance. Reads/writes are charged before the store call;
/// read bytes are charged from the actual returned node, not its maximum size.
#[derive(Clone, Copy, Debug)]
pub struct MapBudget {
    pub node_reads: usize,
    pub read_bytes: usize,
    pub node_writes: usize,
    pub write_bytes: usize,
}
impl MapBudget {
    pub const fn new(
        node_reads: usize,
        read_bytes: usize,
        node_writes: usize,
        write_bytes: usize,
    ) -> Self {
        Self {
            node_reads,
            read_bytes,
            node_writes,
            write_bytes,
        }
    }
}

#[derive(Debug, thiserror::Error)]
pub enum MapError<E> {
    #[error("source target map storage error")]
    Storage(E),
    #[error("source target map read budget exhausted")]
    ReadBudget,
    #[error("source target map write budget exhausted")]
    WriteBudget,
    #[error("source target map node is missing: {0}")]
    MissingNode(ContentHash),
    #[error("source target map node does not match its blob address: {0}")]
    HashMismatch(ContentHash),
    #[error("invalid source target map node: {0}")]
    InvalidNode(&'static str),
}

pub struct SourceTargetMap;
impl SourceTargetMap {
    /// Bounded canonical enumeration for transfer verification and rebuilding
    /// indexes. Capture updates still use individual trie routes.
    pub fn entries<S: SourceTargetMapStore>(
        store: &mut S,
        root: Option<ContentHash>,
        limit: usize,
        budget: &mut MapBudget,
    ) -> Result<Vec<(ContentHash, ContentHash)>, MapError<S::Error>> {
        let mut pending = root
            .map(|root| vec![(root, None, Vec::new())])
            .unwrap_or_default();
        let mut result = Vec::new();
        while let Some((hash, count, prefix)) = pending.pop() {
            match load(store, hash, count, &prefix, budget)? {
                Node::Leaf(entries) => {
                    if result.len().saturating_add(entries.len()) > limit {
                        return Err(MapError::ReadBudget);
                    }
                    result.extend(entries);
                }
                Node::Branch { children, .. } => {
                    for child in children.into_iter().rev() {
                        let mut next = prefix.clone();
                        next.push(child.slot);
                        pending.push((child.link.hash, Some(child.link.count), next));
                    }
                }
            }
        }
        result.sort_unstable_by_key(|entry| entry.0);
        Ok(result)
    }
    pub fn get<S: SourceTargetMapStore>(
        store: &mut S,
        root: Option<ContentHash>,
        key: ContentHash,
        budget: &mut MapBudget,
    ) -> Result<Option<ContentHash>, MapError<S::Error>> {
        let Some(mut hash) = root else {
            return Ok(None);
        };
        let mut expected_count = None;
        let mut prefix = Vec::new();
        loop {
            match load(store, hash, expected_count, &prefix, budget)? {
                Node::Leaf(entries) => {
                    return Ok(entries
                        .binary_search_by_key(&key, |entry| entry.0)
                        .ok()
                        .map(|index| entries[index].1));
                }
                Node::Branch { children, .. } => {
                    let slot = group(key, prefix.len());
                    let Ok(index) = children.binary_search_by_key(&slot, |child| child.slot) else {
                        return Ok(None);
                    };
                    hash = children[index].link.hash;
                    expected_count = Some(children[index].link.count);
                    prefix.push(slot);
                }
            }
        }
    }

    /// `None` deletes the key. Empty maps use a `None` root and store no node.
    /// Equal values and absent deletions write nothing. The returned root is the
    /// only publication point; keep the prior root when this returns an error.
    pub fn update<S: SourceTargetMapStore>(
        store: &mut S,
        root: Option<ContentHash>,
        key: ContentHash,
        value: Option<ContentHash>,
        budget: &mut MapBudget,
    ) -> Result<Option<ContentHash>, MapError<S::Error>> {
        Ok(update_at(
            store,
            root.map(|hash| (hash, None)),
            key,
            value,
            &mut Vec::new(),
            budget,
        )?
        .map(|link| link.hash))
    }
}

#[derive(Clone, Copy, PartialEq, Eq)]
struct Link {
    hash: ContentHash,
    count: u64,
}
struct Child {
    slot: u8,
    link: Link,
}
enum Node {
    Leaf(Vec<(ContentHash, ContentHash)>),
    Branch { depth: u8, children: Vec<Child> },
}
impl Node {
    fn count(&self) -> Option<u64> {
        match self {
            Self::Leaf(entries) => Some(entries.len() as u64),
            Self::Branch { children, .. } => children
                .iter()
                .try_fold(0u64, |total, child| total.checked_add(child.link.count)),
        }
    }
    fn encode(&self) -> Vec<u8> {
        let mut bytes = Vec::new();
        bytes.extend_from_slice(MAGIC);
        match self {
            Self::Leaf(entries) => {
                bytes.extend_from_slice(&[0, entries.len() as u8]);
                for (key, value) in entries {
                    bytes.extend_from_slice(key.as_bytes());
                    bytes.extend_from_slice(value.as_bytes());
                }
            }
            Self::Branch { depth, children } => {
                bytes.extend_from_slice(&[1, *depth]);
                let bitmap = children
                    .iter()
                    .fold(0u32, |bits, child| bits | (1u32 << child.slot));
                bytes.extend_from_slice(&bitmap.to_le_bytes());
                for child in children {
                    bytes.extend_from_slice(child.link.hash.as_bytes());
                    bytes.extend_from_slice(&child.link.count.to_le_bytes());
                }
            }
        }
        bytes
    }
}

fn take<const N: usize>(bytes: &mut &[u8]) -> Result<[u8; N], &'static str> {
    let value = bytes
        .get(..N)
        .ok_or("truncated node")?
        .try_into()
        .map_err(|_| "invalid node field")?;
    *bytes = &bytes[N..];
    Ok(value)
}
fn decode(mut bytes: &[u8], prefix: &[u8]) -> Result<Node, &'static str> {
    if &take::<5>(&mut bytes)? != MAGIC {
        return Err("unsupported node format");
    }
    let kind = take::<1>(&mut bytes)?[0];
    let node = match kind {
        0 => {
            let count = usize::from(take::<1>(&mut bytes)?[0]);
            if !(1..=LEAF_ENTRIES).contains(&count) {
                return Err("invalid leaf size");
            }
            let mut entries = Vec::with_capacity(count);
            for _ in 0..count {
                let key = ContentHash::from_bytes(take::<32>(&mut bytes)?);
                let value = ContentHash::from_bytes(take::<32>(&mut bytes)?);
                if entries
                    .last()
                    .is_some_and(|entry: &(ContentHash, ContentHash)| entry.0 >= key)
                {
                    return Err("leaf keys are not strictly ordered");
                }
                if prefix
                    .iter()
                    .enumerate()
                    .any(|(depth, slot)| group(key, depth) != *slot)
                {
                    return Err("leaf key is outside its route");
                }
                entries.push((key, value));
            }
            Node::Leaf(entries)
        }
        1 => {
            let depth = take::<1>(&mut bytes)?[0];
            if usize::from(depth) != prefix.len() || prefix.len() >= LEVELS {
                return Err("invalid branch depth");
            }
            let bitmap = u32::from_le_bytes(take::<4>(&mut bytes)?);
            if bitmap == 0 {
                return Err("empty branch");
            }
            let mut children = Vec::with_capacity(bitmap.count_ones() as usize);
            for slot in 0..32 {
                if bitmap & (1 << slot) != 0 {
                    let hash = ContentHash::from_bytes(take::<32>(&mut bytes)?);
                    let count = u64::from_le_bytes(take::<8>(&mut bytes)?);
                    if count == 0 {
                        return Err("empty child");
                    }
                    children.push(Child {
                        slot,
                        link: Link { hash, count },
                    });
                }
            }
            let node = Node::Branch { depth, children };
            if node.count().ok_or("subtree count overflow")? <= LEAF_ENTRIES as u64 {
                return Err("branch must collapse to a leaf");
            }
            node
        }
        _ => return Err("unknown node kind"),
    };
    if !bytes.is_empty() {
        return Err("trailing node bytes");
    }
    Ok(node)
}

// Hash keys already provide a fixed 256-bit route. Like manifest routes, read
// five bits most-significant first; the final group has one real bit.
fn group(key: ContentHash, depth: usize) -> u8 {
    let mut slot = 0;
    for offset in 0..5 {
        let bit = depth * 5 + offset;
        slot = (slot << 1)
            | if bit < 256 {
                (key.as_bytes()[bit / 8] >> (7 - bit % 8)) & 1
            } else {
                0
            };
    }
    slot
}
fn load<S: SourceTargetMapStore>(
    store: &mut S,
    hash: ContentHash,
    expected_count: Option<u64>,
    prefix: &[u8],
    budget: &mut MapBudget,
) -> Result<Node, MapError<S::Error>> {
    if budget.node_reads == 0 || budget.read_bytes == 0 {
        return Err(MapError::ReadBudget);
    }
    budget.node_reads -= 1;
    let limit = budget.read_bytes.min(MAX_NODE_BYTES);
    let bytes = store
        .read(hash, limit)
        .map_err(MapError::Storage)?
        .ok_or(MapError::MissingNode(hash))?;
    if bytes.len() > limit {
        return Err(MapError::ReadBudget);
    }
    budget.read_bytes -= bytes.len();
    if ContentHash::compute_typed("blob", &bytes) != hash {
        return Err(MapError::HashMismatch(hash));
    }
    let node = decode(&bytes, prefix).map_err(MapError::InvalidNode)?;
    if expected_count.is_some_and(|count| node.count() != Some(count)) {
        return Err(MapError::InvalidNode("child count differs from parent"));
    }
    Ok(node)
}
fn persist<S: SourceTargetMapStore>(
    store: &mut S,
    node: Node,
    budget: &mut MapBudget,
) -> Result<Link, MapError<S::Error>> {
    let count = node
        .count()
        .ok_or(MapError::InvalidNode("subtree count overflow"))?;
    let bytes = node.encode();
    if budget.node_writes == 0 || budget.write_bytes < bytes.len() {
        return Err(MapError::WriteBudget);
    }
    budget.node_writes -= 1;
    budget.write_bytes -= bytes.len();
    let hash = ContentHash::compute_typed("blob", &bytes);
    store.write(hash, bytes).map_err(MapError::Storage)?;
    Ok(Link { hash, count })
}
fn build<S: SourceTargetMapStore>(
    store: &mut S,
    entries: Vec<(ContentHash, ContentHash)>,
    depth: usize,
    budget: &mut MapBudget,
) -> Result<Link, MapError<S::Error>> {
    if entries.len() <= LEAF_ENTRIES {
        return persist(store, Node::Leaf(entries), budget);
    }
    if depth >= LEVELS {
        return Err(MapError::InvalidNode("key route exhausted"));
    }
    let mut groups: [Vec<_>; 32] = std::array::from_fn(|_| Vec::new());
    for entry in entries {
        groups[usize::from(group(entry.0, depth))].push(entry);
    }
    let mut children = Vec::new();
    for (slot, entries) in groups.into_iter().enumerate() {
        if !entries.is_empty() {
            children.push(Child {
                slot: slot as u8,
                link: build(store, entries, depth + 1, budget)?,
            });
        }
    }
    persist(
        store,
        Node::Branch {
            depth: depth as u8,
            children,
        },
        budget,
    )
}
fn collect<S: SourceTargetMapStore>(
    store: &mut S,
    link: Link,
    prefix: &[u8],
    entries: &mut Vec<(ContentHash, ContentHash)>,
    budget: &mut MapBudget,
) -> Result<(), MapError<S::Error>> {
    if link.count > LEAF_ENTRIES as u64 {
        return Err(MapError::InvalidNode("collapse exceeds leaf bound"));
    }
    match load(store, link.hash, Some(link.count), prefix, budget)? {
        Node::Leaf(mut leaf) => {
            if entries.len() + leaf.len() > LEAF_ENTRIES {
                return Err(MapError::InvalidNode("collapse exceeds leaf bound"));
            }
            entries.append(&mut leaf);
        }
        Node::Branch { .. } => return Err(MapError::InvalidNode("small subtree is not canonical")),
    }
    Ok(())
}
fn update_at<S: SourceTargetMapStore>(
    store: &mut S,
    old: Option<(ContentHash, Option<u64>)>,
    key: ContentHash,
    value: Option<ContentHash>,
    prefix: &mut Vec<u8>,
    budget: &mut MapBudget,
) -> Result<Option<Link>, MapError<S::Error>> {
    let Some((hash, expected_count)) = old else {
        return value
            .map(|value| persist(store, Node::Leaf(vec![(key, value)]), budget))
            .transpose();
    };
    let node = load(store, hash, expected_count, prefix, budget)?;
    let original = Link {
        hash,
        count: node
            .count()
            .ok_or(MapError::InvalidNode("subtree count overflow"))?,
    };
    match node {
        Node::Leaf(mut entries) => {
            match (entries.binary_search_by_key(&key, |entry| entry.0), value) {
                (Ok(index), Some(value)) if entries[index].1 == value => return Ok(Some(original)),
                (Err(_), None) => return Ok(Some(original)),
                (Ok(index), Some(value)) => entries[index].1 = value,
                (Ok(index), None) => {
                    entries.remove(index);
                }
                (Err(index), Some(value)) => entries.insert(index, (key, value)),
            }
            if entries.is_empty() {
                return Ok(None);
            }
            Ok(Some(build(store, entries, prefix.len(), budget)?))
        }
        Node::Branch {
            depth,
            mut children,
        } => {
            let slot = group(key, prefix.len());
            let position = children.binary_search_by_key(&slot, |child| child.slot);
            let previous = position.ok().map(|index| children[index].link);
            prefix.push(slot);
            let result = update_at(
                store,
                previous.map(|link| (link.hash, Some(link.count))),
                key,
                value,
                prefix,
                budget,
            );
            prefix.pop();
            let next = result?;
            if previous == next {
                return Ok(Some(original));
            }
            match (position, next) {
                (Ok(index), Some(link)) => children[index].link = link,
                (Ok(index), None) => {
                    children.remove(index);
                }
                (Err(index), Some(link)) => children.insert(index, Child { slot, link }),
                (Err(_), None) => return Ok(Some(original)),
            }
            if children.is_empty() {
                return Ok(None);
            }
            let node = Node::Branch { depth, children };
            if node
                .count()
                .ok_or(MapError::InvalidNode("subtree count overflow"))?
                <= LEAF_ENTRIES as u64
            {
                let Node::Branch { children, .. } = node else {
                    return Err(MapError::InvalidNode("expected branch"));
                };
                let mut entries = Vec::new();
                for child in children {
                    prefix.push(child.slot);
                    let result = collect(store, child.link, prefix, &mut entries, budget);
                    prefix.pop();
                    result?;
                }
                entries.sort_unstable_by_key(|entry| entry.0);
                return Ok(Some(persist(store, Node::Leaf(entries), budget)?));
            }
            Ok(Some(persist(store, node, budget)?))
        }
    }
}

#[cfg(test)]
mod tests {
    use std::collections::BTreeMap;

    use super::*;

    #[derive(Debug, thiserror::Error)]
    #[error("{0}")]
    struct StoreError(&'static str);
    #[derive(Default)]
    struct MemoryStore {
        nodes: BTreeMap<ContentHash, Vec<u8>>,
        reads: usize,
        read_bytes: usize,
        writes: usize,
        write_bytes: usize,
    }
    impl MemoryStore {
        fn reset_counts(&mut self) {
            self.reads = 0;
            self.read_bytes = 0;
            self.writes = 0;
            self.write_bytes = 0;
        }
    }
    impl SourceTargetMapStore for MemoryStore {
        type Error = StoreError;
        fn read(
            &mut self,
            hash: ContentHash,
            max_bytes: usize,
        ) -> Result<Option<Vec<u8>>, Self::Error> {
            self.reads += 1;
            let Some(bytes) = self.nodes.get(&hash) else {
                return Ok(None);
            };
            if bytes.len() > max_bytes {
                return Err(StoreError("bounded read refused"));
            }
            self.read_bytes += bytes.len();
            Ok(Some(bytes.clone()))
        }
        fn write(&mut self, hash: ContentHash, bytes: Vec<u8>) -> Result<(), Self::Error> {
            assert_eq!(
                hash,
                ContentHash::compute_typed("blob", &bytes),
                "ordinary blob storage key"
            );
            assert!(bytes.len() <= MAX_NODE_BYTES, "bounded canonical node");
            self.writes += 1;
            self.write_bytes += bytes.len();
            if let Some(old) = self.nodes.insert(hash, bytes.clone()) {
                assert_eq!(old, bytes, "writes cannot replace immutable content");
            }
            Ok(())
        }
    }
    fn budget() -> MapBudget {
        MapBudget::new(1024, 2_000_000, 1024, 2_000_000)
    }
    fn key(value: u32) -> ContentHash {
        ContentHash::compute(&value.to_le_bytes())
    }
    fn put(
        store: &mut MemoryStore,
        root: Option<ContentHash>,
        key: ContentHash,
        value: Option<ContentHash>,
    ) -> Option<ContentHash> {
        SourceTargetMap::update(store, root, key, value, &mut budget()).expect("bounded map update")
    }
    fn get(
        store: &mut MemoryStore,
        root: Option<ContentHash>,
        key: ContentHash,
    ) -> Option<ContentHash> {
        SourceTargetMap::get(store, root, key, &mut budget()).expect("bounded map lookup")
    }

    #[test]
    fn one_and_ten_thousand_bindings_touch_only_one_bounded_route() {
        for size in [1, 10_000] {
            let mut store = MemoryStore::default();
            let mut root = None;
            for index in 0..size {
                root = put(&mut store, root, key(index), Some(key(index + 100_000)));
            }
            store.reset_counts();
            let before = budget();
            let mut work = before;
            let changed =
                SourceTargetMap::update(&mut store, root, key(0), Some(key(900_000)), &mut work)
                    .expect("one replacement");
            assert_ne!(changed, root);
            assert!(
                (1..=6).contains(&store.reads),
                "replacement must not scan {size} bindings: {} reads",
                store.reads
            );
            assert!(
                (1..=6).contains(&store.writes),
                "replacement must not rewrite {size} bindings: {} writes",
                store.writes
            );
            eprintln!(
                "{size} bindings: replacement {} reads/{} bytes, {} writes/{} bytes",
                store.reads, store.read_bytes, store.writes, store.write_bytes
            );
            assert_eq!(before.node_reads - work.node_reads, store.reads);
            assert_eq!(before.read_bytes - work.read_bytes, store.read_bytes);
            assert_eq!(before.node_writes - work.node_writes, store.writes);
            assert_eq!(before.write_bytes - work.write_bytes, store.write_bytes);
            store.reset_counts();
            assert_eq!(get(&mut store, changed, key(0)), Some(key(900_000)));
            assert!(
                (1..=6).contains(&store.reads),
                "lookup follows only key's route"
            );
            assert_eq!(store.writes, 0);
            assert_eq!(
                get(&mut store, root, key(0)),
                Some(key(100_000)),
                "parent root is unchanged"
            );
            if size > 1 {
                assert_eq!(
                    get(&mut store, changed, key(size - 1)),
                    Some(key(size - 1 + 100_000))
                );
            }
        }
    }

    #[test]
    fn unchanged_updates_and_absent_deletions_write_zero_nodes() {
        let mut store = MemoryStore::default();
        let mut root = None;
        for index in 0..100 {
            root = put(&mut store, root, key(index), Some(key(index + 100)));
        }
        store.reset_counts();
        let mut no_writes = MapBudget::new(64, 100_000, 0, 0);
        assert_eq!(
            SourceTargetMap::update(&mut store, root, key(37), Some(key(137)), &mut no_writes)
                .expect("equal value needs no writes"),
            root
        );
        assert_eq!(
            SourceTargetMap::update(&mut store, root, key(999), None, &mut no_writes)
                .expect("absent key needs no writes"),
            root
        );
        assert_eq!(store.writes, 0);
        assert_eq!(store.write_bytes, 0);
        store.reset_counts();
        assert_eq!(put(&mut store, None, key(999), None), None);
        assert_eq!((store.reads, store.writes), (0, 0));
    }

    #[test]
    fn forks_share_roots_and_deletions_collapse_to_canonical_nodes() {
        let mut store = MemoryStore::default();
        let mut parent = None;
        for index in 0..48 {
            parent = put(&mut store, parent, key(index), Some(key(index + 100)));
        }
        store.reset_counts();
        let mut fork = parent;
        assert_eq!(
            (store.reads, store.writes),
            (0, 0),
            "fork copies just the root"
        );
        for index in 0..41 {
            fork = put(&mut store, fork, key(index), None);
        }
        let mut rebuilt = None;
        for index in (41..48).rev() {
            rebuilt = put(&mut store, rebuilt, key(index), Some(key(index + 100)));
        }
        assert_eq!(
            fork, rebuilt,
            "collapsed map has a history-independent canonical root"
        );
        assert_eq!(get(&mut store, fork, key(0)), None);
        assert_eq!(get(&mut store, parent, key(0)), Some(key(100)));
        for index in 41..48 {
            fork = put(&mut store, fork, key(index), None);
        }
        assert_eq!(fork, None);
        assert_eq!(get(&mut store, parent, key(47)), Some(key(147)));
    }

    #[test]
    fn hash_integrity_missing_nodes_and_exact_routes_are_checked() {
        let mut store = MemoryStore::default();
        let root = put(&mut store, None, key(1), Some(key(2))).expect("root");
        // Still a valid canonical leaf for the same key; only its claimed blob
        // address is wrong. A decoder or routing check cannot rescue this test.
        let changed = Node::Leaf(vec![(key(1), key(3))]).encode();
        let original = store.nodes.insert(root, changed).expect("original bytes");
        assert!(
            matches!(SourceTargetMap::get(&mut store, Some(root), key(1), &mut budget()), Err(MapError::HashMismatch(hash)) if hash == root)
        );
        store.nodes.remove(&root);
        assert!(
            matches!(SourceTargetMap::get(&mut store, Some(root), key(1), &mut budget()), Err(MapError::MissingNode(hash)) if hash == root)
        );
        store.nodes.insert(root, original);
        assert_eq!(get(&mut store, Some(root), key(1)), Some(key(2)));

        let child = persist(
            &mut store,
            Node::Leaf(vec![(key(1), key(2))]),
            &mut budget(),
        )
        .expect("valid leaf");
        let other_slot = (group(key(1), 0) + 1) % 32;
        let branch = Node::Branch {
            depth: 0,
            children: vec![Child {
                slot: other_slot,
                link: Link { count: 9, ..child },
            }],
        };
        let branch =
            persist(&mut store, branch, &mut budget()).expect("store malformed routing fixture");
        let mut wrong_key = *key(1).as_bytes();
        wrong_key[0] = (other_slot << 3) | (wrong_key[0] & 7);
        assert!(matches!(
            SourceTargetMap::get(
                &mut store,
                Some(branch.hash),
                ContentHash::from_bytes(wrong_key),
                &mut budget()
            ),
            Err(MapError::InvalidNode("leaf key is outside its route"))
        ));
    }

    #[test]
    fn reads_and_updates_stop_at_budget_without_installing_a_partial_root() {
        let mut store = MemoryStore::default();
        let mut root = None;
        for index in 0..100 {
            root = put(&mut store, root, key(index), Some(key(index + 100)));
        }
        store.reset_counts();
        assert!(matches!(
            SourceTargetMap::get(
                &mut store,
                root,
                key(1),
                &mut MapBudget::new(0, 100_000, 0, 0)
            ),
            Err(MapError::ReadBudget)
        ));
        assert_eq!(store.reads, 0, "reject before physical store read");
        let mut bounded = MapBudget::new(64, 100_000, 1, 100_000);
        assert!(matches!(
            SourceTargetMap::update(&mut store, root, key(1), Some(key(900_000)), &mut bounded),
            Err(MapError::WriteBudget)
        ));
        assert_eq!(
            store.writes, 1,
            "only permitted unreachable immutable write occurred"
        );
        assert_eq!(
            get(&mut store, root, key(1)),
            Some(key(101)),
            "caller still has valid original root"
        );
        store.reset_counts();
        assert!(matches!(
            SourceTargetMap::get(&mut store, root, key(1), &mut MapBudget::new(64, 1, 0, 0)),
            Err(MapError::Storage(StoreError("bounded read refused")))
        ));
        assert_eq!(
            store.read_bytes, 0,
            "backend refuses oversized body before fetching it"
        );
        store.reset_counts();
        assert!(matches!(
            SourceTargetMap::update(
                &mut store,
                root,
                key(1),
                Some(key(900_000)),
                &mut MapBudget::new(64, 100_000, 64, 1)
            ),
            Err(MapError::WriteBudget)
        ));
        assert_eq!(
            store.writes, 0,
            "byte budget is checked before physical write"
        );
    }

    #[test]
    fn common_prefix_and_insertion_order_preserve_canonical_roots() {
        let mut store = MemoryStore::default();
        let mut forward = None;
        let mut reverse = None;
        let keys: Vec<_> = (0..16)
            .map(|n| {
                let mut bytes = [0; 32];
                bytes[31] = n;
                ContentHash::from_bytes(bytes)
            })
            .collect();
        for key in &keys {
            forward = put(&mut store, forward, *key, Some(*key));
        }
        for key in keys.iter().rev() {
            reverse = put(&mut store, reverse, *key, Some(*key));
        }
        assert_eq!(
            forward, reverse,
            "routing remains deterministic through a very long common prefix"
        );
        for key in &keys {
            assert_eq!(get(&mut store, forward, *key), Some(*key));
        }
        let mut remaining = forward;
        for key in &keys[..9] {
            remaining = put(&mut store, remaining, *key, None);
        }
        for key in &keys[9..] {
            assert_eq!(get(&mut store, remaining, *key), Some(*key));
        }
    }
}