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:: Client as ElectrumClient ;
@@ -32,6 +38,7 @@ use std::collections::HashMap;
3238use std:: sync:: Arc ;
3339use std:: time:: { Duration , Instant } ;
3440
41+ const BDK_ELECTRUM_CLIENT_BATCH_SIZE : usize = 5 ;
3542const ELECTRUM_CLIENT_NUM_RETRIES : u8 = 3 ;
3643const ELECTRUM_CLIENT_TIMEOUT_SECS : u8 = 20 ;
3744
@@ -109,6 +116,69 @@ impl ElectrumRuntimeClient {
109116 Ok ( res)
110117 }
111118
119+ pub ( crate ) async fn get_full_scan_wallet_update (
120+ & self , request : BdkFullScanRequest < BdkKeyChainKind > ,
121+ cached_txs : impl IntoIterator < Item = impl Into < Arc < Transaction > > > ,
122+ ) -> Result < BdkFullScanResponse < BdkKeyChainKind > , Error > {
123+ let bdk_electrum_client = Arc :: clone ( & self . bdk_electrum_client ) ;
124+ bdk_electrum_client. populate_tx_cache ( cached_txs) ;
125+
126+ let spawn_fut = self . runtime . spawn_blocking ( move || {
127+ bdk_electrum_client. full_scan (
128+ request,
129+ BDK_CLIENT_STOP_GAP ,
130+ BDK_ELECTRUM_CLIENT_BATCH_SIZE ,
131+ true ,
132+ )
133+ } ) ;
134+ let wallet_sync_timeout_fut =
135+ tokio:: time:: timeout ( Duration :: from_secs ( BDK_WALLET_SYNC_TIMEOUT_SECS ) , spawn_fut) ;
136+
137+ wallet_sync_timeout_fut
138+ . await
139+ . map_err ( |e| {
140+ log_error ! ( self . logger, "Sync of on-chain wallet timed out: {}" , e) ;
141+ Error :: WalletOperationTimeout
142+ } ) ?
143+ . map_err ( |e| {
144+ log_error ! ( self . logger, "Sync of on-chain wallet failed: {}" , e) ;
145+ Error :: WalletOperationFailed
146+ } ) ?
147+ . map_err ( |e| {
148+ log_error ! ( self . logger, "Sync of on-chain wallet failed: {}" , e) ;
149+ Error :: WalletOperationFailed
150+ } )
151+ }
152+
153+ pub ( crate ) async fn get_incremental_sync_wallet_update (
154+ & self , request : BdkSyncRequest < ( BdkKeyChainKind , u32 ) > ,
155+ cached_txs : impl IntoIterator < Item = impl Into < Arc < Transaction > > > ,
156+ ) -> Result < BdkSyncResponse , Error > {
157+ let bdk_electrum_client = Arc :: clone ( & self . bdk_electrum_client ) ;
158+ bdk_electrum_client. populate_tx_cache ( cached_txs) ;
159+
160+ let spawn_fut = self . runtime . spawn_blocking ( move || {
161+ bdk_electrum_client. sync ( request, BDK_ELECTRUM_CLIENT_BATCH_SIZE , true )
162+ } ) ;
163+ let wallet_sync_timeout_fut =
164+ tokio:: time:: timeout ( Duration :: from_secs ( BDK_WALLET_SYNC_TIMEOUT_SECS ) , spawn_fut) ;
165+
166+ wallet_sync_timeout_fut
167+ . await
168+ . map_err ( |e| {
169+ log_error ! ( self . logger, "Incremental sync of on-chain wallet timed out: {}" , e) ;
170+ Error :: WalletOperationTimeout
171+ } ) ?
172+ . map_err ( |e| {
173+ log_error ! ( self . logger, "Incremental sync of on-chain wallet failed: {}" , e) ;
174+ Error :: WalletOperationFailed
175+ } ) ?
176+ . map_err ( |e| {
177+ log_error ! ( self . logger, "Incremental sync of on-chain wallet failed: {}" , e) ;
178+ Error :: WalletOperationFailed
179+ } )
180+ }
181+
112182 pub ( crate ) async fn broadcast ( & self , tx : Transaction ) {
113183 let electrum_client = Arc :: clone ( & self . electrum_client ) ;
114184
0 commit comments