@@ -7,6 +7,18 @@ use bitcoin::network::constants::Network;
7
7
8
8
use lightning:: chain;
9
9
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
+
10
22
/// Performs a one-time sync of chain listeners using a single *trusted* block source, bringing each
11
23
/// listener's view of the chain from its paired block hash to `block_source`'s best chain tip.
12
24
///
@@ -110,10 +122,7 @@ pub async fn synchronize_listeners<B: BlockSource, C: Cache>(
110
122
header_cache : & mut C ,
111
123
mut chain_listeners : Vec < ( BlockHash , & mut dyn chain:: Listen ) > ,
112
124
) -> 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 ?;
117
126
118
127
// Fetch the header for the block hash paired with each listener.
119
128
let mut chain_listeners_with_old_headers = Vec :: new ( ) ;
0 commit comments