-
Notifications
You must be signed in to change notification settings - Fork 201
/
mod.rs
347 lines (315 loc) · 11.6 KB
/
mod.rs
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// Copyright 2019 Conflux Foundation. All rights reserved.
// Conflux is free software and distributed under GNU General Public License.
// See http://www.gnu.org/licenses/
use super::{
http::Server as HttpServer, tcp::Server as TcpServer, TESTNET_VERSION,
};
pub use crate::configuration::Configuration;
use blockgen::BlockGenerator;
use crate::{
common::{initialize_txgens, ClientComponents},
rpc::{
extractor::RpcExtractor,
impls::{
cfx::RpcImpl, common::RpcImpl as CommonImpl, pubsub::PubSubClient,
},
setup_debug_rpc_apis, setup_public_rpc_apis,
},
};
use cfx_types::{Address, U256};
use cfxcore::{
block_data_manager::BlockDataManager, genesis, statistics::Statistics,
storage::StorageManager, sync::SyncPhaseType,
transaction_pool::DEFAULT_MAX_BLOCK_GAS_LIMIT, vm_factory::VmFactory,
ConsensusGraph, LightProvider, Notifications, SynchronizationGraph,
SynchronizationService, TransactionPool, WORKER_COMPUTATION_PARALLELISM,
};
use network::NetworkService;
use parking_lot::{Condvar, Mutex};
use runtime::Runtime;
use secret_store::SecretStore;
use std::{str::FromStr, sync::Arc, thread, time::Duration};
use threadpool::ThreadPool;
use txgen::propagate::DataPropagation;
pub struct FullClientExtraComponents {
pub debug_rpc_http_server: Option<HttpServer>,
pub rpc_tcp_server: Option<TcpServer>,
pub rpc_http_server: Option<HttpServer>,
pub consensus: Arc<ConsensusGraph>,
pub txpool: Arc<TransactionPool>,
pub sync: Arc<SynchronizationService>,
pub secret_store: Arc<SecretStore>,
pub runtime: Runtime,
}
pub struct FullClient {}
impl FullClient {
// Start all key components of Conflux and pass out their handles
pub fn start(
mut conf: Configuration, exit: Arc<(Mutex<bool>, Condvar)>,
) -> Result<
Box<ClientComponents<BlockGenerator, FullClientExtraComponents>>,
String,
> {
info!("Working directory: {:?}", std::env::current_dir());
metrics::initialize(conf.metrics_config());
let worker_thread_pool = Arc::new(Mutex::new(ThreadPool::with_name(
"Tx Recover".into(),
WORKER_COMPUTATION_PARALLELISM,
)));
let network_config = conf.net_config()?;
let cache_config = conf.cache_config();
let db_config = conf.db_config();
let ledger_db =
db::open_database(conf.raw_conf.block_db_dir.as_str(), &db_config)
.map_err(|e| format!("Failed to open database {:?}", e))?;
let secret_store = Arc::new(SecretStore::new());
let storage_manager = Arc::new(
StorageManager::new(conf.storage_config())
.expect("Failed to initialize storage"),
);
{
let storage_manager_log_weak_ptr = Arc::downgrade(&storage_manager);
let exit_clone = exit.clone();
thread::spawn(move || loop {
let mut exit_lock = exit_clone.0.lock();
if exit_clone
.1
.wait_for(&mut exit_lock, Duration::from_millis(5000))
.timed_out()
{
let manager = storage_manager_log_weak_ptr.upgrade();
match manager {
None => return,
Some(manager) => manager.log_usage(),
};
} else {
return;
}
});
}
let genesis_accounts = if conf.is_test_or_dev_mode() {
match conf.raw_conf.genesis_secrets {
Some(ref file) => {
genesis::load_secrets_file(file, secret_store.as_ref())?
}
None => genesis::default(conf.is_test_or_dev_mode()),
}
} else {
match conf.raw_conf.genesis_accounts {
Some(ref file) => genesis::load_file(file)?,
None => genesis::default(conf.is_test_or_dev_mode()),
}
};
// FIXME: move genesis block to a dedicated directory near all conflux
// FIXME: parameters.
let genesis_block = storage_manager.initialize(
genesis_accounts,
DEFAULT_MAX_BLOCK_GAS_LIMIT.into(),
Address::from_str(TESTNET_VERSION).unwrap(),
U256::zero(),
);
debug!("Initialize genesis_block={:?}", genesis_block);
let data_man = Arc::new(BlockDataManager::new(
cache_config,
Arc::new(genesis_block),
ledger_db.clone(),
storage_manager,
worker_thread_pool,
conf.data_mananger_config(),
));
let txpool = Arc::new(TransactionPool::new(
conf.txpool_config(),
data_man.clone(),
));
let statistics = Arc::new(Statistics::new());
let vm = VmFactory::new(1024 * 32);
let pow_config = conf.pow_config();
let notifications = Notifications::init();
let consensus = Arc::new(ConsensusGraph::new(
conf.consensus_config(),
vm,
txpool.clone(),
statistics,
data_man.clone(),
pow_config.clone(),
notifications.clone(),
conf.execution_config(),
));
let protocol_config = conf.protocol_config();
let verification_config = conf.verification_config();
let sync_config = conf.sync_graph_config();
let sync_graph = Arc::new(SynchronizationGraph::new(
consensus.clone(),
verification_config,
pow_config.clone(),
sync_config,
notifications.clone(),
true,
));
let network = {
let mut network = NetworkService::new(network_config);
network.start().unwrap();
Arc::new(network)
};
let light_provider = Arc::new(LightProvider::new(
consensus.clone(),
sync_graph.clone(),
Arc::downgrade(&network),
txpool.clone(),
conf.raw_conf.throttling_conf.clone(),
));
light_provider.clone().register(network.clone()).unwrap();
let initial_sync_phase = SyncPhaseType::CatchUpRecoverBlockHeaderFromDB;
let sync = Arc::new(SynchronizationService::new(
true,
network.clone(),
sync_graph.clone(),
protocol_config,
conf.state_sync_config(),
initial_sync_phase,
light_provider,
));
sync.register().unwrap();
if conf.is_test_mode() && conf.raw_conf.data_propagate_enabled {
let dp = Arc::new(DataPropagation::new(
conf.raw_conf.data_propagate_interval_ms,
conf.raw_conf.data_propagate_size,
));
DataPropagation::register(dp, network.clone())?;
}
let (maybe_txgen, maybe_direct_txgen) = initialize_txgens(
consensus.clone(),
txpool.clone(),
sync.clone(),
secret_store.clone(),
&conf,
network.net_key_pair().unwrap(),
);
let maybe_author: Option<Address> = conf.raw_conf.mining_author.clone().map(|hex_str| Address::from_str(hex_str.as_str()).expect("mining-author should be 40-digit hex string without 0x prefix"));
let blockgen = Arc::new(BlockGenerator::new(
sync_graph,
txpool.clone(),
sync.clone(),
maybe_txgen.clone(),
pow_config.clone(),
maybe_author.clone().unwrap_or_default(),
));
if conf.raw_conf.start_mining {
if maybe_author.is_none() {
panic!("mining-author is not set correctly, so you'll not get mining rewards!!!");
}
let bg = blockgen.clone();
info!("Start mining with pow config: {:?}", pow_config);
thread::Builder::new()
.name("mining".into())
.spawn(move || {
BlockGenerator::start_mining(bg, 0);
})
.expect("Mining thread spawn error");
} else {
if conf.is_dev_mode() {
let bg = blockgen.clone();
let interval_ms = conf.raw_conf.dev_block_interval_ms;
info!("Start auto block generation");
thread::Builder::new()
.name("auto_mining".into())
.spawn(move || {
bg.auto_block_generation(interval_ms);
})
.expect("Mining thread spawn error");
}
}
let rpc_impl = Arc::new(RpcImpl::new(
consensus.clone(),
sync.clone(),
blockgen.clone(),
txpool.clone(),
maybe_txgen.clone(),
maybe_direct_txgen,
conf.rpc_impl_config(),
));
let common_impl = Arc::new(CommonImpl::new(
exit,
consensus.clone(),
network,
txpool.clone(),
));
let runtime = Runtime::with_default_thread_count();
let pubsub = PubSubClient::new(
runtime.executor(),
consensus.clone(),
notifications,
);
let debug_rpc_http_server = super::rpc::start_http(
super::rpc::HttpConfiguration::new(
Some((127, 0, 0, 1)),
conf.raw_conf.jsonrpc_local_http_port,
conf.raw_conf.jsonrpc_cors.clone(),
conf.raw_conf.jsonrpc_http_keep_alive,
),
setup_debug_rpc_apis(
common_impl.clone(),
rpc_impl.clone(),
None,
&conf,
),
)?;
if conf.is_dev_mode() {
if conf.raw_conf.jsonrpc_tcp_port.is_none() {
conf.raw_conf.jsonrpc_tcp_port = Some(12536);
}
if conf.raw_conf.jsonrpc_http_port.is_none() {
conf.raw_conf.jsonrpc_http_port = Some(12537);
}
};
let rpc_tcp_server = super::rpc::start_tcp(
super::rpc::TcpConfiguration::new(
None,
conf.raw_conf.jsonrpc_tcp_port,
),
if conf.is_test_or_dev_mode() {
setup_debug_rpc_apis(
common_impl.clone(),
rpc_impl.clone(),
Some(pubsub),
&conf,
)
} else {
setup_public_rpc_apis(
common_impl.clone(),
rpc_impl.clone(),
Some(pubsub),
&conf,
)
},
RpcExtractor,
)?;
let rpc_http_server = super::rpc::start_http(
super::rpc::HttpConfiguration::new(
None,
conf.raw_conf.jsonrpc_http_port,
conf.raw_conf.jsonrpc_cors.clone(),
conf.raw_conf.jsonrpc_http_keep_alive,
),
if conf.is_test_or_dev_mode() {
setup_debug_rpc_apis(common_impl, rpc_impl, None, &conf)
} else {
setup_public_rpc_apis(common_impl, rpc_impl, None, &conf)
},
)?;
Ok(Box::new(ClientComponents {
data_manager_weak_ptr: Arc::downgrade(&data_man),
blockgen: Some(blockgen),
other_components: FullClientExtraComponents {
debug_rpc_http_server,
rpc_http_server,
rpc_tcp_server,
txpool,
consensus,
secret_store,
sync,
runtime,
},
}))
}
}