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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
use std::{ops::Div, str::FromStr, time::Duration};

use crate::{
    bridge::{BridgeBus, BridgeConfig},
    goerli_client::{client::GoerliClient, types::FinalityUpdate},
    pangoro_client::client::PangoroClient,
    web3_helper::wait_for_transaction_confirmation,
};
use client_contracts::beacon_light_client::FinalizedHeaderUpdate;
use lifeline::{Lifeline, Service, Task};
use support_common::config::{Config, Names};
use support_common::error::BridgerError;
use support_lifeline::service::BridgeService;
use web3::{
    ethabi::ethereum_types::H32,
    types::{Bytes, H256},
};

#[derive(Debug)]
pub struct GoerliToPangoroHeaderRelayService {
    _greet: Lifeline,
}

impl BridgeService for GoerliToPangoroHeaderRelayService {}

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

    fn spawn(_bus: &Self::Bus) -> Self::Lifeline {
        let _greet = Self::try_task("header-goerli-to-pangoro", async move {
            while let Err(error) = start().await {
                tracing::error!(
                    target: "pangoro-goerli",
                    "Failed to start goerli-to-pangoro header relay service, restart after some seconds: {:?}",
                    error
                );
                tokio::time::sleep(std::time::Duration::from_secs(10)).await;
            }
            Ok(())
        });
        Ok(Self { _greet })
    }
}

async fn start() -> color_eyre::Result<()> {
    let config: BridgeConfig = Config::restore(Names::BridgePangoroGoerli)?;
    let pangoro_client = PangoroClient::new(
        &config.pangoro_evm.endpoint,
        &config.pangoro_evm.contract_address,
        &config.pangoro_evm.execution_layer_contract_address,
        &config.pangoro_evm.private_key,
        config.pangoro_evm.gas_option(),
    )?;
    let goerli_client = GoerliClient::new(&config.goerli.endpoint)?;
    let header_relay = HeaderRelay {
        pangoro_client,
        goerli_client,
    };

    loop {
        if let Err(error) = header_relay.header_relay().await {
            tracing::error!(
                target: "pangoro-goerli",
                "Failed relay header : {:?}",
                error
            );
            return Err(error);
        }
        tokio::time::sleep(std::time::Duration::from_secs(10)).await;
    }
}

#[derive(Debug)]
pub struct HeaderRelayState {
    // Latest relayed header slot at Pangoro
    pub relayed_slot: u64,
    // Latest relayed period at Pangoro
    pub relayed_period: u64,
    // Latest header slot at Goerli
    pub current_slot: u64,
    // Latest period at Goerli
    pub current_period: u64,
}

pub struct HeaderRelay {
    pub pangoro_client: PangoroClient,
    pub goerli_client: GoerliClient,
}

impl HeaderRelay {
    pub async fn get_state(&self) -> color_eyre::Result<HeaderRelayState> {
        let relayed = self
            .pangoro_client
            .beacon_light_client
            .finalized_header()
            .await?;
        let current_head = self.goerli_client.get_header("head").await?;
        let current_slot = current_head.header.message.slot;
        let current_period = current_slot.div(32).div(256);
        let relayed_period = relayed.slot.div(32).div(256);
        Ok(HeaderRelayState {
            relayed_slot: relayed.slot,
            relayed_period,
            current_slot,
            current_period,
        })
    }

    pub async fn header_relay(&self) -> color_eyre::Result<()> {
        let state = self.get_state().await?;
        let next_sync_aggregate_root = self
            .pangoro_client
            .beacon_light_client
            .sync_committee_roots(state.relayed_period + 1)
            .await?;
        tracing::info!(
            target: "pangoro-goerli",
            "[Header][Goerli=>Pangoro] State: {:?}",
            state
        );
        if state.current_period == state.relayed_period {
            self.relay_latest(state).await
        } else {
            if next_sync_aggregate_root.is_zero() {
                return Ok(());
            }
            if state.current_period == state.relayed_period + 1 {
                self.relay_latest(state).await
            } else {
                self.relay_next_period(state).await
            }
        }
    }

