Remove ChainListener
authorJeffrey Czyz <jkczyz@gmail.com>
Wed, 29 Jul 2020 20:02:29 +0000 (13:02 -0700)
committerJeffrey Czyz <jkczyz@gmail.com>
Thu, 1 Oct 2020 05:40:12 +0000 (22:40 -0700)
BlockNotifier was removed in the previous commit, thus ChainListener is
no longer needed. Instead, anything needing chain events should be
notified directly.

fuzz/src/chanmon_consistency.rs
fuzz/src/full_stack.rs
lightning/src/chain/chaininterface.rs
lightning/src/ln/channelmanager.rs
lightning/src/ln/channelmonitor.rs
lightning/src/ln/functional_test_utils.rs
lightning/src/ln/functional_tests.rs

index ce407f329bd194d3f580ca93fb71ba787fdebd4b..f12c2aa374b423e774283b4876271b682fc34af2 100644 (file)
@@ -30,7 +30,7 @@ use bitcoin::hash_types::{BlockHash, WPubkeyHash};
 
 use lightning::chain;
 use lightning::chain::transaction::OutPoint;
-use lightning::chain::chaininterface::{BroadcasterInterface, ChainListener, ConfirmationTarget, FeeEstimator};
+use lightning::chain::chaininterface::{BroadcasterInterface, ConfirmationTarget, FeeEstimator};
 use lightning::chain::keysinterface::{KeysInterface, InMemoryChannelKeys};
 use lightning::ln::channelmonitor;
 use lightning::ln::channelmonitor::{ChannelMonitor, ChannelMonitorUpdateErr, MonitorEvent};
index 03d2903204dae895a387120c68482b0f8739b8af..5980f9f172cb4b6e4097d16c60e2998b53b18583 100644 (file)
@@ -26,7 +26,7 @@ use bitcoin::hashes::sha256::Hash as Sha256;
 use bitcoin::hash_types::{Txid, BlockHash, WPubkeyHash};
 
 use lightning::chain;
-use lightning::chain::chaininterface::{BroadcasterInterface,ConfirmationTarget,ChainListener,FeeEstimator};
+use lightning::chain::chaininterface::{BroadcasterInterface, ConfirmationTarget, FeeEstimator};
 use lightning::chain::transaction::OutPoint;
 use lightning::chain::keysinterface::{InMemoryChannelKeys, KeysInterface};
 use lightning::ln::channelmonitor;
