From 1599a13643d893277eb3921c1bb15297547eb030 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 | 4 ++-- lightning/src/chain/chaininterface.rs | 16 +------------ lightning/src/ln/channelmanager.rs | 17 ++++++++------ lightning/src/ln/channelmonitor.rs | 28 ++++++++++++++++------- lightning/src/ln/functional_test_utils.rs | 3 +-- lightning/src/ln/functional_tests.rs | 5 +--- 7 files changed, 36 insertions(+), 39 deletions(-) diff --git a/fuzz/src/chanmon_consistency.rs b/fuzz/src/chanmon_consistency.rs index ce407f32..f12c2aa3 100644 --- a/fuzz/src/chanmon_consistency.rs +++ b/fuzz/src/chanmon_consistency.rs @@ -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}; diff --git a/fuzz/src/full_stack.rs b/fuzz/src/full_stack.rs index 03d29032..5980f9f1 100644 --- a/fuzz/src/full_stack.rs +++ b/fuzz/src/full_stack.rs @@ -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; diff --git a/lightning/src/chain/chaininterface.rs b/lightning/src/chain/chaininterface.rs index f65ae361..a63e0bf1 100644 --- a/lightning/src/chain/chaininterface.rs +++ b/lightning/src/chain/chaininterface.rs @@ -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. /// diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index cfc83deb..98e0ec91 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -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 } } -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) { + /// 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 - 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) { + /// 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) {} 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 { diff --git a/lightning/src/ln/functional_tests.rs b/lightning/src/ln/functional_tests.rs index 1b664508..03fedbf3 100644 --- a/lightning/src/ln/functional_tests.rs +++ b/lightning/src/ln/functional_tests.rs @@ -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); -- 2.30.2