From 4a8b8fe9a1cceba3c555bc6e8cf2765663cbecf0 Mon Sep 17 00:00:00 2001 From: Roman Zeyde Date: Tue, 5 Jun 2018 13:40:36 +0300 Subject: [PATCH] Move server-specific code into main.rs --- src/app.rs | 99 ++++++------------------------------------------- src/bin/main.rs | 90 +++++++++++++++++++++++++++++++++++++++++++- src/lib.rs | 18 ++++----- src/query.rs | 6 +-- 4 files changed, 112 insertions(+), 101 deletions(-) diff --git a/src/app.rs b/src/app.rs index 8628bcd..9fae4d2 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,14 +1,9 @@ use chan; use chan_signal; -use error_chain::ChainedError; use std::sync::Arc; -use std::thread; use std::time::Duration; -use config::Config; -use {daemon, index, query, rpc, store}; - -use errors::*; +use {daemon, index, store}; pub struct App { store: store::DBStore, @@ -17,6 +12,14 @@ pub struct App { } impl App { + pub fn new(store: store::DBStore, index: index::Index, daemon: daemon::Daemon) -> Arc { + Arc::new(App { + store, + index, + daemon, + }) + } + pub fn write_store(&self) -> &store::WriteStore { &self.store } @@ -31,17 +34,17 @@ impl App { } } -struct Waiter { +pub struct Waiter { signal: chan::Receiver, duration: Duration, } impl Waiter { - fn new(duration: Duration) -> Waiter { + pub fn new(duration: Duration) -> Waiter { let signal = chan_signal::notify(&[chan_signal::Signal::INT]); Waiter { signal, duration } } - fn wait(&self) -> Option { + pub fn wait(&self) -> Option { let signal = &self.signal; let timeout = chan::after(self.duration); let result; @@ -52,81 +55,3 @@ impl Waiter { result } } - -fn run_server(config: &Config) -> Result<()> { - let signal = Waiter::new(Duration::from_secs(5)); - let daemon = daemon::Daemon::new(config.network_type)?; - debug!("{:?}", daemon.getblockchaininfo()?); - - let store = store::DBStore::open( - config.db_path, - store::StoreOptions { - // compact manually after the first run has finished successfully - auto_compact: false, - }, - ); - let index = index::Index::load(&store); - 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 app = Arc::new(App { - store, - index, - daemon, - }); - - let query = Arc::new(query::Query::new(app.clone())); - rpc::start(&config.rpc_addr, query.clone()); - while let None = signal.wait() { - query.update_mempool()?; - if tip == app.daemon().getbestblockhash()? { - continue; - } - tip = app.index().update(app.write_store(), app.daemon())?; - } - info!("closing server"); - Ok(()) -} - -struct Repeat { - do_restart: bool, - iter_count: usize, -} - -impl Repeat { - fn new(config: &Config) -> Repeat { - Repeat { - do_restart: config.restart, - iter_count: 0, - } - } -} - -impl Iterator for Repeat { - type Item = (); - - fn next(&mut self) -> Option<()> { - self.iter_count += 1; - if self.iter_count == 1 { - return Some(()); // don't sleep before 1st iteration - } - thread::sleep(Duration::from_secs(1)); - if self.do_restart { - Some(()) - } else { - None - } - } -} - -pub fn main() { - let config = Config::from_args(); - for _ in Repeat::new(&config) { - match run_server(&config) { - Ok(_) => break, - Err(e) => error!("{}", e.display_chain()), - } - } -} diff --git a/src/bin/main.rs b/src/bin/main.rs index 44d706d..98a0201 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -1,7 +1,93 @@ extern crate electrs; -use electrs::app; +#[macro_use] +extern crate log; +#[macro_use] +extern crate error_chain; + +use error_chain::ChainedError; +use std::thread; +use std::time::Duration; + +use electrs::{app::{App, Waiter}, + config::Config, + daemon::Daemon, + errors::*, + index::Index, + query::Query, + rpc, + store::{DBStore, StoreOptions}}; + +fn run_server(config: &Config) -> Result<()> { + let daemon = Daemon::new(config.network_type)?; + debug!("{:?}", daemon.getblockchaininfo()?); + + let signal = Waiter::new(Duration::from_secs(5)); + let store = DBStore::open( + config.db_path, + StoreOptions { + // compact manually after the first run has finished successfully + auto_compact: false, + }, + ); + let index = Index::load(&store); + let mut tip = index.update(&store, &daemon)?; + store.compact_if_needed(); + drop(store); // to be re-opened soon + + let store = DBStore::open(config.db_path, StoreOptions { auto_compact: true }); + let app = App::new(store, index, daemon); + + let query = Query::new(app.clone()); + rpc::start(&config.rpc_addr, query.clone()); + while let None = signal.wait() { + query.update_mempool()?; + if tip == app.daemon().getbestblockhash()? { + continue; + } + tip = app.index().update(app.write_store(), app.daemon())?; + } + info!("closing server"); + Ok(()) +} + +struct Repeat { + do_restart: bool, + iter_count: usize, +} + +impl Repeat { + fn new(config: &Config) -> Repeat { + Repeat { + do_restart: config.restart, + iter_count: 0, + } + } +} + +impl Iterator for Repeat { + type Item = (); + + fn next(&mut self) -> Option<()> { + self.iter_count += 1; + if self.iter_count == 1 { + return Some(()); // don't sleep before 1st iteration + } + thread::sleep(Duration::from_secs(1)); + if self.do_restart { + Some(()) + } else { + None + } + } +} fn main() { - app::main() + let config = Config::from_args(); + for _ in Repeat::new(&config) { + match run_server(&config) { + Ok(_) => break, + Err(e) => error!("{}", e.display_chain()), + } + } } diff --git a/src/lib.rs b/src/lib.rs index 6bd0b5a..7c6fdbe 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,12 +27,12 @@ extern crate serde_derive; extern crate serde_json; pub mod app; -mod config; -mod daemon; -mod errors; -mod index; -mod mempool; -mod query; -mod rpc; -mod store; -mod util; +pub mod config; +pub mod daemon; +pub mod errors; +pub mod index; +pub mod mempool; +pub mod query; +pub mod rpc; +pub mod store; +pub mod util; diff --git a/src/query.rs b/src/query.rs index c14111d..5f90b87 100644 --- a/src/query.rs +++ b/src/query.rs @@ -138,11 +138,11 @@ pub struct Query { } impl Query { - pub fn new(app: Arc) -> Query { - Query { + pub fn new(app: Arc) -> Arc { + Arc::new(Query { app, tracker: RwLock::new(Tracker::new()), - } + }) } fn load_txns_by_prefix(