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
use std::{fmt::Debug, marker::PhantomData};

use siphasher::sip128::SipHasher24;

use crate::{
    diff::PageRange,
    digest::{self, siphash::SipHasher, Hasher, RootHash, ValueDigest},
    node::Node,
    node_iter::NodeIter,
    page::{insert_intermediate_page, Page},
    visitor::{page_range_hash::PageRangeHashVisitor, Visitor},
    UpsertResult,
};

/// An alias for the default hash implementation.
type DefaultHasher = SipHasher;

/// An implementation of the Merkle Search Tree as described in [Merkle Search
/// Trees: Efficient State-Based CRDTs in Open Networks][paper].
///
/// This implementation stores only keys directly in the tree - hashes of values
/// are retained instead of the actual value. This allows greatest flexibility,
/// as the user can choose the most appropriate key/value storage data
/// structure, while using the MST strictly for anti-entropy / Merkle proofs.
///
/// # Merkle Search Trees
///
/// In addition to the normal hash & consistency properties of a regular
/// Merkle/hash tree, a MST is a searchable balanced B-tree with variable,
/// probabilistically bounded page sizes and a deterministic representation
/// irrespective of insert order - these properties make a MST a useful data
/// structure for efficient state-based CRDT replication and anti-entropy.
///
/// Keys are stored in sort order (from min to max) in an MST. If monotonic keys
/// are inserted, a minimal amount of hash re-computation needs to be performed
/// as the nodes & pages for most of the older keys remain unchanged; this
/// reduces the overhead of anti-entropy as fewer intermediate hashes need
/// recomputing and exchanging during reconciliation.
///
/// # Portability & Compatibility
///
/// For two [`MerkleSearchTree`] to be useful, both instances must produce
/// identical hash digests for a given input. To do so, they must be using the
/// same [`Hasher`] implementation, and in turn it must output a deterministic
/// hash across all peers interacting with the [`MerkleSearchTree`].
///
/// For ease of use, this library uses the standard library [`Hash`] trait by
/// default to hash key and value types. The documentation for the trait warns
/// it is not guaranteed to produce identical hashes for the same data across
/// different compilation platforms and Rust compiler versions.
///
/// If you intend to interact with peers across multiple platforms and/or Rust
/// versions, you should consider implementing a fully-deterministic [`Hasher`]
/// specialised to your key/value types that does not make use of the [`Hash`]
/// trait for correctness.
///
/// Any change to the underlying hash construction algorithm implemented in this
/// crate that would cause existing hashes to no longer match will not occur
/// without a breaking change major semver version bump once this crate reaches
/// stability (>=1.0.0).
///
/// # Lazy Tree Hash Generation
///
/// Each page within the tree maintains a cache of the pre-computed hash of
/// itself, and the sub-tree rooted from it (all pages & nodes below it).
/// Successive root hash queries will re-use this cached value to avoid hashing
/// the full tree each time.
///
/// Upserting elements into the tree invalidates the cached hashes of the pages
/// along the path to the leaf, and the leaf page itself. To amortise the cost
/// of regenerating these hashes, the affected pages are marked as "dirty",
/// causing them to be rehashed next time the root hash is requested. This
/// allows hash regeneration to be occur once per batch of upsert operations.
///
/// # Example
///
/// ```
/// use merkle_search_tree::MerkleSearchTree;
///
/// let mut t = MerkleSearchTree::default();
/// t.upsert("bananas", &"great");
/// t.upsert("plátano", &"muy bien");
///
/// // Obtain a root hash / merkle proof covering all the tree data
/// let hash_1 = t.root_hash().to_owned();
/// println!("{:?}", hash_1.as_ref());
///
/// // Modify the MST, reflecting the new value of an existing key
/// t.upsert("bananas", &"amazing");
///
/// // Obtain an updated root hash
/// let hash_2 = t.root_hash().to_owned();
/// println!("{:?}", hash_2);
///
/// // The root hash changes to reflect the changed state
/// assert_ne!(hash_1.as_ref(), hash_2.as_ref());
/// ```
///
/// [paper]: https://inria.hal.science/hal-02303490
#[derive(Debug, Clone)]
pub struct MerkleSearchTree<K, V, H = DefaultHasher, const N: usize = 16> {
    /// User-provided hasher implementation used for key/value digests.
    hasher: H,

