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
use crate::{
backend::ArrayLike,
error::MerkleMountainRangeError,
functions::{prune_mutable_mmr, PrunedMutableMmr},
merkle_checkpoint::MerkleCheckPoint,
Hash,
MutableMmr,
};
use croaring::Bitmap;
use digest::Digest;
use std::ops::Deref;
#[derive(Debug, Clone, Copy)]
pub struct MmrCacheConfig {
pub rewind_hist_len: usize,
}
impl Default for MmrCacheConfig {
fn default() -> Self {
Self { rewind_hist_len: 100 }
}
}
#[derive(Debug)]
pub struct MmrCache<D, BaseBackend, CpBackend>
where
D: Digest,
BaseBackend: ArrayLike<Value = Hash>,
{
base_cp_index: usize,
curr_cp_index: usize,
base_mmr: MutableMmr<D, BaseBackend>,
pub curr_mmr: PrunedMutableMmr<D>,
checkpoints: CpBackend,
config: MmrCacheConfig,
}
impl<D, BaseBackend, CpBackend> MmrCache<D, BaseBackend, CpBackend>
where
D: Digest,
BaseBackend: ArrayLike<Value = Hash>,
CpBackend: ArrayLike<Value = MerkleCheckPoint>,
{
pub fn new(
base_mmr: BaseBackend,
checkpoints: CpBackend,
config: MmrCacheConfig,
) -> Result<MmrCache<D, BaseBackend, CpBackend>, MerkleMountainRangeError>
{
let base_mmr = MutableMmr::new(base_mmr, Bitmap::create());
let curr_mmr = prune_mutable_mmr::<D, _>(&base_mmr)?;
let mut mmr_cache = MmrCache {
base_cp_index: 0,
curr_cp_index: 0,
base_mmr,
curr_mmr,
checkpoints,
config,
};
mmr_cache.reset()?;
Ok(mmr_cache)
}
fn calculate_base_cp_index(&mut self) -> Result<usize, MerkleMountainRangeError> {
let cp_count = self
.checkpoints
.len()
.map_err(|e| MerkleMountainRangeError::BackendError(e.to_string()))?;
if cp_count > self.config.rewind_hist_len {
return Ok(cp_count - self.config.rewind_hist_len);
}
Ok(0)
}
fn create_base_mmr(&mut self) -> Result<(), MerkleMountainRangeError> {
self.base_mmr.clear()?;
self.base_cp_index = self.calculate_base_cp_index()?;
for cp_index in 0..=self.base_cp_index {
if let Some(cp) = self
.checkpoints
.get(cp_index)
.map_err(|e| MerkleMountainRangeError::BackendError(e.to_string()))?
{
cp.apply(&mut self.base_mmr)?;
}
}
Ok(())
}
fn create_curr_mmr(&mut self) -> Result<(), MerkleMountainRangeError> {
self.curr_cp_index = self
.checkpoints
.len()
.map_err(|e| MerkleMountainRangeError::BackendError(e.to_string()))?;
self.curr_mmr = prune_mutable_mmr::<D, _>(&self.base_mmr)?;
for cp_index in self.base_cp_index + 1..self.curr_cp_index {
if let Some(cp) = self
.checkpoints
.get(cp_index)
.map_err(|e| MerkleMountainRangeError::BackendError(e.to_string()))?
{
cp.apply(&mut self.curr_mmr)?;
}
}
Ok(())
}
fn update_base_mmr(&mut self) -> Result<(), MerkleMountainRangeError> {
let prev_cp_index = self.base_cp_index;
self.base_cp_index = self.calculate_base_cp_index()?;
if prev_cp_index < self.base_cp_index {
for cp_index in prev_cp_index + 1..=self.base_cp_index {
if let Some(cp) = self
.checkpoints
.get(cp_index)
.map_err(|e| MerkleMountainRangeError::BackendError(e.to_string()))?
{
cp.apply(&mut self.base_mmr)?;
}
}
} else {
self.create_base_mmr()?;
}
Ok(())
}
pub fn update(&mut self) -> Result<(), MerkleMountainRangeError> {
let cp_count = self
.checkpoints
.len()
.map_err(|e| MerkleMountainRangeError::BackendError(e.to_string()))?;
if cp_count < self.base_cp_index {
self.create_base_mmr()?;
self.create_curr_mmr()?;
} else if cp_count < self.curr_cp_index {
self.create_curr_mmr()?;
} else if cp_count > self.curr_cp_index {
self.update_base_mmr()?;
self.create_curr_mmr()?;
}
Ok(())
}
pub fn reset(&mut self) -> Result<(), MerkleMountainRangeError> {
self.create_base_mmr()?;
self.create_curr_mmr()
}
pub fn fetch_mmr_node(&self, leaf_index: u32) -> Result<(Option<Hash>, bool), MerkleMountainRangeError> {
let (base_hash, base_deleted) = self.base_mmr.get_leaf_status(leaf_index)?;
let (curr_hash, curr_deleted) = self.curr_mmr.get_leaf_status(leaf_index)?;
if let Some(base_hash) = base_hash {
return Ok((Some(base_hash), base_deleted | curr_deleted));
}
Ok((curr_hash, base_deleted | curr_deleted))
}
}
impl<D, BaseBackend, CpBackend> Deref for MmrCache<D, BaseBackend, CpBackend>
where
D: Digest,
BaseBackend: ArrayLike<Value = Hash>,
{
type Target = PrunedMutableMmr<D>;
fn deref(&self) -> &Self::Target {
&self.curr_mmr
}
}