1
0
mirror of https://github.com/romanz/electrs.git synced 2024-11-19 01:43:29 +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>"]
[dependencies]
argparse = "0.2.1"
arrayref = "0.3.4"
bincode = "1.0.0"
bitcoin = "0.12"

View File

@ -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)
}