Function tallytree::proof::util::directions_to_node_index[][src]

pub fn directions_to_node_index(directions: &[TraverseDirection]) -> usize
Expand description

Calculate a index to a node given traverse directions to it in the merkle tree.

Note: Directions given by collapse_branches may cause the index to be off-by-one. Use find_node_index for always accurate result.

The index is zero based numbered.

Example:

use tallytree::proof::util::{collapse_branches, create_merkle_proof, CollapseBranchConstrains, directions_to_node_index};
use tallytree::navigate::find_node_by_vote_reference;
use tallytree::{Validation, VoteReference};
use tallytree::generate::generate_tree;

let tree = generate_tree(vec![
     ([0xaa; 32], vec![1, 0]),
     ([0xbb; 32], vec![1, 0]),
     ([0xcc; 32], vec![0, 1]),
], true).unwrap();

let v = &Validation::Strict;
let proof = create_merkle_proof(
    &find_node_by_vote_reference(&tree, &[0xcc; 32]).unwrap(),
    v
).unwrap();

let ((_, _), directions) = collapse_branches(
    proof.branches.clone(),
    &CollapseBranchConstrains::None,
    v
).unwrap();
assert_eq!(2, directions_to_node_index(&directions));