66// accordance with one or both of these licenses.
77
88use crate :: config:: {
9- Config , FEE_RATE_CACHE_UPDATE_TIMEOUT_SECS , LDK_WALLET_SYNC_TIMEOUT_SECS ,
10- TX_BROADCAST_TIMEOUT_SECS ,
9+ Config , BDK_CLIENT_STOP_GAP , BDK_WALLET_SYNC_TIMEOUT_SECS , FEE_RATE_CACHE_UPDATE_TIMEOUT_SECS ,
10+ LDK_WALLET_SYNC_TIMEOUT_SECS , TX_BROADCAST_TIMEOUT_SECS ,
1111} ;
1212use crate :: error:: Error ;
1313use crate :: fee_estimator:: {
@@ -20,6 +20,12 @@ use lightning::chain::{Confirm, Filter, WatchedOutput};
2020use lightning:: util:: ser:: Writeable ;
2121use lightning_transaction_sync:: ElectrumSyncClient ;
2222
23+ use bdk_chain:: bdk_core:: spk_client:: FullScanRequest as BdkFullScanRequest ;
24+ use bdk_chain:: bdk_core:: spk_client:: FullScanResponse as BdkFullScanResponse ;
25+ use bdk_chain:: bdk_core:: spk_client:: SyncRequest as BdkSyncRequest ;
26+ use bdk_chain:: bdk_core:: spk_client:: SyncResponse as BdkSyncResponse ;
27+ use bdk_wallet:: KeychainKind as BdkKeyChainKind ;
28+
2329use bdk_electrum:: BdkElectrumClient ;
2430
2531use electrum_client:: { Batch , Client as ElectrumClient , ElectrumApi } ;
@@ -30,6 +36,8 @@ use std::collections::HashMap;
3036use std:: sync:: Arc ;
3137use std:: time:: { Duration , Instant } ;
3238
39+ const BDK_ELECTRUM_CLIENT_BATCH_SIZE : usize = 5 ;
40+
3341pub ( crate ) struct ElectrumRuntimeClient {
3442 electrum_client : Arc < ElectrumClient > ,
3543 bdk_electrum_client : Arc < BdkElectrumClient < ElectrumClient > > ,
@@ -96,6 +104,69 @@ impl ElectrumRuntimeClient {
96104 Ok ( res)
97105 }
98106
107+ pub ( crate ) async fn get_full_scan_wallet_update (
108+ & self , request : BdkFullScanRequest < BdkKeyChainKind > ,
109+ cached_txs : impl IntoIterator < Item = impl Into < Arc < Transaction > > > ,
110+ ) -> Result < BdkFullScanResponse < BdkKeyChainKind > , Error > {
111+ let bdk_electrum_client = Arc :: clone ( & self . bdk_electrum_client ) ;
112+ bdk_electrum_client. populate_tx_cache ( cached_txs) ;
113+
114+ let spawn_fut = self . runtime . spawn_blocking ( move || {
115+ bdk_electrum_client. full_scan (
116+ request,
117+ BDK_CLIENT_STOP_GAP ,
118+ BDK_ELECTRUM_CLIENT_BATCH_SIZE ,
119+ true ,
120+ )
121+ } ) ;
122+ let wallet_sync_timeout_fut =
123+ tokio:: time:: timeout ( Duration :: from_secs ( BDK_WALLET_SYNC_TIMEOUT_SECS ) , spawn_fut) ;
124+
125+ wallet_sync_timeout_fut
126+ . await
127+ . map_err ( |e| {
128+ log_error ! ( self . logger, "Sync of on-chain wallet timed out: {}" , e) ;
129+ Error :: WalletOperationTimeout
130+ } ) ?
131+ . map_err ( |e| {
132+ log_error ! ( self . logger, "Sync of on-chain wallet failed: {}" , e) ;
133+ Error :: WalletOperationFailed
134+ } ) ?
135+ . map_err ( |e| {
136+ log_error ! ( self . logger, "Sync of on-chain wallet failed: {}" , e) ;
137+ Error :: WalletOperationFailed
138+ } )
139+ }
140+
141+ pub ( crate ) async fn get_incremental_sync_wallet_update (
142+ & self , request : BdkSyncRequest < ( BdkKeyChainKind , u32 ) > ,
143+ cached_txs : impl IntoIterator < Item = impl Into < Arc < Transaction > > > ,
144+ ) -> Result < BdkSyncResponse , Error > {
145+ let bdk_electrum_client = Arc :: clone ( & self . bdk_electrum_client ) ;
146+ bdk_electrum_client. populate_tx_cache ( cached_txs) ;
147+
148+ let spawn_fut = self . runtime . spawn_blocking ( move || {
149+ bdk_electrum_client. sync ( request, BDK_ELECTRUM_CLIENT_BATCH_SIZE , true )
150+ } ) ;
151+ let wallet_sync_timeout_fut =
152+ tokio:: time:: timeout ( Duration :: from_secs ( BDK_WALLET_SYNC_TIMEOUT_SECS ) , spawn_fut) ;
153+
154+ wallet_sync_timeout_fut
155+ . await
156+ . map_err ( |e| {
157+ log_error ! ( self . logger, "Incremental sync of on-chain wallet timed out: {}" , e) ;
158+ Error :: WalletOperationTimeout
159+ } ) ?
160+ . map_err ( |e| {
161+ log_error ! ( self . logger, "Incremental sync of on-chain wallet failed: {}" , e) ;
162+ Error :: WalletOperationFailed
163+ } ) ?
164+ . map_err ( |e| {
165+ log_error ! ( self . logger, "Incremental sync of on-chain wallet failed: {}" , e) ;
166+ Error :: WalletOperationFailed
167+ } )
168+ }
169+
99170 pub ( crate ) async fn broadcast ( & self , tx : Transaction ) {
100171 let electrum_client = Arc :: clone ( & self . electrum_client ) ;
101172
0 commit comments