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:
parent
1af76c2833
commit
78ce5c9203
@ -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"
|
||||||
|
@ -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,23 +67,12 @@ 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 {
|
loop {
|
||||||
let script_hash = sock.recv_bytes(0).unwrap();
|
if store.read_header(&waiter.wait()).is_none() {
|
||||||
let b = q.balance(&script_hash);
|
index::update(&store, &daemon);
|
||||||
let reply = format!("{}", b);
|
}
|
||||||
sock.send(&reply.into_bytes(), 0).unwrap();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
loop {
|
|
||||||
if store.read_header(&waiter.wait()).is_none() {
|
|
||||||
index::update(&store, &daemon);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -68,6 +80,20 @@ fn run_server() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
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();
|
setup_logging();
|
||||||
run_server()
|
run_server(config)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user