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:: { Batch , Client as ElectrumClient , ElectrumApi } ;
@@ -30,6 +36,8 @@ use std::collections::HashMap;
30
36
use std:: sync:: Arc ;
31
37
use std:: time:: { Duration , Instant } ;
32
38
39
+ const BDK_ELECTRUM_CLIENT_BATCH_SIZE : usize = 5 ;
40
+
33
41
pub ( crate ) struct ElectrumRuntimeClient {
34
42
electrum_client : Arc < ElectrumClient > ,
35
43
bdk_electrum_client : Arc < BdkElectrumClient < ElectrumClient > > ,
@@ -96,6 +104,69 @@ impl ElectrumRuntimeClient {
96
104
Ok ( res)
97
105
}
98
106
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
+
99
170
pub ( crate ) async fn broadcast ( & self , tx : Transaction ) {
100
171
let electrum_client = Arc :: clone ( & self . electrum_client ) ;
101
172
0 commit comments