55// http://opensource.org/licenses/MIT>, at your option. You may not use this file except in
66// accordance with one or both of these licenses.
77
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+ } ;
912use crate :: error:: Error ;
1013use crate :: fee_estimator:: {
1114 apply_post_estimation_adjustments, get_all_conf_targets, get_num_block_defaults_for_target,
1215 ConfirmationTarget ,
1316} ;
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 } ;
1518
16- use lightning:: chain:: { Filter , WatchedOutput } ;
19+ use lightning:: chain:: { Confirm , Filter , WatchedOutput } ;
1720use lightning:: util:: ser:: Writeable ;
1821use lightning_transaction_sync:: ElectrumSyncClient ;
1922
@@ -27,7 +30,7 @@ use bitcoin::{FeeRate, Network, Script, Transaction, Txid};
2730
2831use std:: collections:: HashMap ;
2932use std:: sync:: Arc ;
30- use std:: time:: Duration ;
33+ use std:: time:: { Duration , Instant } ;
3134
3235const ELECTRUM_CLIENT_NUM_RETRIES : u8 = 3 ;
3336const ELECTRUM_CLIENT_TIMEOUT_SECS : u8 = 20 ;
@@ -72,6 +75,40 @@ impl ElectrumRuntimeClient {
7275 Ok ( Self { electrum_client, bdk_electrum_client, tx_sync, runtime, config, logger } )
7376 }
7477
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+
75112 pub ( crate ) async fn broadcast ( & self , tx : Transaction ) {
76113 let electrum_client = Arc :: clone ( & self . electrum_client ) ;
77114
0 commit comments