From 38bb8411e17e38671b1bec8a145785cc38f03557 Mon Sep 17 00:00:00 2001 From: Roman Zeyde Date: Wed, 23 May 2018 17:40:29 +0300 Subject: [PATCH] Move most of the application code back into the library --- src/app.rs | 106 +++++++++++++++++++++++++++++++++++++++++++ src/bin/indexrs.rs | 110 +-------------------------------------------- src/lib.rs | 17 ++++--- 3 files changed, 118 insertions(+), 115 deletions(-) create mode 100644 src/app.rs diff --git a/src/app.rs b/src/app.rs new file mode 100644 index 0000000..d4a64cc --- /dev/null +++ b/src/app.rs @@ -0,0 +1,106 @@ +use argparse::{ArgumentParser, StoreTrue}; +use crossbeam; +use std::fs::OpenOptions; +use std::net::SocketAddr; +use std::thread; +use std::time::Duration; + +use daemon::Network; +use {daemon, index, query, rpc, store}; + +#[derive(Debug)] +struct Config { + log_file: String, + network_type: Network, // bitcoind JSONRPC endpoint + db_path: &'static str, // RocksDB directory path + rpc_addr: SocketAddr, // for serving Electrum clients +} + +impl Config { + pub fn from_args() -> Config { + let mut testnet = false; + { + let mut parser = ArgumentParser::new(); + parser.set_description("Bitcoin indexing server."); + parser.refer(&mut testnet).add_option( + &["--testnet"], + StoreTrue, + "Connect to a testnet bitcoind instance", + ); + parser.parse_args_or_exit(); + } + let network_type = match testnet { + false => Network::Mainnet, + true => Network::Testnet, + }; + Config { + log_file: "indexrs.log".to_string(), + network_type: network_type, + db_path: match network_type { + Network::Mainnet => "./db/mainnet", + Network::Testnet => "./db/testnet", + }, + rpc_addr: match network_type { + Network::Mainnet => "127.0.0.1:50001", + Network::Testnet => "127.0.0.1:60001", + }.parse() + .unwrap(), + } + } +} + +fn run_server(config: &Config) { + let index = index::Index::new(); + let daemon = daemon::Daemon::new(config.network_type); + + let store = store::DBStore::open( + config.db_path, + store::StoreOptions { + // compact manually after the first run has finished successfully + auto_compact: false, + }, + ); + let mut tip = index.update(&store, &daemon); + store.compact_if_needed(); + drop(store); // to be re-opened soon + + let store = store::DBStore::open(config.db_path, store::StoreOptions { auto_compact: true }); + let query = query::Query::new(&store, &daemon, &index); + + crossbeam::scope(|scope| { + let poll_delay = Duration::from_secs(5); + scope.spawn(|| rpc::serve(&config.rpc_addr, &query)); + loop { + thread::sleep(poll_delay); + query.update_mempool().unwrap(); + if tip != daemon.getbestblockhash().unwrap() { + tip = index.update(&store, &daemon); + } + } + }); +} + +fn setup_logging(config: &Config) { + use simplelog::*; + let mut cfg = Config::default(); + cfg.time_format = Some("%F %H:%M:%S%.3f"); + CombinedLogger::init(vec![ + TermLogger::new(LevelFilter::Info, cfg.clone()).unwrap(), + WriteLogger::new( + LevelFilter::Debug, + cfg.clone(), + OpenOptions::new() + .create(true) + .append(true) + .open(&config.log_file) + .unwrap(), + ), + ]).unwrap(); + info!("config: {:?}", config); +} + +pub fn main() { + let config = Config::from_args(); + setup_logging(&config); + run_server(&config) +} diff --git a/src/bin/indexrs.rs b/src/bin/indexrs.rs index e966ed4..68ca9be 100644 --- a/src/bin/indexrs.rs +++ b/src/bin/indexrs.rs @@ -1,113 +1,7 @@ -extern crate argparse; -extern crate bitcoin; -extern crate crossbeam; extern crate indexrs; -extern crate simplelog; -#[macro_use] -extern crate log; - -use argparse::{ArgumentParser, StoreTrue}; -use indexrs::daemon::Network; -use indexrs::{daemon, index, query, rpc, store}; -use std::fs::OpenOptions; -use std::net::SocketAddr; -use std::thread; -use std::time::Duration; - -#[derive(Debug)] -struct Config { - log_file: String, - network_type: Network, // bitcoind JSONRPC endpoint - db_path: &'static str, // RocksDB directory path - rpc_addr: SocketAddr, // for serving Electrum clients -} - -impl Config { - pub fn from_args() -> Config { - let mut testnet = false; - { - let mut parser = ArgumentParser::new(); - parser.set_description("Bitcoin indexing server."); - parser.refer(&mut testnet).add_option( - &["--testnet"], - StoreTrue, - "Connect to a testnet bitcoind instance", - ); - parser.parse_args_or_exit(); - } - let network_type = match testnet { - false => Network::Mainnet, - true => Network::Testnet, - }; - Config { - log_file: "indexrs.log".to_string(), - network_type: network_type, - db_path: match network_type { - Network::Mainnet => "./db/mainnet", - Network::Testnet => "./db/testnet", - }, - rpc_addr: match network_type { - Network::Mainnet => "127.0.0.1:50001", - Network::Testnet => "127.0.0.1:60001", - }.parse() - .unwrap(), - } - } -} - -fn run_server(config: &Config) { - let index = index::Index::new(); - let daemon = daemon::Daemon::new(config.network_type); - - let store = store::DBStore::open( - config.db_path, - store::StoreOptions { - // compact manually after the first run has finished successfully - auto_compact: false, - }, - ); - let mut tip = index.update(&store, &daemon); - store.compact_if_needed(); - drop(store); // to be re-opened soon - - let store = store::DBStore::open(config.db_path, store::StoreOptions { auto_compact: true }); - let query = query::Query::new(&store, &daemon, &index); - - crossbeam::scope(|scope| { - let poll_delay = Duration::from_secs(5); - scope.spawn(|| rpc::serve(&config.rpc_addr, &query)); - loop { - thread::sleep(poll_delay); - query.update_mempool().unwrap(); - if tip != daemon.getbestblockhash().unwrap() { - tip = index.update(&store, &daemon); - } - } - }); -} - -fn setup_logging(config: &Config) { - use simplelog::*; - let mut cfg = Config::default(); - cfg.time_format = Some("%F %H:%M:%S%.3f"); - CombinedLogger::init(vec![ - TermLogger::new(LevelFilter::Info, cfg.clone()).unwrap(), - WriteLogger::new( - LevelFilter::Debug, - cfg.clone(), - OpenOptions::new() - .create(true) - .append(true) - .open(&config.log_file) - .unwrap(), - ), - ]).unwrap(); - info!("config: {:?}", config); -} +use indexrs::app; fn main() { - let config = Config::from_args(); - setup_logging(&config); - run_server(&config) + app::main() } diff --git a/src/lib.rs b/src/lib.rs index 21d281c..dfa3e3c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,6 @@ #![recursion_limit = "1024"] +extern crate argparse; extern crate base64; extern crate bincode; extern crate bitcoin; @@ -10,6 +11,7 @@ extern crate hex; extern crate pbr; extern crate rocksdb; extern crate serde; +extern crate simplelog; extern crate time; extern crate tokio; @@ -24,10 +26,11 @@ extern crate serde_derive; #[macro_use] extern crate serde_json; -pub mod daemon; -pub mod index; -pub mod mempool; -pub mod query; -pub mod rpc; -pub mod store; -pub mod util; +pub mod app; +mod daemon; +mod index; +mod mempool; +mod query; +mod rpc; +mod store; +mod util;