    /// Internal hasher used to produce page/root digests.
    tree_hasher: SipHasher24,

    root: Page<N, K>,
    root_hash: Option<RootHash>,

    _value_type: PhantomData<V>,
}

impl<K, V> Default for MerkleSearchTree<K, V> {
    fn default() -> Self {
        Self {
            hasher: SipHasher::default(),
            tree_hasher: SipHasher24::default(),
            root: Page::new(0, vec![]),
            root_hash: None,
            _value_type: Default::default(),
        }
    }
}

impl<K, V, H, const N: usize> MerkleSearchTree<K, V, H, N> {
    /// Initialise a new [`MerkleSearchTree`] using the provided [`Hasher`] of
    /// keys & values.
    pub fn new_with_hasher(hasher: H) -> Self {
        Self {
            hasher,
            tree_hasher: SipHasher24::default(),
            root: Page::new(0, vec![]),
            root_hash: None,
            _value_type: PhantomData,
        }
    }

    /// Return the precomputed root hash, if any.
    ///
    /// This method never performs any hashing - if there's no precomputed hash
    /// available, this immediately returns [`None`]. This operation is `O(1)`.
    ///
    /// If this returns [`None`], call [`MerkleSearchTree::root_hash()`] to
    /// regenerate the root hash.
    #[inline]
    pub fn root_hash_cached(&self) -> Option<&RootHash> {
        self.root_hash.as_ref()
    }

    /// Perform a depth-first, in-order traversal, yielding each [`Page`] and
    /// [`Node`] to `visitor`.
    ///
    /// An in-order traversal yields nodes in key order, from min to max.
    pub fn in_order_traversal<'a, T>(&'a self, visitor: &mut T)
    where
        T: Visitor<'a, N, K>,
    {
        self.root.in_order_traversal(visitor, false);
    }

    /// Iterate over all [`Node`] in the tree in ascending key order.
    ///
    /// This method can be used to inspect the keys stored in the tree:
    ///
    /// ```
    /// # use merkle_search_tree::*;
    /// #
    /// let mut t = MerkleSearchTree::default();
    /// t.upsert("bananas1", &42);
    /// t.upsert("bananas3", &42);
    /// t.upsert("bananas4", &42);
    /// t.upsert("bananas2", &42);
    ///
    /// // Collect the keys within the tree
    /// let keys = t.node_iter().map(|v| *v.key()).collect::<Vec<_>>();
    ///
    /// // Nodes are yield in ascending key order:
    /// assert_eq!(
    ///     keys.as_slice(),
    ///     ["bananas1", "bananas2", "bananas3", "bananas4",]
    /// )
    /// ```
    pub fn node_iter(&self) -> impl Iterator<Item = &'_ Node<N, K>>
    where
        K: Debug,
    {
        NodeIter::new(&self.root)
    }
}

