Merge pull request #386 from TheBlueMatt/2019-10-useless-lints
[rust-lightning] / src / chain / chaininterface.rs
index 6f6362284072958115e97d5fe585d4e93e96e2ec..c0330fb2963ea868d753c16dd633d5cbbd8890c9 100644 (file)
@@ -1,10 +1,16 @@
+//! Traits and utility impls which allow other parts of rust-lightning to interact with the
+//! blockchain.
+//!
+//! Includes traits for monitoring and receiving notifications of new blocks and block
+//! disconnections, transaction broadcasting, and feerate information requests.
+
 use bitcoin::blockdata::block::{Block, BlockHeader};
 use bitcoin::blockdata::transaction::Transaction;
 use bitcoin::blockdata::script::Script;
 use bitcoin::blockdata::constants::genesis_block;
-use bitcoin::util::hash::Sha256dHash;
+use bitcoin::util::hash::BitcoinHash;
+use bitcoin_hashes::sha256d::Hash as Sha256dHash;
 use bitcoin::network::constants::Network;
-use bitcoin::network::serialize::BitcoinHash;
 
 use util::logger::Logger;
 
@@ -24,6 +30,7 @@ pub enum ChainError {
 
 /// An interface to request notification of certain scripts as they appear the
 /// chain.
+///
 /// Note that all of the functions implemented here *must* be reentrant-safe (obviously - they're
 /// called from inside the library in response to ChainListener events, P2P events, or timer
 /// events).
@@ -38,6 +45,8 @@ pub trait ChainWatchInterface: Sync + Send {
        /// Indicates that a listener needs to see all transactions.
        fn watch_all_txn(&self);
 
+       /// Register the given listener to receive events. Only a weak pointer is provided and the
+       /// registration should be freed once that pointer expires.
        fn register_listener(&self, listener: Weak<ChainListener>);
        //TODO: unregister
 
@@ -60,14 +69,17 @@ pub trait ChainListener: Sync + Send {
        /// Note that if a new transaction/outpoint is watched during a block_connected call, the block
        /// *must* be re-scanned with the new transaction/outpoints and block_connected should be
        /// called again with the same header and (at least) the new transactions.
+       ///
        /// Note that if non-new transaction/outpoints may be registered during a call, a second call
        /// *must not* happen.
+       ///
        /// This also means those counting confirmations using block_connected callbacks should watch
        /// for duplicate headers and not count them towards confirmations!
        fn block_connected(&self, header: &BlockHeader, height: u32, txn_matched: &[&Transaction], indexes_of_txn_matched: &[u32]);
        /// Notifies a listener that a block was disconnected.
        /// Unlike block_connected, this *must* never be called twice for the same disconnect event.
-       fn block_disconnected(&self, header: &BlockHeader);
+       /// Height must be the one of the block which was disconnected (not new height of the best chain)
+       fn block_disconnected(&self, header: &BlockHeader, disconnected_height: u32);
 }
 
 /// An enum that represents the speed at which we want a transaction to confirm used for feerate
@@ -83,15 +95,19 @@ pub enum ConfirmationTarget {
 
 /// A trait which should be implemented to provide feerate information on a number of time
 /// horizons.
+///
 /// Note that all of the functions implemented here *must* be reentrant-safe (obviously - they're
 /// called from inside the library in response to ChainListener events, P2P events, or timer
 /// events).
 pub trait FeeEstimator: Sync + Send {
-       /// Gets estimated satoshis of fee required per 1000 Weight-Units. This translates to:
-       ///  * satoshis-per-byte * 250
-       ///  * ceil(satoshis-per-kbyte / 4)
+       /// Gets estimated satoshis of fee required per 1000 Weight-Units.
+       ///
        /// 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).
+       ///
+       /// This translates to:
+       ///  * satoshis-per-byte * 250
+       ///  * ceil(satoshis-per-kbyte / 4)
        fn get_est_sat_per_1000_weight(&self, confirmation_target: ConfirmationTarget) -> u64;
 }
 
@@ -129,7 +145,7 @@ impl ChainWatchedUtil {
                }
                #[cfg(not(test))]
                {
-                       let _tx_unused = txid; // Its used in cfg(test), though
+                       let _tx_unused = txid; // It's used in cfg(test), though
                        self.watched_txn.insert(script_pub_key.clone())
                }
        }
@@ -141,7 +157,7 @@ impl ChainWatchedUtil {
                self.watched_outpoints.insert(outpoint)
        }
 
-       /// Sets us to match all transactions, returning true if this is a new setting anf false if
+       /// Sets us to match all transactions, returning true if this is a new setting and false if
        /// we'd already been set to match everything.
        pub fn watch_all(&mut self) -> bool {
                if self.watch_all { return false; }
@@ -183,6 +199,7 @@ impl ChainWatchedUtil {
 }
 
 /// Utility to capture some common parts of ChainWatchInterface implementors.
+///
 /// Keeping a local copy of this in a ChainWatchInterface implementor is likely useful.
 pub struct ChainWatchInterfaceUtil {
        network: Network,
@@ -229,6 +246,7 @@ impl ChainWatchInterface for ChainWatchInterfaceUtil {
 }
 
 impl ChainWatchInterfaceUtil {
+       /// Creates a new ChainWatchInterfaceUtil for the given network
        pub fn new(network: Network, logger: Arc<Logger>) -> ChainWatchInterfaceUtil {
                ChainWatchInterfaceUtil {
                        network: network,
@@ -240,6 +258,7 @@ impl ChainWatchInterfaceUtil {
        }
 
        /// 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) {
@@ -261,11 +280,11 @@ impl ChainWatchInterfaceUtil {
        }
 
        /// Notify listeners that a block was disconnected.
-       pub fn block_disconnected(&self, header: &BlockHeader) {
+       pub fn block_disconnected(&self, header: &BlockHeader, disconnected_height: u32) {
                let listeners = self.listeners.lock().unwrap().clone();
                for listener in listeners.iter() {
                        match listener.upgrade() {
-                               Some(arc) => arc.block_disconnected(header),
+                               Some(arc) => arc.block_disconnected(&header, disconnected_height),
                                None => ()
                        }
                }
@@ -273,6 +292,7 @@ impl ChainWatchInterfaceUtil {
 
        /// 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).