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
use crate::Config;
use fuel_core_interfaces::{
    block_importer::ImportBlockMpsc,
    p2p::{
        BlockGossipData,
        P2pRequestEvent,
    },
    sync::SyncMpsc,
};
use parking_lot::Mutex;
use tokio::{
    sync::mpsc,
    task::JoinHandle,
};

pub struct Service {
    join: Mutex<Option<JoinHandle<()>>>,
    sender: mpsc::Sender<SyncMpsc>,
}

impl Service {
    pub async fn new(_config: &Config) -> anyhow::Result<Self> {
        let (sender, _receiver) = mpsc::channel(100);
        Ok(Self {
            sender,
            join: Mutex::new(None),
        })
    }

    pub async fn start(
        &self,
        _p2p_block: mpsc::Receiver<BlockGossipData>,
        _p2p_request: mpsc::Sender<P2pRequestEvent>,
        // TODO: re-introduce this when sync actually depends on the coordinator
        // _bft: mpsc::Sender<BftMpsc>,
        _block_importer: mpsc::Sender<ImportBlockMpsc>,
    ) {
        let mut join = self.join.lock();
        if join.is_none() {
            *join = Some(tokio::spawn(async {}));
        }
    }

    pub async fn stop(&self) -> Option<JoinHandle<()>> {
        let join = self.join.lock().take();
        if join.is_some() {
            let _ = self.sender.send(SyncMpsc::Stop);
        }
        join
    }

    pub fn sender(&self) -> &mpsc::Sender<SyncMpsc> {
        &self.sender
    }
}