From 4747e380ecbe224f6d162a82449d69caa63ea8fe Mon Sep 17 00:00:00 2001 From: Roman Zeyde Date: Tue, 22 May 2018 10:17:02 +0300 Subject: [PATCH] Update index once (instead at each status RPC) --- src/bin/indexrs.rs | 7 +++---- src/mempool.rs | 26 +++++++++++++++++--------- src/query.rs | 9 ++++----- 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/src/bin/indexrs.rs b/src/bin/indexrs.rs index dc647bd..a573ff2 100644 --- a/src/bin/indexrs.rs +++ b/src/bin/indexrs.rs @@ -83,15 +83,14 @@ fn run_server(config: &Config) { let query = query::Query::new(&store, &daemon, &index); crossbeam::scope(|scope| { - let poll_delay = Duration::from_secs(1); + let poll_delay = Duration::from_secs(5); scope.spawn(|| rpc::serve(config.rpc_addr(), &query)); loop { thread::sleep(poll_delay); query.update_mempool().unwrap(); - if tip == daemon.getbestblockhash().unwrap() { - continue; + if tip != daemon.getbestblockhash().unwrap() { + tip = index.update(&store, &daemon); } - tip = index.update(&store, &daemon); } }); } diff --git a/src/mempool.rs b/src/mempool.rs index f723b38..e5b5886 100644 --- a/src/mempool.rs +++ b/src/mempool.rs @@ -21,11 +21,15 @@ struct MempoolStore { } impl MempoolStore { - pub fn new() -> MempoolStore { + fn new() -> MempoolStore { MempoolStore { map: RwLock::new(BTreeMap::new()), } } + + fn clear(&self) { + self.map.write().unwrap().clear() + } } impl Store for MempoolStore { @@ -69,12 +73,14 @@ impl Stats { pub struct Tracker { stats: HashMap, + index: MempoolStore, } impl Tracker { pub fn new() -> Tracker { Tracker { stats: HashMap::new(), + index: MempoolStore::new(), } } @@ -128,6 +134,14 @@ impl Tracker { for txid in old_txids.difference(&new_txids) { self.remove(txid); } + + let mut rows = Vec::new(); + for stats in self.stats.values() { + index_transaction(&stats.tx, 0, &mut rows) + } + self.index.clear(); + self.index.persist(rows); + debug!( "mempool update took {:.1} ms ({} txns)", t.elapsed().in_seconds() * 1e3, @@ -144,14 +158,8 @@ impl Tracker { self.stats.remove(txid); } - pub fn build_index(&self) -> Box { - let mut rows = Vec::new(); - for stats in self.stats.values() { - index_transaction(&stats.tx, 0, &mut rows) - } - let store = MempoolStore::new(); - store.persist(rows); - Box::new(store) + pub fn index(&self) -> &Store { + &self.index } } diff --git a/src/query.rs b/src/query.rs index 7e3ede1..f63dfd3 100644 --- a/src/query.rs +++ b/src/query.rs @@ -213,17 +213,16 @@ impl<'a> Query<'a> { fn mempool_status(&self, script_hash: &[u8], confirmed_status: &Status) -> Status { let mut funding = vec![]; let mut spending = vec![]; - // TODO: build index once per Tracker::update() - let mempool_store = self.tracker.read().unwrap().build_index(); + let tracker = self.tracker.read().unwrap(); for t in self.load_txns( - &*mempool_store, - txids_by_script_hash(&*mempool_store, script_hash), + tracker.index(), + txids_by_script_hash(tracker.index(), script_hash), ) { funding.extend(self.find_funding_outputs(&t, script_hash)); } // // TODO: dedup outputs (somehow) both confirmed and in mempool (e.g. reorg?) for funding_output in funding.iter().chain(confirmed_status.funding.iter()) { - if let Some(spent) = self.find_spending_input(&*mempool_store, &funding_output) { + if let Some(spent) = self.find_spending_input(tracker.index(), &funding_output) { spending.push(spent); } }