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
use lifeline::Sender;
use postage::broadcast;
use client_pangolin::client::PangolinClient;
use client_pangolin::component::PangolinClientComponent;
use shadow_liketh::component::ShadowComponent;
use shadow_liketh::shadow::Shadow;
use shadow_liketh::types::BridgeName;
use support_common::config::{Config, Names};
use support_common::error::BridgerError;
use thegraph_liketh::types::TransactionEntity;
use crate::bridge::{Extrinsic, PangolinRopstenConfig, ToExtrinsicsMessage};
pub struct RedeemHandler {
sender_to_extrinsics: broadcast::Sender<ToExtrinsicsMessage>,
client: PangolinClient,
shadow: Shadow,
}
impl RedeemHandler {
pub async fn new(sender_to_extrinsics: broadcast::Sender<ToExtrinsicsMessage>) -> Self {
let mut times = 0;
loop {
times += 1;
match Self::build(sender_to_extrinsics.clone()).await {
Ok(v) => return v,
Err(err) => {
tracing::error!(
target: "pangolin-ropsten",
"[ropsten] [redeem] Failed to create redeem handler, times: [{}] err: {:#?}",
times,
err
);
tokio::time::sleep(std::time::Duration::from_secs(10)).await;
}
}
}
}
async fn build(
sender_to_extrinsics: broadcast::Sender<ToExtrinsicsMessage>,
) -> color_eyre::Result<Self> {
tracing::info!(target: "pangolin-ropsten", "[ropsten] [redeem] RECREATE SCAN REDEEM HANDLER...");
let bridge_config: PangolinRopstenConfig = Config::restore(Names::BridgePangolinRopsten)?;
let client = PangolinClientComponent::component(bridge_config.darwinia).await?;
let shadow = ShadowComponent::component(
bridge_config.shadow,
bridge_config.ethereum,
bridge_config.web3,
BridgeName::PangolinRopsten,
)?;
tracing::info!(
target: "pangolin-ropsten",
"[ropsten] [redeem] ✨ REDEEM HANDLER CREATED: ROPSTEN <> PANGOLIN REDEEM"
);
Ok(RedeemHandler {
sender_to_extrinsics,
client,
shadow,
})
}
}
impl RedeemHandler {
pub async fn redeem(&mut self, tx: TransactionEntity) -> color_eyre::Result<Option<u64>> {
tracing::trace!(
target: "pangolin-ropsten",
"[ropsten] [redeem] Try to redeem ethereum tx {:?}... in block {}",
tx.tx_hash,
tx.block_number
);
let block_hash = array_bytes::hex2bytes(&tx.block_hash).map_err(|_e| {
BridgerError::Hex(format!(
"Failed to convert hex({}) to bytes.",
&tx.block_hash
))
})?;
let tx_index = tx.tx_index;
if self
.client
.ethereum()
.is_verified(&block_hash, tx_index)
.await?
{
tracing::trace!(
target: "pangolin-ropsten",
"[ropsten] [redeem] Ethereum tx {:?} redeemed",
tx.tx_hash
);
return Ok(Some(tx.block_number));
}
tracing::trace!(
target: "pangolin-ropsten",
"[ropsten] [redeem] Ethereum tx {:?} is not verified, will be redeem",
tx.tx_hash
);
let last_confirmed = self.client.ethereum().last_confirmed().await?;
if tx.block_number >= last_confirmed {
tracing::trace!(
target: "pangolin-ropsten",
"[ropsten] [redeem] Ethereum tx {:?}'s block {} is large than last confirmed block {}",
tx.tx_hash,
tx.block_number,
last_confirmed,
);
return Ok(None);
}
tracing::trace!(
target: "pangolin-ropsten",
"[ropsten] [redeem] Ethereum tx {:?} ({}) all check passed, let's ready to redeem, and last confirmed is {}",
tx.tx_hash,
tx.block_number,
last_confirmed,
);
let proof = self.shadow.receipt(&tx.tx_hash, last_confirmed).await?;
tracing::trace!(
target: "pangolin-ropsten",
"[ropsten] [redeem] Queried ethereum tx {:?} proof: {:?}",
tx.tx_hash,
proof,
);
let ex = Extrinsic::Redeem(proof.try_into()?, tx.clone());
tracing::info!(
target: "pangolin-ropsten",
"[ropsten] [redeem] Redeem extrinsic send to extrinsics service: {:?}. at ropsten block: {}",
ex,
tx.block_number
);
self.sender_to_extrinsics
.send(ToExtrinsicsMessage::Extrinsic(ex))
.await?;
Ok(Some(tx.block_number))
}
}