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
use crate::{chain_config::ChainConfig, database::Database};
use anyhow::Error as AnyError;
use modules::Modules;
use std::{
net::{Ipv4Addr, SocketAddr},
panic,
path::PathBuf,
};
use strum_macros::{Display, EnumString, EnumVariantNames};
use thiserror::Error;
use tokio::task::JoinHandle;
use tracing::log::warn;
pub(crate) mod genesis;
pub mod graph_api;
pub mod metrics;
pub mod modules;
#[derive(Clone, Debug)]
pub struct Config {
pub addr: SocketAddr,
pub database_path: PathBuf,
pub database_type: DbType,
pub chain_conf: ChainConfig,
pub utxo_validation: bool,
pub predicates: bool,
pub vm: VMConfig,
pub tx_pool_config: fuel_txpool::Config,
}
impl Config {
pub fn local_node() -> Self {
Self {
addr: SocketAddr::new(Ipv4Addr::new(127, 0, 0, 1).into(), 0),
database_path: Default::default(),
database_type: DbType::InMemory,
chain_conf: ChainConfig::local_testnet(),
vm: Default::default(),
utxo_validation: false,
predicates: false,
tx_pool_config: Default::default(),
}
}
}
#[derive(Clone, Debug, Default)]
pub struct VMConfig {
pub backtrace: bool,
}
#[derive(Clone, Debug, Display, PartialEq, EnumString, EnumVariantNames)]
#[strum(serialize_all = "kebab_case")]
pub enum DbType {
InMemory,
RocksDb,
}
pub struct FuelService {
tasks: Vec<JoinHandle<Result<(), AnyError>>>,
modules: Modules,
pub bound_address: SocketAddr,
}
impl FuelService {
#[tracing::instrument(skip(config))]
pub async fn new_node(config: Config) -> Result<Self, AnyError> {
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(),
};
Self::init_service(database, config).await
}
#[cfg(any(test, feature = "test-helpers"))]
pub async fn from_database(database: Database, config: Config) -> Result<Self, AnyError> {
Self::init_service(database, config).await
}
async fn init_service(database: Database, config: Config) -> Result<Self, AnyError> {
if config.predicates {
warn!("Predicates are currently an unstable feature!");
}
Self::import_state(&config.chain_conf, &database)?;
let modules = modules::start_modules(&config, &database).await?;
let mut tasks = vec![];
let (bound_address, api_server) =
graph_api::start_server(config.clone(), database, &modules).await?;
tasks.push(api_server);
Ok(FuelService {
tasks,
bound_address,
modules,
})
}
pub async fn run(self) {
for task in self.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(_)) => {}
}
}
}
pub async fn stop(&self) {
for task in &self.tasks {
task.abort();
}
self.modules.stop().await;
}
}
#[derive(Debug, Error)]
pub enum Error {
#[error("An api server error occurred {0}")]
ApiServer(#[from] hyper::Error),
}