    pub async fn relay_latest(&self, state: HeaderRelayState) -> color_eyre::Result<()> {
        let finality_update: FinalityUpdate = self.goerli_client.get_finality_update().await?;
        let update_finality_slot = finality_update.finalized_header.slot;
        let update_finality_period = update_finality_slot.div(32).div(256);

        tracing::info!(
            target: "pangoro-goerli",
            "[Header][Goerli=>Pangoro] Latest finality slot: {:?}",
            &update_finality_slot
        );
        // The latest finality header has been relayed
        if update_finality_slot == state.relayed_slot {
            return Ok(());
        }

        let (_slot, sync_aggregate_slot, _attested_header, _sync_aggregate_block) = match self
            .goerli_client
            .find_valid_attested_header(state.current_slot, finality_update.attested_header.slot)
            .await?
        {
            None => {
                tracing::info!(
                    target: "pangoro-goerli",
                    "[Header][Goerli=>Pangoro] Wait for valid attested header",
                );
                return Ok(());
            }
            Some((slot, sync_aggregate_slot, attested_header, sync_aggregate_block)) => (
                slot,
                sync_aggregate_slot,
                attested_header,
                sync_aggregate_block,
            ),
        };

        let sync_change = self
            .goerli_client
            .get_sync_committee_period_update(update_finality_period - 1, 1)
            .await?;
        if sync_change.is_empty() {
            return Err(BridgerError::Custom("Failed to get sync committee update".into()).into());
        }
        let fork_version = self
            .goerli_client
            .get_fork_version(sync_aggregate_slot)
            .await?;

        let finalized_header_update = FinalizedHeaderUpdate {
            attested_header: finality_update.attested_header.to_contract_type()?,
            signature_sync_committee: sync_change[0].next_sync_committee.to_contract_type()?,
            finalized_header: finality_update.finalized_header.to_contract_type()?,
            finality_branch: finality_update
                .finality_branch
                .iter()
                .map(|x| H256::from_str(x))
                .collect::<Result<Vec<H256>, _>>()?,
            sync_aggregate: finality_update.sync_aggregate.to_contract_type()?,
            fork_version: Bytes(fork_version.current_version.as_ref().to_vec()),
            signature_slot: sync_aggregate_slot,
        };
        self.import_finalized_header_with_confirmation(finalized_header_update)
            .await?;
        Ok(())
    }

    pub async fn relay_next_period(&self, state: HeaderRelayState) -> color_eyre::Result<()> {
        let _target_period = state.relayed_period + 1;
        let sync_change = self
            .goerli_client
            .get_sync_committee_period_update(state.relayed_period, 2)
            .await?;

        if let [last_finality, target_finality] = sync_change.as_slice() {
            let attested_slot: u64 = target_finality.attested_header.slot;
            let (_slot, sync_aggregate_slot, _attested_header, _sync_aggregate_block) = match self
                .goerli_client
                .find_valid_attested_header(state.current_slot, attested_slot)
                .await?
            {
                None => {
                    tracing::info!(
                        target: "pangoro-goerli",
                        "[Header][Goerli=>Pangoro] Wait for valid attested header",
                    );
                    return Ok(());
                }
                Some((slot, sync_aggregate_slot, attested_header, sync_aggregate_block)) => (
                    slot,
                    sync_aggregate_slot,
                    attested_header,
                    sync_aggregate_block,
                ),
            };
            let fork_version = H32::from_str(&target_finality.fork_version)?
                .as_ref()
                .to_vec();

            let finalized_header_update = FinalizedHeaderUpdate {
                attested_header: target_finality.attested_header.to_contract_type()?,
                signature_sync_committee: last_finality.next_sync_committee.to_contract_type()?,
                finalized_header: target_finality.finalized_header.to_contract_type()?,
                finality_branch: target_finality
                    .finality_branch
                    .iter()
                    .map(|x| H256::from_str(x))
                    .collect::<Result<Vec<H256>, _>>()?,
                sync_aggregate: target_finality.sync_aggregate.to_contract_type()?,
                fork_version: Bytes(fork_version),
                signature_slot: sync_aggregate_slot,
            };
            self.import_finalized_header_with_confirmation(finalized_header_update)
                .await?;
            Ok(())
        } else {
            Err(BridgerError::Custom("Failed to get sync committee update".into()).into())
        }
    }

    async fn import_finalized_header_with_confirmation(
        &self,
        finalized_header_update: FinalizedHeaderUpdate,
    ) -> color_eyre::Result<()> {
        let tx = self
            .pangoro_client
            .beacon_light_client
            .import_finalized_header(
                finalized_header_update,
                &self.pangoro_client.private_key,
                self.pangoro_client.gas_option.clone(),
            )
            .await?;
        tracing::info!(
        target: "pangoro-goerli",
            "[Header][Goerli=>Pangoro] Sending tx: {:?}",
            &tx
        );
        wait_for_transaction_confirmation(
            tx,
            self.pangoro_client.client.transport(),
            Duration::from_secs(5),
            3,
        )
        .await?;
        Ok(())
    }
}