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

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

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

    pub async fn start(
        &self,
        _p2p_consensus: mpsc::Sender<P2pRequestEvent>,
        _block_importer_sender: mpsc::Sender<ImportBlockMpsc>,
        _block_importer_broadcast: broadcast::Receiver<ImportBlockBroadcast>,
    ) {
        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(BftMpsc::Stop);
        }
        join
    }

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