From 78ce5c92035e5d76673e552aaff0f84b2e7a9165 Mon Sep 17 00:00:00 2001 From: Roman Zeyde Date: Mon, 23 Apr 2018 10:58:39 +0300 Subject: [PATCH] Allow query-only server via --disable-indexing flag --- Cargo.toml | 1 + src/bin/index_server.rs | 66 ++++++++++++++++++++++++++++------------- 2 files changed, 47 insertions(+), 20 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a3e0390..e8a7b72 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,7 @@ version = "0.1.0" authors = ["Roman Zeyde "] [dependencies] +argparse = "0.2.1" arrayref = "0.3.4" bincode = "1.0.0" bitcoin = "0.12" diff --git a/src/bin/index_server.rs b/src/bin/index_server.rs index 751a06f..1971f68 100644 --- a/src/bin/index_server.rs +++ b/src/bin/index_server.rs @@ -1,9 +1,11 @@ extern crate simplelog; +extern crate argparse; extern crate crossbeam; extern crate indexrs; extern crate zmq; +use argparse::{ArgumentParser, StoreFalse}; use std::fs::OpenOptions; use indexrs::{daemon, index, store, waiter}; @@ -25,15 +27,36 @@ fn setup_logging() { ]).unwrap(); } -const DB_PATH: &str = "db/mainnet"; +const DB_PATH: &str = "./db/mainnet"; +const RPC_ADDRESS: &str = "ipc:///tmp/indexrs.rpc"; -fn run_server() { +struct Config { + enable_indexing: bool, +} + +fn handle_queries(store: &store::Store, daemon: &daemon::Daemon) { + let query = index::Query::new(&store, &daemon); + + let ctx = zmq::Context::new(); + let sock = ctx.socket(zmq::SocketType::REP).unwrap(); + sock.bind(RPC_ADDRESS).unwrap(); + + loop { + let script_hash = sock.recv_bytes(0).unwrap(); + let balance = query.balance(&script_hash); + let reply = format!("{}", balance); + sock.send(&reply.into_bytes(), 0).unwrap(); + } +} + +fn run_server(config: Config) { let waiter = waiter::Waiter::new("tcp://localhost:28332"); let daemon = daemon::Daemon::new("http://localhost:8332"); - { + if config.enable_indexing { let store = store::Store::open( DB_PATH, store::StoreOptions { + // compact manually after the first run has finished successfully auto_compact: false, }, ); @@ -44,23 +67,12 @@ fn run_server() { let store = store::Store::open(DB_PATH, store::StoreOptions { auto_compact: true }); { crossbeam::scope(|scope| { - scope.spawn(|| { - let q = index::Query::new(&store, &daemon); - - let ctx = zmq::Context::new(); - let sock = ctx.socket(zmq::SocketType::REP).unwrap(); - sock.bind("tcp://127.0.0.1:19740").unwrap(); - + scope.spawn(|| handle_queries(&store, &daemon)); + if config.enable_indexing { loop { - let script_hash = sock.recv_bytes(0).unwrap(); - let b = q.balance(&script_hash); - let reply = format!("{}", b); - sock.send(&reply.into_bytes(), 0).unwrap(); - } - }); - loop { - if store.read_header(&waiter.wait()).is_none() { - index::update(&store, &daemon); + if store.read_header(&waiter.wait()).is_none() { + index::update(&store, &daemon); + } } } }); @@ -68,6 +80,20 @@ fn run_server() { } fn main() { + let mut config = Config { + enable_indexing: true, + }; + { + let mut parser = ArgumentParser::new(); + parser.set_description("Bitcoin indexing server."); + parser.refer(&mut config.enable_indexing).add_option( + &["--disable-indexing"], + StoreFalse, + "Disable indexing server (allow queries on existing DB)", + ); + parser.parse_args_or_exit(); + } + setup_logging(); - run_server() + run_server(config) }