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
// Copyright 2020 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0

use super::*;
use std::{cmp::Ordering, marker::PhantomData};

// modes of tree operation

/// Type state mode for a tree
pub trait HeightMode {
    /// Gets the fixed height if applicable
    fn fixed_height(&self) -> Option<u32>;
}

#[derive(Default, Debug, Clone)]
/// Type state mode for a tree with a fixed height
pub struct FixedHeightMode(u32);

impl HeightMode for FixedHeightMode {
    fn fixed_height(&self) -> Option<u32> {
        Some(self.0)
    }
}

#[derive(Default, Debug, Clone)]
/// Type state mode for a tree with a variable height that increases as data is added
pub struct VariableHeightMode;

impl HeightMode for VariableHeightMode {
    fn fixed_height(&self) -> Option<u32> {
        None
    }
}

/// Special complete binary merkle tree that is compatible with
/// <https://github.com/o1-labs/snarky/blob/master/src/base/merkle_tree.ml>
/// whose leaf nodes are at the same height
pub struct MinaMerkleTree<Item, Hash, Hasher, Merger, Mode>
where
    Hasher: MerkleHasher<Item = Item, Hash = Hash>,
    Merger: MerkleMerger<Hash = Hash>,
    Hash: Clone + PartialEq + std::fmt::Debug,
    Item: Clone,
    Mode: HeightMode,
{
    mode: Mode,
    variable_height: u32,
    leafs: Vec<(Item, Option<Hash>)>,
    nodes: Vec<Option<Hash>>,

    _pd_hasher: PhantomData<Hasher>,
    _pd_merger: PhantomData<Merger>,
}

impl<Item, Hash, Hasher, Merger> MinaMerkleTree<Item, Hash, Hasher, Merger, FixedHeightMode>
where
    Hasher: MerkleHasher<Item = Item, Hash = Hash>,
    Merger: MerkleMerger<Hash = Hash>,
    Hash: Clone + PartialEq + std::fmt::Debug,
    Item: Clone,
{
    /// Creates a new instance of a fixed height MinaMerkleTree
    pub fn new(height: u32) -> Self {
        Self {
            mode: FixedHeightMode(height),
            ..Default::default()
        }
    }
}

impl<Item, Hash, Hasher, Merger> MinaMerkleTree<Item, Hash, Hasher, Merger, VariableHeightMode>
where
    Hasher: MerkleHasher<Item = Item, Hash = Hash>,
    Merger: MerkleMerger<Hash = Hash>,
    Hash: Clone + PartialEq + std::fmt::Debug,
    Item: Clone,
{
    /// Creates a new instance of a variable height MinaMerkletree
    pub fn new() -> Self {
        Default::default()
    }
}

