From: Jeffrey Czyz Date: Mon, 22 Feb 2021 05:40:22 +0000 (-0800) Subject: WIP: Common ChainListener implementations and example X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=e012c4c6ab8540e3918cb780277c4f7483d75f5a;p=rust-lightning WIP: Common ChainListener implementations and example --- diff --git a/lightning-block-sync/src/init.rs b/lightning-block-sync/src/init.rs index b8418fe89..70fe9b6ad 100644 --- a/lightning-block-sync/src/init.rs +++ b/lightning-block-sync/src/init.rs @@ -1,10 +1,12 @@ -use crate::{BlockSource, BlockSourceResult, Cache, ChainListener, ChainNotifier}; +use crate::{BlockSource, BlockSourceResult, Cache, ChainNotifier}; use crate::poll::{ChainPoller, Validate, ValidatedBlockHeader}; use bitcoin::blockdata::block::{Block, BlockHeader}; use bitcoin::hash_types::BlockHash; use bitcoin::network::constants::Network; +use lightning::chain::ChainListener; + /// 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. /// @@ -13,7 +15,88 @@ use bitcoin::network::constants::Network; /// paired with. /// /// Useful during startup to bring the [`ChannelManager`] and each [`ChannelMonitor`] in sync before -/// switching to [`SpvClient`]. +/// switching to [`SpvClient`]. For example: +/// +/// ``` +/// use bitcoin::hash_types::BlockHash; +/// 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; +/// use lightning::chain::channelmonitor::ChannelMonitor; +/// use lightning::chain::chaininterface::BroadcasterInterface; +/// use lightning::chain::chaininterface::FeeEstimator; +/// use lightning::chain::keysinterface; +/// use lightning::chain::keysinterface::KeysInterface; +/// use lightning::ln::channelmanager::ChannelManager; +/// use lightning::ln::channelmanager::ChannelManagerReadArgs; +/// use lightning::util::config::UserConfig; +/// use lightning::util::logger::Logger; +/// use lightning::util::ser::ReadableArgs; +/// +/// use lightning_block_sync::*; +/// +/// use std::cell::RefCell; +/// use std::io::Cursor; +/// +/// async fn init_sync< +/// B: BlockSource, +/// K: KeysInterface, +/// S: keysinterface::Sign, +/// T: BroadcasterInterface, +/// F: FeeEstimator, +/// L: Logger, +/// C: chain::Filter, +/// P: channelmonitor::Persist, +/// >( +/// block_source: &mut B, +/// chain_monitor: &ChainMonitor, +/// config: UserConfig, +/// keys_manager: &K, +/// tx_broadcaster: &T, +/// fee_estimator: &F, +/// logger: &L, +/// persister: &P, +/// ) { +/// let serialized_monitor = "..."; +/// let (monitor_block_hash, mut monitor) = <(BlockHash, ChannelMonitor)>::read( +/// &mut Cursor::new(&serialized_monitor), keys_manager).unwrap(); +/// +/// let serialized_manager = "..."; +/// let (manager_block_hash, mut manager) = { +/// let read_args = ChannelManagerReadArgs::new( +/// keys_manager, +/// fee_estimator, +/// chain_monitor, +/// tx_broadcaster, +/// logger, +/// config, +/// vec![&mut monitor], +/// ); +/// <(BlockHash, ChannelManager, &T, &K, &F, &L>)>::read( +/// &mut Cursor::new(&serialized_manager), read_args).unwrap() +/// }; +/// +/// 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), +/// ]; +/// let chain_tip = +/// init::sync_listeners(block_source, Network::Bitcoin, &mut cache, listeners).await.unwrap(); +/// +/// let monitor = monitor_listener.0.into_inner(); +/// chain_monitor.watch_channel(monitor.get_funding_txo().0, monitor); +/// +/// let chain_poller = poll::ChainPoller::new(block_source, Network::Bitcoin); +/// let mut chain_listener = (chain_monitor, &manager); +/// let spv_client = SpvClient::new(chain_tip, chain_poller, &mut cache, &chain_listener); +/// } +/// ``` /// /// [`SpvClient`]: ../struct.SpvClient.html /// [`ChannelManager`]: ../../lightning/ln/channelmanager/struct.ChannelManager.html @@ -49,32 +132,29 @@ pub async fn sync_listeners( for (old_header, chain_listener) in chain_listeners_with_old_headers.drain(..) { // Disconnect any stale blocks, but keep them in the cache for the next iteration. let header_cache = &mut ReadOnlyCache(header_cache); - let mut chain_notifier = ChainNotifier { header_cache }; - let difference = - chain_notifier.find_difference(new_header, &old_header, &mut chain_poller).await?; - chain_notifier.disconnect_blocks( - difference.disconnected_blocks, - &mut DynamicChainListener(chain_listener), - ); + let (common_ancestor, connected_blocks) = { + let chain_listener = &DynamicChainListener(chain_listener); + let mut chain_notifier = ChainNotifier { header_cache, chain_listener }; + let difference = + chain_notifier.find_difference(new_header, &old_header, &mut chain_poller).await?; + chain_notifier.disconnect_blocks(difference.disconnected_blocks); + (difference.common_ancestor, difference.connected_blocks) + }; // Keep track of the most common ancestor and all blocks connected across all listeners. - chain_listeners_at_height.push((difference.common_ancestor.height, chain_listener)); - if difference.connected_blocks.len() > most_connected_blocks.len() { - most_common_ancestor = Some(difference.common_ancestor); - most_connected_blocks = difference.connected_blocks; + chain_listeners_at_height.push((common_ancestor.height, chain_listener)); + if connected_blocks.len() > most_connected_blocks.len() { + most_common_ancestor = Some(common_ancestor); + most_connected_blocks = connected_blocks; } } // Connect new blocks for all listeners at once to avoid re-fetching blocks. if let Some(common_ancestor) = most_common_ancestor { - let mut chain_notifier = ChainNotifier { header_cache }; - let mut chain_listener = ChainListenerSet(chain_listeners_at_height); - chain_notifier.connect_blocks( - common_ancestor, - most_connected_blocks, - &mut chain_poller, - &mut chain_listener, - ).await.or_else(|(e, _)| Err(e))?; + let chain_listener = &ChainListenerSet(chain_listeners_at_height); + let mut chain_notifier = ChainNotifier { header_cache, chain_listener }; + chain_notifier.connect_blocks(common_ancestor, most_connected_blocks, &mut chain_poller) + .await.or_else(|(e, _)| Err(e))?; } Ok(new_header) @@ -104,11 +184,11 @@ impl<'a, C: Cache> Cache for ReadOnlyCache<'a, C> { struct DynamicChainListener<'a>(&'a mut dyn ChainListener); impl<'a> ChainListener for DynamicChainListener<'a> { - fn block_connected(&mut self, _block: &Block, _height: u32) { + fn block_connected(&self, _block: &Block, _height: u32) { unreachable!() } - fn block_disconnected(&mut self, header: &BlockHeader, height: u32) { + fn block_disconnected(&self, header: &BlockHeader, height: u32) { self.0.block_disconnected(header, height) } } @@ -117,15 +197,15 @@ impl<'a> ChainListener for DynamicChainListener<'a> { struct ChainListenerSet<'a>(Vec<(u32, &'a mut dyn ChainListener)>); impl<'a> ChainListener for ChainListenerSet<'a> { - fn block_connected(&mut self, block: &Block, height: u32) { - for (starting_height, chain_listener) in self.0.iter_mut() { + fn block_connected(&self, block: &Block, height: u32) { + for (starting_height, chain_listener) in self.0.iter() { if height > *starting_height { chain_listener.block_connected(block, height); } } } - fn block_disconnected(&mut self, _header: &BlockHeader, _height: u32) { + fn block_disconnected(&self, _header: &BlockHeader, _height: u32) { unreachable!() } } diff --git a/lightning-block-sync/src/lib.rs b/lightning-block-sync/src/lib.rs index 13e7cdaf0..41b1321cb 100644 --- a/lightning-block-sync/src/lib.rs +++ b/lightning-block-sync/src/lib.rs @@ -43,7 +43,10 @@ use bitcoin::blockdata::block::{Block, BlockHeader}; use bitcoin::hash_types::BlockHash; use bitcoin::util::uint::Uint256; +use lightning::chain::ChainListener; + use std::future::Future; +use std::ops::Deref; use std::pin::Pin; /// Abstract type for retrieving block headers and data. @@ -155,22 +158,11 @@ pub struct BlockHeaderData { /// custom cache eviction policy. This offers flexibility to those sensitive to resource usage. /// 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: ChainListener> { +pub struct SpvClient<'a, P: Poll, C: Cache, L: Deref> +where L::Target: ChainListener { chain_tip: ValidatedBlockHeader, chain_poller: P, - chain_notifier: ChainNotifier<'a, C>, - chain_listener: L, -} - -/// Adaptor used for notifying 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 { - /// Notifies the listener that a block was added at the given height. - fn block_connected(&mut self, block: &Block, height: u32); - - /// Notifies the listener that a block was removed at the given height. - fn block_disconnected(&mut self, header: &BlockHeader, height: u32); + chain_notifier: ChainNotifier<'a, C, L>, } /// The `Cache` trait defines behavior for managing a block header cache, where block headers are @@ -215,7 +207,8 @@ impl Cache for UnboundedCache { } } -impl<'a, P: Poll, C: Cache, L: ChainListener> SpvClient<'a, P, C, L> { +impl<'a, P: Poll, C: Cache, L: Deref> SpvClient<'a, P, C, L> +where L::Target: ChainListener { /// 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 @@ -232,8 +225,8 @@ impl<'a, P: Poll, C: Cache, L: ChainListener> SpvClient<'a, P, C, L> { header_cache: &'a mut C, chain_listener: L, ) -> Self { - let chain_notifier = ChainNotifier { header_cache }; - Self { chain_tip, chain_poller, chain_notifier, chain_listener } + let chain_notifier = ChainNotifier { header_cache, chain_listener }; + Self { chain_tip, chain_poller, chain_notifier } } /// Polls for the best tip and updates the chain listener with any connected or disconnected @@ -262,7 +255,7 @@ impl<'a, P: Poll, C: Cache, L: ChainListener> SpvClient<'a, P, C, L> { /// Updates the chain tip, syncing the chain listener with any connected or disconnected /// blocks. Returns whether there were any such blocks. async fn update_chain_tip(&mut self, best_chain_tip: ValidatedBlockHeader) -> bool { - match self.chain_notifier.sync_listener(best_chain_tip, &self.chain_tip, &mut self.chain_poller, &mut self.chain_listener).await { + match self.chain_notifier.sync_listener(best_chain_tip, &self.chain_tip, &mut self.chain_poller).await { Ok(_) => { self.chain_tip = best_chain_tip; true @@ -279,9 +272,12 @@ impl<'a, P: Poll, C: Cache, L: ChainListener> SpvClient<'a, P, C, L> { /// Notifies [listeners] of blocks that have been connected or disconnected from the chain. /// /// [listeners]: trait.ChainListener.html -pub struct ChainNotifier<'a, C: Cache> { +pub struct ChainNotifier<'a, C: Cache, L: Deref> where L::Target: ChainListener { /// Cache for looking up headers before fetching from a block source. header_cache: &'a mut C, + + /// Listener that will be notified of connected or disconnected blocks. + chain_listener: L, } /// Changes made to the chain between subsequent polls that transformed it from having one chain tip @@ -302,7 +298,7 @@ struct ChainDifference { connected_blocks: Vec, } -impl<'a, C: Cache> ChainNotifier<'a, C> { +impl<'a, C: Cache, L: Deref> ChainNotifier<'a, C, L> where L::Target: ChainListener { /// 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`. /// @@ -310,21 +306,19 @@ impl<'a, C: Cache> ChainNotifier<'a, C> { /// disconnected to the fork point. Thus, this may return an `Err` that includes where the tip /// ended up which may not be `new_header`. Note that iff the returned `Err` contains `Some` /// header then the transition from `old_header` to `new_header` is valid. - async fn sync_listener( + async fn sync_listener( &mut self, new_header: ValidatedBlockHeader, old_header: &ValidatedBlockHeader, chain_poller: &mut P, - chain_listener: &mut L, ) -> Result<(), (BlockSourceError, Option)> { let difference = self.find_difference(new_header, old_header, chain_poller).await .map_err(|e| (e, None))?; - self.disconnect_blocks(difference.disconnected_blocks, chain_listener); + self.disconnect_blocks(difference.disconnected_blocks); self.connect_blocks( difference.common_ancestor, difference.connected_blocks, chain_poller, - chain_listener, ).await } @@ -380,26 +374,21 @@ impl<'a, C: Cache> ChainNotifier<'a, C> { } /// Notifies the chain listeners of disconnected blocks. - fn disconnect_blocks( - &mut self, - mut disconnected_blocks: Vec, - chain_listener: &mut L, - ) { + fn disconnect_blocks(&mut self, mut disconnected_blocks: Vec) { for header in disconnected_blocks.drain(..) { if let Some(cached_header) = self.header_cache.block_disconnected(&header.block_hash) { assert_eq!(cached_header, header); } - chain_listener.block_disconnected(&header.header, header.height); + self.chain_listener.block_disconnected(&header.header, header.height); } } /// Notifies the chain listeners of connected blocks. - async fn connect_blocks( + async fn connect_blocks( &mut self, mut new_tip: ValidatedBlockHeader, mut connected_blocks: Vec, chain_poller: &mut P, - chain_listener: &mut L, ) -> Result<(), (BlockSourceError, Option)> { for header in connected_blocks.drain(..).rev() { let block = chain_poller @@ -408,7 +397,7 @@ impl<'a, C: Cache> ChainNotifier<'a, C> { debug_assert_eq!(block.block_hash, header.block_hash); self.header_cache.block_connected(header.block_hash, header); - chain_listener.block_connected(&block, header.height); + self.chain_listener.block_connected(&block, header.height); new_tip = header; } @@ -430,7 +419,8 @@ mod spv_client_tests { let poller = poll::ChainPoller::new(&mut chain, Network::Testnet); let mut cache = UnboundedCache::new(); - let mut client = SpvClient::new(best_tip, poller, &mut cache, NullChainListener {}); + let mut listener = NullChainListener {}; + let mut client = SpvClient::new(best_tip, poller, &mut cache, &mut listener); match client.poll_best_tip().await { Err(e) => { assert_eq!(e.kind(), BlockSourceErrorKind::Persistent); @@ -448,7 +438,8 @@ mod spv_client_tests { let poller = poll::ChainPoller::new(&mut chain, Network::Testnet); let mut cache = UnboundedCache::new(); - let mut client = SpvClient::new(common_tip, poller, &mut cache, NullChainListener {}); + let mut listener = NullChainListener {}; + let mut client = SpvClient::new(common_tip, poller, &mut cache, &mut listener); match client.poll_best_tip().await { Err(e) => panic!("Unexpected error: {:?}", e), Ok((chain_tip, blocks_connected)) => { @@ -467,7 +458,8 @@ mod spv_client_tests { let poller = poll::ChainPoller::new(&mut chain, Network::Testnet); let mut cache = UnboundedCache::new(); - let mut client = SpvClient::new(old_tip, poller, &mut cache, NullChainListener {}); + let mut listener = NullChainListener {}; + let mut client = SpvClient::new(old_tip, poller, &mut cache, &mut listener); match client.poll_best_tip().await { Err(e) => panic!("Unexpected error: {:?}", e), Ok((chain_tip, blocks_connected)) => { @@ -486,7 +478,8 @@ mod spv_client_tests { let poller = poll::ChainPoller::new(&mut chain, Network::Testnet); let mut cache = UnboundedCache::new(); - let mut client = SpvClient::new(old_tip, poller, &mut cache, NullChainListener {}); + let mut listener = NullChainListener {}; + let mut client = SpvClient::new(old_tip, poller, &mut cache, &mut listener); match client.poll_best_tip().await { Err(e) => panic!("Unexpected error: {:?}", e), Ok((chain_tip, blocks_connected)) => { @@ -505,7 +498,8 @@ mod spv_client_tests { let poller = poll::ChainPoller::new(&mut chain, Network::Testnet); let mut cache = UnboundedCache::new(); - let mut client = SpvClient::new(old_tip, poller, &mut cache, NullChainListener {}); + let mut listener = NullChainListener {}; + let mut client = SpvClient::new(old_tip, poller, &mut cache, &mut listener); match client.poll_best_tip().await { Err(e) => panic!("Unexpected error: {:?}", e), Ok((chain_tip, blocks_connected)) => { @@ -525,7 +519,8 @@ mod spv_client_tests { let poller = poll::ChainPoller::new(&mut chain, Network::Testnet); let mut cache = UnboundedCache::new(); - let mut client = SpvClient::new(best_tip, poller, &mut cache, NullChainListener {}); + let mut listener = NullChainListener {}; + let mut client = SpvClient::new(best_tip, poller, &mut cache, &mut listener); match client.poll_best_tip().await { Err(e) => panic!("Unexpected error: {:?}", e), Ok((chain_tip, blocks_connected)) => { @@ -550,12 +545,15 @@ mod chain_notifier_tests { let new_tip = chain.tip(); let old_tip = chain.at_height(1); - let mut listener = MockChainListener::new() + let chain_listener = &MockChainListener::new() .expect_block_connected(*chain.at_height(2)) .expect_block_connected(*new_tip); - let mut notifier = ChainNotifier { header_cache: &mut chain.header_cache(0..=1) }; + let mut notifier = ChainNotifier { + header_cache: &mut chain.header_cache(0..=1), + chain_listener, + }; let mut poller = poll::ChainPoller::new(&mut chain, Network::Testnet); - match notifier.sync_listener(new_tip, &old_tip, &mut poller, &mut listener).await { + match notifier.sync_listener(new_tip, &old_tip, &mut poller).await { Err((e, _)) => panic!("Unexpected error: {:?}", e), Ok(_) => {}, } @@ -568,10 +566,13 @@ mod chain_notifier_tests { let new_tip = test_chain.tip(); let old_tip = main_chain.tip(); - let mut listener = MockChainListener::new(); - let mut notifier = ChainNotifier { header_cache: &mut main_chain.header_cache(0..=1) }; + let chain_listener = &MockChainListener::new(); + let mut notifier = ChainNotifier { + header_cache: &mut main_chain.header_cache(0..=1), + chain_listener, + }; let mut poller = poll::ChainPoller::new(&mut test_chain, Network::Testnet); - match notifier.sync_listener(new_tip, &old_tip, &mut poller, &mut listener).await { + match notifier.sync_listener(new_tip, &old_tip, &mut poller).await { Err((e, _)) => { assert_eq!(e.kind(), BlockSourceErrorKind::Persistent); assert_eq!(e.into_inner().as_ref().to_string(), "genesis block reached"); @@ -587,12 +588,15 @@ mod chain_notifier_tests { let new_tip = fork_chain.tip(); let old_tip = main_chain.tip(); - let mut listener = MockChainListener::new() + let chain_listener = &MockChainListener::new() .expect_block_disconnected(*old_tip) .expect_block_connected(*new_tip); - let mut notifier = ChainNotifier { header_cache: &mut main_chain.header_cache(0..=2) }; + let mut notifier = ChainNotifier { + header_cache: &mut main_chain.header_cache(0..=2), + chain_listener, + }; let mut poller = poll::ChainPoller::new(&mut fork_chain, Network::Testnet); - match notifier.sync_listener(new_tip, &old_tip, &mut poller, &mut listener).await { + match notifier.sync_listener(new_tip, &old_tip, &mut poller).await { Err((e, _)) => panic!("Unexpected error: {:?}", e), Ok(_) => {}, } @@ -606,13 +610,16 @@ mod chain_notifier_tests { let new_tip = fork_chain.tip(); let old_tip = main_chain.tip(); - let mut listener = MockChainListener::new() + let chain_listener = &MockChainListener::new() .expect_block_disconnected(*old_tip) .expect_block_disconnected(*main_chain.at_height(2)) .expect_block_connected(*new_tip); - let mut notifier = ChainNotifier { header_cache: &mut main_chain.header_cache(0..=3) }; + let mut notifier = ChainNotifier { + header_cache: &mut main_chain.header_cache(0..=3), + chain_listener, + }; let mut poller = poll::ChainPoller::new(&mut fork_chain, Network::Testnet); - match notifier.sync_listener(new_tip, &old_tip, &mut poller, &mut listener).await { + match notifier.sync_listener(new_tip, &old_tip, &mut poller).await { Err((e, _)) => panic!("Unexpected error: {:?}", e), Ok(_) => {}, } @@ -626,13 +633,16 @@ mod chain_notifier_tests { let new_tip = fork_chain.tip(); let old_tip = main_chain.tip(); - let mut listener = MockChainListener::new() + let chain_listener = &MockChainListener::new() .expect_block_disconnected(*old_tip) .expect_block_connected(*fork_chain.at_height(2)) .expect_block_connected(*new_tip); - let mut notifier = ChainNotifier { header_cache: &mut main_chain.header_cache(0..=2) }; + let mut notifier = ChainNotifier { + header_cache: &mut main_chain.header_cache(0..=2), + chain_listener, + }; let mut poller = poll::ChainPoller::new(&mut fork_chain, Network::Testnet); - match notifier.sync_listener(new_tip, &old_tip, &mut poller, &mut listener).await { + match notifier.sync_listener(new_tip, &old_tip, &mut poller).await { Err((e, _)) => panic!("Unexpected error: {:?}", e), Ok(_) => {}, } @@ -644,10 +654,13 @@ mod chain_notifier_tests { let new_tip = chain.tip(); let old_tip = chain.at_height(1); - let mut listener = MockChainListener::new(); - let mut notifier = ChainNotifier { header_cache: &mut chain.header_cache(0..=1) }; + let chain_listener = &MockChainListener::new(); + let mut notifier = ChainNotifier { + header_cache: &mut chain.header_cache(0..=1), + chain_listener, + }; let mut poller = poll::ChainPoller::new(&mut chain, Network::Testnet); - match notifier.sync_listener(new_tip, &old_tip, &mut poller, &mut listener).await { + match notifier.sync_listener(new_tip, &old_tip, &mut poller).await { Err((_, tip)) => assert_eq!(tip, None), Ok(_) => panic!("Expected error"), } @@ -659,10 +672,13 @@ mod chain_notifier_tests { let new_tip = chain.tip(); let old_tip = chain.at_height(1); - let mut listener = MockChainListener::new(); - let mut notifier = ChainNotifier { header_cache: &mut chain.header_cache(0..=3) }; + let chain_listener = &MockChainListener::new(); + let mut notifier = ChainNotifier { + header_cache: &mut chain.header_cache(0..=3), + chain_listener, + }; let mut poller = poll::ChainPoller::new(&mut chain, Network::Testnet); - match notifier.sync_listener(new_tip, &old_tip, &mut poller, &mut listener).await { + match notifier.sync_listener(new_tip, &old_tip, &mut poller).await { Err((_, tip)) => assert_eq!(tip, Some(old_tip)), Ok(_) => panic!("Expected error"), } @@ -674,11 +690,14 @@ mod chain_notifier_tests { let new_tip = chain.tip(); let old_tip = chain.at_height(1); - let mut listener = MockChainListener::new() + let chain_listener = &MockChainListener::new() .expect_block_connected(*chain.at_height(2)); - let mut notifier = ChainNotifier { header_cache: &mut chain.header_cache(0..=3) }; + let mut notifier = ChainNotifier { + header_cache: &mut chain.header_cache(0..=3), + chain_listener, + }; let mut poller = poll::ChainPoller::new(&mut chain, Network::Testnet); - match notifier.sync_listener(new_tip, &old_tip, &mut poller, &mut listener).await { + match notifier.sync_listener(new_tip, &old_tip, &mut poller).await { Err((_, tip)) => assert_eq!(tip, Some(chain.at_height(2))), Ok(_) => panic!("Expected error"), } diff --git a/lightning-block-sync/src/test_utils.rs b/lightning-block-sync/src/test_utils.rs index 807a33a69..9f5c183c4 100644 --- a/lightning-block-sync/src/test_utils.rs +++ b/lightning-block-sync/src/test_utils.rs @@ -7,6 +7,7 @@ use bitcoin::hash_types::BlockHash; use bitcoin::network::constants::Network; use bitcoin::util::uint::Uint256; +use std::cell::RefCell; use std::collections::VecDeque; #[derive(Default)] @@ -163,37 +164,37 @@ impl BlockSource for Blockchain { pub struct NullChainListener; impl ChainListener for NullChainListener { - fn block_connected(&mut self, _block: &Block, _height: u32) {} - fn block_disconnected(&mut self, _header: &BlockHeader, _height: u32) {} + fn block_connected(&self, _block: &Block, _height: u32) {} + fn block_disconnected(&self, _header: &BlockHeader, _height: u32) {} } pub struct MockChainListener { - expected_blocks_connected: VecDeque, - expected_blocks_disconnected: VecDeque, + expected_blocks_connected: RefCell>, + expected_blocks_disconnected: RefCell>, } impl MockChainListener { pub fn new() -> Self { Self { - expected_blocks_connected: VecDeque::new(), - expected_blocks_disconnected: VecDeque::new(), + expected_blocks_connected: RefCell::new(VecDeque::new()), + expected_blocks_disconnected: RefCell::new(VecDeque::new()), } } - pub fn expect_block_connected(mut self, block: BlockHeaderData) -> Self { - self.expected_blocks_connected.push_back(block); + pub fn expect_block_connected(self, block: BlockHeaderData) -> Self { + self.expected_blocks_connected.borrow_mut().push_back(block); self } - pub fn expect_block_disconnected(mut self, block: BlockHeaderData) -> Self { - self.expected_blocks_disconnected.push_back(block); + pub fn expect_block_disconnected(self, block: BlockHeaderData) -> Self { + self.expected_blocks_disconnected.borrow_mut().push_back(block); self } } impl ChainListener for MockChainListener { - fn block_connected(&mut self, block: &Block, height: u32) { - match self.expected_blocks_connected.pop_front() { + fn block_connected(&self, block: &Block, height: u32) { + match self.expected_blocks_connected.borrow_mut().pop_front() { None => { panic!("Unexpected block connected: {:?}", block.block_hash()); }, @@ -204,8 +205,8 @@ impl ChainListener for MockChainListener { } } - fn block_disconnected(&mut self, header: &BlockHeader, height: u32) { - match self.expected_blocks_disconnected.pop_front() { + fn block_disconnected(&self, header: &BlockHeader, height: u32) { + match self.expected_blocks_disconnected.borrow_mut().pop_front() { None => { panic!("Unexpected block disconnected: {:?}", header.block_hash()); }, @@ -222,11 +223,15 @@ impl Drop for MockChainListener { if std::thread::panicking() { return; } - if !self.expected_blocks_connected.is_empty() { - panic!("Expected blocks connected: {:?}", self.expected_blocks_connected); + + let expected_blocks_connected = self.expected_blocks_connected.borrow(); + if !expected_blocks_connected.is_empty() { + panic!("Expected blocks connected: {:?}", expected_blocks_connected); } - if !self.expected_blocks_disconnected.is_empty() { - panic!("Expected blocks disconnected: {:?}", self.expected_blocks_disconnected); + + let expected_blocks_disconnected = self.expected_blocks_disconnected.borrow(); + if !expected_blocks_disconnected.is_empty() { + panic!("Expected blocks disconnected: {:?}", expected_blocks_disconnected); } } } diff --git a/lightning/src/chain/chainmonitor.rs b/lightning/src/chain/chainmonitor.rs index 8483f5ca8..0dad2b968 100644 --- a/lightning/src/chain/chainmonitor.rs +++ b/lightning/src/chain/chainmonitor.rs @@ -29,9 +29,10 @@ //! [`ChannelMonitor`]: ../channelmonitor/struct.ChannelMonitor.html //! [`MonitorEvent`]: ../channelmonitor/enum.MonitorEvent.html -use bitcoin::blockdata::block::BlockHeader; +use bitcoin::blockdata::block::{Block, BlockHeader}; use chain; +use chain::ChainListener; use chain::Filter; use chain::chaininterface::{BroadcasterInterface, FeeEstimator}; use chain::channelmonitor; @@ -140,6 +141,26 @@ where C::Target: chain::Filter, } } +impl +ChainListener for ChainMonitor +where + ChannelSigner: Sign, + C::Target: chain::Filter, + T::Target: BroadcasterInterface, + F::Target: FeeEstimator, + L::Target: Logger, + P::Target: channelmonitor::Persist, +{ + fn block_connected(&self, block: &Block, height: u32) { + let txdata: Vec<_> = block.txdata.iter().enumerate().collect(); + ChainMonitor::block_connected(self, &block.header, &txdata, height); + } + + fn block_disconnected(&self, header: &BlockHeader, height: u32) { + ChainMonitor::block_disconnected(self, header, height); + } +} + impl chain::Watch for ChainMonitor where C::Target: chain::Filter, diff --git a/lightning/src/chain/channelmonitor.rs b/lightning/src/chain/channelmonitor.rs index 0266f31e2..84bfb5f7e 100644 --- a/lightning/src/chain/channelmonitor.rs +++ b/lightning/src/chain/channelmonitor.rs @@ -22,7 +22,7 @@ //! //! [`chain::Watch`]: ../trait.Watch.html -use bitcoin::blockdata::block::BlockHeader; +use bitcoin::blockdata::block::{Block, BlockHeader}; use bitcoin::blockdata::transaction::{TxOut,Transaction}; use bitcoin::blockdata::transaction::OutPoint as BitcoinOutPoint; use bitcoin::blockdata::script::{Script, Builder}; @@ -41,6 +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::chaininterface::{BroadcasterInterface, FeeEstimator}; use chain::transaction::{OutPoint, TransactionData}; use chain::keysinterface::{SpendableOutputDescriptor, StaticPaymentOutputDescriptor, DelayedPaymentOutputDescriptor, Sign, KeysInterface}; @@ -49,6 +50,7 @@ use util::ser::{Readable, ReadableArgs, MaybeReadable, Writer, Writeable, U48}; use util::byte_utils; use util::events::Event; +use std::cell::RefCell; use std::collections::{HashMap, HashSet, hash_map}; use std::{cmp, mem}; use std::ops::Deref; @@ -2297,6 +2299,22 @@ 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) +where + T::Target: BroadcasterInterface, + F::Target: FeeEstimator, + L::Target: Logger, +{ + fn block_connected(&self, block: &Block, height: u32) { + let txdata: Vec<_> = block.txdata.iter().enumerate().collect(); + self.0.borrow_mut().block_connected(&block.header, &txdata, height, &*self.1, &*self.2, &*self.3); + } + + fn block_disconnected(&self, header: &BlockHeader, height: u32) { + self.0.borrow_mut().block_disconnected(header, height, &*self.1, &*self.2, &*self.3); + } +} + const MAX_ALLOC_SIZE: usize = 64*1024; impl<'a, Signer: Sign, K: KeysInterface> ReadableArgs<&'a K> diff --git a/lightning/src/chain/mod.rs b/lightning/src/chain/mod.rs index 1d51f2622..f31866fa4 100644 --- a/lightning/src/chain/mod.rs +++ b/lightning/src/chain/mod.rs @@ -9,6 +9,7 @@ //! Structs and traits which allow other parts of rust-lightning to interact with the blockchain. +use bitcoin::blockdata::block::{Block, BlockHeader}; use bitcoin::blockdata::script::Script; use bitcoin::blockdata::transaction::TxOut; use bitcoin::hash_types::{BlockHash, Txid}; @@ -46,6 +47,17 @@ 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. +/// +/// Used when needing to replay chain data upon startup or as new chain events occur. +pub trait ChainListener { + /// Notifies the listener that a block was added at the given height. + fn block_connected(&self, block: &Block, height: u32); + + /// Notifies the listener that a block was removed at the given height. + fn block_disconnected(&self, header: &BlockHeader, height: u32); +} + /// The `Watch` trait defines behavior for watching on-chain activity pertaining to channels as /// blocks are connected and disconnected. /// @@ -123,3 +135,29 @@ pub trait Filter: Send + Sync { /// `script_pubkey` as the spending condition. fn register_output(&self, outpoint: &OutPoint, script_pubkey: &Script); } + +impl ChainListener for std::ops::Deref { + fn block_connected(&self, block: &Block, height: u32) { + (**self).block_connected(block, height); + } + + fn block_disconnected(&self, header: &BlockHeader, height: u32) { + (**self).block_disconnected(header, height); + } +} + +impl ChainListener for (T, U) +where + T::Target: ChainListener, + U::Target: ChainListener, +{ + fn block_connected(&self, block: &Block, height: u32) { + self.0.block_connected(block, height); + self.1.block_connected(block, height); + } + + fn block_disconnected(&self, header: &BlockHeader, height: u32) { + self.0.block_disconnected(header, height); + self.1.block_disconnected(header, height); + } +} diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index a759443d8..c21f6a774 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -18,7 +18,7 @@ //! imply it needs to fail HTLCs/payments/channels it manages). //! -use bitcoin::blockdata::block::BlockHeader; +use bitcoin::blockdata::block::{Block, BlockHeader}; use bitcoin::blockdata::constants::genesis_block; use bitcoin::network::constants::Network; @@ -35,6 +35,7 @@ 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}; @@ -3139,6 +3140,24 @@ impl EventsProvi } } +impl ChainListener for ChannelManager +where + M::Target: chain::Watch, + T::Target: BroadcasterInterface, + K::Target: KeysInterface, + F::Target: FeeEstimator, + L::Target: Logger, +{ + fn block_connected(&self, block: &Block, height: u32) { + let txdata: Vec<_> = block.txdata.iter().enumerate().collect(); + ChannelManager::block_connected(self, &block.header, &txdata, height); + } + + fn block_disconnected(&self, header: &BlockHeader, _height: u32) { + ChannelManager::block_disconnected(self, header); + } +} + impl ChannelManager where M::Target: chain::Watch, T::Target: BroadcasterInterface,