impl<K, V, H, const N: usize> MerkleSearchTree<K, V, H, N>
where
    K: AsRef<[u8]>,
{
    /// Generate the root hash if necessary, returning the result.
    ///
    /// If there's a precomputed root hash, it is immediately returned.
    ///
    /// If no cached root hash is available all tree pages with modified child
    /// nodes are rehashed and the resulting new root hash is returned.
    #[inline]
    pub fn root_hash(&mut self) -> &RootHash {
        self.root.maybe_generate_hash(&self.tree_hasher);
        self.root_hash = self.root.hash().cloned().map(RootHash::new);

        #[cfg(feature = "digest_base64")] // Required for display impl
        debug!(
            root_hash=%self.root_hash.as_ref().unwrap(),
            "regenerated root hash"
        );

        self.root_hash.as_ref().unwrap()
    }

    /// Serialise the key interval and hash covering each [`Page`] within this
    /// tree.
    ///
    /// Page hashes are generated on demand - this method returns [`None`] if
    /// the tree needs rehashing (call [`MerkleSearchTree::root_hash()`] and
    /// retry).
    ///
    /// Performs a pre-order traversal of all pages within this tree and emits a
    /// [`PageRange`] per page that covers the min/max key of the subtree at the
    /// given page.
    ///
    /// The first page is the tree root, and as such has a key min/max that
    /// equals the min/max of the keys stored within this tree.
    ///
    /// # Reference vs. Owned
    ///
    /// This method borrows the underlying keys within the tree - this avoids
    /// cloning the keys that form the page bounds when generating the
    /// [`PageRange`] to maximise performance, however this also prevents the
    /// caller from mutating the tree whilst holding onto the serialised pages
    /// (an immutable reference to the tree).
    ///
    /// If the key type (`K`) implements [`Clone`], a set of owned serialised
    /// pages that do not borrow from the tree can be created by constructing a
    /// [`PageRangeSnapshot`] from the returned [`PageRange`] array:
    ///
    /// ```
    /// # use merkle_search_tree::{*, diff::*};
    /// #
    /// let mut t = MerkleSearchTree::default();
    /// t.upsert("bananas", &42);
    ///
    /// // Rehash the tree before generating the page ranges
    /// let _ = t.root_hash();
    ///
    /// // Generate the hashes & page ranges
    /// let ranges = t.serialise_page_ranges().unwrap();
    ///
    /// // At this point, attempting to insert into the tree fails because the
    /// // tree is already borrowed as immutable.
    /// //
    /// // Instead clone all the keys and generate a snapshot:
    /// let snap = PageRangeSnapshot::from(ranges);
    ///
    /// // And the tree is free to be mutated while `snap` exists!
    /// t.upsert("plátanos", &42);
    ///
    /// // The `snap` yields `PageRange` for iteration:
    /// assert_eq!(diff(snap.iter(), snap.iter()), vec![]);
    /// ```
    ///
    /// [`PageRangeSnapshot`]: crate::diff::PageRangeSnapshot
    #[inline]
    pub fn serialise_page_ranges(&self) -> Option<Vec<PageRange<'_, K>>>
    where
        K: PartialOrd,
    {
        // The tree hash must be up-to-date.
        self.root_hash_cached()?;

        if self.root.nodes().is_empty() {
            return Some(vec![]);
        }

        let mut v = PageRangeHashVisitor::default();
        self.root.in_order_traversal(&mut v, false);
        Some(v.finalise())
    }
}

impl<K, V, H, const N: usize> MerkleSearchTree<K, V, H, N>
where
    K: PartialOrd,
    H: Hasher<N, K> + Hasher<N, V>,
{
    /// Add or update the value for `key`.
    ///
    /// This method invalidates the cached, precomputed root hash value, if any
    /// (even if the value is not modified).
    #[inline]
    pub fn upsert(&mut self, key: K, value: &'_ V) {
        let value_hash = ValueDigest::new(self.hasher.hash(value));
        let level = digest::level(&self.hasher.hash(&key));

        // Invalidate the root hash - it always changes when a key is upserted.
        self.root_hash = None;

        if let UpsertResult::InsertIntermediate(key) =
            self.root.upsert(key, level, value_hash.clone())
        {
            // As an optimisation and simplification, if the current root is
            // empty, simply replace it with the new root.
            if self.root.nodes().is_empty() {
                let node = Node::new(key, value_hash, None);
                self.root = Page::new(level, vec![node]);
                return;
            }

            insert_intermediate_page(&mut &mut self.root, key, level, value_hash);
        }
    }
}

#[cfg(test)]
mod tests {
    use std::{
        collections::{BTreeSet, HashSet},
        hash::Hasher as _,
    };

    use proptest::prelude::*;
    use siphasher::sip128::Hasher128;

    use super::*;
    use crate::{
        assert_tree,
        digest::{
            mock::{LevelKey, MockHasher},
            Digest,
        },
        test_util::IntKey,
        visitor::{
            assert_count::InvariantAssertCount, assert_order::InvariantAssertOrder, nop::NopVisitor,
        },
    };

