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
use crate::primitives::{LeafIndex, NodeIndex};
use mmr_lib::helper;
pub struct NodesUtils {
no_of_leaves: LeafIndex,
}
impl NodesUtils {
pub fn new(no_of_leaves: LeafIndex) -> Self {
Self { no_of_leaves }
}
pub fn number_of_peaks(&self) -> NodeIndex {
self.number_of_leaves().count_ones() as NodeIndex
}
pub fn number_of_leaves(&self) -> LeafIndex {
self.no_of_leaves
}
pub fn size(&self) -> NodeIndex {
2 * self.no_of_leaves - self.number_of_peaks()
}
pub fn leaf_index_that_added_node(node_index: NodeIndex) -> LeafIndex {
let rightmost_leaf_pos = Self::rightmost_leaf_node_index_from_pos(node_index);
Self::leaf_node_index_to_leaf_index(rightmost_leaf_pos)
}
fn leaf_node_index_to_leaf_index(pos: NodeIndex) -> LeafIndex {
if pos == 0 {
return 0
}
let peaks = helper::get_peaks(pos);
(pos + peaks.len() as u64) >> 1
}
fn rightmost_leaf_node_index_from_pos(pos: NodeIndex) -> NodeIndex {
pos - (helper::pos_height_in_tree(pos) as u64)
}
pub fn right_branch_ending_in_leaf(leaf_index: LeafIndex) -> crate::Vec<NodeIndex> {
let pos = helper::leaf_index_to_pos(leaf_index);
let num_parents = leaf_index.trailing_ones() as u64;
return (pos..=pos + num_parents).collect()
}
}
#[cfg(test)]
mod tests {
use super::*;
use mmr_lib::helper::leaf_index_to_pos;
#[test]
fn should_calculate_node_index_from_leaf_index() {
for index in 0..100000 {
let pos = leaf_index_to_pos(index);
assert_eq!(NodesUtils::leaf_node_index_to_leaf_index(pos), index);
}
}
#[test]
fn should_calculate_right_branch_correctly() {
fn left_jump_sequence(leaf_index: LeafIndex) -> Vec<u64> {
let pos = leaf_index_to_pos(leaf_index);
let mut right_branch_ending_in_leaf = vec![pos];
let mut next_pos = pos + 1;
while mmr_lib::helper::pos_height_in_tree(next_pos) > 0 {
right_branch_ending_in_leaf.push(next_pos);
next_pos += 1;
}
right_branch_ending_in_leaf
}
for leaf_index in 0..100000 {
let pos = mmr_lib::helper::leaf_index_to_pos(leaf_index);
assert_eq!(NodesUtils::right_branch_ending_in_leaf(pos), left_jump_sequence(pos));
}
}
#[test]
fn should_calculate_rightmost_leaf_node_index_from_pos() {
for pos in 0..100000 {
let leaf_pos = NodesUtils::rightmost_leaf_node_index_from_pos(pos);
let leaf_index = NodesUtils::leaf_node_index_to_leaf_index(leaf_pos);
assert!(NodesUtils::right_branch_ending_in_leaf(leaf_index).contains(&pos));
}
}
#[test]
fn should_calculate_depth_correctly() {
assert_eq!(
vec![0, 1, 2, 3, 4, 9, 15, 21]
.into_iter()
.map(|n| NodesUtils::new(n).number_of_leaves())
.collect::<Vec<_>>(),
vec![0, 1, 2, 3, 4, 9, 15, 21]
);
}
#[test]
fn should_calculate_number_of_peaks_correctly() {
assert_eq!(
vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 21]
.into_iter()
.map(|n| NodesUtils::new(n).number_of_peaks())
.collect::<Vec<_>>(),
vec![0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 3]
);
}
#[test]
fn should_calculate_the_size_correctly() {
let _ = env_logger::try_init();
let leaves = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 21];
let sizes = vec![0, 1, 3, 4, 7, 8, 10, 11, 15, 16, 18, 19, 22, 23, 25, 26, 39];
assert_eq!(
leaves
.clone()
.into_iter()
.map(|n| NodesUtils::new(n).size())
.collect::<Vec<_>>(),
sizes.clone()
);
let mut actual_sizes = vec![];
for s in &leaves[1..] {
crate::tests::new_test_ext().execute_with(|| {
let mut mmr = crate::mmr::Mmr::<
crate::mmr::storage::RuntimeStorage,
crate::mock::Test,
_,
_,
>::new(0);
for i in 0..*s {
mmr.push(i);
}
actual_sizes.push(mmr.size());
})
}
assert_eq!(sizes[1..], actual_sizes[..]);
}
}