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
#[derive(Debug, Clone, Default)]
pub struct MerkleTreeNodeMetadata {
depth: u32,
height: u32,
}
impl MerkleTreeNodeMetadata {
pub fn new(node_index: usize, tree_height: u32) -> Self {
let depth = depth::<2>(node_index);
let height = height::<2>(node_index, tree_height);
Self { depth, height }
}
pub fn depth(&self) -> u32 {
self.depth
}
pub fn height(&self) -> u32 {
self.height
}
}
fn depth<const DEGREE: usize>(index: usize) -> u32 {
((index + DEGREE) as f64).log(DEGREE as f64).ceil() as u32 - 1
}
fn height<const DEGREE: usize>(index: usize, height: u32) -> u32 {
height - depth::<DEGREE>(index)
}