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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
use std::time::{SystemTime, UNIX_EPOCH};

use microkv::namespace::NamespaceMicroKV;
use rand::distributions::Alphanumeric;
use rand::{thread_rng, Rng};

use client_pangolin::client::PangolinClient;
use client_pangolin::component::PangolinClientComponent;
use client_pangolin::config::ClientConfig;
use client_pangolin::types::runtime_types::darwinia_bridge_ethereum::EthereumRelayHeaderParcel;
use client_pangolin::types::runtime_types::to_ethereum_backing::pallet::RedeemFor;
use client_pangolin::types::{EcdsaMessage, EthereumAccount, EthereumReceiptProofThing};
use component_ethereum::web3::Web3Config;
use component_state::state::BridgeState;
use support_common::config::{Config, Names};
use support_common::error::BridgerError;
use thegraph_liketh::types::{TransactionEntity, TransactionType};

use crate::bridge::Extrinsic;
use crate::bridge::{PangolinRopstenConfig, PangolinRopstenTask};

pub struct ExtrinsicsHandler {
    client: PangolinClient,
    ethereum_account: EthereumAccount,
    microkv: NamespaceMicroKV,
    message_kv: NamespaceMicroKV,
}

impl ExtrinsicsHandler {
    pub async fn new(state: BridgeState) -> Self {
        loop {
            match Self::build(state.clone()).await {
                Ok(handler) => return handler,
                Err(err) => {
                    tracing::error!(
                        target: "pangolin-ropsten",
                        "[pangolin] [extrinsics] extrinsics init err: {:#?}",
                        err
                    );
                    tokio::time::sleep(std::time::Duration::from_secs(10)).await;
                }
            }
        }
    }

    async fn build(state: BridgeState) -> color_eyre::Result<Self> {
        tracing::info!(
            target: "pangolin-ropsten",
            "[pangolin] [extrinsics] EXTRINSICS SERVICE RESTARTING..."
        );
        let bridge_config: PangolinRopstenConfig = Config::restore(Names::BridgePangolinRopsten)?;

        // Config
        let config_darwinia: ClientConfig = bridge_config.darwinia;
        let config_web3: Web3Config = bridge_config.web3;

        let ethereum_account = EthereumAccount::new(
            config_web3.endpoint,
            config_darwinia.ecdsa_authority_private_key.clone(),
        );

        let client = PangolinClientComponent::component(config_darwinia).await?;

        tracing::info!(
            target: "pangolin-ropsten",
            "[pangolin] [extrinsics] ✨ SERVICE STARTED: ROPSTEN <> PANGOLIN EXTRINSICS"
        );

        let microkv = state.microkv_with_namespace(PangolinRopstenTask::name());
        let message_kv =
            state.microkv_with_namespace(format!("{}-messages", PangolinRopstenTask::name()));
        Ok(ExtrinsicsHandler {
            client,
            ethereum_account,
            microkv,
            message_kv,
        })
    }
}

impl ExtrinsicsHandler {
    pub async fn send_extrinsic(&self, extrinsic: Extrinsic) -> color_eyre::Result<()> {
        match extrinsic {
            Extrinsic::Affirm(parcel) => self.send_affirm(parcel).await?,
            Extrinsic::Redeem(proof, ethereum_tx) => self.send_redeem(proof, ethereum_tx).await?,
            Extrinsic::GuardVote(pending_block_number, aye) => {
                self.send_guard_vote(pending_block_number, aye).await?
            }
            Extrinsic::SignAndSendMmrRoot(block_number) => {
                self.send_sign_and_send_mmr_root(block_number).await?
            }
            Extrinsic::SignAndSendAuthorities(message) => {
                self.send_sign_and_send_authorities(message).await?
            }
        }
        // Delay for waiting to fininsh
        tokio::time::sleep(std::time::Duration::from_secs(12)).await;
        Ok(())
    }

    async fn send_affirm(&self, parcel: EthereumRelayHeaderParcel) -> color_eyre::Result<()> {
        let block_number = parcel.header.number;
        let ex_hash = self.client.ethereum().affirm(parcel).await?;
        tracing::info!(
            target: "pangolin-ropsten",
            "[pangolin] [extrinsics] [affirm] Affirmed ethereum block {} in extrinsic {:?}",
            block_number,
            ex_hash
        );
        self.microkv.put("relayed", &block_number)?;
        Ok(())
    }

    async fn send_redeem(
        &self,
        proof: EthereumReceiptProofThing,
        ethereum_tx: TransactionEntity,
    ) -> color_eyre::Result<()> {
        tracing::info!(
            target: "pangolin-ropsten",
            "[pangolin] [extrinsics] [redeem] Ready to send redeem. type is [{:?}] and tx is [{:?}]",
            ethereum_tx.tx_type,
            ethereum_tx.tx_hash
        );
        match ethereum_tx.tx_type {
            TransactionType::SetAuthorities => {
                let ex_hash = self
                    .client
                    .ethereum()
                    .sync_authorities_change(proof)
                    .await?;
                tracing::info!(
                    target: "pangolin-ropsten",
                    "[pangolin] [extrinsics] [redeem] Sent ethereum tx {:?} with extrinsic {:?}",
                    ethereum_tx.tx_hash,
                    ex_hash
                );
            }
            TransactionType::RegisterErc20Token => {}
            TransactionType::RedeemErc20Token => {}
            _ => {
                let redeem_for = match ethereum_tx.tx_type {
                    TransactionType::Deposit => RedeemFor::Deposit,
                    TransactionType::Token => RedeemFor::Token,
                    // TransactionType::SetAuthorities => RedeemFor::SetAuthorities,
                    // TransactionType::RegisterErc20Token => RedeemFor::RegisterErc20Token,
                    // TransactionType::RedeemErc20Token => RedeemFor::RedeemErc20Token,
                    _ => return Err(BridgerError::Custom("Unreachable".to_string()).into()),
                };
                let ex_hash = self.client.ethereum().redeem(redeem_for, proof).await?;
                tracing::info!(
                    target: "pangolin-ropsten",
                    "[pangolin] [extrinsics] [redeem] Redeemed ethereum tx {:?} with extrinsic {:?}",
                    ethereum_tx.tx_hash,
                    ex_hash
                );
            }
        }
        Ok(())
    }

