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
67
68
69
70
71
72
73
74
75
76
use lifeline::{Lifeline, Service, Task};
use relay_s2s::subscribe::SubscribeJustification;
use relay_s2s::types::JustificationInput;

use support_common::config::{Config, Names};
use support_lifeline::service::BridgeService;

use crate::bridge::{BridgeBus, BridgeConfig, BridgeTask};

#[derive(Debug)]
pub struct SubscribeService {
    _greet_crab: Lifeline,
    _greet_kusama: Lifeline,
}

impl BridgeService for SubscribeService {}

impl Service for SubscribeService {
    type Bus = BridgeBus;
    type Lifeline = color_eyre::Result<Self>;

    fn spawn(_bus: &Self::Bus) -> Self::Lifeline {
        let _greet_crab = Self::try_task(
            &format!("{}-subscribe-crab", BridgeTask::name()),
            async move {
                while let Err(e) = start_crab().await {
                    tracing::error!(target: "crab-crabparachain", "[subscribe] [crab] Failed to start subscribe {:?}", e);
                    tokio::time::sleep(std::time::Duration::from_secs(5)).await;
                    tracing::info!(target: "crab-crabparachain", "[subscribe] [crab] Try to restart subscription service.");
                }
                Ok(())
            },
        );
        let _greet_kusama = Self::try_task(
            &format!("{}-subscribe-kusama", BridgeTask::name()),
            async move {
                while let Err(e) = start_kusama().await {
                    tracing::error!(target: "crab-crabparachain", "[subscribe] [kusama] Failed to start subscribe {:?}", e);
                    tokio::time::sleep(std::time::Duration::from_secs(5)).await;
                    tracing::info!(target: "crab-crabparachain", "[subscribe] [kusama] Try to restart subscription service.");
                }
                Ok(())
            },
        );
        Ok(Self {
            _greet_crab,
            _greet_kusama,
        })
    }
}

async fn start_crab() -> color_eyre::Result<()> {
    let bridge_config: BridgeConfig = Config::restore(Names::BridgeCrabCrabParachain)?;

    let client_crab = bridge_config.crab.to_crab_client().await?;

    let input = JustificationInput {
        client: client_crab,
    };
    let subscribe = SubscribeJustification::new(input);
    subscribe.start().await?;
    Ok(())
}

async fn start_kusama() -> color_eyre::Result<()> {
    let bridge_config: BridgeConfig = Config::restore(Names::BridgeCrabCrabParachain)?;

    let client_kusama = bridge_config.kusama.to_kusama_client().await?;

    let input = JustificationInput {
        client: client_kusama,
    };
    let subscribe = SubscribeJustification::new(input);
    subscribe.start().await?;
    Ok(())
}