From: Jeffrey Czyz Date: Wed, 24 Feb 2021 03:40:53 +0000 (-0800) Subject: f - Rename ChainListener to chain::Listen X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=da79fb05fa13f0a7db1bb7ef03f1a4ce9e3b8b35;p=rust-lightning f - Rename ChainListener to chain::Listen --- diff --git a/lightning-block-sync/src/init.rs b/lightning-block-sync/src/init.rs index 70fe9b6ad..abefd1bcd 100644 --- a/lightning-block-sync/src/init.rs +++ b/lightning-block-sync/src/init.rs @@ -5,7 +5,7 @@ use bitcoin::blockdata::block::{Block, BlockHeader}; use bitcoin::hash_types::BlockHash; use bitcoin::network::constants::Network; -use lightning::chain::ChainListener; +use lightning::chain; /// Performs a one-time sync of chain listeners using a single *trusted* block source, bringing each /// listener's view of the chain from its paired block hash to `block_source`'s best chain tip. @@ -22,7 +22,6 @@ use lightning::chain::ChainListener; /// use bitcoin::network::constants::Network; /// /// use lightning::chain; -/// use lightning::chain::ChainListener; /// use lightning::chain::Watch; /// use lightning::chain::chainmonitor::ChainMonitor; /// use lightning::chain::channelmonitor; @@ -83,8 +82,8 @@ use lightning::chain::ChainListener; /// let mut cache = UnboundedCache::new(); /// let mut monitor_listener = (RefCell::new(monitor), &*tx_broadcaster, &*fee_estimator, &*logger); /// let listeners = vec![ -/// (monitor_block_hash, &mut monitor_listener as &mut dyn ChainListener), -/// (manager_block_hash, &mut manager as &mut dyn ChainListener), +/// (monitor_block_hash, &mut monitor_listener as &mut dyn chain::Listen), +/// (manager_block_hash, &mut manager as &mut dyn chain::Listen), /// ]; /// let chain_tip = /// init::sync_listeners(block_source, Network::Bitcoin, &mut cache, listeners).await.unwrap(); @@ -105,7 +104,7 @@ pub async fn sync_listeners( block_source: &mut B, network: Network, header_cache: &mut C, - mut chain_listeners: Vec<(BlockHash, &mut dyn ChainListener)>, + mut chain_listeners: Vec<(BlockHash, &mut dyn chain::Listen)>, ) -> BlockSourceResult { let (best_block_hash, best_block_height) = block_source.get_best_block().await?; let new_header = block_source @@ -181,9 +180,9 @@ impl<'a, C: Cache> Cache for ReadOnlyCache<'a, C> { } /// Wrapper for supporting dynamically sized chain listeners. -struct DynamicChainListener<'a>(&'a mut dyn ChainListener); +struct DynamicChainListener<'a>(&'a mut dyn chain::Listen); -impl<'a> ChainListener for DynamicChainListener<'a> { +impl<'a> chain::Listen for DynamicChainListener<'a> { fn block_connected(&self, _block: &Block, _height: u32) { unreachable!() } @@ -194,9 +193,9 @@ impl<'a> ChainListener for DynamicChainListener<'a> { } /// A set of dynamically sized chain listeners, each paired with a starting block height. -struct ChainListenerSet<'a>(Vec<(u32, &'a mut dyn ChainListener)>); +struct ChainListenerSet<'a>(Vec<(u32, &'a mut dyn chain::Listen)>); -impl<'a> ChainListener for ChainListenerSet<'a> { +impl<'a> chain::Listen for ChainListenerSet<'a> { fn block_connected(&self, block: &Block, height: u32) { for (starting_height, chain_listener) in self.0.iter() { if height > *starting_height { @@ -232,9 +231,9 @@ mod tests { .expect_block_connected(*chain.at_height(4)); let listeners = vec![ - (chain.at_height(1).block_hash, &mut listener_1 as &mut dyn ChainListener), - (chain.at_height(2).block_hash, &mut listener_2 as &mut dyn ChainListener), - (chain.at_height(3).block_hash, &mut listener_3 as &mut dyn ChainListener), + (chain.at_height(1).block_hash, &mut listener_1 as &mut dyn chain::Listen), + (chain.at_height(2).block_hash, &mut listener_2 as &mut dyn chain::Listen), + (chain.at_height(3).block_hash, &mut listener_3 as &mut dyn chain::Listen), ]; let mut cache = chain.header_cache(0..=4); match sync_listeners(&mut chain, Network::Bitcoin, &mut cache, listeners).await { @@ -267,9 +266,9 @@ mod tests { .expect_block_connected(*main_chain.at_height(4)); let listeners = vec![ - (fork_chain_1.tip().block_hash, &mut listener_1 as &mut dyn ChainListener), - (fork_chain_2.tip().block_hash, &mut listener_2 as &mut dyn ChainListener), - (fork_chain_3.tip().block_hash, &mut listener_3 as &mut dyn ChainListener), + (fork_chain_1.tip().block_hash, &mut listener_1 as &mut dyn chain::Listen), + (fork_chain_2.tip().block_hash, &mut listener_2 as &mut dyn chain::Listen), + (fork_chain_3.tip().block_hash, &mut listener_3 as &mut dyn chain::Listen), ]; let mut cache = fork_chain_1.header_cache(2..=4); cache.extend(fork_chain_2.header_cache(3..=4)); @@ -310,9 +309,9 @@ mod tests { .expect_block_connected(*main_chain.at_height(4)); let listeners = vec![ - (fork_chain_1.tip().block_hash, &mut listener_1 as &mut dyn ChainListener), - (fork_chain_2.tip().block_hash, &mut listener_2 as &mut dyn ChainListener), - (fork_chain_3.tip().block_hash, &mut listener_3 as &mut dyn ChainListener), + (fork_chain_1.tip().block_hash, &mut listener_1 as &mut dyn chain::Listen), + (fork_chain_2.tip().block_hash, &mut listener_2 as &mut dyn chain::Listen), + (fork_chain_3.tip().block_hash, &mut listener_3 as &mut dyn chain::Listen), ]; let mut cache = fork_chain_1.header_cache(2..=4); cache.extend(fork_chain_2.header_cache(3..=4)); @@ -334,7 +333,7 @@ mod tests { .expect_block_disconnected(*old_tip) .expect_block_connected(*new_tip); - let listeners = vec![(old_tip.block_hash, &mut listener as &mut dyn ChainListener)]; + let listeners = vec![(old_tip.block_hash, &mut listener as &mut dyn chain::Listen)]; let mut cache = fork_chain.header_cache(2..=2); match sync_listeners(&mut main_chain, Network::Bitcoin, &mut cache, listeners).await { Ok(_) => { diff --git a/lightning-block-sync/src/lib.rs b/lightning-block-sync/src/lib.rs index 41b1321cb..f7e9db97f 100644 --- a/lightning-block-sync/src/lib.rs +++ b/lightning-block-sync/src/lib.rs @@ -43,7 +43,8 @@ use bitcoin::blockdata::block::{Block, BlockHeader}; use bitcoin::hash_types::BlockHash; use bitcoin::util::uint::Uint256; -use lightning::chain::ChainListener; +use lightning::chain; +use lightning::chain::Listen; use std::future::Future; use std::ops::Deref; @@ -159,7 +160,7 @@ pub struct BlockHeaderData { /// Hence, there is a trade-off between a lower memory footprint and potentially increased network /// I/O as headers are re-fetched during fork detection. pub struct SpvClient<'a, P: Poll, C: Cache, L: Deref> -where L::Target: ChainListener { +where L::Target: chain::Listen { chain_tip: ValidatedBlockHeader, chain_poller: P, chain_notifier: ChainNotifier<'a, C, L>, @@ -207,8 +208,7 @@ impl Cache for UnboundedCache { } } -impl<'a, P: Poll, C: Cache, L: Deref> SpvClient<'a, P, C, L> -where L::Target: ChainListener { +impl<'a, P: Poll, C: Cache, L: Deref> SpvClient<'a, P, C, L> where L::Target: chain::Listen { /// Creates a new SPV client using `chain_tip` as the best known chain tip. /// /// Subsequent calls to [`poll_best_tip`] will poll for the best chain tip using the given chain @@ -271,8 +271,8 @@ where L::Target: ChainListener { /// Notifies [listeners] of blocks that have been connected or disconnected from the chain. /// -/// [listeners]: trait.ChainListener.html -pub struct ChainNotifier<'a, C: Cache, L: Deref> where L::Target: ChainListener { +/// [listeners]: ../../lightning/chain/trait.Listen.html +pub struct ChainNotifier<'a, C: Cache, L: Deref> where L::Target: chain::Listen { /// Cache for looking up headers before fetching from a block source. header_cache: &'a mut C, @@ -298,7 +298,7 @@ struct ChainDifference { connected_blocks: Vec, } -impl<'a, C: Cache, L: Deref> ChainNotifier<'a, C, L> where L::Target: ChainListener { +impl<'a, C: Cache, L: Deref> ChainNotifier<'a, C, L> where L::Target: chain::Listen { /// Finds the first common ancestor between `new_header` and `old_header`, disconnecting blocks /// from `old_header` to get to that point and then connecting blocks until `new_header`. /// diff --git a/lightning-block-sync/src/test_utils.rs b/lightning-block-sync/src/test_utils.rs index 9f5c183c4..8c37c94d8 100644 --- a/lightning-block-sync/src/test_utils.rs +++ b/lightning-block-sync/src/test_utils.rs @@ -1,4 +1,4 @@ -use crate::{AsyncBlockSourceResult, BlockHeaderData, BlockSource, BlockSourceError, ChainListener, UnboundedCache}; +use crate::{AsyncBlockSourceResult, BlockHeaderData, BlockSource, BlockSourceError, UnboundedCache}; use crate::poll::{Validate, ValidatedBlockHeader}; use bitcoin::blockdata::block::{Block, BlockHeader}; @@ -7,6 +7,8 @@ use bitcoin::hash_types::BlockHash; use bitcoin::network::constants::Network; use bitcoin::util::uint::Uint256; +use lightning::chain; + use std::cell::RefCell; use std::collections::VecDeque; @@ -163,7 +165,7 @@ impl BlockSource for Blockchain { pub struct NullChainListener; -impl ChainListener for NullChainListener { +impl chain::Listen for NullChainListener { fn block_connected(&self, _block: &Block, _height: u32) {} fn block_disconnected(&self, _header: &BlockHeader, _height: u32) {} } @@ -192,7 +194,7 @@ impl MockChainListener { } } -impl ChainListener for MockChainListener { +impl chain::Listen for MockChainListener { fn block_connected(&self, block: &Block, height: u32) { match self.expected_blocks_connected.borrow_mut().pop_front() { None => { diff --git a/lightning/src/chain/chainmonitor.rs b/lightning/src/chain/chainmonitor.rs index 0dad2b968..4cb9128d9 100644 --- a/lightning/src/chain/chainmonitor.rs +++ b/lightning/src/chain/chainmonitor.rs @@ -32,7 +32,6 @@ use bitcoin::blockdata::block::{Block, BlockHeader}; use chain; -use chain::ChainListener; use chain::Filter; use chain::chaininterface::{BroadcasterInterface, FeeEstimator}; use chain::channelmonitor; @@ -142,7 +141,7 @@ where C::Target: chain::Filter, } impl -ChainListener for ChainMonitor +chain::Listen for ChainMonitor where ChannelSigner: Sign, C::Target: chain::Filter, diff --git a/lightning/src/chain/channelmonitor.rs b/lightning/src/chain/channelmonitor.rs index 84bfb5f7e..71490dd6e 100644 --- a/lightning/src/chain/channelmonitor.rs +++ b/lightning/src/chain/channelmonitor.rs @@ -41,7 +41,7 @@ use ln::chan_utils; use ln::chan_utils::{CounterpartyCommitmentSecrets, HTLCOutputInCommitment, HTLCType, ChannelTransactionParameters, HolderCommitmentTransaction}; use ln::channelmanager::{HTLCSource, PaymentPreimage, PaymentHash}; use ln::onchaintx::{OnchainTxHandler, InputDescriptors}; -use chain::ChainListener; +use chain; use chain::chaininterface::{BroadcasterInterface, FeeEstimator}; use chain::transaction::{OutPoint, TransactionData}; use chain::keysinterface::{SpendableOutputDescriptor, StaticPaymentOutputDescriptor, DelayedPaymentOutputDescriptor, Sign, KeysInterface}; @@ -2299,7 +2299,7 @@ pub trait Persist: Send + Sync { fn update_persisted_channel(&self, id: OutPoint, update: &ChannelMonitorUpdate, data: &ChannelMonitor) -> Result<(), ChannelMonitorUpdateErr>; } -impl ChainListener for (RefCell>, T, F, L) +impl chain::Listen for (RefCell>, T, F, L) where T::Target: BroadcasterInterface, F::Target: FeeEstimator, diff --git a/lightning/src/chain/mod.rs b/lightning/src/chain/mod.rs index f31866fa4..c1fceec8c 100644 --- a/lightning/src/chain/mod.rs +++ b/lightning/src/chain/mod.rs @@ -47,10 +47,11 @@ pub trait Access: Send + Sync { fn get_utxo(&self, genesis_hash: &BlockHash, short_channel_id: u64) -> Result; } -/// Adaptor used for notifying when blocks have been connected or disconnected from the chain. +/// The `Listen` trait is used to be notified of when blocks have been connected or disconnected +/// from the chain. /// -/// Used when needing to replay chain data upon startup or as new chain events occur. -pub trait ChainListener { +/// Useful when needing to replay chain data upon startup or as new chain events occur. +pub trait Listen { /// Notifies the listener that a block was added at the given height. fn block_connected(&self, block: &Block, height: u32); @@ -136,7 +137,7 @@ pub trait Filter: Send + Sync { fn register_output(&self, outpoint: &OutPoint, script_pubkey: &Script); } -impl ChainListener for std::ops::Deref { +impl Listen for std::ops::Deref { fn block_connected(&self, block: &Block, height: u32) { (**self).block_connected(block, height); } @@ -146,10 +147,10 @@ impl ChainListener for std::ops::Deref { } } -impl ChainListener for (T, U) +impl Listen for (T, U) where - T::Target: ChainListener, - U::Target: ChainListener, + T::Target: Listen, + U::Target: Listen, { fn block_connected(&self, block: &Block, height: u32) { self.0.block_connected(block, height); diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index c21f6a774..8f7fbde5f 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -35,7 +35,6 @@ use bitcoin::secp256k1::ecdh::SharedSecret; use bitcoin::secp256k1; use chain; -use chain::ChainListener; use chain::Watch; use chain::chaininterface::{BroadcasterInterface, FeeEstimator}; use chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateStep, ChannelMonitorUpdateErr, HTLC_FAIL_BACK_BUFFER, CLTV_CLAIM_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS, ANTI_REORG_DELAY, MonitorEvent, CLOSED_CHANNEL_UPDATE_ID}; @@ -3140,7 +3139,7 @@ impl EventsProvi } } -impl ChainListener for ChannelManager +impl chain::Listen for ChannelManager where M::Target: chain::Watch, T::Target: BroadcasterInterface,