From cb7badac3d054dfbe75d263ae514b7edcd1e1222 Mon Sep 17 00:00:00 2001 From: Jeffrey Czyz Date: Wed, 29 Jul 2020 13:02:29 -0700 Subject: [PATCH] Remove ChainListener 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 | 2 +- fuzz/src/full_stack.rs | 2 +- lightning/src/chain/chaininterface.rs | 16 +--------------- lightning/src/ln/channelmanager.rs | 12 ++++++------ lightning/src/ln/channelmonitor.rs | 19 +++++++++++++------ lightning/src/ln/functional_test_utils.rs | 1 - lightning/src/ln/functional_tests.rs | 1 - 7 files changed, 22 insertions(+), 31 deletions(-) diff --git a/fuzz/src/chanmon_consistency.rs b/fuzz/src/chanmon_consistency.rs index 2face646a..43a4fb2aa 100644 --- a/fuzz/src/chanmon_consistency.rs +++ b/fuzz/src/chanmon_consistency.rs @@ -22,7 +22,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, HTLCUpdate}; diff --git a/fuzz/src/full_stack.rs b/fuzz/src/full_stack.rs index 251c96599..a869078f6 100644 --- a/fuzz/src/full_stack.rs +++ b/fuzz/src/full_stack.rs @@ -18,7 +18,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; diff --git a/lightning/src/chain/chaininterface.rs b/lightning/src/chain/chaininterface.rs index 2b56853df..f9f725421 100644 --- a/lightning/src/chain/chaininterface.rs +++ b/lightning/src/chain/chaininterface.rs @@ -4,7 +4,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; @@ -17,18 +16,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 { @@ -44,8 +31,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. /// diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index 1dad51b1c..c853150f6 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -28,7 +28,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}; @@ -2968,15 +2968,15 @@ impl } } -impl - ChainListener for ChannelManager +impl ChannelManager where M::Target: chain::Watch, T::Target: BroadcasterInterface, K::Target: KeysInterface, F::Target: FeeEstimator, - L::Target: Logger, + L::Target: Logger, { - fn block_connected(&self, header: &BlockHeader, txdata: &[(usize, &Transaction)], height: u32) { + /// + pub fn block_connected(&self, header: &BlockHeader, txdata: &[(usize, &Transaction)], height: u32) { let header_hash = header.bitcoin_hash(); log_trace!(self.logger, "Block {} at height {} connected", header_hash, height); let _ = self.total_consistency_lock.read().unwrap(); @@ -3103,7 +3103,7 @@ impl - ChainListener for ChainMonitor +impl ChainMonitor where T::Target: BroadcasterInterface, F::Target: FeeEstimator, L::Target: Logger, { - fn block_connected(&self, header: &BlockHeader, txdata: &[(usize, &Transaction)], height: u32) { + /// Delegates to [`ChannelMonitor::block_connected`] for each watched channel. Any HTLCs that + /// were resolved on chain will be retuned by [`chain::Watch::release_pending_htlc_updates`]. + /// + /// [`ChannelMonitor::block_connected`]: struct.ChannelMonitor.html#method.block_connected + /// [`chain::Watch::release_pending_htlc_updates`]: ../../chain/trait.Watch.html#tymethod.release_pending_htlc_updates + pub fn block_connected(&self, header: &BlockHeader, txdata: &[(usize, &Transaction)], height: u32) { let mut watch_events = self.watch_events.lock().unwrap(); let matched_txn: Vec<_> = txdata.iter().filter(|&&(_, tx)| watch_events.watched.does_match_tx(tx)).map(|e| *e).collect(); { @@ -241,7 +245,10 @@ impl