-
Notifications
You must be signed in to change notification settings - Fork 40
/
utils.rs
56 lines (50 loc) · 1.79 KB
/
utils.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
use pallas::network::{
miniprotocols::{chainsync::TipFinder, run_agent, Point},
multiplexer::StdChannelBuffer,
};
use crate::{crosscut, storage};
pub fn find_end_of_chain(
chain: &crosscut::ChainWellKnownInfo,
channel: &mut StdChannelBuffer,
) -> Result<Point, crate::Error> {
let point = Point::Specific(
chain.shelley_known_slot,
hex::decode(&chain.shelley_known_hash)
.map_err(|_| crate::Error::config("can't decode shelley known hash"))?,
);
let agent = TipFinder::initial(point);
let agent = run_agent(agent, channel).map_err(crate::Error::ouroboros)?;
match agent.output {
Some(tip) => Ok(tip.0),
None => Err(crate::Error::message("failure acquiring end of chain")),
}
}
pub fn define_chainsync_start(
chain: &crosscut::ChainWellKnownInfo,
intersect: &crosscut::IntersectConfig,
cursor: &mut storage::Cursor,
channel: &mut StdChannelBuffer,
) -> Result<Option<Vec<Point>>, crate::Error> {
match cursor.last_point()? {
Some(x) => {
log::info!("found existing cursor in storage plugin: {:?}", x);
return Ok(Some(vec![x.clone().try_into()?]));
}
None => log::info!("no cursor found in storage plugin"),
};
match &intersect {
crosscut::IntersectConfig::Origin => Ok(None),
crosscut::IntersectConfig::Tip => {
let tip = find_end_of_chain(chain, channel)?;
Ok(Some(vec![tip]))
}
crosscut::IntersectConfig::Point(_, _) => {
let point = intersect.get_point().expect("point value");
Ok(Some(vec![point]))
}
crosscut::IntersectConfig::Fallbacks(_) => {
let points = intersect.get_fallbacks().expect("fallback values");
Ok(Some(points))
}
}
}