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
use crate::node::{is_null_node_ref, is_wrapper_node, Node};
use crate::{NodeRef, VoteReference};
use std::collections::VecDeque;
use std::sync::Arc;

/// A pair of vote references
pub type VoteReferencePair = (Option<VoteReference>, Option<VoteReference>);

/// Find the path to the inner node furthest down to the right.
/// Returns all nodes in the path.
/// ```text
///            A
///          /  \
///         B    C <- We want the path to this one
///        / \   | \
///       D   E  F  Ø
///       |   |  |
///       V1  V2 V3
/// ```
/// Example:
/// ```
/// use tallytree::generate::generate_tree;
/// use tallytree::navigate::find_down_right_most_branch;
/// let tree = generate_tree(vec![
///     ([0xaa; 32], vec![1, 0]),
///     ([0xbb; 32], vec![0, 1]),
///     ([0xcc; 32], vec![1, 0]),
/// ], false).unwrap();
/// let path = find_down_right_most_branch(&tree);
/// ```
pub fn find_down_right_most_branch(node: &NodeRef) -> Option<Vec<NodeRef>> {
    if node.is_none() {
        return None;
    }
    if is_null_node_ref(node) {
        return None;
    }
    let node_ref = node.as_ref().unwrap();
    let node_vec = vec![node.clone()];
    if is_wrapper_node(&node_ref.left) {
        // We're at the last branch, success.
        return Some(node_vec);
    }
    if is_null_node_ref(&node_ref.right) {
        return match find_down_right_most_branch(&node_ref.left) {
            Some(path) => Some([node_vec, path].concat()),
            None => Some(node_vec),
        };
    }
    match find_down_right_most_branch(&node_ref.right) {
        Some(path) => Some([node_vec, path].concat()),
        None => Some(node_vec),
    }
}

/// Find a node in a merkle tally tree given a vote reference.
/// Returns all nodes in the path.
///
/// Example:
/// ```
/// use tallytree::generate::generate_tree;
/// use tallytree::navigate::find_node_by_vote_reference;
/// let tree = generate_tree(vec![
///     ([0xaa; 32], vec![1, 0]),
///     ([0xbb; 32], vec![1, 0]),
///     ([0xcc; 32], vec![0, 1]),
/// ], false).unwrap();
/// let path = find_node_by_vote_reference(&tree, &[0xbb; 32]);
/// ```
/// The above example returns A, B, E, bb
/// ```text
///            A
///          /  \
///         B    C
///        / \   | \
///       D   E  F  Ø
///       |   |  |
///       aa  bb cc
/// ```
pub fn find_node_by_vote_reference(
    node: &NodeRef,
    reference: &VoteReference,
) -> Option<Vec<NodeRef>> {
    // TODO: This can probably be optimized by using the fact that the leafs are sorted.

    if node.is_none() {
        return None;
    }

    if let Some((r, _)) = node.as_ref().unwrap().vote {
        return if &r == reference {
            // Found it
            Some(vec![node.clone()])
        } else {
            None
        };
    }
    let node_vec = vec![node.clone()];
    let node = node.as_ref().unwrap();
    if let Some(path) = find_node_by_vote_reference(&node.left, reference) {
        return Some([node_vec, path].concat());
    }
    if let Some(path) = find_node_by_vote_reference(&node.right, reference) {
        return Some([node_vec, path].concat());
    }
    None
}

/**
 * Finds the two nearest sibling vote references of a given reference. The
 * given reference does not need to exist. A boolean representing if it exists
 * or not is returned.
 *
 * Example:
 * ```
 * use tallytree::generate::generate_tree;
 * use tallytree::navigate::find_closest_siblings;
 * let tree = generate_tree(vec![
 *     ([0xaa; 32], vec![1, 0]),
 *     ([0xbb; 32], vec![1, 0]),
 *     ([0xdd; 32], vec![0, 1]),
 * ], false).unwrap();
 * let ((left, right), exists) = find_closest_siblings(&tree, &[0xcc; 32]).unwrap();
 * assert!(!exists);
 * assert_eq!(left, Some([0xbb; 32]));
 * assert_eq!(right, Some([0xdd; 32]));
 * ```
 */
pub fn find_closest_siblings(
    root: &NodeRef,
    reference: &VoteReference,
) -> Result<(VoteReferencePair, bool), String> {
    // TODO: This can probably be optimized by using the fact that the leafs are sorted.

    let root: &Arc<Node> = root
        .as_ref()
        .ok_or_else(|| "Invalid root node".to_string())?;

    let mut queue = VecDeque::from([root]);
    let mut left_sibling: Option<VoteReference> = None;
    let mut right_sibling: Option<VoteReference> = None;
    let mut reference_exists = false;

    while let Some(node) = queue.pop_front() {
        if let Some(n) = &node.left {
            queue.push_back(n);
        }
        if let Some(n) = &node.right {
            queue.push_back(n);
        }

        if let Some((r, _)) = &node.vote {
            if r == reference {
                reference_exists = true;
            }
            if r < reference {
                if let Some(l) = left_sibling {
                    if r > &l {
                        left_sibling = Some(*r);
                    }
                } else {
                    left_sibling = Some(*r);
                }
            }
            if r > reference {
                if let Some(l) = right_sibling {
                    if r < &l {
                        right_sibling = Some(*r);
                    }
                } else {
                    right_sibling = Some(*r);
                }
            }
        }
    }
    Ok(((left_sibling, right_sibling), reference_exists))
}

