5
5
// http://opensource.org/licenses/MIT>, at your option. You may not use this file except in
6
6
// accordance with one or both of these licenses.
7
7
8
- use crate :: config:: { Config , FEE_RATE_CACHE_UPDATE_TIMEOUT_SECS , TX_BROADCAST_TIMEOUT_SECS } ;
8
+ use crate :: config:: {
9
+ Config , FEE_RATE_CACHE_UPDATE_TIMEOUT_SECS , LDK_WALLET_SYNC_TIMEOUT_SECS ,
10
+ TX_BROADCAST_TIMEOUT_SECS ,
11
+ } ;
9
12
use crate :: error:: Error ;
10
13
use crate :: fee_estimator:: {
11
14
apply_post_estimation_adjustments, get_all_conf_targets, get_num_block_defaults_for_target,
12
15
ConfirmationTarget ,
13
16
} ;
14
- use crate :: logger:: { log_bytes, log_error, log_trace, LdkLogger , Logger } ;
17
+ use crate :: logger:: { log_bytes, log_error, log_info , log_trace, LdkLogger , Logger } ;
15
18
16
- use lightning:: chain:: { Filter , WatchedOutput } ;
19
+ use lightning:: chain:: { Confirm , Filter , WatchedOutput } ;
17
20
use lightning:: util:: ser:: Writeable ;
18
21
use lightning_transaction_sync:: ElectrumSyncClient ;
19
22
@@ -27,7 +30,7 @@ use bitcoin::{FeeRate, Network, Script, Transaction, Txid};
27
30
28
31
use std:: collections:: HashMap ;
29
32
use std:: sync:: Arc ;
30
- use std:: time:: Duration ;
33
+ use std:: time:: { Duration , Instant } ;
31
34
32
35
const ELECTRUM_CLIENT_NUM_RETRIES : u8 = 3 ;
33
36
const ELECTRUM_CLIENT_TIMEOUT_SECS : u8 = 20 ;
@@ -72,6 +75,40 @@ impl ElectrumRuntimeClient {
72
75
Ok ( Self { electrum_client, bdk_electrum_client, tx_sync, runtime, config, logger } )
73
76
}
74
77
78
+ pub ( crate ) async fn sync_confirmables (
79
+ & self , confirmables : Vec < Arc < dyn Confirm + Sync + Send > > ,
80
+ ) -> Result < ( ) , Error > {
81
+ let now = Instant :: now ( ) ;
82
+
83
+ let tx_sync = Arc :: clone ( & self . tx_sync ) ;
84
+ let spawn_fut = self . runtime . spawn_blocking ( move || tx_sync. sync ( confirmables) ) ;
85
+ let timeout_fut =
86
+ tokio:: time:: timeout ( Duration :: from_secs ( LDK_WALLET_SYNC_TIMEOUT_SECS ) , spawn_fut) ;
87
+
88
+ let res = timeout_fut
89
+ . await
90
+ . map_err ( |e| {
91
+ log_error ! ( self . logger, "Sync of Lightning wallet timed out: {}" , e) ;
92
+ Error :: TxSyncTimeout
93
+ } ) ?
94
+ . map_err ( |e| {
95
+ log_error ! ( self . logger, "Sync of Lightning wallet failed: {}" , e) ;
96
+ Error :: TxSyncFailed
97
+ } ) ?
98
+ . map_err ( |e| {
99
+ log_error ! ( self . logger, "Sync of Lightning wallet failed: {}" , e) ;
100
+ Error :: TxSyncFailed
101
+ } ) ?;
102
+
103
+ log_info ! (
104
+ self . logger,
105
+ "Sync of Lightning wallet finished in {}ms." ,
106
+ now. elapsed( ) . as_millis( )
107
+ ) ;
108
+
109
+ Ok ( res)
110
+ }
111
+
75
112
pub ( crate ) async fn broadcast ( & self , tx : Transaction ) {
76
113
let electrum_client = Arc :: clone ( & self . electrum_client ) ;
77
114
0 commit comments