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 std::sync::Arc;

use async_trait::async_trait;
use slog::Logger;
use tokio::sync::Mutex;

use crate::cardano_block_scanner::{BlockScanner, BlockStreamer, RawCardanoPoint};
use crate::chain_reader::ChainBlockReader;
use crate::entities::BlockNumber;
use crate::StdResult;

use super::ChainReaderBlockStreamer;

/// Cardano block scanner
///
/// This scanner reads the blocks with a chain block reader
pub struct CardanoBlockScanner {
    chain_reader: Arc<Mutex<dyn ChainBlockReader>>,
    max_roll_forwards_per_poll: usize,
    logger: Logger,
}

impl CardanoBlockScanner {
    /// Factory
    pub fn new(
        chain_reader: Arc<Mutex<dyn ChainBlockReader>>,
        max_roll_forwards_per_poll: usize,
        logger: Logger,
    ) -> Self {
        Self {
            chain_reader,
            max_roll_forwards_per_poll,
            logger,
        }
    }
}

#[async_trait]
impl BlockScanner for CardanoBlockScanner {
    async fn scan(
        &self,
        from: Option<RawCardanoPoint>,
        until: BlockNumber,
    ) -> StdResult<Box<dyn BlockStreamer>> {
        Ok(Box::new(
            ChainReaderBlockStreamer::try_new(
                self.chain_reader.clone(),
                from,
                until,
                self.max_roll_forwards_per_poll,
                self.logger.clone(),
            )
            .await?,
        ))
    }
}