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
57
58
59
60
61
62
63
64
65
66
use crate::db::{error::BlockNotFound, ExplorerDb};
use chain_impl_mockchain::block::{Block, HeaderId as HeaderHash};
use std::sync::Arc;
use thiserror::Error;
use tokio::sync::Mutex;

#[derive(Debug, Error)]
pub enum IndexerError {
    #[error("could not deserialize response")]
    CannotDeserialize(#[from] serde_json::Error),
    #[error("io error")]
    IOError(#[from] std::io::Error),
    #[error("hash error")]
    HashError(#[from] chain_crypto::hash::Error),
    #[error("url error")]
    UrlError(#[from] url::ParseError),
    #[error(transparent)]
    DbError(#[from] crate::db::error::ExplorerError),
}

#[derive(Clone)]
pub struct Indexer {
    pub db: ExplorerDb,
    tip_candidate: Arc<Mutex<Option<HeaderHash>>>,
}

impl Indexer {
    pub fn new(db: crate::db::ExplorerDb) -> Self {
        let tip_candidate = Arc::new(Mutex::new(None));
        Indexer { db, tip_candidate }
    }

    pub async fn apply_block(&self, block: Block) -> Result<(), IndexerError> {
        tracing::info!("applying {}", block.header().id());

        // TODO: technically this could dispatch a task, as there is a possibility of applying
        // blocks (siblings) in parallel, but that is a mission for another day.  biggest concern
        // is that the we receive two consecutive blocks, if the first is really big and costly to
        // apply, we may try to apply the next one too soon...
        let _state_ref = self.db.apply_block(block.clone()).await?;

        let mut guard = self.tip_candidate.lock().await;
        if guard
            .map(|hash| hash == block.header().id())
            .unwrap_or(false)
        {
            let hash = guard.take().unwrap();
            self.set_tip(hash).await;
        }

        Ok(())
    }

    pub async fn set_tip(&self, tip: HeaderHash) {
        match self.db.set_tip(tip).await {
            Ok(_) => {
                tracing::info!("tip set to {}", tip);
            }
            Err(BlockNotFound { hash: _ }) => {
                // we don't use the value in the error since `tip` is copy anyway
                let mut guard = self.tip_candidate.lock().await;
                guard.replace(tip);
            }
        }
    }
}