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
@@ -25,7 +28,7 @@ use bitcoin::{FeeRate, Network, Script, Transaction, Txid};
2528
2629use std:: collections:: HashMap ;
2730use std:: sync:: Arc ;
28- use std:: time:: Duration ;
31+ use std:: time:: { Duration , Instant } ;
2932
3033pub ( crate ) struct ElectrumRuntimeClient {
3134 electrum_client : Arc < ElectrumClient > ,
@@ -59,6 +62,40 @@ impl ElectrumRuntimeClient {
5962 Ok ( Self { electrum_client, bdk_electrum_client, tx_sync, runtime, config, logger } )
6063 }
6164
65+ pub ( crate ) async fn sync_confirmables (
66+ & self , confirmables : Vec < Arc < dyn Confirm + Sync + Send > > ,
67+ ) -> Result < ( ) , Error > {
68+ let now = Instant :: now ( ) ;
69+
70+ let tx_sync = Arc :: clone ( & self . tx_sync ) ;
71+ let spawn_fut = self . runtime . spawn_blocking ( move || tx_sync. sync ( confirmables) ) ;
72+ let timeout_fut =
73+ tokio:: time:: timeout ( Duration :: from_secs ( LDK_WALLET_SYNC_TIMEOUT_SECS ) , spawn_fut) ;
74+
75+ let res = timeout_fut
76+ . await
77+ . map_err ( |e| {
78+ log_error ! ( self . logger, "Sync of Lightning wallet timed out: {}" , e) ;
79+ Error :: TxSyncTimeout
80+ } ) ?
81+ . map_err ( |e| {
82+ log_error ! ( self . logger, "Sync of Lightning wallet failed: {}" , e) ;
83+ Error :: TxSyncFailed
84+ } ) ?
85+ . map_err ( |e| {
86+ log_error ! ( self . logger, "Sync of Lightning wallet failed: {}" , e) ;
87+ Error :: TxSyncFailed
88+ } ) ?;
89+
90+ log_info ! (
91+ self . logger,
92+ "Sync of Lightning wallet finished in {}ms." ,
93+ now. elapsed( ) . as_millis( )
94+ ) ;
95+
96+ Ok ( res)
97+ }
98+
6299 pub ( crate ) async fn broadcast ( & self , tx : Transaction ) {
63100 let electrum_client = Arc :: clone ( & self . electrum_client ) ;
64101
0 commit comments