use bitcoin::blockdata::transaction::Transaction;
use bitcoin::blockdata::script::Script;
use bitcoin::util::hash::Sha256dHash;
-use std::sync::{Mutex,Weak,MutexGuard};
+use util::logger::Logger;
+use std::sync::{Mutex,Weak,MutexGuard,Arc};
use std::sync::atomic::{AtomicUsize, Ordering};
/// An interface to request notification of certain scripts as they appear the
/// events).
pub trait ChainWatchInterface: Sync + Send {
/// Provides a scriptPubKey which much be watched for.
- fn install_watch_script(&self, script_pub_key: Script);
+ fn install_watch_script(&self, script_pub_key: &Script);
/// Provides an outpoint which must be watched for, providing any transactions which spend the
/// given outpoint.
/// called from inside the library in response to ChainListener events, P2P events, or timer
/// events).
pub trait FeeEstimator: Sync + Send {
- fn get_est_sat_per_vbyte(&self, confirmation_target: ConfirmationTarget) -> u64;
+ /// Gets estimated satoshis of fee required per 1000 Weight-Units. This translates to:
+ /// * satoshis-per-byte * 250
+ /// * ceil(satoshis-per-kbyte / 4)
+ /// Must be no smaller than 253 (ie 1 satoshi-per-byte rounded up to ensure later round-downs
+ /// don't put us below 1 satoshi-per-byte).
+ fn get_est_sat_per_1000_weight(&self, confirmation_target: ConfirmationTarget) -> u64;
}
/// Utility to capture some common parts of ChainWatchInterface implementors.
pub struct ChainWatchInterfaceUtil {
watched: Mutex<(Vec<Script>, Vec<(Sha256dHash, u32)>, bool)>, //TODO: Something clever to optimize this
listeners: Mutex<Vec<Weak<ChainListener>>>,
- reentered: AtomicUsize
+ reentered: AtomicUsize,
+ logger: Arc<Logger>,
}
/// Register listener
impl ChainWatchInterface for ChainWatchInterfaceUtil {
- fn install_watch_script(&self, script_pub_key: Script) {
+ fn install_watch_script(&self, script_pub_key: &Script) {
let mut watched = self.watched.lock().unwrap();
- watched.0.push(Script::from(script_pub_key));
+ watched.0.push(script_pub_key.clone());
self.reentered.fetch_add(1, Ordering::Relaxed);
}
}
impl ChainWatchInterfaceUtil {
- pub fn new() -> ChainWatchInterfaceUtil {
+ pub fn new(logger: Arc<Logger>) -> ChainWatchInterfaceUtil {
ChainWatchInterfaceUtil {
watched: Mutex::new((Vec::new(), Vec::new(), false)),
listeners: Mutex::new(Vec::new()),
- reentered: AtomicUsize::new(1)
+ reentered: AtomicUsize::new(1),
+ logger: logger,
}
}
- /// Notify listeners that a block was connected.
+ /// Notify listeners that a block was connected given a full, unfiltered block.
/// Handles re-scanning the block and calling block_connected again if listeners register new
/// watch data during the callbacks for you (see ChainListener::block_connected for more info).
pub fn block_connected_with_filtering(&self, block: &Block, height: u32) {
}
}
- /// Notify listeners that a block was connected.
+ /// Notify listeners that a block was connected, given pre-filtered list of transactions in the
+ /// block which matched the filter (probably using does_match_tx).
/// Returns true if notified listeners registered additional watch data (implying that the
/// block must be re-scanned and this function called again prior to further block_connected
/// calls, see ChainListener::block_connected for more info).
for input in tx.input.iter() {
for outpoint in watched.1.iter() {
let &(outpoint_hash, outpoint_index) = outpoint;
- if outpoint_hash == input.prev_hash && outpoint_index == input.prev_index {
+ if outpoint_hash == input.previous_output.txid && outpoint_index == input.previous_output.vout {
return true;
}
}