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
use crate::{
is_node_right, nodes::MerkleNode, payloads::RangeMap, positions::Position, sibling_index,
};
use decanter::prelude::{hasher, Hashable, H256};
use digest::Digest;
use serde::{Deserialize, Serialize};
use std::convert::From;
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
pub struct MerkleMountainRange<T: Hashable> {
data: RangeMap<T>,
mmr: Vec<MerkleNode>, position: Position,
}
impl<T: Hashable> MerkleMountainRange<T> {
pub fn new(mmr: Vec<MerkleNode>, data: RangeMap<T>, position: Position) -> Self {
Self {
mmr,
data,
position,
}
}
pub fn get_object(&self, hash: &H256) -> Option<&T> {
self.data.0.get(hash)
}
pub fn get_mut_object(&mut self, hash: &H256) -> Option<&mut T> {
self.data.0.get_mut(hash)
}
pub fn get_hash(&self, index: usize) -> Option<H256> {
if index > self.get_last_added_index() {
return None;
};
Some(self.mmr[index].hash)
}
pub fn get_hash_proof<D: Digest>(&self, hash: &H256) -> Vec<H256> {
let mut result = Vec::new();
let mut i = self.mmr.len();
for counter in 0..self.mmr.len() {
if self.mmr[counter].hash == *hash {
i = counter;
break;
}
}
if i == self.mmr.len() {
return result;
};
self.get_ordered_hash_proof(i, &mut result);
if self.position.index == self.get_last_added_index() {
return result;
}
let mut peaks = self.bag_mmr();
let mut i = peaks.len();
let mut was_on_correct_height = false;
while i > 1 {
if was_on_correct_height {
result.push(peaks[i - 2]);
result.push(peaks[i - 1]);
} else if peaks[i - 1] == result[result.len() - 1] {
result.insert(result.len() - 1, peaks[i - 2]);
was_on_correct_height = true;
} else if peaks[i - 2] == result[result.len() - 1] {
result.push(peaks[i - 1]);
was_on_correct_height = true;
}
let mut hasher = D::new();
hasher.update(peaks[i - 2]);
hasher.update(peaks[i - 1]);
peaks[i - 2] = hasher.finalize().to_vec().into();
i -= 1;
}
let mut hasher = D::new();
hasher.update(self.mmr[self.position.index].hash);
hasher.update(peaks[0]);
if was_on_correct_height {
result.push(self.mmr[self.position.index].hash);
}
result.push(peaks[0]);
result.push(hasher.finalize().to_vec().into());
result
}
fn get_ordered_hash_proof(&self, index: usize, results: &mut Vec<H256>) {
let sibling = sibling_index(index);
let mut next_index = index + 1;
if sibling >= self.mmr.len() {
results.push(self.mmr[index].hash);
return;
}
if sibling < index {
results.push(self.mmr[sibling].hash);
results.push(self.mmr[index].hash);
} else {
results.push(self.mmr[index].hash);
results.push(self.mmr[sibling].hash);
next_index = sibling + 1;
}
self.get_ordered_hash_proof(next_index, results);
}
pub fn verify_proof<D: Digest>(&self, hashes: &Vec<H256>) -> bool {
if hashes.is_empty() {
return false;
}
if self.get_object(&hashes[0]).is_none() && self.get_object(&hashes[1]).is_none() {
return false;
}
let proof = self.get_hash_proof::<D>(&hashes[0]);
hashes.eq(&proof)
}
fn calc_peak_height(&self) -> (usize, usize) {
let mut height_counter = 0;
let mmr_len = self.get_last_added_index();
let mut index: usize = (1 << (height_counter + 2)) - 2;
let mut actual_height_index = 0;
while mmr_len >= index {
height_counter += 1;
actual_height_index = index;
index = (1 << (height_counter + 2)) - 2;
}
(height_counter, actual_height_index)
}
pub fn get_peak_height(&self) -> usize {
self.position.height
}
pub fn get_merkle_root<D: Digest>(&self) -> H256 {
let mut peaks = self.bag_mmr();
let mut i = peaks.len();
while i > 1 {
let mut hasher = D::new();
hasher.update(peaks[i - 2]);
hasher.update(peaks[i - 1]);
peaks[i - 2] = hasher.finalize().to_vec().into();
i -= 1;
}
if !peaks.is_empty() {
let mut hasher = D::new();
hasher.update(self.mmr[self.position.index].hash);
hasher.update(peaks[0]);
return hasher.finalize().to_vec().into();
}
self.mmr[self.position.index].hash
}
pub fn add_vec<D: Digest>(&mut self, objects: Vec<T>) {
for object in objects {
self.add_single::<D>(object);
}
}
pub fn add_single<D: Digest>(&mut self, object: T) {
let node_hash = object.hash();
let node = MerkleNode::from(node_hash);
self.data.0.insert(node_hash, object);
self.mmr.push(node);
if is_node_right(self.get_last_added_index()) {
self.add_single_no_leaf::<D>(self.get_last_added_index())
}
}
fn add_single_no_leaf<D: Digest>(&mut self, index: usize) {
let mut hasher = D::new();
hasher.update(self.mmr[sibling_index(index)].hash);
hasher.update(self.mmr[index].hash);
let new_hash: H256 = hasher.finalize().to_vec().into();
let new_node = MerkleNode::from(new_hash);
self.mmr.push(new_node);
if is_node_right(self.get_last_added_index()) {
self.add_single_no_leaf::<D>(self.get_last_added_index())
} else {
self.position = self.calc_peak_height().into(); }
}
fn get_last_added_index(&self) -> usize {
self.mmr.len() - 1
}
fn bag_mmr(&self) -> Vec<H256> {
let mut peaks = Vec::new();
self.find_bagging_indexes(self.position.height as i64, self.position.index, &mut peaks);
peaks
}
fn find_bagging_indexes(&self, mut height: i64, index: usize, peaks: &mut Vec<H256>) {
let mut new_index = index + (1 << (height + 1)) - 1; while (new_index > self.get_last_added_index()) && (height > 0) {
new_index -= 1 << height;
height -= 1;
}
if (new_index <= self.get_last_added_index()) && (height >= 0) {
peaks.push(self.mmr[new_index].hash);
self.find_bagging_indexes(height, new_index, peaks); }
}
}
impl<T: Hashable + ToString + Serialize> Hashable for MerkleMountainRange<T> {
fn hash(&self) -> H256 {
hasher(&self).as_slice().to_owned().into()
}
}
impl<T: Hashable + Serialize> std::fmt::Display for MerkleMountainRange<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", serde_json::to_string(&self).unwrap())
}
}