X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning-block-sync%2Fsrc%2Finit.rs;h=d51cd3ba606f9bd513326cc527ab677459b58b0d;hb=cff6abf3996977b9aaae2de4d66d5d1b70d64ae9;hp=3e421fc44a9542b7e0a9fb5aa963d2c4bcca4c0d;hpb=7c8e740b6e82feeb60938083dcf677ea7e21f7bd;p=rust-lightning diff --git a/lightning-block-sync/src/init.rs b/lightning-block-sync/src/init.rs index 3e421fc4..d51cd3ba 100644 --- a/lightning-block-sync/src/init.rs +++ b/lightning-block-sync/src/init.rs @@ -1,3 +1,6 @@ +//! Utilities to assist in the initial sync required to initialize or reload Rust-Lightning objects +//! from disk. + use crate::{BlockSource, BlockSourceResult, Cache, ChainNotifier}; use crate::poll::{ChainPoller, Validate, ValidatedBlockHeader}; @@ -7,6 +10,20 @@ 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. +/// +/// [`SpvClient`]: crate::SpvClient +pub async fn validate_best_block_header(block_source: &mut B) -> +BlockSourceResult { + 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. /// @@ -61,12 +78,12 @@ use lightning::chain; /// ) { /// // Read a serialized channel monitor paired with the block hash when it was persisted. /// let serialized_monitor = "..."; -/// let (monitor_block_hash_option, mut monitor) = <(Option, ChannelMonitor)>::read( +/// let (monitor_block_hash, mut monitor) = <(BlockHash, ChannelMonitor)>::read( /// &mut Cursor::new(&serialized_monitor), keys_manager).unwrap(); /// /// // Read the channel manager paired with the block hash when it was persisted. /// let serialized_manager = "..."; -/// let (manager_block_hash_option, mut manager) = { +/// let (manager_block_hash, mut manager) = { /// let read_args = ChannelManagerReadArgs::new( /// keys_manager, /// fee_estimator, @@ -76,20 +93,17 @@ use lightning::chain; /// config, /// vec![&mut monitor], /// ); -/// <(Option, ChannelManager, &T, &K, &F, &L>)>::read( +/// <(BlockHash, ChannelManager, &T, &K, &F, &L>)>::read( /// &mut Cursor::new(&serialized_manager), read_args).unwrap() /// }; /// /// // Synchronize any channel monitors and the channel manager to be on the best block. /// let mut cache = UnboundedCache::new(); /// let mut monitor_listener = (monitor, &*tx_broadcaster, &*fee_estimator, &*logger); -/// let mut listeners = vec![]; -/// if let Some(monitor_block_hash) = monitor_block_hash_option { -/// listeners.push((monitor_block_hash, &mut monitor_listener as &mut dyn chain::Listen)) -/// } -/// if let Some(manager_block_hash) = manager_block_hash_option { -/// listeners.push((manager_block_hash, &mut manager as &mut dyn chain::Listen)) -/// } +/// let listeners = vec![ +/// (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::synchronize_listeners( /// block_source, Network::Bitcoin, &mut cache, listeners).await.unwrap(); /// @@ -104,19 +118,16 @@ use lightning::chain; /// } /// ``` /// -/// [`SpvClient`]: ../struct.SpvClient.html -/// [`ChannelManager`]: ../../lightning/ln/channelmanager/struct.ChannelManager.html -/// [`ChannelMonitor`]: ../../lightning/chain/channelmonitor/struct.ChannelMonitor.html +/// [`SpvClient`]: crate::SpvClient +/// [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager +/// [`ChannelMonitor`]: lightning::chain::channelmonitor::ChannelMonitor pub async fn synchronize_listeners( block_source: &mut B, network: Network, header_cache: &mut C, mut chain_listeners: Vec<(BlockHash, &mut dyn chain::Listen)>, ) -> BlockSourceResult { - 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();