Revert "Merge pull request #819 from TheBlueMatt/2021-03-810-rebased"
[rust-lightning] / lightning-block-sync / src / init.rs
index da9895ae7213f0a529aa7d77f4d84f8ca8d077fd..83e356919b8e231a8d4a3bda41b0d65645e146fc 100644 (file)
@@ -7,6 +7,18 @@ use bitcoin::network::constants::Network;
 
 use lightning::chain;
 
+/// Returns a validated block header of the source's best chain tip.
+///
+/// Upon success, the returned header can be used to initialize [`SpvClient`]. Useful during a fresh
+/// start when there are no chain listeners to sync yet.
+pub async fn validate_best_block_header<B: BlockSource>(block_source: &mut B) ->
+BlockSourceResult<ValidatedBlockHeader> {
+       let (best_block_hash, best_block_height) = block_source.get_best_block().await?;
+       block_source
+               .get_header(&best_block_hash, best_block_height).await?
+               .validate(best_block_hash)
+}
+
 /// 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.
 ///
@@ -38,7 +50,6 @@ use lightning::chain;
 ///
 /// use lightning_block_sync::*;
 ///
-/// use std::cell::RefCell;
 /// use std::io::Cursor;
 ///
 /// async fn init_sync<
@@ -83,7 +94,7 @@ use lightning::chain;
 ///
 ///    // Synchronize any channel monitors and the channel manager to be on the best block.
 ///    let mut cache = UnboundedCache::new();
-///    let mut monitor_listener = (RefCell::new(monitor), &*tx_broadcaster, &*fee_estimator, &*logger);
+///    let mut monitor_listener = (monitor, &*tx_broadcaster, &*fee_estimator, &*logger);
 ///    let listeners = vec![
 ///            (monitor_block_hash, &mut monitor_listener as &mut dyn chain::Listen),
 ///            (manager_block_hash, &mut manager as &mut dyn chain::Listen),
@@ -92,7 +103,7 @@ use lightning::chain;
 ///            block_source, Network::Bitcoin, &mut cache, listeners).await.unwrap();
 ///
 ///    // Allow the chain monitor to watch any channels.
-///    let monitor = monitor_listener.0.into_inner();
+///    let monitor = monitor_listener.0;
 ///    chain_monitor.watch_channel(monitor.get_funding_txo().0, monitor);
 ///
 ///    // Create an SPV client to notify the chain monitor and channel manager of block events.
@@ -111,10 +122,7 @@ pub async fn synchronize_listeners<B: BlockSource, C: Cache>(
        header_cache: &mut C,
        mut chain_listeners: Vec<(BlockHash, &mut dyn chain::Listen)>,
 ) -> BlockSourceResult<ValidatedBlockHeader> {
-       let (best_block_hash, best_block_height) = block_source.get_best_block().await?;
-       let best_header = block_source
-               .get_header(&best_block_hash, best_block_height).await?
-               .validate(best_block_hash)?;
+       let best_header = validate_best_block_header(block_source).await?;
 
        // Fetch the header for the block hash paired with each listener.
        let mut chain_listeners_with_old_headers = Vec::new();