impl<Item, Hash, Hasher, Merger, Mode> MinaMerkleTree<Item, Hash, Hasher, Merger, Mode>
where
    Hasher: MerkleHasher<Item = Item, Hash = Hash>,
    Merger: MerkleMerger<Hash = Hash>,
    Hash: Clone + PartialEq + std::fmt::Debug,
    Item: Clone,
    Mode: Default + HeightMode,
{
    /// Creates a new instance of [MinaMerkleTree] with estimated capacity of leaves
    pub fn with_capacity(capacity: usize) -> Self {
        let potential_height = calculate_height(capacity);
        let potential_node_count = calculate_node_count(potential_height);
        Self {
            leafs: Vec::with_capacity(capacity),
            nodes: Vec::with_capacity(potential_node_count),
            ..Default::default()
        }
    }

    /// Gets the merkle proof of an item with the 0-based index of the item
    /// being added, e.g. the first item is index 0.
    /// This function panics when the index is out of range.
    pub fn get_proof(
        &mut self,
        index: usize,
    ) -> Option<DefaultMerkleProof<Item, Hash, Hasher, Merger>> {
        if self.calculate_hash_if_needed(0).is_some() {
            // 1. Calculates the number of nodes above the data node,
            // for calculating its index in the tree (0 for the root, counted from topdown, left to right)
            let height_above = if let Some(fixed_height) = self.mode.fixed_height() {
                fixed_height
            } else {
                self.variable_height
            };
            let index_offset = calculate_node_count(height_above);
            let capacity = self.mode.fixed_height().unwrap_or(self.variable_height) as usize;
            let mut peer_indices = Vec::with_capacity(capacity);
            let mut peer_hashes = Vec::with_capacity(capacity);
            let (item, _) = &self.leafs[index];
            let index_with_offset = index_offset + index;
            // 2. Gets the index and hash of its sibling in the tree, and push to the proof vec
            let peer_index = if index % 2 == 0 { index + 1 } else { index - 1 };
            let peer_index_with_offset = index_offset + peer_index;
            let peer_hash = if peer_index < self.leafs.len() {
                self.leafs[peer_index].1.clone()
            } else {
                None
            };
            peer_indices.push(peer_index_with_offset);
            peer_hashes.push(peer_hash);
            // 3. Gets the index of their parent node and point the cursor to it
            // parent_index is the index of the actual tree with the variable height
            let mut parent_index = calculate_parent_index(self.nodes.len() + index);
            // and parent_index_with_offset is the index of the virtual tree with possible fixed height
            // when the fixed height equals to the variable height, parent_index equals to parent_index_with_offset
            let mut parent_index_with_offset = calculate_parent_index(index_with_offset);
            while parent_index_with_offset > 0 {
                if parent_index > 0 {
                    // 4. Gets the index and hash of the sibling of this node, and push to the proof vec
                    let parent_peer_index = if parent_index % 2 == 0 {
                        parent_index - 1
                    } else {
                        parent_index + 1
                    };
                    let parent_peer_index_with_offset =
                        parent_index_with_offset + parent_peer_index - parent_index;
                    peer_indices.push(parent_peer_index_with_offset);
                    peer_hashes.push(self.nodes[parent_peer_index].clone());
                    parent_index = calculate_parent_index(parent_index);
                } else {
                    // 4.1 When it comes to virtual nodes(only when fixed node count > variable node count)
                    // that are not stored in the tree, use None hash and leave it for the merger to recalculate
                    // in proof verification flow
                    peer_indices.push(parent_index_with_offset + 1);
                    peer_hashes.push(None);
                }
                // 5. Go back to step 3, point the cursor to its parent and apply the same flow
                // until root node is hit
                parent_index_with_offset = calculate_parent_index(parent_index_with_offset);
            }
            Some(DefaultMerkleProof::new(
                index_with_offset,
                item.clone(),
                peer_indices,
                peer_hashes,
            ))
        } else {
            None
        }
    }

    /// Clears cached hashes of all ancester nodes of the give leaf
    /// because the values become invaid once the leaf is updated
    fn clear_dirty_hashes(&mut self, leaf_index: usize) {
        let mut parent = leaf_index;
        while parent > 0 {
            parent = calculate_parent_index(parent);
            if self.nodes[parent].is_some() {
                self.nodes[parent] = None;
            } else {
                break;
            }
        }
    }

    /// Calucates hash of a node if it's not available in the node cache
    /// either apply hash algorithm if it's a leaf node
    /// or apply merge algorithm if it's a non-leaf node
    /// update the cache once calculated
    fn calculate_hash_if_needed(&mut self, index: usize) -> Option<Hash> {
        if index < self.nodes.len() {
            if let Some(hash) = &self.nodes[index] {
                Some(hash.clone())
            } else {
                let left = index * 2 + 1;
                let right = index * 2 + 2;
                let left_hash = self.calculate_hash_if_needed(left);
                let right_hash = self.calculate_hash_if_needed(right);
                let hash = Merger::merge(
                    [left_hash, right_hash],
                    MerkleTreeNodeMetadata::new(index, self.variable_height),
                );
                self.nodes[index] = hash.clone();
                hash
            }
        } else {
            let leaf_index = index - self.nodes.len();
            if leaf_index < self.leafs.len() {
                let (data, hash) = &mut self.leafs[leaf_index];
                match hash {
                    None => {
                        let node_hash = Some(Hasher::hash(
                            data,
                            MerkleTreeNodeMetadata::new(index, self.variable_height),
                        ));
                        *hash = node_hash.clone();
                        node_hash
                    }
                    _ => hash.clone(),
                }
            } else {
                None
            }
        }
    }
}

impl<Item, Hash, Hasher, Merger> MerkleTree
    for MinaMerkleTree<Item, Hash, Hasher, Merger, VariableHeightMode>
