6
6
// accordance with one or both of these licenses.
7
7
8
8
use 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 ,
11
11
} ;
12
12
use crate :: error:: Error ;
13
13
use crate :: fee_estimator:: {
@@ -20,6 +20,12 @@ use lightning::chain::{Confirm, Filter, WatchedOutput};
20
20
use lightning:: util:: ser:: Writeable ;
21
21
use lightning_transaction_sync:: ElectrumSyncClient ;
22
22
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
+
23
29
use bdk_electrum:: BdkElectrumClient ;
24
30
25
31
use electrum_client:: Client as ElectrumClient ;
@@ -32,6 +38,7 @@ use std::collections::HashMap;
32
38
use std:: sync:: Arc ;
33
39
use std:: time:: { Duration , Instant } ;
34
40
41
+ const BDK_ELECTRUM_CLIENT_BATCH_SIZE : usize = 5 ;
35
42
const ELECTRUM_CLIENT_NUM_RETRIES : u8 = 3 ;
36
43
const ELECTRUM_CLIENT_TIMEOUT_SECS : u8 = 20 ;
37
44
@@ -109,6 +116,69 @@ impl ElectrumRuntimeClient {
109
116
Ok ( res)
110
117
}
111
118
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
+
112
182
pub ( crate ) async fn broadcast ( & self , tx : Transaction ) {
113
183
let electrum_client = Arc :: clone ( & self . electrum_client ) ;
114
184
0 commit comments