From: Matt Corallo <649246+TheBlueMatt@users.noreply.github.com> Date: Wed, 3 Mar 2021 00:03:42 +0000 (-0800) Subject: Merge pull request #818 from jkczyz/2021-03-validate-best-block-header X-Git-Tag: v0.0.13~13 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=03a518965100b6852f36e4f95ead4c1d93f5c4b0;hp=7caadd446bd497cfd47e753fa8a931cb49d5b6d6;p=rust-lightning Merge pull request #818 from jkczyz/2021-03-validate-best-block-header Add validate_best_block_header utility --- diff --git a/lightning-block-sync/src/init.rs b/lightning-block-sync/src/init.rs index 24080b15..83e35691 100644 --- a/lightning-block-sync/src/init.rs +++ b/lightning-block-sync/src/init.rs @@ -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(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. /// @@ -110,10 +122,7 @@ pub async fn synchronize_listeners( 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();