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
use std::marker;
use crate::core::hash::{Hash, ZERO_HASH};
use crate::core::pmmr::pmmr::{bintree_rightmost, insertion_to_pmmr_index, peaks};
use crate::core::pmmr::{is_leaf, Backend};
use crate::ser::{PMMRIndexHashable, PMMRable};
pub struct ReadonlyPMMR<'a, T, B>
where
T: PMMRable,
B: Backend<T>,
{
last_pos: u64,
backend: &'a B,
_marker: marker::PhantomData<T>,
}
impl<'a, T, B> ReadonlyPMMR<'a, T, B>
where
T: PMMRable,
B: 'a + Backend<T>,
{
pub fn new(backend: &'a B) -> ReadonlyPMMR<'_, T, B> {
ReadonlyPMMR {
backend,
last_pos: 0,
_marker: marker::PhantomData,
}
}
pub fn at(backend: &'a B, last_pos: u64) -> ReadonlyPMMR<'_, T, B> {
ReadonlyPMMR {
backend,
last_pos,
_marker: marker::PhantomData,
}
}
pub fn get_data(&self, pos: u64) -> Option<T::E> {
if pos > self.last_pos {
None
} else if is_leaf(pos) {
self.backend.get_data(pos)
} else {
None
}
}
pub fn get_hash(&self, pos: u64) -> Option<Hash> {
if pos > self.last_pos {
None
} else if is_leaf(pos) {
self.backend.get_hash(pos)
} else {
self.backend.get_from_file(pos)
}
}
pub fn is_empty(&self) -> bool {
self.last_pos == 0
}
pub fn root(&self) -> Hash {
if self.is_empty() {
return ZERO_HASH;
}
let mut res = None;
for peak in self.peaks().iter().rev() {
res = match res {
None => Some(*peak),
Some(rhash) => Some((*peak, rhash).hash_with_index(self.unpruned_size())),
}
}
res.expect("no root, invalid tree")
}
pub fn peaks(&self) -> Vec<Hash> {
let peaks_pos = peaks(self.last_pos);
peaks_pos
.into_iter()
.filter_map(|pi| {
self.backend.get_from_file(pi)
})
.collect()
}
pub fn unpruned_size(&self) -> u64 {
self.last_pos
}
pub fn elements_from_insertion_index(
&self,
mut index: u64,
max_count: u64,
) -> (u64, Vec<T::E>) {
let mut return_vec = vec![];
if index == 0 {
index = 1;
}
let mut return_index = index;
let mut pmmr_index = insertion_to_pmmr_index(index);
while return_vec.len() < max_count as usize && pmmr_index <= self.last_pos {
if let Some(t) = self.get_data(pmmr_index) {
return_vec.push(t);
return_index = index;
}
index += 1;
pmmr_index = insertion_to_pmmr_index(index);
}
(return_index, return_vec)
}
pub fn get_last_n_insertions(&self, n: u64) -> Vec<(Hash, T::E)> {
let mut return_vec = vec![];
let mut last_leaf = self.last_pos;
for _ in 0..n as u64 {
if last_leaf == 0 {
break;
}
last_leaf = bintree_rightmost(last_leaf);
if let Some(hash) = self.backend.get_hash(last_leaf) {
if let Some(data) = self.backend.get_data(last_leaf) {
return_vec.push((hash, data));
}
}
last_leaf -= 1;
}
return_vec
}
}