@@ -212,7 +212,7 @@ impl<'a> MoneyLossDetector<'a> {
        fn disconnect_block(&mut self) {
                if self.height > 0 && (self.max_height < 6 || self.height >= self.max_height - 6) {
                        let header = BlockHeader { version: 0x20000000, prev_blockhash: self.header_hashes[self.height], merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-                       self.manager.block_disconnected(&header, self.height as u32);
+                       self.manager.block_disconnected(&header);
                        self.monitor.block_disconnected(&header, self.height as u32);
                        self.height -= 1;
                        let removal_height = self.height;
index f65ae3611f623af549da1f75a556463363f8e0ae..a63e0bf1069475f4362ae69c17265519fadc8381 100644 (file)
@@ -13,7 +13,6 @@
 //! Includes traits for monitoring and receiving notifications of new blocks and block
 //! disconnections, transaction broadcasting, and feerate information requests.
 
-use bitcoin::blockdata::block::BlockHeader;
 use bitcoin::blockdata::transaction::Transaction;
 use bitcoin::blockdata::script::Script;
 use bitcoin::hash_types::Txid;
@@ -26,18 +25,6 @@ pub trait BroadcasterInterface: Sync + Send {
        fn broadcast_transaction(&self, tx: &Transaction);
 }
 
-/// A trait indicating a desire to listen for events from the chain
-pub trait ChainListener: Sync + Send {
-       /// Notifies a listener that a block was connected. Transactions may be filtered and are given
-       /// paired with their position within the block.
-       fn block_connected(&self, header: &BlockHeader, txdata: &[(usize, &Transaction)], height: u32);
-
-       /// Notifies a listener that a block was disconnected.
-       /// Unlike block_connected, this *must* never be called twice for the same disconnect event.
-       /// 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
 /// estimation.
 pub enum ConfirmationTarget {
@@ -53,8 +40,7 @@ pub enum ConfirmationTarget {
 /// 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).
+/// called from inside the library in response to chain events, P2P events, or timer events).
 pub trait FeeEstimator: Sync + Send {
        /// Gets estimated satoshis of fee required per 1000 Weight-Units.
        ///
index cfc83deb833c7e194c83f6a57c0936fb3a25d4e4..98e0ec91d50c098b56e693f964642f67b9eeaeea 100644 (file)
@@ -36,7 +36,7 @@ use bitcoin::secp256k1;
 
 use chain;
 use chain::Watch;
-use chain::chaininterface::{BroadcasterInterface,ChainListener,FeeEstimator};
+use chain::chaininterface::{BroadcasterInterface, FeeEstimator};
 use chain::transaction::OutPoint;
 use ln::channel::{Channel, ChannelError};
 use ln::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateErr, HTLC_FAIL_BACK_BUFFER, CLTV_CLAIM_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS, ANTI_REORG_DELAY, MonitorEvent};
@@ -3052,15 +3052,15 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
        }
 }
 
-impl<ChanSigner: ChannelKeys, M: Deref + Sync + Send, T: Deref + Sync + Send, K: Deref + Sync + Send, F: Deref + Sync + Send, L: Deref + Sync + Send>
-       ChainListener for ChannelManager<ChanSigner, M, T, K, F, L>
+impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<ChanSigner, M, T, K, F, L>
        where M::Target: chain::Watch<Keys=ChanSigner>,
         T::Target: BroadcasterInterface,
         K::Target: KeysInterface<ChanKeySigner = ChanSigner>,
         F::Target: FeeEstimator,
-                               L::Target: Logger,
+        L::Target: Logger,
 {
-       fn block_connected(&self, header: &BlockHeader, txdata: &[(usize, &Transaction)], height: u32) {
+       /// Updates channel state based on transactions seen in a connected block.
+       pub fn block_connected(&self, header: &BlockHeader, txdata: &[(usize, &Transaction)], height: u32) {
                let header_hash = header.block_hash();
                log_trace!(self.logger, "Block {} at height {} connected", header_hash, height);
                let _ = self.total_consistency_lock.read().unwrap();
@@ -3171,8 +3171,11 @@ impl<ChanSigner: ChannelKeys, M: Deref + Sync + Send, T: Deref + Sync + Send, K:
                }
        }
 
-       /// We force-close the channel without letting our counterparty participate in the shutdown
-       fn block_disconnected(&self, header: &BlockHeader, _: u32) {
+       /// Updates channel state based on a disconnected block.
+       ///
+       /// If necessary, the channel may be force-closed without letting the counterparty participate
+       /// in the shutdown.
+       pub fn block_disconnected(&self, header: &BlockHeader) {
                let _ = self.total_consistency_lock.read().unwrap();
                let mut failed_channels = Vec::new();
                {
index b601f741ed978a2d3827adf7b9c4b804409e7428..e93d1a4d583f41f4fd13e3433aac2b86bb61d332 100644 (file)
@@ -43,7 +43,7 @@ use ln::chan_utils::{CounterpartyCommitmentSecrets, HTLCOutputInCommitment, Hold
 use ln::channelmanager::{HTLCSource, PaymentPreimage, PaymentHash};
 use ln::onchaintx::{OnchainTxHandler, InputDescriptors};
 use chain;
-use chain::chaininterface::{ChainListener, ChainWatchedUtil, BroadcasterInterface, FeeEstimator};
+use chain::chaininterface::{ChainWatchedUtil, BroadcasterInterface, FeeEstimator};
 use chain::transaction::OutPoint;
 use chain::keysinterface::{SpendableOutputDescriptor, ChannelKeys};
 use util::logger::Logger;
@@ -188,10 +188,11 @@ pub struct HTLCUpdate {
 }
 impl_writeable!(HTLCUpdate, 0, { payment_hash, payment_preimage, source });
 
-/// An implementation of a [`chain::Watch`] and ChainListener.
+/// An implementation of [`chain::Watch`] for monitoring channels.
 ///
-/// May be used in conjunction with [`ChannelManager`] to monitor channels locally or used
-/// independently to monitor channels remotely.
+/// Connected and disconnected blocks must be provided to `ChainMonitor` as documented by
+/// [`chain::Watch`]. May be used in conjunction with [`ChannelManager`] to monitor channels locally
+/// or used independently to monitor channels remotely.
 ///
 /// [`chain::Watch`]: ../../chain/trait.Watch.html
 /// [`ChannelManager`]: ../channelmanager/struct.ChannelManager.html
@@ -269,13 +270,19 @@ impl WatchEventQueue {
        }
 }
 
-impl<ChanSigner: ChannelKeys, T: Deref + Sync + Send, F: Deref + Sync + Send, L: Deref + Sync + Send>
-       ChainListener for ChainMonitor<ChanSigner, T, F, L>
+impl<ChanSigner: ChannelKeys, T: Deref, F: Deref, L: Deref> ChainMonitor<ChanSigner, T, F, L>
        where T::Target: BroadcasterInterface,
              F::Target: FeeEstimator,
              L::Target: Logger,
 {
-       fn block_connected(&self, header: &BlockHeader, txdata: &[(usize, &Transaction)], height: u32) {
+       /// Dispatches to per-channel monitors, which are responsible for updating their on-chain view
+       /// of a channel and reacting accordingly based on transactions in the connected block. See
+       /// [`ChannelMonitor::block_connected`] for details. Any HTLCs that were resolved on chain will
+       /// be returned by [`chain::Watch::release_pending_monitor_events`].
+       ///
+       /// [`ChannelMonitor::block_connected`]: struct.ChannelMonitor.html#method.block_connected
+       /// [`chain::Watch::release_pending_monitor_events`]: ../../chain/trait.Watch.html#tymethod.release_pending_monitor_events
+       pub fn block_connected(&self, header: &BlockHeader, txdata: &[(usize, &Transaction)], height: u32) {
                let mut watch_events = self.watch_events.lock().unwrap();
                let matched_txn = watch_events.filter_block(txdata);
                {
@@ -292,7 +299,12 @@ impl<ChanSigner: ChannelKeys, T: Deref + Sync + Send, F: Deref + Sync + Send, L:
                }
        }
 
-       fn block_disconnected(&self, header: &BlockHeader, disconnected_height: u32) {
+       /// Dispatches to per-channel monitors, which are responsible for updating their on-chain view
+       /// of a channel based on the disconnected block. See [`ChannelMonitor::block_disconnected`] for
+       /// details.
+       ///
+       /// [`ChannelMonitor::block_disconnected`]: struct.ChannelMonitor.html#method.block_disconnected
+       pub fn block_disconnected(&self, header: &BlockHeader, disconnected_height: u32) {
                let mut monitors = self.monitors.lock().unwrap();
                for monitor in monitors.values_mut() {
                        monitor.block_disconnected(header, disconnected_height, &*self.broadcaster, &*self.fee_estimator, &*self.logger);
index eadc5787832f0b404199d9fe0f230e1f9bc41b52..44430a6453b4a3c52180218360f15aa57c666e94 100644 (file)
@@ -12,7 +12,6 @@
 
 use chain;
 use chain::Watch;
-use chain::chaininterface::ChainListener;
 use chain::transaction::OutPoint;
 use ln::channelmanager::{ChannelManager, ChannelManagerReadArgs, RAACommitmentOrder, PaymentPreimage, PaymentHash, PaymentSecret, PaymentSendFailure};
 use ln::channelmonitor::ChannelMonitor;
@@ -106,7 +105,7 @@ fn process_chain_watch_events(_events: &Vec<chain::WatchEvent>) {}
 
 pub fn disconnect_block<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, header: &BlockHeader, height: u32) {
        node.chain_monitor.chain_monitor.block_disconnected(header, height);
-       node.node.block_disconnected(header, height);
+       node.node.block_disconnected(header);
 }
 
 pub struct TestChanMonCfg {
index 1b664508b8aa49a7685385e442b7a7718c0c2478..03fedbf33f7dfcd5c660300a5d2a7b24adaf8346 100644 (file)
@@ -14,7 +14,6 @@
 use chain::Watch;
 use chain::transaction::OutPoint;
 use chain::keysinterface::{ChannelKeys, KeysInterface, SpendableOutputDescriptor};
-use chain::chaininterface::ChainListener;
 use ln::channel::{COMMITMENT_TX_BASE_WEIGHT, COMMITMENT_TX_WEIGHT_PER_HTLC};
 use ln::channelmanager::{ChannelManager, ChannelManagerReadArgs, RAACommitmentOrder, PaymentPreimage, PaymentHash, PaymentSecret, PaymentSendFailure, BREAKDOWN_TIMEOUT};
 use ln::channelmonitor::{ChannelMonitor, CLTV_CLAIM_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS, ANTI_REORG_DELAY};
@@ -3558,10 +3557,8 @@ fn test_unconf_chan() {
                header = BlockHeader { version: 0x20000000, prev_blockhash: header.block_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
                headers.push(header.clone());
        }
-       let mut height = 99;
        while !headers.is_empty() {
-               nodes[0].node.block_disconnected(&headers.pop().unwrap(), height);
-               height -= 1;
+               nodes[0].node.block_disconnected(&headers.pop().unwrap());
        }
        check_closed_broadcast!(nodes[0], false);
        check_added_monitors!(nodes[0], 1);