    /// A hash implementation that does not rely on the stdlib Hash trait, and
    /// therefore produces stable hashes across rust version changes /
    /// platforms.
    #[derive(Debug, Default)]
    struct FixtureHasher;
    impl Hasher<16, IntKey> for FixtureHasher {
        fn hash(&self, value: &IntKey) -> Digest<16> {
            self.hash(&value.unwrap())
        }
    }
    impl Hasher<16, u64> for FixtureHasher {
        fn hash(&self, value: &u64) -> Digest<16> {
            let mut h = SipHasher24::default();
            h.write_u64(*value);
            Digest::new(h.finish128().as_bytes())
        }
    }

    #[test]
    fn test_hash_fixture() {
        let mut t = MerkleSearchTree::new_with_hasher(FixtureHasher::default());

        for i in 0..1000 {
            t.upsert(IntKey::new(i), &i);
        }

        // This hash ensures that any changes to this construction do not result
        // in existing hashes being invalidated / unequal for the same data.
        let fixture_hash = [
            57, 77, 199, 66, 89, 217, 207, 166, 136, 181, 45, 80, 108, 80, 94, 3,
        ];

        assert_eq!(t.root_hash().as_ref(), &fixture_hash);
    }

    #[test]
    fn test_level_generation() {
        let h = Digest::new([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
        assert_eq!(digest::level(&h), 32);

        let h = Digest::new([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
        assert_eq!(digest::level(&h), 0);

        let h = Digest::new([0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
        assert_eq!(digest::level(&h), 1);

        let h = Digest::new([0, 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
        assert_eq!(digest::level(&h), 3);
    }

    macro_rules! test_insert {
        (
			$name:ident,
			values = [$(($key:expr, $value:expr)),*]
		) => {
            paste::paste! {
                #[test]
                fn [<test_ $name>]() {
                    let mut t = MerkleSearchTree::new_with_hasher(MockHasher::default());

					$(
						t.upsert($key, $value);
					)*

                    assert_tree!(t)
                }
            }
        };
    }

    test_insert!(one, values = [(LevelKey::new("key", 0), &"bananas")]);

    test_insert!(
        one_non_zero_level,
        values = [(LevelKey::new("key", 4), &"bananas")]
    );

    // Assert the tree is ordered by key.
    test_insert!(
        two_in_order,
        values = [
            (LevelKey::new("A", 0), &"bananas"),
            (LevelKey::new("B", 0), &"bananas")
        ]
    );

    // Assert the tree becomes ordered by key.
    test_insert!(
        two_unordered,
        values = [
            (LevelKey::new("B", 0), &"bananas"),
            (LevelKey::new("A", 0), &"bananas")
        ]
    );

    // Insert two values
    //
    // Level 0 [ A ]
    //
    // Then insert B, which is destined for level 1, and greater than A;
    // therefore B is placed in level 1 as the new root, and A/level 0 is
    // attached to the lt_pointer of B.
    test_insert!(
        root_split_page_gt,
        values = [
            (LevelKey::new("A", 0), &"bananas"),
            (LevelKey::new("B", 1), &"bananas")
        ]
    );

    // Insert two values
    //
    // Level 0 [ B ]
    //
    // Then insert A, which is destined for level 1, and less than B;
    // therefore A is placed in level 1 as the new root, and B/level 0 is
    // attached to the high page of level 1 because A < B.
    test_insert!(
        root_split_page_lt,
        values = [
            (LevelKey::new("B", 0), &"bananas"),
            (LevelKey::new("A", 1), &"bananas")
        ]
    );

    // Insert two values, the second with a level higher than the first, causing
    // the root page to be split, both with differing (non-consecutive) levels.
    test_insert!(
        root_split_non_zero_step_gt,
        values = [
            (LevelKey::new("A", 3), &"bananas"),
            (LevelKey::new("B", 9), &"bananas")
        ]
    );

    // Insert two values, the second with a level higher than the first, causing
    // the root page to be split, both with differing (non-consecutive) levels.
    test_insert!(
        root_split_non_zero_step_lt,
        values = [
            (LevelKey::new("B", 3), &"bananas"),
            (LevelKey::new("A", 9), &"bananas")
        ]
    );

    // Insert elements that cause the root to split, and then the child page to
    // split. Each successive element causes a new page to be created and added
    // to the previous level's high page.
    test_insert!(
        non_root_page_split_gt,
        values = [
            (LevelKey::new("A", 6), &"bananas"),
            (LevelKey::new("B", 4), &"bananas"),
            (LevelKey::new("C", 2), &"bananas")
        ]
    );

    test_insert!(
        non_root_page_split_lt,
        values = [
            (LevelKey::new("C", 6), &"bananas"),
            (LevelKey::new("B", 4), &"bananas"),
            (LevelKey::new("A", 2), &"bananas")
        ]
    );

    // Upsert for an existing key does not create more nodes.
    test_insert!(
        update,
        values = [
            (LevelKey::new("A", 6), &"bananas"),
            (LevelKey::new("A", 6), &"platanos")
        ]
    );

    // Upsert for an existing key does not create more nodes.
    test_insert!(
        split_child_into_two_empty_gte_page,
        values = [
            (LevelKey::new("A", 5), &"platanos"),
            (LevelKey::new("B", 0), &"platanos"),
            (LevelKey::new("C", 0), &"platanos"),
            (LevelKey::new("D", 1), &"platanos")
        ]
    );

    // Upsert for an existing key does not create more nodes.
    test_insert!(
        split_child_into_two_with_gte_page,
        values = [
            (LevelKey::new("A", 5), &"platanos"),
            (LevelKey::new("B", 0), &"platanos"),
            (LevelKey::new("C", 0), &"platanos"),
            (LevelKey::new("E", 0), &"platanos"),
            (LevelKey::new("D", 1), &"platanos")
        ]
    );

    // Ensure that when inserting a node greater than all existing nodes in a
    // page, the high page is split if necessary
    test_insert!(
        greatest_key_splits_high_page,
        values = [
            (LevelKey::new(11, 1), &"bananas"),
            (LevelKey::new(10, 2), &"bananas"),
            (LevelKey::new(12, 2), &"bananas")
        ]
    );

    // When inserting an intermediate page, ensure the high-page of the
    // less-than split is processed.
    test_insert!(
        intermediate_page_move_all_nodes_and_high_page,
        values = [
            (LevelKey::new(1, 1), &"bananas"),
            (LevelKey::new(2, 1), &"bananas"),
            (LevelKey::new(4, 0), &"bananas"),
            (LevelKey::new(3, 2), &"bananas")
        ]
    );

    test_insert!(
        intermediate_page_move_all_nodes_and_high_page_subset,
        values = [
            (LevelKey::new(1, 1), &"bananas"),
            (LevelKey::new(2, 1), &"bananas"),
            (LevelKey::new(3, 0), &"bananas"),
            (LevelKey::new(5, 0), &"bananas"),
            (LevelKey::new(4, 2), &"bananas")
        ]
    );

    test_insert!(
        child_page_split_add_intermediate,
        values = [
            (LevelKey::new("K", 2), &"bananas"),
            (LevelKey::new("D", 0), &"bananas"),
            (LevelKey::new("E", 1), &"bananas")
        ]
    );

    test_insert!(
        equal_page_move_all_nodes_and_high_page,
        values = [
            (LevelKey::new(2_usize, 64), &"bananas"),
            (LevelKey::new(5_usize, 20), &"bananas"),
            (LevelKey::new(3_usize, 52), &"bananas"),
            (LevelKey::new(4_usize, 64), &"bananas")
        ]
    );

    test_insert!(
        equal_page_move_all_nodes_and_high_page_subset,
        values = [
            (LevelKey::new(2_usize, 64), &"bananas"),
            (LevelKey::new(6_usize, 20), &"bananas"),
            (LevelKey::new(4_usize, 20), &"bananas"),
            (LevelKey::new(3_usize, 52), &"bananas"),
            (LevelKey::new(5_usize, 64), &"bananas")
        ]
    );

    test_insert!(
        split_page_all_gte_nodes_with_lt_pointer,
        values = [
            (LevelKey::new(1, 0), &"bananas"),
            (LevelKey::new(0, 1), &"bananas")
        ]
    );

    test_insert!(
        split_page_all_lt_nodes_with_high_page,
        values = [
            (LevelKey::new(0, 0), &"bananas"),
            (LevelKey::new(1, 1), &"bananas")
        ]
    );

    test_insert!(
        insert_intermediate_recursive_lt_pointer,
        values = [
            (LevelKey::new(1_usize, 1), &""),
            (LevelKey::new(2_usize, 0), &""),
            (LevelKey::new(4_usize, 1), &""),
            (LevelKey::new(3_usize, 2), &"")
        ]
    );

    test_insert!(
        split_page_move_gte_lt_pointer_to_high_page,
        values = [
            (LevelKey::new(1_usize, 1), &""),
            (LevelKey::new(2_usize, 0), &""),
            (LevelKey::new(4_usize, 1), &""),
            (LevelKey::new(3_usize, 2), &"")
        ]
    );

    test_insert!(
        split_page_move_input_high_page_to_gte_page,
        values = [
            (LevelKey::new(6, 0), &"bananas"),
            (LevelKey::new(3, 21), &"bananas"),
            (LevelKey::new(0, 21), &"bananas"),
            (LevelKey::new(1, 22), &"bananas")
        ]
    );

    proptest! {
        // Invariant 1: the tree structure is deterministic for a given set of
        // inputs (regardless of insert order)
        #[test]
        fn prop_deterministic_construction(keys in proptest::collection::vec(any::<u64>(), 0..64)) {
            // keys is a HashSet of (keys, level), which will iterate in random
            // order.
            //
            // Collect the items into a vector and sort it, producing a
            // different insert ordering from the HashSet iter.
            let mut b_values = keys.to_vec();
            b_values.sort();
            b_values.dedup();

            let a_values = keys;

            let mut a = MerkleSearchTree::default();
            let mut b = MerkleSearchTree::default();

            let want_len = b_values.len();

            let mut unique = HashSet::new();
            for key in a_values {
                if unique.insert(key) {
                    a.upsert(IntKey::new(key), &"bananas");
                }
            }
            for key in b_values {
                b.upsert(IntKey::new(key), &"bananas");
            }

            assert_node_equal(&mut a, &mut b);

            let mut asserter = InvariantAssertCount::new(InvariantAssertOrder::new(NopVisitor::default()));
            a.in_order_traversal(&mut asserter);
            asserter.unwrap_count(want_len);

            let mut asserter = InvariantAssertCount::new(InvariantAssertOrder::new(NopVisitor::default()));
            b.in_order_traversal(&mut asserter);
            asserter.unwrap_count(want_len);
        }

        // Invariant 2: values in the tree are stored in key order.
        #[test]
        fn prop_in_order_traversal_key_order(keys in proptest::collection::vec(any::<u64>(), 0..64)) {
            let mut t = MerkleSearchTree::default();

            let mut unique = HashSet::new();
            let mut want_len = 0;

            for key in keys {
                if unique.insert(key) {
                    want_len += 1;
                    t.upsert(IntKey::new(key), &"bananas");
                }
            }

            let mut asserter = InvariantAssertCount::new(InvariantAssertOrder::new(NopVisitor::default()));
            t.in_order_traversal(&mut asserter);
            asserter.unwrap_count(want_len);
        }

        // Invariant 3: two independent trees contain the same data iff their
        // root hashes are identical.
        //
        // Additionally the serialised page ranges MUST match iff the trees
        // match.
        #[test]
        fn prop_root_hash_data_equality(keys in proptest::collection::vec(any::<u64>(), 0..64)) {
            let mut a = MerkleSearchTree::default();
            let mut b = MerkleSearchTree::default();

            // They are equal when empty.
            assert_eq!(a.root_hash(), b.root_hash());

            let mut unique = HashSet::new();
            let last_entry = keys.first().cloned();
            for key in keys {
                if !unique.insert(key) {
                    // Root hashes may compute to the same value if the same
                    // (key, value) pair is inserted twice, causing the
                    // divergence assert below to spuriously trigger.
                    continue;
                }

                // Add the key to tree A
                a.upsert(IntKey::new(key), &"bananas");
                assert_eq!(a.root_hash_cached(), None);

                // The trees have now diverged
                assert_node_not_equal(&mut a, &mut b);

                // Add the key to tree B
                b.upsert(IntKey::new(key), &"bananas");
                assert_eq!(b.root_hash_cached(), None);

                // And now the tees have converged
                assert_node_equal(&mut a, &mut b);
            }

            // Update a value for an existing key
            if let Some(key) = last_entry {
                 b.upsert(IntKey::new(key), &"platanos");
                 assert_eq!(b.root_hash_cached(), None);

                 // The trees diverge
                assert_node_not_equal(&mut a, &mut b);

                 // And converge once again
                 a.upsert(IntKey::new(key), &"platanos");
                 assert_eq!(a.root_hash_cached(), None);

                // And now the tees have converged
                assert_node_equal(&mut a, &mut b);
            }

            let mut asserter = InvariantAssertCount::new(InvariantAssertOrder::new(NopVisitor::default()));
            a.in_order_traversal(&mut asserter);
            asserter.unwrap_count(unique.len());

            let mut asserter = InvariantAssertCount::new(InvariantAssertOrder::new(NopVisitor::default()));
            b.in_order_traversal(&mut asserter);
            asserter.unwrap_count(unique.len());
        }

        // Invariant: the node iter yields every node in the tree in ascending
        // key order.
        #[test]
        fn prop_node_iter(keys in proptest::collection::vec(any::<u64>(), 0..64)) {
            let mut t = MerkleSearchTree::default();

            let mut inserted = BTreeSet::new();
            for key in keys {
                t.upsert(key, &key);
                inserted.insert(key);
            }

            // Use the node iter to visit all nodes, preserving the key order in
            // the returned iterator.
            let got = t
                .node_iter()
                .map(|v| *v.key());

            // The iterator must yield all keys in the same order as a (sorted!)
            // BTreeSet to satisfy the invariant.
            assert!(inserted.into_iter().eq(got));
        }
    }

    fn assert_node_equal<K, V>(a: &mut MerkleSearchTree<K, V>, b: &mut MerkleSearchTree<K, V>)
    where
        K: AsRef<[u8]> + PartialOrd + Debug,
    {
        assert_eq!(a.root_hash(), b.root_hash(), "root hashes should be equal");
        assert_eq!(
            a.serialise_page_ranges(),
            b.serialise_page_ranges(),
            "serialised pages should match"
        );
        // The cached values must always match their computed values.
        assert_eq!(
            b.root_hash_cached().unwrap().clone(),
            *b.root_hash(),
            "cached hashes should be equal for b"
        );
        assert_eq!(
            a.root_hash_cached().unwrap().clone(),
            *a.root_hash(),
            "cached hashes should be equal for a"
        );
    }

    fn assert_node_not_equal<K, V>(a: &mut MerkleSearchTree<K, V>, b: &mut MerkleSearchTree<K, V>)
    where
        K: AsRef<[u8]> + PartialOrd + Debug,
    {
        assert_ne!(
            a.root_hash(),
            b.root_hash(),
            "root hash should not be equal"
        );
        assert_ne!(
            a.serialise_page_ranges(),
            b.serialise_page_ranges(),
            "serialised pages should not match"
        );
        // The cached values must always match their computed values.
        assert_eq!(
            b.root_hash_cached().unwrap().clone(),
            *b.root_hash(),
            "cached hashes should always be equal for b"
        );
        assert_eq!(
            a.root_hash_cached().unwrap().clone(),
            *a.root_hash(),
            "cached hashes should always be equal for a"
        );
    }
}