clementine_core/task/
lcp_syncer.rs

1use std::sync::Arc;
2
3use crate::bitcoin_syncer::{BlockHandler, FinalizedBlockFetcherTask};
4use crate::builder::block_cache::BlockCache;
5use crate::citrea::CitreaClientT;
6use crate::config::protocol::ProtocolParamset;
7use crate::database::{Database, DatabaseTransaction};
8use crate::task::{RecoverableTask, Task, TaskVariant};
9use crate::verifier::Verifier;
10use clementine_errors::BridgeError;
11use tonic::async_trait;
12
13#[derive(Debug)]
14pub struct LcpSyncerTask<H: BlockHandler>(FinalizedBlockFetcherTask<H>);
15
16impl<H: BlockHandler> LcpSyncerTask<H> {
17    pub async fn new(
18        db: Database,
19        btc_syncer_consumer_id: String,
20        paramset: &'static ProtocolParamset,
21        handler: H,
22    ) -> Result<Self, BridgeError> {
23        let next_height = db
24            .get_next_finalized_block_height_for_consumer(None, &btc_syncer_consumer_id, paramset)
25            .await?;
26
27        Ok(Self(FinalizedBlockFetcherTask::new(
28            db,
29            btc_syncer_consumer_id,
30            paramset,
31            next_height,
32            handler,
33        )))
34    }
35}
36
37#[async_trait]
38impl<H: BlockHandler> Task for LcpSyncerTask<H> {
39    type Output = bool;
40    const VARIANT: TaskVariant = TaskVariant::LcpSyncer;
41
42    async fn run_once(&mut self) -> Result<Self::Output, BridgeError> {
43        self.0.run_once().await
44    }
45}
46
47#[async_trait]
48impl<H: BlockHandler> RecoverableTask for LcpSyncerTask<H> {
49    async fn recover_from_error(&mut self, error: &BridgeError) -> Result<(), BridgeError> {
50        self.0.recover_from_error(error).await
51    }
52}
53
54#[async_trait::async_trait]
55impl<C> crate::bitcoin_syncer::BlockHandler for Verifier<C>
56where
57    C: CitreaClientT,
58{
59    async fn handle_new_block(
60        &mut self,
61        dbtx: DatabaseTransaction<'_>,
62        block_id: u32,
63        block: bitcoin::Block,
64        height: u32,
65    ) -> Result<(), BridgeError> {
66        self.handle_finalized_block(
67            dbtx,
68            block_id,
69            height,
70            Arc::new(BlockCache::from_block(block, height)),
71            None,
72        )
73        .await
74    }
75}