1
- use crate :: error :: { TxSyncError , InternalError } ;
2
- use crate :: common :: { SyncState , FilterQueue , ConfirmedTx } ;
1
+ use crate :: common :: { ConfirmedTx , FilterQueue , SyncState } ;
2
+ use crate :: error :: { InternalError , TxSyncError } ;
3
3
4
- use lightning:: util:: logger:: Logger ;
5
- use lightning:: { log_error, log_debug, log_trace} ;
6
4
use lightning:: chain:: WatchedOutput ;
7
5
use lightning:: chain:: { Confirm , Filter } ;
6
+ use lightning:: util:: logger:: Logger ;
7
+ use lightning:: { log_debug, log_error, log_trace} ;
8
8
9
9
use bitcoin:: { BlockHash , Script , Txid } ;
10
10
11
- use esplora_client:: Builder ;
12
- #[ cfg( feature = "async-interface" ) ]
13
- use esplora_client:: r#async:: AsyncClient ;
14
11
#[ cfg( not( feature = "async-interface" ) ) ]
15
12
use esplora_client:: blocking:: BlockingClient ;
13
+ #[ cfg( feature = "async-interface" ) ]
14
+ use esplora_client:: r#async:: AsyncClient ;
15
+ use esplora_client:: Builder ;
16
16
17
- use std:: collections:: HashSet ;
18
17
use core:: ops:: Deref ;
18
+ use std:: collections:: HashSet ;
19
19
20
20
/// Synchronizes LDK with a given [`Esplora`] server.
21
21
///
64
64
pub fn from_client ( client : EsploraClientType , logger : L ) -> Self {
65
65
let sync_state = MutexType :: new ( SyncState :: new ( ) ) ;
66
66
let queue = std:: sync:: Mutex :: new ( FilterQueue :: new ( ) ) ;
67
- Self {
68
- sync_state,
69
- queue,
70
- client,
71
- logger,
72
- }
67
+ Self { sync_state, queue, client, logger }
73
68
}
74
69
75
70
/// Synchronizes the given `confirmables` via their [`Confirm`] interface implementations. This
85
80
/// [`Filter`]: lightning::chain::Filter
86
81
#[ maybe_async]
87
82
pub fn sync < C : Deref > ( & self , confirmables : Vec < C > ) -> Result < ( ) , TxSyncError >
88
- where C :: Target : Confirm
83
+ where
84
+ C :: Target : Confirm ,
89
85
{
90
86
// This lock makes sure we're syncing once at a time.
91
87
#[ cfg( not( feature = "async-interface" ) ) ]
@@ -130,9 +126,9 @@ where
130
126
num_unconfirmed += unconfirmed_txs. len ( ) ;
131
127
sync_state. sync_unconfirmed_transactions (
132
128
& confirmables,
133
- unconfirmed_txs
129
+ unconfirmed_txs,
134
130
) ;
135
- }
131
+ } ,
136
132
Err ( err) => {
137
133
// (Semi-)permanent failure, retry later.
138
134
log_error ! ( self . logger,
@@ -142,7 +138,7 @@ where
142
138
) ;
143
139
sync_state. pending_sync = true ;
144
140
return Err ( TxSyncError :: from ( err) ) ;
145
- }
141
+ } ,
146
142
}
147
143
} ,
148
144
Err ( err) => {
@@ -154,17 +150,24 @@ where
154
150
) ;
155
151
sync_state. pending_sync = true ;
156
152
return Err ( TxSyncError :: from ( err) ) ;
157
- }
153
+ } ,
158
154
}
159
155
160
- match maybe_await ! ( self . sync_best_block_updated( & confirmables, & mut sync_state, & tip_hash) ) {
161
- Ok ( ( ) ) => { }
156
+ match maybe_await ! ( self . sync_best_block_updated(
157
+ & confirmables,
158
+ & mut sync_state,
159
+ & tip_hash
160
+ ) ) {
161
+ Ok ( ( ) ) => { } ,
162
162
Err ( InternalError :: Inconsistency ) => {
163
163
// Immediately restart syncing when we encounter any inconsistencies.
164
- log_debug ! ( self . logger, "Encountered inconsistency during transaction sync, restarting." ) ;
164
+ log_debug ! (
165
+ self . logger,
166
+ "Encountered inconsistency during transaction sync, restarting."
167
+ ) ;
165
168
sync_state. pending_sync = true ;
166
169
continue ;
167
- }
170
+ } ,
168
171
Err ( err) => {
169
172
// (Semi-)permanent failure, retry later.
170
173
log_error ! ( self . logger,
@@ -174,7 +177,7 @@ where
174
177
) ;
175
178
sync_state. pending_sync = true ;
176
179
return Err ( TxSyncError :: from ( err) ) ;
177
- }
180
+ } ,
178
181
}
179
182
}
180
183
@@ -193,11 +196,9 @@ where
193
196
continue ;
194
197
}
195
198
num_confirmed += confirmed_txs. len ( ) ;
196
- sync_state. sync_confirmed_transactions (
197
- & confirmables,
198
- confirmed_txs
199
- ) ;
200
- }
199
+ sync_state
200
+ . sync_confirmed_transactions ( & confirmables, confirmed_txs) ;
201
+ } ,
201
202
Err ( err) => {
202
203
// (Semi-)permanent failure, retry later.
203
204
log_error ! ( self . logger,
@@ -207,15 +208,18 @@ where
207
208
) ;
208
209
sync_state. pending_sync = true ;
209
210
return Err ( TxSyncError :: from ( err) ) ;
210
- }
211
+ } ,
211
212
}
212
- }
213
+ } ,
213
214
Err ( InternalError :: Inconsistency ) => {
214
215
// Immediately restart syncing when we encounter any inconsistencies.
215
- log_debug ! ( self . logger, "Encountered inconsistency during transaction sync, restarting." ) ;
216
+ log_debug ! (
217
+ self . logger,
218
+ "Encountered inconsistency during transaction sync, restarting."
219
+ ) ;
216
220
sync_state. pending_sync = true ;
217
221
continue ;
218
- }
222
+ } ,
219
223
Err ( err) => {
220
224
// (Semi-)permanent failure, retry later.
221
225
log_error ! ( self . logger,
@@ -225,26 +229,38 @@ where
225
229
) ;
226
230
sync_state. pending_sync = true ;
227
231
return Err ( TxSyncError :: from ( err) ) ;
228
- }
232
+ } ,
229
233
}
230
234
sync_state. last_sync_hash = Some ( tip_hash) ;
231
235
sync_state. pending_sync = false ;
232
236
}
233
237
}
234
238
#[ cfg( feature = "time" ) ]
235
- log_debug ! ( self . logger, "Finished transaction sync at tip {} in {}ms: {} confirmed, {} unconfirmed." ,
236
- tip_hash, start_time. elapsed( ) . as_millis( ) , num_confirmed, num_unconfirmed) ;
239
+ log_debug ! (
240
+ self . logger,
241
+ "Finished transaction sync at tip {} in {}ms: {} confirmed, {} unconfirmed." ,
242
+ tip_hash,
243
+ start_time. elapsed( ) . as_millis( ) ,
244
+ num_confirmed,
245
+ num_unconfirmed
246
+ ) ;
237
247
#[ cfg( not( feature = "time" ) ) ]
238
- log_debug ! ( self . logger, "Finished transaction sync at tip {}: {} confirmed, {} unconfirmed." ,
239
- tip_hash, num_confirmed, num_unconfirmed) ;
248
+ log_debug ! (
249
+ self . logger,
250
+ "Finished transaction sync at tip {}: {} confirmed, {} unconfirmed." ,
251
+ tip_hash,
252
+ num_confirmed,
253
+ num_unconfirmed
254
+ ) ;
240
255
Ok ( ( ) )
241
256
}
242
257
243
258
#[ maybe_async]
244
259
fn sync_best_block_updated < C : Deref > (
245
260
& self , confirmables : & Vec < C > , sync_state : & mut SyncState , tip_hash : & BlockHash ,
246
261
) -> Result < ( ) , InternalError >
247
- where C :: Target : Confirm
262
+ where
263
+ C :: Target : Confirm ,
248
264
{
249
265
// Inform the interface of the new block.
250
266
let tip_header = maybe_await ! ( self . client. get_header_by_hash( tip_hash) ) ?;
@@ -268,7 +284,6 @@ where
268
284
fn get_confirmed_transactions (
269
285
& self , sync_state : & SyncState ,
270
286
) -> Result < Vec < ConfirmedTx > , InternalError > {
271
-
272
287
// First, check the confirmation status of registered transactions as well as the
273
288
// status of dependent transactions of registered outputs.
274
289
@@ -284,7 +299,8 @@ where
284
299
}
285
300
286
301
for ( _, output) in & sync_state. watched_outputs {
287
- if let Some ( output_status) = maybe_await ! ( self . client
302
+ if let Some ( output_status) = maybe_await ! ( self
303
+ . client
288
304
. get_output_status( & output. outpoint. txid, output. outpoint. index as u64 ) ) ?
289
305
{
290
306
if let Some ( spending_txid) = output_status. txid {
@@ -299,13 +315,11 @@ where
299
315
}
300
316
}
301
317
302
- if let Some ( confirmed_tx) = maybe_await ! ( self
303
- . get_confirmed_tx(
304
- spending_txid,
305
- spending_tx_status. block_hash,
306
- spending_tx_status. block_height,
307
- ) ) ?
308
- {
318
+ if let Some ( confirmed_tx) = maybe_await ! ( self . get_confirmed_tx(
319
+ spending_txid,
320
+ spending_tx_status. block_hash,
321
+ spending_tx_status. block_height,
322
+ ) ) ? {
309
323
confirmed_txs. push ( confirmed_tx) ;
310
324
}
311
325
}
@@ -331,7 +345,13 @@ where
331
345
let block_hash = block_header. block_hash ( ) ;
332
346
if let Some ( expected_block_hash) = expected_block_hash {
333
347
if expected_block_hash != block_hash {
334
- log_trace ! ( self . logger, "Inconsistency: Tx {} expected in block {}, but is confirmed in {}" , txid, expected_block_hash, block_hash) ;
348
+ log_trace ! (
349
+ self . logger,
350
+ "Inconsistency: Tx {} expected in block {}, but is confirmed in {}" ,
351
+ txid,
352
+ expected_block_hash,
353
+ block_hash
354
+ ) ;
335
355
return Err ( InternalError :: Inconsistency ) ;
336
356
}
337
357
}
@@ -363,7 +383,11 @@ where
363
383
} else {
364
384
// If any previously-confirmed block suddenly is no longer confirmed, we found
365
385
// an inconsistency and should start over.
366
- log_trace ! ( self . logger, "Inconsistency: Tx {} was unconfirmed during syncing." , txid) ;
386
+ log_trace ! (
387
+ self . logger,
388
+ "Inconsistency: Tx {} was unconfirmed during syncing." ,
389
+ txid
390
+ ) ;
367
391
return Err ( InternalError :: Inconsistency ) ;
368
392
}
369
393
}
@@ -375,7 +399,8 @@ where
375
399
fn get_unconfirmed_transactions < C : Deref > (
376
400
& self , confirmables : & Vec < C > ,
377
401
) -> Result < Vec < Txid > , InternalError >
378
- where C :: Target : Confirm
402
+ where
403
+ C :: Target : Confirm ,
379
404
{
380
405
// Query the interface for relevant txids and check whether the relevant blocks are still
381
406
// in the best chain, mark them unconfirmed otherwise
@@ -422,7 +447,6 @@ type EsploraClientType = AsyncClient;
422
447
#[ cfg( not( feature = "async-interface" ) ) ]
423
448
type EsploraClientType = BlockingClient ;
424
449
425
-
426
450
impl < L : Deref > Filter for EsploraSyncClient < L >
427
451
where
428
452
L :: Target : Logger ,
0 commit comments