Protect mempool tracker with a RwLock inside a Query
This commit is contained in:
parent
95676647f3
commit
4b0f3d470d
|
@ -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");
|
||||
|
|
24
src/query.rs
24
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<Tracker>,
|
||||
}
|
||||
|
||||
// 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")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user