    async fn send_guard_vote(
        &self,
        pending_block_number: u64,
        aye: bool,
    ) -> color_eyre::Result<()> {
        let ex_hash = self
            .client
            .ethereum()
            .vote_pending_relay_header_parcel(pending_block_number, aye)
            .await?;
        if aye {
            tracing::info!(
                target: "pangolin-ropsten",
                "[pangolin] [extrinsics] [guard] Voted to approve: {}, ex hash: {:?}",
                pending_block_number,
                ex_hash
            );
        } else {
            tracing::info!(
                target: "pangolin-ropsten",
                "[pangolin] [extrinsics] [guard] Voted to reject: {}, ex hash: {:?}",
                pending_block_number,
                ex_hash
            );
        }
        Ok(())
    }

    async fn send_sign_and_send_mmr_root(&self, block_number: u32) -> color_eyre::Result<()> {
        tracing::trace!(
            target: "pangolin-ropsten",
            "[pangolin] [extrinsics] [mmr-root] Start sign and send mmr_root for block: {}",
            block_number,
        );

        let ex_hash = self
            .client
            .ethereum()
            .ecdsa_sign_and_submit_signed_mmr_root(self.ethereum_account.clone(), block_number)
            .await?;
        tracing::info!(
            target: "pangolin-ropsten",
            "[pangolin] [extrinsics] [mmr-root] Sign and send mmr root of block {} in extrinsic {:?}",
            block_number,
            ex_hash
        );
        Ok(())
    }

    async fn send_sign_and_send_authorities(
        &self,
        message: EcdsaMessage,
    ) -> color_eyre::Result<()> {
        tracing::trace!(
            target: "pangolin-ropsten",
            "[pangolin] [extrinsics] [signed-authorities] Start sign and send authorities..."
        );
        let ex_hash = self
            .client
            .ethereum()
            .ecdsa_sign_and_submit_signed_authorities(self.ethereum_account.clone(), message)
            .await?;
        tracing::info!(
            target: "pangolin-ropsten",
            "[pangolin] [extrinsics] [signed-authorities] Sign and send authorities in extrinsic {:?}",
            ex_hash
        );
        Ok(())
    }
}

impl ExtrinsicsHandler {
    pub fn collect_message(&self, message: &Extrinsic) -> color_eyre::Result<()> {
        // If there is a same message already, skip it and return Ok(()).
        if self.message_kv.keys()?.into_iter().any(|key| {
            self.message_kv
                .get_as_unwrap(&key)
                .map_or(false, |value: Extrinsic| &value == message)
        }) {
            return Ok(());
        }

        let mut key: String = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_nanos()
            .to_string();
        if let Ok(true) = self.message_kv.exists(&key) {
            let random: String = thread_rng()
                .sample_iter(&Alphanumeric)
                .take(1)
                .map(char::from)
                .collect();
            key += &random;
        }
        self.message_kv.put(key, message)?;
        Ok(())
    }

    pub async fn consume_message(&self) -> color_eyre::Result<()> {
        loop {
            let extrinsics = self.message_kv.sorted_keys()?;
            if extrinsics.is_empty() {
                tokio::time::sleep(std::time::Duration::from_secs(3)).await;
                continue;
            }
            let mut times = 0;
            let mut index = 0;
            loop {
                times += 1;
                let key = match extrinsics.get(index) {
                    Some(v) => v,
                    None => break,
                };
                let ex: Extrinsic = self.message_kv.get_as_unwrap(&key)?;
                match self.send_extrinsic(ex.clone()).await {
                    Ok(_) => self.message_kv.delete(&key)?,
                    Err(err) => {
                        tracing::error!(
                            target: "pangolin-pangoro",
                            "[pangolin] [extrinsics] [{}] Failed to send extrinsic {:?} err: {:?}",
                            times,
                            ex,
                            err,
                        );
                        if let Some(client_error) =
                            err.downcast_ref::<client_pangolin::error::ClientError>()
                        {
                            if client_error.is_restart_need() {
                                tracing::error!(
                                    target: "pangolin-pangoro",
                                    "[pangolin] [extrinsics] [{}] Connection Error. Try to resend later.",
                                    times,
                                );
                                tokio::time::sleep(std::time::Duration::from_secs(5)).await;
                                return Err(err);
                            }
                        }
                        tokio::time::sleep(std::time::Duration::from_secs(5)).await;
                        if times > 5 {
                            self.message_kv.delete(&key)?;
                        } else {
                            continue;
                        }
                    }
                }
                index += 1;
                times = 0;
            }
        }
    }
}