cardano_net/
chainsync.rs

1use cardano_sdk::protocol::{ChainSync, ChainSyncKind, Point, Points, Tip};
2
3use super::channel::Channel;
4use super::packet::ProtocolError;
5
6pub enum ChainIntersection {
7    Found(Point, Tip),
8    NotFound(Tip),
9}
10
11impl Channel<ChainSync> {
12    /// Perform the Chainsync `FindIntersect` sub protocol packet with the opposite side
13    pub async fn intersect(
14        &mut self,
15        points: Vec<Point>,
16    ) -> Result<ChainIntersection, ProtocolError> {
17        self.tx(false, ChainSync::FindIntersect(Points::from(points)))
18            .await?;
19        match self.rx().await? {
20            ChainSync::IntersectionFound(point, tip) => Ok(ChainIntersection::Found(point, tip)),
21            ChainSync::IntersectionNotFound(tip) => Ok(ChainIntersection::NotFound(tip)),
22            cs => Err(ProtocolError::UnexpectedVariant(
23                "chain sync (find-intersection)".to_string(),
24                format!("{:?}", ChainSyncKind::from(cs)),
25            )),
26        }
27    }
28
29    pub async fn get_tip(&mut self) -> Result<Tip, ProtocolError> {
30        let points = vec![Point::Origin];
31        self.tx(false, ChainSync::FindIntersect(Points::from(points)))
32            .await?;
33        match self.rx().await? {
34            ChainSync::IntersectionFound(_point, tip) => Ok(tip),
35            ChainSync::IntersectionNotFound(tip) => Ok(tip),
36            cs => Err(ProtocolError::UnexpectedVariant(
37                "chain sync (find-intersection for get_tip)".to_string(),
38                format!("{:?}", ChainSyncKind::from(cs)),
39            )),
40        }
41    }
42}