From 4b0f3d470dfd1d6cd8ec16365c574f46f8074dd7 Mon Sep 17 00:00:00 2001 From: Roman Zeyde Date: Thu, 17 May 2018 12:28:15 +0300 Subject: [PATCH] Protect mempool tracker with a RwLock inside a Query --- src/bin/indexrs.rs | 1 + src/query.rs | 24 ++++++++++++++++++------ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/bin/indexrs.rs b/src/bin/indexrs.rs index c72210e..b594f45 100644 --- a/src/bin/indexrs.rs +++ b/src/bin/indexrs.rs @@ -89,6 +89,7 @@ fn run_server(config: &Config) { scope.spawn(|| rpc::serve(config.rpc_addr(), &query, chan)); loop { thread::sleep(poll_delay); + query.update_mempool(); let current_tip = daemon .getbestblockhash() .expect("failed to get latest blockhash"); diff --git a/src/query.rs b/src/query.rs index 6c48e40..884873e 100644 --- a/src/query.rs +++ b/src/query.rs @@ -3,18 +3,14 @@ use bitcoin::blockdata::transaction::Transaction; use bitcoin::network::serialize::deserialize; use bitcoin::util::hash::Sha256dHash; use itertools::enumerate; +use std::sync::RwLock; use daemon::Daemon; use index::{compute_script_hash, HeaderEntry, Index, TxInRow, TxOutRow, TxRow}; +use mempool::Tracker; use store::Store; use types::HashPrefix; -pub struct Query<'a> { - store: &'a Store, - daemon: &'a Daemon, - index: &'a Index, -} - pub struct FundingOutput { pub txn_id: Sha256dHash, pub height: i32, @@ -44,6 +40,13 @@ fn merklize(left: Sha256dHash, right: Sha256dHash) -> Sha256dHash { Sha256dHash::from_data(&data) } +pub struct Query<'a> { + store: &'a Store, + daemon: &'a Daemon, + index: &'a Index, + tracker: RwLock, +} + // TODO: return errors instead of panics impl<'a> Query<'a> { pub fn new(store: &'a Store, daemon: &'a Daemon, index: &'a Index) -> Query<'a> { @@ -51,6 +54,7 @@ impl<'a> Query<'a> { store, daemon, index, + tracker: RwLock::new(Tracker::new()), } } @@ -202,4 +206,12 @@ impl<'a> Query<'a> { } Some((merkle, pos)) } + + pub fn update_mempool(&self) { + self.tracker + .write() + .unwrap() + .update(self.daemon) + .expect("failed to update mempool") + } }