2019-05-25 10:51:15 +00:00
|
|
|
use client::{
|
|
|
|
error, notifier, BeaconChainTypes, Client, ClientConfig, DBType, TestnetDiskBeaconChainTypes,
|
|
|
|
TestnetMemoryBeaconChainTypes,
|
|
|
|
};
|
2019-02-27 23:24:27 +00:00
|
|
|
use futures::sync::oneshot;
|
2019-03-01 01:45:01 +00:00
|
|
|
use futures::Future;
|
|
|
|
use slog::info;
|
2019-02-27 23:24:27 +00:00
|
|
|
use std::cell::RefCell;
|
2019-03-01 01:45:01 +00:00
|
|
|
use tokio::runtime::Builder;
|
2019-03-31 07:57:48 +00:00
|
|
|
use tokio::runtime::Runtime;
|
|
|
|
use tokio::runtime::TaskExecutor;
|
2019-03-26 23:36:20 +00:00
|
|
|
use tokio_timer::clock::Clock;
|
2019-02-27 23:24:27 +00:00
|
|
|
|
2019-03-19 11:53:51 +00:00
|
|
|
pub fn run_beacon_node(config: ClientConfig, log: &slog::Logger) -> error::Result<()> {
|
2019-03-31 07:57:48 +00:00
|
|
|
let runtime = Builder::new()
|
2019-02-27 23:24:27 +00:00
|
|
|
.name_prefix("main-")
|
2019-03-26 23:36:20 +00:00
|
|
|
.clock(Clock::system())
|
2019-02-27 23:24:27 +00:00
|
|
|
.build()
|
|
|
|
.map_err(|e| format!("{:?}", e))?;
|
|
|
|
|
|
|
|
// Log configuration
|
2019-03-17 03:25:47 +00:00
|
|
|
info!(log, "Listening on {:?}", &config.net_conf.listen_addresses;
|
2019-02-27 23:24:27 +00:00
|
|
|
"data_dir" => &config.data_dir.to_str(),
|
|
|
|
"port" => &config.net_conf.listen_port);
|
|
|
|
|
2019-03-31 07:57:48 +00:00
|
|
|
let executor = runtime.executor();
|
|
|
|
|
|
|
|
match config.db_type {
|
2019-05-21 07:45:35 +00:00
|
|
|
DBType::Disk => {
|
2019-03-31 21:59:59 +00:00
|
|
|
info!(
|
|
|
|
log,
|
|
|
|
"BeaconNode starting";
|
2019-05-25 10:51:15 +00:00
|
|
|
"type" => "TestnetDiskBeaconChainTypes"
|
2019-03-31 21:59:59 +00:00
|
|
|
);
|
2019-05-25 10:51:15 +00:00
|
|
|
let client: Client<TestnetDiskBeaconChainTypes> =
|
2019-03-31 07:57:48 +00:00
|
|
|
Client::new(config, log.clone(), &executor)?;
|
|
|
|
|
|
|
|
run(client, executor, runtime, log)
|
|
|
|
}
|
|
|
|
DBType::Memory => {
|
2019-03-31 21:59:59 +00:00
|
|
|
info!(
|
|
|
|
log,
|
|
|
|
"BeaconNode starting";
|
2019-05-25 10:51:15 +00:00
|
|
|
"type" => "TestnetMemoryBeaconChainTypes"
|
2019-03-31 21:59:59 +00:00
|
|
|
);
|
2019-05-25 10:51:15 +00:00
|
|
|
let client: Client<TestnetMemoryBeaconChainTypes> =
|
2019-03-31 07:57:48 +00:00
|
|
|
Client::new(config, log.clone(), &executor)?;
|
|
|
|
|
|
|
|
run(client, executor, runtime, log)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-25 10:51:15 +00:00
|
|
|
pub fn run<T: BeaconChainTypes + Send + Sync + 'static>(
|
2019-03-31 07:57:48 +00:00
|
|
|
client: Client<T>,
|
|
|
|
executor: TaskExecutor,
|
|
|
|
mut runtime: Runtime,
|
|
|
|
log: &slog::Logger,
|
|
|
|
) -> error::Result<()> {
|
2019-02-27 23:24:27 +00:00
|
|
|
// run service until ctrl-c
|
2019-03-31 07:57:48 +00:00
|
|
|
let (ctrlc_send, ctrlc_oneshot) = oneshot::channel();
|
2019-02-27 23:24:27 +00:00
|
|
|
let ctrlc_send_c = RefCell::new(Some(ctrlc_send));
|
|
|
|
ctrlc::set_handler(move || {
|
|
|
|
if let Some(ctrlc_send) = ctrlc_send_c.try_borrow_mut().unwrap().take() {
|
2019-03-01 01:45:01 +00:00
|
|
|
ctrlc_send.send(()).expect("Error sending ctrl-c message");
|
2019-02-27 23:24:27 +00:00
|
|
|
}
|
2019-03-19 11:53:51 +00:00
|
|
|
})
|
|
|
|
.map_err(|e| format!("Could not set ctrlc hander: {:?}", e))?;
|
2019-02-27 23:24:27 +00:00
|
|
|
|
2019-03-01 01:45:01 +00:00
|
|
|
let (exit_signal, exit) = exit_future::signal();
|
|
|
|
|
|
|
|
notifier::run(&client, executor, exit);
|
2019-02-27 23:24:27 +00:00
|
|
|
|
2019-03-19 11:53:51 +00:00
|
|
|
runtime
|
2019-03-31 07:57:48 +00:00
|
|
|
.block_on(ctrlc_oneshot)
|
2019-03-19 11:53:51 +00:00
|
|
|
.map_err(|e| format!("Ctrlc oneshot failed: {:?}", e))?;
|
2019-02-27 23:24:27 +00:00
|
|
|
|
2019-03-04 05:39:37 +00:00
|
|
|
// perform global shutdown operations.
|
2019-03-01 01:45:01 +00:00
|
|
|
info!(log, "Shutting down..");
|
|
|
|
exit_signal.fire();
|
2019-03-12 06:28:11 +00:00
|
|
|
// shutdown the client
|
|
|
|
// client.exit_signal.fire();
|
2019-03-01 01:45:01 +00:00
|
|
|
drop(client);
|
|
|
|
runtime.shutdown_on_idle().wait().unwrap();
|
2019-02-27 23:24:27 +00:00
|
|
|
Ok(())
|
|
|
|
}
|