-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmithril_snapshot_iterator.rs
275 lines (236 loc) · 9.65 KB
/
mithril_snapshot_iterator.rs
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
263
264
265
266
267
268
269
270
271
272
273
274
275
//! Internal Mithril snapshot iterator functions.
use std::{
fmt::Debug,
path::{Path, PathBuf},
sync::{Arc, Mutex},
};
use cardano_blockchain_types::{Fork, MultiEraBlock, Network, Point};
use logcall::logcall;
use tokio::task;
use tracing::{debug, error};
use tracing_log::log;
use crate::{
error::{Error, Result},
mithril_query::{make_mithril_iterator, ImmutableBlockIterator},
};
/// Search backwards by 60 slots (seconds) looking for a previous block.
/// This search window is doubled until the search succeeds.
const BACKWARD_SEARCH_SLOT_INTERVAL: u64 = 60;
/// Synchronous Inner Iterator state
struct MithrilSnapshotIteratorInner {
/// The chain being iterated
chain: Network,
/// Where we really want to start iterating from
start: Point,
/// Previous iteration point.
previous: Point,
/// Inner iterator.
inner: ImmutableBlockIterator,
}
impl Debug for MithrilSnapshotIteratorInner {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"MithrilSnapshotIteratorInner {{ chain: {:?}, start: {:?}, previous: {:?} }}",
self.chain, self.start, self.previous
)
}
}
/// Wraps the iterator type returned by Pallas.
#[derive(Debug)]
pub(crate) struct MithrilSnapshotIterator {
/// Mithril snapshot directory path
path: PathBuf,
/// Inner Mutable Synchronous Iterator State
inner: Arc<Mutex<MithrilSnapshotIteratorInner>>,
}
/// Create a probe point used in iterations to find the start when its not exactly known.
pub(crate) fn probe_point(point: &Point, distance: u64) -> Point {
/// Step back point to indicate origin.
const STEP_BACK_ORIGIN: u64 = 0;
// Now that we have the tip, step back about 4 block intervals from tip, and do a fuzzy
// iteration to find the exact two blocks at the end of the immutable chain.
// It is ok because slot implement saturating subtraction.
#[allow(clippy::arithmetic_side_effects)]
let step_back_search = point.slot_or_default() - distance.into();
// We stepped back to the origin, so just return Origin
if step_back_search == STEP_BACK_ORIGIN.into() {
return Point::ORIGIN;
}
// Create a fuzzy search probe by making the hash zero length.
Point::fuzzy(step_back_search)
}
impl MithrilSnapshotIterator {
/// Returns `true` if the `MithrilSnapshotIterator` could read data without any issues
/// (underlying mithril snapshot directory exists)
pub(crate) fn is_valid(&self) -> bool {
self.path.exists()
}
/// Try and probe to establish the iterator from the desired point.
async fn try_fuzzy_iterator(
chain: Network, path: &Path, from: &Point, search_interval: u64,
) -> Option<MithrilSnapshotIterator> {
let point = probe_point(from, search_interval);
let Ok(mut iterator) = make_mithril_iterator(path, &point, chain).await else {
return None;
};
let mut previous = None;
let mut this = None;
loop {
let next = iterator.next();
match next {
Some(Ok(raw_block)) => {
let Ok(block) = pallas::ledger::traverse::MultiEraBlock::decode(&raw_block)
else {
return None;
};
let point = Point::new(block.slot().into(), block.hash().into());
previous = this;
this = Some(point.clone());
debug!("Searching for {from}. {this:?} > {previous:?}");
// Stop as soon as we find the point, or exceed it.
if point >= *from {
break;
}
},
Some(Err(err)) => {
error!("Error while iterating fuzzy mithril data: {}", err);
return None;
},
None => break,
};
}
debug!("Best Found for {from}. {this:?} > {previous:?}");
// Fail if we didn't find the destination block, or its immediate predecessor.
previous.as_ref()?;
let this = this?;
// Remake the iterator, based on the new known point.
let Ok(iterator) = make_mithril_iterator(path, &this, chain).await else {
return None;
};
Some(MithrilSnapshotIterator {
path: path.to_path_buf(),
inner: Arc::new(Mutex::new(MithrilSnapshotIteratorInner {
chain,
start: this,
previous: previous?,
inner: iterator,
})),
})
}
/// Do a fuzzy search to establish the iterator.
/// We use this when we don't know the previous point, and need to find it.
#[allow(clippy::indexing_slicing)]
#[logcall("debug")]
async fn fuzzy_iterator(chain: Network, path: &Path, from: &Point) -> MithrilSnapshotIterator {
let mut backwards_search = BACKWARD_SEARCH_SLOT_INTERVAL;
loop {
if let Some(iterator) =
Self::try_fuzzy_iterator(chain, path, from, backwards_search).await
{
return iterator;
}
backwards_search = backwards_search.saturating_mul(2);
}
}
/// Create a mithril iterator, optionally where we know the previous point.
///
/// # Arguments
///
/// `chain`: The blockchain network to iterate.
/// `from`: The point to start iterating from. If the `Point` does not contain a
/// hash, the iteration start is fuzzy. `previous`: The previous point we are
/// iterating, if known. If the previous is NOT known, then the first block
/// yielded by the iterator is discarded and becomes the known previous.
#[allow(clippy::indexing_slicing)]
#[logcall(ok = "debug", err = "error")]
pub(crate) async fn new(
chain: Network, path: &Path, from: &Point, previous_point: Option<Point>,
) -> Result<Self> {
if from.is_fuzzy() || (!from.is_origin() && previous_point.is_none()) {
return Ok(Self::fuzzy_iterator(chain, path, from).await);
}
let previous = if from.is_origin() {
Point::ORIGIN
} else {
let Some(previous) = previous_point else {
return Err(Error::Internal);
};
previous
};
debug!("Actual Mithril Iterator Start: {}", from);
let iterator = make_mithril_iterator(path, from, chain).await?;
Ok(MithrilSnapshotIterator {
path: path.to_path_buf(),
inner: Arc::new(Mutex::new(MithrilSnapshotIteratorInner {
chain,
start: from.clone(),
previous,
inner: iterator,
})),
})
}
/// Get the next block, in a way that is Async friendly.
/// Returns the next block, or None if there are no more blocks.
pub(crate) async fn next(&self) -> Option<MultiEraBlock> {
let inner = self.inner.clone();
let res = task::spawn_blocking(move || {
#[allow(clippy::expect_used)]
let mut inner_iterator = inner
.lock()
.expect("Safe here because the lock can't be poisoned");
inner_iterator.next()
})
.await;
res.unwrap_or_default()
}
}
impl Iterator for MithrilSnapshotIteratorInner {
type Item = MultiEraBlock;
fn next(&mut self) -> Option<Self::Item> {
for maybe_block in self.inner.by_ref() {
if let Ok(block) = maybe_block {
if !self.previous.is_unknown() {
// We can safely fully decode this block.
match MultiEraBlock::new(self.chain, block, &self.previous, Fork::IMMUTABLE) {
Ok(block_data) => {
// Update the previous point
// debug!("Pre Previous update 1 : {:?}", self.previous);
self.previous = block_data.point();
// debug!("Post Previous update 1 : {:?}", self.previous);
// Make sure we got to the start, otherwise this could be a block
// artifact from a discover previous point
// search.
if block_data < self.start {
continue;
}
return Some(block_data);
},
Err(error) => {
error!(previous=%self.previous, error=%error, "Error decoding a block from the snapshot");
break;
},
}
}
// We cannot fully decode this block because we don't know its previous point,
// So this MUST be the first block in iteration, so use it as the previous.
if let Ok(raw_decoded_block) =
pallas::ledger::traverse::MultiEraBlock::decode(&block)
{
// debug!("Pre Previous update 2 : {:?}", self.previous);
self.previous = Point::new(
raw_decoded_block.slot().into(),
raw_decoded_block.hash().into(),
);
// debug!("Post Previous update 2 : {:?}", self.previous);
continue;
}
error!("Error decoding block to use for previous from the snapshot.");
break;
}
error!("Error while fetching a block from the snapshot");
break;
}
None
}
}