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 lifeline::dyn_bus::DynBus;
use lifeline::{Bus, Sender};

use component_state::state::{BridgeState, StateOptions};
use support_common::config::{Config, Names};
use support_lifeline::task::TaskStack;

use crate::bridge::DarwiniaEthereumBus;
use crate::bridge::DarwiniaEthereumConfig;
use crate::bridge::{DarwiniaEthereumMessage, EthereumScanMessage};
use crate::service::affirm::AffirmService;
use crate::service::check::CheckService;
use crate::service::darwinia::DarwiniaService;
use crate::service::extrinsics::ExtrinsicsService;
use crate::service::guard::GuardService;
use crate::service::redeem::RedeemService;

#[derive(Debug)]
pub struct DarwiniaEthereumTask {
    stack: TaskStack<DarwiniaEthereumBus>,
}

impl DarwiniaEthereumTask {
    pub fn name() -> &'static str {
        "darwinia-ethereum"
    }
}

impl DarwiniaEthereumTask {
    pub async fn new() -> color_eyre::Result<Self> {
        let state = BridgeState::new(StateOptions {
            db_name: Self::name().to_string(),
        })?;
        // check config
        let _bridge_config: DarwiniaEthereumConfig =
            Config::restore(Names::BridgeDarwiniaEthereum)?;
        let microkv = state.microkv();

        crate::migrate::migrate(microkv, 3)?;

        let bus = DarwiniaEthereumBus::default();
        bus.store_resource::<BridgeState>(state);

        let mut stack = TaskStack::new(bus);
        stack.spawn_service::<AffirmService>()?;
        stack.spawn_service::<CheckService>()?;
        stack.spawn_service::<RedeemService>()?;
        stack.spawn_service::<GuardService>()?;
        stack.spawn_service::<DarwiniaService>()?;
        stack.spawn_service::<ExtrinsicsService>()?;

        let mut tx_scan = stack.bus().tx::<DarwiniaEthereumMessage>()?;
        tx_scan
            .send(DarwiniaEthereumMessage::Scan(EthereumScanMessage::Start))
            .await?;

        Ok(Self { stack })
    }
}

impl DarwiniaEthereumTask {
    #[allow(dead_code)]
    pub fn stack(&self) -> &TaskStack<DarwiniaEthereumBus> {
        &self.stack
    }
}