zebra_scan/service/
scan_task.rs

1//! Types and method implementations for [`ScanTask`]
2
3use std::sync::Arc;
4
5use color_eyre::Report;
6use tokio::task::JoinHandle;
7
8use zebra_state::ChainTipChange;
9
10use crate::storage::Storage;
11
12mod commands;
13mod executor;
14pub mod scan;
15
16pub use commands::ScanTaskCommand;
17
18#[cfg(any(test, feature = "proptest-impl"))]
19pub mod tests;
20
21#[derive(Debug, Clone)]
22/// Scan task handle and command channel sender
23pub struct ScanTask {
24    /// [`JoinHandle`] of scan task
25    pub handle: Arc<JoinHandle<Result<(), Report>>>,
26
27    /// Task command channel sender
28    pub cmd_sender: tokio::sync::mpsc::Sender<ScanTaskCommand>,
29}
30
31/// The size of the command channel buffer
32const SCAN_TASK_BUFFER_SIZE: usize = 100;
33
34impl ScanTask {
35    /// Spawns a new [`ScanTask`].
36    pub fn spawn(db: Storage, state: scan::State, chain_tip_change: ChainTipChange) -> Self {
37        // TODO: Use a bounded channel or move this logic to the scan service or another service.
38        let (cmd_sender, cmd_receiver) = tokio::sync::mpsc::channel(SCAN_TASK_BUFFER_SIZE);
39
40        Self {
41            handle: Arc::new(scan::spawn_init(db, state, chain_tip_change, cmd_receiver)),
42            cmd_sender,
43        }
44    }
45}