where
    Hasher: MerkleHasher<Item = Item, Hash = Hash>,
    Merger: MerkleMerger<Hash = Hash>,
    Hash: Clone + PartialEq + std::fmt::Debug,
    Item: Clone,
{
    type Item = Item;
    type Hash = Hash;

    fn height(&self) -> u32 {
        self.variable_height
    }

    fn count(&self) -> usize {
        self.leafs.len()
    }

    fn root(&mut self) -> Option<Self::Hash> {
        self.calculate_hash_if_needed(0)
    }

    fn add_batch(&mut self, items: impl IntoIterator<Item = Self::Item>) {
        add_batch(self, items)
    }
}

impl<Item, Hash, Hasher, Merger> MerkleTree
    for MinaMerkleTree<Item, Hash, Hasher, Merger, FixedHeightMode>
where
    Hasher: MerkleHasher<Item = Item, Hash = Hash>,
    Merger: MerkleMerger<Hash = Hash>,
    Hash: Clone + PartialEq + std::fmt::Debug,
    Item: Clone,
{
    type Item = Item;
    type Hash = Hash;

    fn height(&self) -> u32 {
        self.mode.0
    }

    fn count(&self) -> usize {
        self.leafs.len()
    }

    fn root(&mut self) -> Option<Self::Hash> {
        let mut hash = self.calculate_hash_if_needed(0);
        let fixed_height = self.mode.0;
        match fixed_height.cmp(&self.variable_height) {
            Ordering::Less => panic!(
                "fixed_height {fixed_height} should not be smaller than current height {}",
                self.variable_height,
            ),
            Ordering::Equal => hash,
            Ordering::Greater => {
                for h in (self.variable_height + 1)..=fixed_height {
                    hash = Merger::merge([hash, None], MerkleTreeNodeMetadata::new(0, h));
                }
                hash
            }
        }
    }

    fn add_batch(&mut self, items: impl IntoIterator<Item = Self::Item>) {
        add_batch(self, items)
    }
}

impl<Item, Hash, Hasher, Merger, Mode> Default for MinaMerkleTree<Item, Hash, Hasher, Merger, Mode>
where
    Hasher: MerkleHasher<Item = Item, Hash = Hash>,
    Merger: MerkleMerger<Hash = Hash>,
    Hash: Clone + PartialEq + std::fmt::Debug,
    Item: Clone,
    Mode: Default + HeightMode,
{
    fn default() -> Self {
        Self {
            mode: Default::default(),
            variable_height: 0,
            leafs: Vec::new(),
            nodes: Vec::new(),
            _pd_hasher: Default::default(),
            _pd_merger: Default::default(),
        }
    }
}

fn add_batch<Item, Hash, Hasher, Merger, Mode>(
    tree: &mut MinaMerkleTree<Item, Hash, Hasher, Merger, Mode>,
    items: impl IntoIterator<Item = Item>,
) where
    Hasher: MerkleHasher<Item = Item, Hash = Hash>,
    Merger: MerkleMerger<Hash = Hash>,
    Hash: Clone + PartialEq + std::fmt::Debug,
    Item: Clone,
    Mode: Default + HeightMode,
{
    let mut leaves: Vec<_> = items
        .into_iter()
        .map(|item| {
            (
                // Tree height might be changed, do not calculate hash here.
                item, None,
            )
        })
        .collect();
    let new_leaf_count = tree.leafs.len() + leaves.len();
    let new_height = calculate_height(new_leaf_count);
    if new_height != tree.variable_height {
        let new_node_count = calculate_node_count(new_height);
        tree.variable_height = new_height;
        tree.nodes = vec![None; new_node_count];
    } else {
        let start = tree.nodes.len() + tree.leafs.len();
        for i in start..(start + leaves.len()) {
            tree.clear_dirty_hashes(i);
        }
    }
    tree.leafs.append(&mut leaves);
}

fn calculate_height(size: usize) -> u32 {
    if size < 2 {
        0
    } else {
        (size as f64).log2().ceil() as u32
    }
}

fn calculate_node_count(height: u32) -> usize {
    2_usize.pow(height) - 1
}

fn calculate_parent_index(index: usize) -> usize {
    debug_assert!(index > 0);
    (index - 1) / 2
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn calculate_height_tests() {
        assert_eq!(0, calculate_height(0));
        assert_eq!(0, calculate_height(1));
        assert_eq!(1, calculate_height(2));
        assert_eq!(2, calculate_height(3));
        assert_eq!(2, calculate_height(4));
        assert_eq!(3, calculate_height(5));
        assert_eq!(4, calculate_height(11));
        assert_eq!(5, calculate_height(29));
        // Genesis ledger account number
        assert_eq!(11, calculate_height(1676));
    }
}