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
use crate::{
log::EthEventLog,
Config,
};
use anyhow::Result;
use async_trait::async_trait;
use core::time::Duration;
use ethers_core::types::{
Filter,
Log,
SyncingStatus,
ValueOrArray,
H160,
};
use ethers_providers::{
Http,
Middleware,
Provider,
ProviderError,
};
use fuel_core_interfaces::{
db::Messages,
model::Message,
relayer::RelayerDb,
};
use std::{
convert::TryInto,
ops::Deref,
sync::{
atomic::AtomicBool,
Arc,
},
};
use synced::update_synced;
use tokio::sync::watch;
use self::{
get_logs::*,
run::RelayerData,
state::EthLocal,
};
mod get_logs;
mod run;
mod state;
mod synced;
mod syncing;
#[cfg(test)]
mod test;
type Synced = watch::Receiver<bool>;
type NotifySynced = watch::Sender<bool>;
type Database = Box<dyn RelayerDb>;
pub struct RelayerHandle {
synced: RelayerSynced,
shutdown: RelayerShutdown,
}
#[derive(Clone)]
pub struct RelayerSynced {
synced: Synced,
}
struct Relayer<P>
where
P: Middleware<Error = ProviderError>,
{
synced: NotifySynced,
eth_node: Arc<P>,
database: Database,
config: Config,
}
struct RelayerShutdown {
join_handle: tokio::task::JoinHandle<()>,
shutdown: Arc<AtomicBool>,
}
impl<P> Relayer<P>
where
P: Middleware<Error = ProviderError> + 'static,
{
fn new(
synced: NotifySynced,
eth_node: P,
database: Database,
config: Config,
) -> Self {
Self {
synced,
eth_node: Arc::new(eth_node),
database,
config,
}
}
async fn set_deploy_height(&self) {
if self.finalized().await.unwrap_or_default() < *self.config.da_deploy_height {
self.database
.set_finalized_da_height(self.config.da_deploy_height)
.await;
}
}
}
#[async_trait]
impl<P> RelayerData for Relayer<P>
where
P: Middleware<Error = ProviderError> + 'static,
{
async fn download_logs(
&mut self,
eth_sync_gap: &state::EthSyncGap,
) -> anyhow::Result<()> {
let logs = download_logs(
eth_sync_gap,
self.config.eth_v2_listening_contracts.clone(),
self.eth_node.clone(),
self.config.log_page_size,
);
write_logs(self.database.as_mut(), logs).await
}
async fn set_finalized_da_height(
&self,
height: fuel_core_interfaces::model::DaBlockHeight,
) {
self.database.set_finalized_da_height(height).await
}
fn update_synced(&self, state: &state::EthState) {
update_synced(&self.synced, state)
}
async fn wait_if_eth_syncing(&self) -> anyhow::Result<()> {
syncing::wait_if_eth_syncing(
&self.eth_node,
self.config.syncing_call_frequency,
self.config.syncing_log_frequency,
)
.await
}
}
impl RelayerHandle {
pub fn start(database: Database, config: Config) -> anyhow::Result<Self> {
let url = config.eth_client.clone().ok_or_else(|| {
anyhow::anyhow!(
"Tried to start Relayer without setting an eth_client in the config"
)
})?;
let http = Http::new(url);
let eth_node = Provider::new(http);
Ok(Self::start_inner::<Provider<Http>>(
eth_node, database, config,
))
}
#[cfg(any(test, feature = "test-helpers"))]
pub fn start_test<P>(eth_node: P, database: Database, config: Config) -> Self
where
P: Middleware<Error = ProviderError> + 'static,
{
Self::start_inner(eth_node, database, config)
}
fn start_inner<P>(eth_node: P, database: Database, config: Config) -> Self
where
P: Middleware<Error = ProviderError> + 'static,
{
let (tx, rx) = watch::channel(false);
let synced = RelayerSynced { synced: rx };
let shutdown = run(Relayer::new(tx, eth_node, database, config));
Self { synced, shutdown }
}
pub fn listen_synced(&self) -> RelayerSynced {
self.synced.clone()
}
pub fn is_running(&self) -> bool {
!self.shutdown.join_handle.is_finished()
}
pub async fn shutdown(self) -> anyhow::Result<()> {
self.shutdown
.shutdown
.store(true, core::sync::atomic::Ordering::Relaxed);
Ok(self.shutdown.join_handle.await?)
}
}
impl RelayerSynced {
pub async fn await_synced(&self) -> Result<()> {
let mut rx = self.synced.clone();
if !rx.borrow_and_update().deref() {
rx.changed().await?;
}
Ok(())
}
}
#[async_trait]
impl<P> state::EthRemote for Relayer<P>
where
P: Middleware<Error = ProviderError>,
{
async fn current(&self) -> anyhow::Result<u64> {
Ok(self.eth_node.get_block_number().await?.as_u64())
}
fn finalization_period(&self) -> u64 {
*self.config.da_finalization
}
}
#[async_trait]
impl<P> state::EthLocal for Relayer<P>
where
P: Middleware<Error = ProviderError>,
{
async fn finalized(&self) -> Option<u64> {
self.database.get_finalized_da_height().await.map(|h| *h)
}
}
fn run<P>(mut relayer: Relayer<P>) -> RelayerShutdown
where
P: Middleware<Error = ProviderError> + 'static,
{
let shutdown = Arc::new(AtomicBool::new(false));
let join_handle = tokio::task::spawn({
let shutdown = shutdown.clone();
async move {
relayer.set_deploy_height().await;
while !shutdown.load(core::sync::atomic::Ordering::Relaxed) {
let now = tokio::time::Instant::now();
if let Err(e) = run::run(&mut relayer).await {
let e: &dyn std::error::Error = &*e;
tracing::error!(e);
}
tokio::time::sleep(
relayer
.config
.sync_minimum_duration
.saturating_sub(now.elapsed()),
)
.await;
}
}
});
RelayerShutdown {
join_handle,
shutdown,
}
}