Fix non-HTLC-inclusion balance calculation (mark II)
[rust-lightning] / src / chain / chaininterface.rs
index f456afda691b023c3285ffe466aecb9bfd411f76..7e9e9aeff05b902923703846337cc5c9b9666edd 100644 (file)
@@ -12,11 +12,11 @@ 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.
-       fn install_watch_outpoint(&self, outpoint: (Sha256dHash, u32));
+       fn install_watch_outpoint(&self, outpoint: (Sha256dHash, u32), out_script: &Script);
 
        /// Indicates that a listener needs to see all transactions.
        fn watch_all_txn(&self);
@@ -25,11 +25,9 @@ pub trait ChainWatchInterface: Sync + Send {
        //TODO: unregister
 }
 
-/// An interface to send a transaction to connected Bitcoin peers.
-/// This is for final settlement. An error might indicate that no peers can be reached or
-/// that peers rejected the transaction.
+/// An interface to send a transaction to the Bitcoin network.
 pub trait BroadcasterInterface: Sync + Send {
-       /// Sends a transaction out to (hopefully) be mined
+       /// Sends a transaction out to (hopefully) be mined.
        fn broadcast_transaction(&self, tx: &Transaction);
 }
 
@@ -59,7 +57,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.
@@ -72,13 +75,13 @@ pub struct ChainWatchInterfaceUtil {
 
 /// 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);
        }
 
-       fn install_watch_outpoint(&self, outpoint: (Sha256dHash, u32)) {
+       fn install_watch_outpoint(&self, outpoint: (Sha256dHash, u32), _out_script: &Script) {
                let mut watched = self.watched.lock().unwrap();
                watched.1.push(outpoint);
                self.reentered.fetch_add(1, Ordering::Relaxed);
@@ -105,8 +108,9 @@ impl ChainWatchInterfaceUtil {
                }
        }
 
-       /// notify listener that a block was connected
-       /// notification will repeat if notified listener register new listeners
+       /// 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) {
                let mut reentered = true;
                while reentered {
@@ -125,7 +129,7 @@ impl ChainWatchInterfaceUtil {
                }
        }
 
-       /// notify listener that a block was disconnected
+       /// Notify listeners that a block was disconnected.
        pub fn block_disconnected(&self, header: &BlockHeader) {
                let listeners = self.listeners.lock().unwrap().clone();
                for listener in listeners.iter() {
@@ -136,8 +140,11 @@ impl ChainWatchInterfaceUtil {
                }
        }
 
-       /// call listeners for connected blocks if they are still around.
-       /// returns true if notified listeners registered additional listener
+       /// 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).
        pub fn block_connected_checked(&self, header: &BlockHeader, height: u32, txn_matched: &[&Transaction], indexes_of_txn_matched: &[u32]) -> bool {
                let last_seen = self.reentered.load(Ordering::Relaxed);
 
@@ -151,13 +158,13 @@ impl ChainWatchInterfaceUtil {
                return last_seen != self.reentered.load(Ordering::Relaxed);
        }
 
-       /// Checks if a given transaction matches the current filter
+       /// Checks if a given transaction matches the current filter.
        pub fn does_match_tx(&self, tx: &Transaction) -> bool {
                let watched = self.watched.lock().unwrap();
                self.does_match_tx_unguarded (tx, &watched)
        }
 
-       fn does_match_tx_unguarded (&self, tx: &Transaction, watched: &MutexGuard<(Vec<Script>, Vec<(Sha256dHash, u32)>, bool)>) -> bool {
+       fn does_match_tx_unguarded(&self, tx: &Transaction, watched: &MutexGuard<(Vec<Script>, Vec<(Sha256dHash, u32)>, bool)>) -> bool {
                if watched.2 {
                        return true;
                }