ogmios_client/chain_synchronization/
mod.rs1mod client;
7
8pub use client::*;
9
10use crate::connection::InteractionContext;
11use crate::error::Result;
12use crate::schema::{Block, Point, Tip, responses::{FindIntersectionResponse, NextBlockResponse}};
13use serde::{Deserialize, Serialize};
14
15#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
17#[serde(rename_all = "camelCase")]
18pub struct Intersection {
19 pub point: Point,
21 pub tip: Tip,
23}
24
25pub trait ChainSynchronizationMessageHandlers: Send + Sync {
29 fn on_roll_forward(&mut self, block: Block, tip: Tip) -> Result<()>;
31
32 fn on_roll_backward(&mut self, point: Point, tip: Tip) -> Result<()>;
34}
35
36pub async fn find_intersection(
50 context: &InteractionContext,
51 points: Vec<Point>,
52) -> Result<Intersection> {
53 #[derive(Serialize)]
54 struct Params {
55 points: Vec<Point>,
56 }
57
58 let response: FindIntersectionResponse = context
59 .request("findIntersection", Some(Params { points }))
60 .await?;
61
62 if let Some(point) = response.intersection {
63 Ok(Intersection {
64 point,
65 tip: response.tip,
66 })
67 } else {
68 Err(crate::error::OgmiosError::IntersectionNotFound {
69 tip: Some(format!("{:?}", response.tip)),
70 })
71 }
72}
73
74pub async fn next_block(context: &InteractionContext) -> Result<NextBlockResponse> {
87 context.request("nextBlock", None::<()>).await
88}
89
90#[cfg(test)]
91mod tests {
92 use super::*;
93
94 #[test]
95 fn test_intersection_creation() {
96 let intersection = Intersection {
97 point: Point::origin(),
98 tip: Tip::Origin("origin".to_string()),
99 };
100 assert!(matches!(intersection.point, Point::Origin(_)));
101 }
102}