mod client;
pub use client::*;
use crate::connection::InteractionContext;
use crate::error::Result;
use crate::schema::{Block, Point, Tip, responses::{FindIntersectionResponse, NextBlockResponse}};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Intersection {
pub point: Point,
pub tip: Tip,
}
pub trait ChainSynchronizationMessageHandlers: Send + Sync {
fn on_roll_forward(&mut self, block: Block, tip: Tip) -> Result<()>;
fn on_roll_backward(&mut self, point: Point, tip: Tip) -> Result<()>;
}
pub async fn find_intersection(
context: &InteractionContext,
points: Vec<Point>,
) -> Result<Intersection> {
#[derive(Serialize)]
struct Params {
points: Vec<Point>,
}
let response: FindIntersectionResponse = context
.request("findIntersection", Some(Params { points }))
.await?;
if let Some(point) = response.intersection {
Ok(Intersection {
point,
tip: response.tip,
})
} else {
Err(crate::error::OgmiosError::IntersectionNotFound {
tip: Some(format!("{:?}", response.tip)),
})
}
}
pub async fn next_block(context: &InteractionContext) -> Result<NextBlockResponse> {
context.request("nextBlock", None::<()>).await
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_intersection_creation() {
let intersection = Intersection {
point: Point::origin(),
tip: Tip::Origin("origin".to_string()),
};
assert!(matches!(intersection.point, Point::Origin(_)));
}
}