Update to rust-secp256k1 v0.11 and rust-bitcoin v0.14
[rust-lightning] / src / chain / chaininterface.rs
index f99f581f133a4fea18642c152444418d250a25f1..bda99b2d83c2d47ecf95a274b9cdc412f47db705 100644 (file)
@@ -2,7 +2,8 @@ use bitcoin::blockdata::block::{Block, BlockHeader};
 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
@@ -12,7 +13,7 @@ use std::sync::atomic::{AtomicUsize, Ordering};
 /// 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.
@@ -57,7 +58,12 @@ pub enum ConfirmationTarget {
 /// 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.
@@ -65,14 +71,15 @@ pub trait FeeEstimator: Sync + Send {
 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);
        }
 
@@ -95,15 +102,16 @@ impl ChainWatchInterface for ChainWatchInterfaceUtil {
 }
 
 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) {
@@ -135,7 +143,8 @@ impl ChainWatchInterfaceUtil {
                }
        }
 
-       /// 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).
@@ -172,7 +181,7 @@ impl ChainWatchInterfaceUtil {
                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;
                                }
                        }