Protect mempool tracker with a RwLock inside a Query

This commit is contained in:
Roman Zeyde 2018-05-17 12:28:15 +03:00
parent 95676647f3
commit 4b0f3d470d
No known key found for this signature in database
GPG Key ID: 87CAE5FA46917CBB
2 changed files with 19 additions and 6 deletions

View File

@ -89,6 +89,7 @@ fn run_server(config: &Config) {
scope.spawn(|| rpc::serve(config.rpc_addr(), &query, chan)); scope.spawn(|| rpc::serve(config.rpc_addr(), &query, chan));
loop { loop {
thread::sleep(poll_delay); thread::sleep(poll_delay);
query.update_mempool();
let current_tip = daemon let current_tip = daemon
.getbestblockhash() .getbestblockhash()
.expect("failed to get latest blockhash"); .expect("failed to get latest blockhash");

View File

@ -3,18 +3,14 @@ use bitcoin::blockdata::transaction::Transaction;
use bitcoin::network::serialize::deserialize; use bitcoin::network::serialize::deserialize;
use bitcoin::util::hash::Sha256dHash; use bitcoin::util::hash::Sha256dHash;
use itertools::enumerate; use itertools::enumerate;
use std::sync::RwLock;
use daemon::Daemon; use daemon::Daemon;
use index::{compute_script_hash, HeaderEntry, Index, TxInRow, TxOutRow, TxRow}; use index::{compute_script_hash, HeaderEntry, Index, TxInRow, TxOutRow, TxRow};
use mempool::Tracker;
use store::Store; use store::Store;
use types::HashPrefix; use types::HashPrefix;
pub struct Query<'a> {
store: &'a Store,
daemon: &'a Daemon,
index: &'a Index,
}
pub struct FundingOutput { pub struct FundingOutput {
pub txn_id: Sha256dHash, pub txn_id: Sha256dHash,
pub height: i32, pub height: i32,
@ -44,6 +40,13 @@ fn merklize(left: Sha256dHash, right: Sha256dHash) -> Sha256dHash {
Sha256dHash::from_data(&data) Sha256dHash::from_data(&data)
} }
pub struct Query<'a> {
store: &'a Store,
daemon: &'a Daemon,
index: &'a Index,
tracker: RwLock<Tracker>,
}
// TODO: return errors instead of panics // TODO: return errors instead of panics
impl<'a> Query<'a> { impl<'a> Query<'a> {
pub fn new(store: &'a Store, daemon: &'a Daemon, index: &'a Index) -> 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, store,
daemon, daemon,
index, index,
tracker: RwLock::new(Tracker::new()),
} }
} }
@ -202,4 +206,12 @@ impl<'a> Query<'a> {
} }
Some((merkle, pos)) Some((merkle, pos))
} }
pub fn update_mempool(&self) {
self.tracker
.write()
.unwrap()
.update(self.daemon)
.expect("failed to update mempool")
}
} }