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
use crate::database::Database;
use anyhow::Error as AnyError;
use futures::FutureExt;
use modules::Modules;
use std::{
net::SocketAddr,
panic,
};
use tokio::{
sync::oneshot,
task::JoinHandle,
};
use tracing::log::warn;
pub use config::{
Config,
DbType,
VMConfig,
};
pub mod adapters;
pub mod config;
pub(crate) mod genesis;
pub mod graph_api;
pub mod metrics;
pub mod modules;
pub struct FuelService {
handle: JoinHandle<()>,
shutdown: oneshot::Sender<()>,
#[cfg(feature = "relayer")]
relayer_handle: Option<fuel_relayer::RelayerSynced>,
pub bound_address: SocketAddr,
}
struct FuelServiceInner {
tasks: Vec<JoinHandle<Result<(), AnyError>>>,
modules: Modules,
pub bound_address: SocketAddr,
stop_graphql_api: oneshot::Sender<()>,
}
impl FuelService {
#[tracing::instrument(skip(config))]
pub async fn new_node(mut config: Config) -> Result<Self, AnyError> {
Self::make_config_consistent(&mut config);
let database = match config.database_type {
#[cfg(feature = "rocksdb")]
DbType::RocksDb => Database::open(&config.database_path)?,
DbType::InMemory => Database::in_memory(),
#[cfg(not(feature = "rocksdb"))]
_ => Database::in_memory(),
};
Ok(Self::spawn_service(
Self::init_service(database, config).await?,
))
}
fn spawn_service(service: FuelServiceInner) -> Self {
let bound_address = service.bound_address;
let (shutdown, stop_rx) = oneshot::channel();
#[cfg(feature = "relayer")]
let relayer_handle = service
.modules
.relayer
.as_ref()
.map(fuel_relayer::RelayerHandle::listen_synced);
let handle = tokio::spawn(async move {
let run_fut = service.run();
let shutdown_fut = stop_rx.then(|stop| async move {
if stop.is_err() {
futures::future::pending::<()>().await;
}
});
tokio::pin!(run_fut);
tokio::pin!(shutdown_fut);
futures::future::select(shutdown_fut, run_fut).await;
});
Self {
handle,
shutdown,
bound_address,
#[cfg(feature = "relayer")]
relayer_handle,
}
}
fn make_config_consistent(config: &mut Config) {
if config.txpool.chain_config != config.chain_conf {
warn!("The `ChainConfig` of `TxPool` was inconsistent");
config.txpool.chain_config = config.chain_conf.clone();
}
if config.txpool.utxo_validation != config.utxo_validation {
warn!("The `utxo_validation` of `TxPool` was inconsistent");
config.txpool.utxo_validation = config.utxo_validation;
}
if config.block_producer.utxo_validation != config.utxo_validation {
warn!("The `utxo_validation` of `BlockProducer` was inconsistent");
config.block_producer.utxo_validation = config.utxo_validation;
}
}
pub async fn from_database(
database: Database,
config: Config,
) -> Result<Self, AnyError> {
Ok(Self::spawn_service(
Self::init_service(database, config).await?,
))
}
async fn init_service(
database: Database,
config: Config,
) -> Result<FuelServiceInner, AnyError> {
Self::initialize_state(&config, &database)?;
let modules = modules::start_modules(&config, &database).await?;
let (stop_tx, stop_rx) = oneshot::channel();
let mut tasks = vec![];
let (bound_address, api_server) =
graph_api::start_server(config.clone(), database, &modules, stop_rx).await?;
tasks.push(api_server);
Ok(FuelServiceInner {
tasks,
bound_address,
modules,
stop_graphql_api: stop_tx,
})
}
pub async fn run(self) {
Self::wait_for_handle(self.handle).await;
}
pub async fn stop(self) {
let Self {
handle, shutdown, ..
} = self;
let _ = shutdown.send(());
Self::wait_for_handle(handle).await;
}
async fn wait_for_handle(handle: JoinHandle<()>) {
if let Err(err) = handle.await {
if err.is_panic() {
panic::resume_unwind(err.into_panic());
}
}
}
#[cfg(feature = "relayer")]
pub async fn await_relayer_synced(&self) -> anyhow::Result<()> {
if let Some(relayer_handle) = &self.relayer_handle {
relayer_handle.await_synced().await?;
}
Ok(())
}
}
impl FuelServiceInner {
pub async fn run(self) {
let Self {
tasks,
modules,
stop_graphql_api,
..
} = self;
let run_fut = Self::run_inner(tasks);
let shutdown_fut = shutdown_signal(stop_graphql_api);
tokio::pin!(run_fut);
tokio::pin!(shutdown_fut);
futures::future::select(shutdown_fut, run_fut).await;
modules.stop().await;
}
async fn run_inner(tasks: Vec<JoinHandle<anyhow::Result<()>>>) {
for task in tasks {
match task.await {
Err(err) => {
if err.is_panic() {
panic::resume_unwind(err.into_panic());
}
}
Ok(Err(e)) => {
eprintln!("server error: {:?}", e);
}
Ok(Ok(_)) => {}
}
}
}
}
async fn shutdown_signal(stop_graphql_api: oneshot::Sender<()>) {
#[cfg(unix)]
{
let mut sigterm =
tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())
.expect("failed to install sigterm handler");
let mut sigint =
tokio::signal::unix::signal(tokio::signal::unix::SignalKind::interrupt())
.expect("failed to install sigint handler");
loop {
tokio::select! {
_ = sigterm.recv() => {
tracing::info!("sigterm received");
let _ = stop_graphql_api.send(());
break;
}
_ = sigint.recv() => {
tracing::log::info!("sigint received");
let _ = stop_graphql_api.send(());
break;
}
}
}
}
#[cfg(not(unix))]
{
tokio::signal::ctrl_c()
.await
.expect("failed to install CTRL+C signal handler");
let _ = stop_graphql_api.send(());
info!("CTRL+C received");
}
}