1
1
//! Utilities to assist in the initial sync required to initialize or reload Rust-Lightning objects
2
2
//! from disk.
3
3
4
- use crate :: { BlockSource , BlockSourceResult , Cache , ChainNotifier } ;
5
4
use crate :: poll:: { ChainPoller , Validate , ValidatedBlockHeader } ;
5
+ use crate :: { BlockSource , BlockSourceResult , Cache , ChainNotifier } ;
6
6
7
7
use bitcoin:: blockdata:: block:: Header ;
8
8
use bitcoin:: hash_types:: BlockHash ;
@@ -18,12 +18,14 @@ use std::ops::Deref;
18
18
/// start when there are no chain listeners to sync yet.
19
19
///
20
20
/// [`SpvClient`]: crate::SpvClient
21
- pub async fn validate_best_block_header < B : Deref > ( block_source : B ) ->
22
- BlockSourceResult < ValidatedBlockHeader > where B :: Target : BlockSource {
21
+ pub async fn validate_best_block_header < B : Deref > (
22
+ block_source : B ,
23
+ ) -> BlockSourceResult < ValidatedBlockHeader >
24
+ where
25
+ B :: Target : BlockSource ,
26
+ {
23
27
let ( best_block_hash, best_block_height) = block_source. get_best_block ( ) . await ?;
24
- block_source
25
- . get_header ( & best_block_hash, best_block_height) . await ?
26
- . validate ( best_block_hash)
28
+ block_source. get_header ( & best_block_hash, best_block_height) . await ?. validate ( best_block_hash)
27
29
}
28
30
29
31
/// Performs a one-time sync of chain listeners using a single *trusted* block source, bringing each
@@ -131,22 +133,27 @@ BlockSourceResult<ValidatedBlockHeader> where B::Target: BlockSource {
131
133
/// [`SpvClient`]: crate::SpvClient
132
134
/// [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
133
135
/// [`ChannelMonitor`]: lightning::chain::channelmonitor::ChannelMonitor
134
- pub async fn synchronize_listeners < B : Deref + Sized + Send + Sync , C : Cache , L : chain:: Listen + ?Sized > (
135
- block_source : B ,
136
- network : Network ,
137
- header_cache : & mut C ,
136
+ pub async fn synchronize_listeners <
137
+ B : Deref + Sized + Send + Sync ,
138
+ C : Cache ,
139
+ L : chain:: Listen + ?Sized ,
140
+ > (
141
+ block_source : B , network : Network , header_cache : & mut C ,
138
142
mut chain_listeners : Vec < ( BlockHash , & L ) > ,
139
- ) -> BlockSourceResult < ValidatedBlockHeader > where B :: Target : BlockSource {
143
+ ) -> BlockSourceResult < ValidatedBlockHeader >
144
+ where
145
+ B :: Target : BlockSource ,
146
+ {
140
147
let best_header = validate_best_block_header ( & * block_source) . await ?;
141
148
142
149
// Fetch the header for the block hash paired with each listener.
143
150
let mut chain_listeners_with_old_headers = Vec :: new ( ) ;
144
151
for ( old_block_hash, chain_listener) in chain_listeners. drain ( ..) {
145
152
let old_header = match header_cache. look_up ( & old_block_hash) {
146
153
Some ( header) => * header,
147
- None => block_source
148
- . get_header ( & old_block_hash, None ) . await ?
149
- . validate ( old_block_hash ) ?
154
+ None => {
155
+ block_source . get_header ( & old_block_hash, None ) . await ? . validate ( old_block_hash ) ?
156
+ } ,
150
157
} ;
151
158
chain_listeners_with_old_headers. push ( ( old_header, chain_listener) )
152
159
}
@@ -180,8 +187,10 @@ pub async fn synchronize_listeners<B: Deref + Sized + Send + Sync, C: Cache, L:
180
187
if let Some ( common_ancestor) = most_common_ancestor {
181
188
let chain_listener = & ChainListenerSet ( chain_listeners_at_height) ;
182
189
let mut chain_notifier = ChainNotifier { header_cache, chain_listener } ;
183
- chain_notifier. connect_blocks ( common_ancestor, most_connected_blocks, & mut chain_poller)
184
- . await . map_err ( |( e, _) | e) ?;
190
+ chain_notifier
191
+ . connect_blocks ( common_ancestor, most_connected_blocks, & mut chain_poller)
192
+ . await
193
+ . map_err ( |( e, _) | e) ?;
185
194
}
186
195
187
196
Ok ( best_header)
@@ -211,7 +220,9 @@ impl<'a, C: Cache> Cache for ReadOnlyCache<'a, C> {
211
220
struct DynamicChainListener < ' a , L : chain:: Listen + ?Sized > ( & ' a L ) ;
212
221
213
222
impl < ' a , L : chain:: Listen + ?Sized > chain:: Listen for DynamicChainListener < ' a , L > {
214
- fn filtered_block_connected ( & self , _header : & Header , _txdata : & chain:: transaction:: TransactionData , _height : u32 ) {
223
+ fn filtered_block_connected (
224
+ & self , _header : & Header , _txdata : & chain:: transaction:: TransactionData , _height : u32 ,
225
+ ) {
215
226
unreachable ! ( )
216
227
}
217
228
@@ -234,7 +245,9 @@ impl<'a, L: chain::Listen + ?Sized> chain::Listen for ChainListenerSet<'a, L> {
234
245
}
235
246
}
236
247
237
- fn filtered_block_connected ( & self , header : & Header , txdata : & chain:: transaction:: TransactionData , height : u32 ) {
248
+ fn filtered_block_connected (
249
+ & self , header : & Header , txdata : & chain:: transaction:: TransactionData , height : u32 ,
250
+ ) {
238
251
for ( starting_height, chain_listener) in self . 0 . iter ( ) {
239
252
if height > * starting_height {
240
253
chain_listener. filtered_block_connected ( header, txdata, height) ;
@@ -249,8 +262,8 @@ impl<'a, L: chain::Listen + ?Sized> chain::Listen for ChainListenerSet<'a, L> {
249
262
250
263
#[ cfg( test) ]
251
264
mod tests {
252
- use crate :: test_utils:: { Blockchain , MockChainListener } ;
253
265
use super :: * ;
266
+ use crate :: test_utils:: { Blockchain , MockChainListener } ;
254
267
255
268
#[ tokio:: test]
256
269
async fn sync_from_same_chain ( ) {
@@ -263,8 +276,7 @@ mod tests {
263
276
let listener_2 = MockChainListener :: new ( )
264
277
. expect_block_connected ( * chain. at_height ( 3 ) )
265
278
. expect_block_connected ( * chain. at_height ( 4 ) ) ;
266
- let listener_3 = MockChainListener :: new ( )
267
- . expect_block_connected ( * chain. at_height ( 4 ) ) ;
279
+ let listener_3 = MockChainListener :: new ( ) . expect_block_connected ( * chain. at_height ( 4 ) ) ;
268
280
269
281
let listeners = vec ! [
270
282
( chain. at_height( 1 ) . block_hash, & listener_1 as & dyn chain:: Listen ) ,
0 commit comments