mithril_cardano_node_chain/chain_scanner/interface.rs
1use async_trait::async_trait;
2
3use mithril_common::StdResult;
4use mithril_common::entities::{BlockNumber, SlotNumber};
5
6use crate::entities::{RawCardanoPoint, ScannedBlock};
7
8/// A scanner that can read cardano transactions in a cardano database
9///
10/// If you want to mock it using mockall:
11/// ```
12/// mod test {
13/// use std::path::Path;
14///
15/// use anyhow::anyhow;
16/// use async_trait::async_trait;
17/// use mockall::mock;
18///
19/// use mithril_common::entities::{BlockNumber};
20/// use mithril_common::StdResult;
21///
22/// use mithril_cardano_node_chain::chain_scanner::{BlockScanner, BlockStreamer};
23/// use mithril_cardano_node_chain::entities::{RawCardanoPoint};
24///
25/// mock! {
26/// pub BlockScannerImpl { }
27///
28/// #[async_trait]
29/// impl BlockScanner for BlockScannerImpl {
30/// async fn scan(
31/// &self,
32/// from: Option<RawCardanoPoint>,
33/// until: BlockNumber,
34/// ) -> StdResult<Box<dyn BlockStreamer>>;
35/// }
36/// }
37///
38/// #[test]
39/// fn test_mock() {
40/// let mut mock = MockBlockScannerImpl::new();
41/// mock.expect_scan().return_once(|_, _| {
42/// Err(anyhow!("parse error"))
43/// });
44/// }
45/// }
46/// ```
47#[async_trait]
48pub trait BlockScanner: Sync + Send {
49 /// Scan the transactions
50 async fn scan(
51 &self,
52 from: Option<RawCardanoPoint>,
53 until: BlockNumber,
54 ) -> StdResult<Box<dyn BlockStreamer>>;
55}
56
57/// [ChainScannedBlocks] allows to scan new blocks and handle rollbacks
58#[derive(Debug, Clone, PartialEq)]
59pub enum ChainScannedBlocks {
60 /// Roll forward on the chain to the next list of [ScannedBlock]
61 RollForwards(Vec<ScannedBlock>),
62 /// Roll backward on the chain to the previous [SlotNumber]
63 RollBackward(SlotNumber),
64}
65
66/// Trait that define how blocks are streamed from a Cardano database
67#[async_trait]
68pub trait BlockStreamer: Sync + Send {
69 /// Stream the next available blocks
70 async fn poll_next(&mut self) -> StdResult<Option<ChainScannedBlocks>>;
71
72 /// Get the last polled point of the chain
73 fn last_polled_point(&self) -> Option<RawCardanoPoint>;
74}