1
0
mirror of https://github.com/romanz/electrs.git synced 2024-11-19 09:54:09 +01:00

Allow query-only server via --disable-indexing flag

This commit is contained in:
Roman Zeyde 2018-04-23 10:58:39 +03:00
parent 1af76c2833
commit 78ce5c9203
No known key found for this signature in database
GPG Key ID: 87CAE5FA46917CBB
2 changed files with 47 additions and 20 deletions

View File

@ -4,6 +4,7 @@ version = "0.1.0"
authors = ["Roman Zeyde <me@romanzey.de>"] authors = ["Roman Zeyde <me@romanzey.de>"]
[dependencies] [dependencies]
argparse = "0.2.1"
arrayref = "0.3.4" arrayref = "0.3.4"
bincode = "1.0.0" bincode = "1.0.0"
bitcoin = "0.12" bitcoin = "0.12"

View File

@ -1,9 +1,11 @@
extern crate simplelog; extern crate simplelog;
extern crate argparse;
extern crate crossbeam; extern crate crossbeam;
extern crate indexrs; extern crate indexrs;
extern crate zmq; extern crate zmq;
use argparse::{ArgumentParser, StoreFalse};
use std::fs::OpenOptions; use std::fs::OpenOptions;
use indexrs::{daemon, index, store, waiter}; use indexrs::{daemon, index, store, waiter};
@ -25,15 +27,36 @@ fn setup_logging() {
]).unwrap(); ]).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 waiter = waiter::Waiter::new("tcp://localhost:28332");
let daemon = daemon::Daemon::new("http://localhost:8332"); let daemon = daemon::Daemon::new("http://localhost:8332");
{ if config.enable_indexing {
let store = store::Store::open( let store = store::Store::open(
DB_PATH, DB_PATH,
store::StoreOptions { store::StoreOptions {
// compact manually after the first run has finished successfully
auto_compact: false, auto_compact: false,
}, },
); );
@ -44,30 +67,33 @@ fn run_server() {
let store = store::Store::open(DB_PATH, store::StoreOptions { auto_compact: true }); let store = store::Store::open(DB_PATH, store::StoreOptions { auto_compact: true });
{ {
crossbeam::scope(|scope| { crossbeam::scope(|scope| {
scope.spawn(|| { scope.spawn(|| handle_queries(&store, &daemon));
let q = index::Query::new(&store, &daemon); if config.enable_indexing {
let ctx = zmq::Context::new();
let sock = ctx.socket(zmq::SocketType::REP).unwrap();
sock.bind("tcp://127.0.0.1:19740").unwrap();
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 { loop {
if store.read_header(&waiter.wait()).is_none() { if store.read_header(&waiter.wait()).is_none() {
index::update(&store, &daemon); index::update(&store, &daemon);
} }
} }
}
}); });
} }
} }
fn main() { fn main() {
setup_logging(); let mut config = Config {
run_server() 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(config)
} }