/// Find the depth of the merkle tally tree at this node, including leaf wrapper nodes.
///
/// For example, a tree with 3 nodes will have a depth of 5.
/// ```text
///            A
///          /  \
///         B    C
///        / \   | \
///       D   E  F  Ø
///       |   |  |
///       V1  V2 V3
/// ```
pub fn tree_depth(node: &NodeRef) -> usize {
    match node {
        Some(n) => 1 + tree_depth(&n.left),
        None => 0,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::generate::generate_tree;
    use crate::hash::hash_node_ref;
    use crate::Validation;

    #[test]
    fn test_find_down_right_most_branch() {
        // We want to find A:
        //
        // ```text
        //          A
        //        /   \
        //       B     Ø
        //       |
        //       V
        // ```
        let root = generate_tree(vec![([0xaa; 32], vec![1, 0])], true).unwrap();
        let path = find_down_right_most_branch(&root).unwrap();
        assert_eq!(path.len(), 1);
        assert_eq!(
            hash_node_ref(&path[0], &Validation::Strict),
            hash_node_ref(&root, &Validation::Strict)
        );

        // We want to find C
        // ```text
        //            A
        //          /  \
        //         B    C
        //        / \   | \
        //       D   E  F  Ø
        //       |   |  |
        //       V1  V2 V3
        // ```
        let root = generate_tree(
            vec![
                ([0xaa; 32], vec![1, 0]),
                ([0xbb; 32], vec![1, 0]),
                ([0xcc; 32], vec![0, 1]),
            ],
            true,
        )
        .unwrap();
        let path = find_down_right_most_branch(&root).unwrap();
        assert_eq!(path.len(), 2);
        assert_eq!(
            hash_node_ref(&path[1], &Validation::Strict),
            hash_node_ref(&root.unwrap().right, &Validation::Strict)
        );
    }

    #[test]
    fn test_find_node_by_vote_reference() {
        // ```text
        //            A
        //          /  \
        //         B    C
        //        / \   | \
        //       D   E  F  Ø
        //       |   |  |
        //       V1  V2 V3
        // ```
        let root = generate_tree(
            vec![
                ([0xaa; 32], vec![1, 0]),
                ([0xbb; 32], vec![1, 0]),
                ([0xcc; 32], vec![0, 1]),
            ],
            true,
        )
        .unwrap();

        // Find V2
        let path = find_node_by_vote_reference(&root, &[0xbb; 32]).unwrap();
        assert_eq!(path.len(), 4);
        assert_eq!(
            hash_node_ref(&path[0], &Validation::Strict),
            hash_node_ref(&root, &Validation::Strict)
        );
        assert_eq!(
            hash_node_ref(&path[1], &Validation::Strict),
            hash_node_ref(&root.as_ref().unwrap().left, &Validation::Strict)
        );
        assert_eq!(
            hash_node_ref(&path[2], &Validation::Strict),
            hash_node_ref(
                &root.as_ref().unwrap().left.as_ref().unwrap().right,
                &Validation::Strict
            )
        );
        assert_eq!(
            hash_node_ref(&path[3], &Validation::Strict),
            hash_node_ref(
                &root
                    .as_ref()
                    .unwrap()
                    .left
                    .as_ref()
                    .unwrap()
                    .right
                    .as_ref()
                    .unwrap()
                    .left,
                &Validation::Strict
            )
        );
    }

    #[test]
    fn test_find_closest_siblings() {
        // ```text
        //            A
        //          /  \
        //         B    C
        //        / \   | \
        //       D   E  F  Ø
        //       |   |  |
        //       V1  V2 V3
        // ```
        let root = generate_tree(
            vec![
                ([0xaa; 32], vec![1, 0]),
                ([0xbb; 32], vec![1, 0]),
                ([0xdd; 32], vec![0, 1]),
            ],
            false,
        )
        .unwrap();

        // V1 has no sibling to the left
        let ((left, right), exists) = find_closest_siblings(&root, &[0xaa; 32]).unwrap();
        assert!(exists);
        assert!(left.is_none());
        assert_eq!(right, Some([0xbb; 32]));

        // V2 has V1 to the left, and V3 to the right.
        let ((left, right), exists) = find_closest_siblings(&root, &[0xbb; 32]).unwrap();
        assert!(exists);
        assert_eq!(left, Some([0xaa; 32]));
        assert_eq!(right, Some([0xdd; 32]));

        // V3 has V2 to the left, and none to the right.
        let ((left, right), exists) = find_closest_siblings(&root, &[0xdd; 32]).unwrap();
        assert!(exists);
        assert_eq!(left, Some([0xbb; 32]));
        assert!(right.is_none());

        // 0xcc does not exist, but would have V2 to the left and V3 to the right.
        let ((left, right), exists) = find_closest_siblings(&root, &[0xcc; 32]).unwrap();
        assert!(!exists);
        assert_eq!(left, Some([0xbb; 32]));
        assert_eq!(right, Some([0xdd; 32]));
    }

    #[test]
    fn test_tree_depth() {
        // ```text
        //          A
        //        /   \
        //       B     Ø
        //       |
        //       V
        // ```
        let root = generate_tree(vec![([0xaa; 32], vec![1, 0])], false).unwrap();
        assert_eq!(3, tree_depth(&root));

        // ```text
        //            A
        //          /  \
        //         B    C
        //        / \   | \
        //       D   E  F  Ø
        //       |   |  |
        //       V1  V2 V3
        // ```
        let root = generate_tree(
            vec![
                ([0xaa; 32], vec![1, 0]),
                ([0xbb; 32], vec![1, 0]),
                ([0xdd; 32], vec![0, 1]),
            ],
            false,
        )
        .unwrap();

        assert_eq!(4, tree_depth(&root));
    }
}