Skip to content

Commit 03a5189

Browse files
authored
Merge pull request #818 from jkczyz/2021-03-validate-best-block-header
Add validate_best_block_header utility
2 parents 7caadd4 + 60f4daf commit 03a5189

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

lightning-block-sync/src/init.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@ use bitcoin::network::constants::Network;
77

88
use lightning::chain;
99

10+
/// Returns a validated block header of the source's best chain tip.
11+
///
12+
/// Upon success, the returned header can be used to initialize [`SpvClient`]. Useful during a fresh
13+
/// start when there are no chain listeners to sync yet.
14+
pub async fn validate_best_block_header<B: BlockSource>(block_source: &mut B) ->
15+
BlockSourceResult<ValidatedBlockHeader> {
16+
let (best_block_hash, best_block_height) = block_source.get_best_block().await?;
17+
block_source
18+
.get_header(&best_block_hash, best_block_height).await?
19+
.validate(best_block_hash)
20+
}
21+
1022
/// Performs a one-time sync of chain listeners using a single *trusted* block source, bringing each
1123
/// listener's view of the chain from its paired block hash to `block_source`'s best chain tip.
1224
///
@@ -110,10 +122,7 @@ pub async fn synchronize_listeners<B: BlockSource, C: Cache>(
110122
header_cache: &mut C,
111123
mut chain_listeners: Vec<(BlockHash, &mut dyn chain::Listen)>,
112124
) -> BlockSourceResult<ValidatedBlockHeader> {
113-
let (best_block_hash, best_block_height) = block_source.get_best_block().await?;
114-
let best_header = block_source
115-
.get_header(&best_block_hash, best_block_height).await?
116-
.validate(best_block_hash)?;
125+
let best_header = validate_best_block_header(block_source).await?;
117126

118127
// Fetch the header for the block hash paired with each listener.
119128
let mut chain_listeners_with_old_headers = Vec::new();

0 commit comments

Comments
 (0)