1
1
use crate :: error:: LdkLiteError as Error ;
2
+ #[ allow( unused_imports) ]
3
+ use crate :: logger:: {
4
+ log_error, log_given_level, log_info, log_internal, log_trace, log_warn, FilesystemLogger ,
5
+ Logger ,
6
+ } ;
7
+
2
8
3
9
use lightning:: chain:: chaininterface:: { BroadcasterInterface , ConfirmationTarget , FeeEstimator } ;
4
10
use lightning:: chain:: WatchedOutput ;
@@ -11,7 +17,8 @@ use bdk::{SignOptions, SyncOptions};
11
17
12
18
use bitcoin:: { BlockHash , Script , Transaction , Txid } ;
13
19
14
- use std:: sync:: Mutex ;
20
+ use std:: sync:: { Mutex , Arc } ;
21
+ use std:: time:: Instant ;
15
22
16
23
pub struct LdkLiteChainAccess < D >
17
24
where
@@ -24,13 +31,14 @@ where
24
31
queued_outputs : Mutex < Vec < WatchedOutput > > ,
25
32
watched_outputs : Mutex < Vec < WatchedOutput > > ,
26
33
last_sync_height : Mutex < Option < u32 > > ,
34
+ logger : Arc < FilesystemLogger > ,
27
35
}
28
36
29
37
impl < D > LdkLiteChainAccess < D >
30
38
where
31
39
D : BatchDatabase ,
32
40
{
33
- pub fn new ( blockchain : EsploraBlockchain , wallet : bdk:: Wallet < D > ) -> Self {
41
+ pub ( crate ) fn new ( blockchain : EsploraBlockchain , wallet : bdk:: Wallet < D > , logger : Arc < FilesystemLogger > ) -> Self {
34
42
let wallet = Mutex :: new ( wallet) ;
35
43
let watched_transactions = Mutex :: new ( Vec :: new ( ) ) ;
36
44
let queued_transactions = Mutex :: new ( Vec :: new ( ) ) ;
@@ -45,21 +53,26 @@ where
45
53
queued_outputs,
46
54
watched_outputs,
47
55
last_sync_height,
56
+ logger,
48
57
}
49
58
}
50
59
51
- pub fn sync_wallet ( & self ) -> Result < ( ) , Error > {
60
+ pub ( crate ) fn sync_wallet ( & self ) -> Result < ( ) , Error > {
52
61
let sync_options = SyncOptions { progress : None } ;
53
62
63
+ let now = Instant :: now ( ) ;
54
64
self . wallet
55
65
. lock ( )
56
66
. unwrap ( )
57
67
. sync ( & self . blockchain , sync_options)
58
68
. map_err ( |e| Error :: Bdk ( e) ) ?;
69
+
70
+ log_info ! ( self . logger, "On-chain wallet sync finished in {} seconds." , now. elapsed( ) . as_secs( ) ) ;
71
+
59
72
Ok ( ( ) )
60
73
}
61
74
62
- pub fn sync ( & self , confirmables : Vec < & dyn Confirm > ) -> Result < ( ) , Error > {
75
+ pub ( crate ) fn sync ( & self , confirmables : Vec < & dyn Confirm > ) -> Result < ( ) , Error > {
63
76
let client = & * self . blockchain ;
64
77
65
78
let cur_height = client. get_height ( ) ?;
@@ -209,7 +222,7 @@ where
209
222
Ok ( ( ) )
210
223
}
211
224
212
- pub fn create_funding_transaction (
225
+ pub ( crate ) fn create_funding_transaction (
213
226
& self , output_script : & Script , value_sats : u64 , confirmation_target : ConfirmationTarget ,
214
227
) -> Result < Transaction , Error > {
215
228
let num_blocks = num_blocks_from_conf_target ( confirmation_target) ;
@@ -238,7 +251,7 @@ where
238
251
Ok ( psbt. extract_tx ( ) )
239
252
}
240
253
241
- pub fn get_new_address ( & self ) -> Result < bitcoin:: Address , Error > {
254
+ pub ( crate ) fn get_new_address ( & self ) -> Result < bitcoin:: Address , Error > {
242
255
let address_info = self . wallet . lock ( ) . unwrap ( ) . get_address ( AddressIndex :: New ) ?;
243
256
Ok ( address_info. address )
244
257
}
0 commit comments