@@ -15,9 +15,10 @@ use crate::fee_estimator::{
15
15
apply_post_estimation_adjustments, get_all_conf_targets, get_num_block_defaults_for_target,
16
16
OnchainFeeEstimator ,
17
17
} ;
18
+ use crate :: io:: utils:: write_node_metrics;
18
19
use crate :: logger:: { log_bytes, log_error, log_info, log_trace, FilesystemLogger , Logger } ;
19
- use crate :: types:: { Broadcaster , ChainMonitor , ChannelManager , Sweeper , Wallet } ;
20
- use crate :: Error ;
20
+ use crate :: types:: { Broadcaster , ChainMonitor , ChannelManager , DynStore , Sweeper , Wallet } ;
21
+ use crate :: { Error , NodeMetrics } ;
21
22
22
23
use lightning:: chain:: { Confirm , Filter } ;
23
24
use lightning:: util:: ser:: Writeable ;
@@ -102,23 +103,18 @@ pub(crate) enum ChainSource {
102
103
lightning_wallet_sync_status : Mutex < WalletSyncStatus > ,
103
104
fee_estimator : Arc < OnchainFeeEstimator > ,
104
105
tx_broadcaster : Arc < Broadcaster > ,
106
+ kv_store : Arc < DynStore > ,
105
107
config : Arc < Config > ,
106
108
logger : Arc < FilesystemLogger > ,
107
- latest_wallet_sync_timestamp : Arc < RwLock < Option < u64 > > > ,
108
- latest_onchain_wallet_sync_timestamp : Arc < RwLock < Option < u64 > > > ,
109
- latest_fee_rate_cache_update_timestamp : Arc < RwLock < Option < u64 > > > ,
110
- latest_channel_monitor_archival_height : Arc < RwLock < Option < u32 > > > ,
109
+ node_metrics : Arc < RwLock < NodeMetrics > > ,
111
110
} ,
112
111
}
113
112
114
113
impl ChainSource {
115
114
pub ( crate ) fn new_esplora (
116
115
server_url : String , onchain_wallet : Arc < Wallet > , fee_estimator : Arc < OnchainFeeEstimator > ,
117
- tx_broadcaster : Arc < Broadcaster > , config : Arc < Config > , logger : Arc < FilesystemLogger > ,
118
- latest_wallet_sync_timestamp : Arc < RwLock < Option < u64 > > > ,
119
- latest_onchain_wallet_sync_timestamp : Arc < RwLock < Option < u64 > > > ,
120
- latest_fee_rate_cache_update_timestamp : Arc < RwLock < Option < u64 > > > ,
121
- latest_channel_monitor_archival_height : Arc < RwLock < Option < u32 > > > ,
116
+ tx_broadcaster : Arc < Broadcaster > , kv_store : Arc < DynStore > , config : Arc < Config > ,
117
+ logger : Arc < FilesystemLogger > , node_metrics : Arc < RwLock < NodeMetrics > > ,
122
118
) -> Self {
123
119
let mut client_builder = esplora_client:: Builder :: new ( & server_url) ;
124
120
client_builder = client_builder. timeout ( DEFAULT_ESPLORA_CLIENT_TIMEOUT_SECS ) ;
@@ -135,12 +131,10 @@ impl ChainSource {
135
131
lightning_wallet_sync_status,
136
132
fee_estimator,
137
133
tx_broadcaster,
134
+ kv_store,
138
135
config,
139
136
logger,
140
- latest_wallet_sync_timestamp,
141
- latest_onchain_wallet_sync_timestamp,
142
- latest_fee_rate_cache_update_timestamp,
143
- latest_channel_monitor_archival_height,
137
+ node_metrics,
144
138
}
145
139
}
146
140
@@ -211,8 +205,9 @@ impl ChainSource {
211
205
esplora_client,
212
206
onchain_wallet,
213
207
onchain_wallet_sync_status,
208
+ kv_store,
214
209
logger,
215
- latest_onchain_wallet_sync_timestamp ,
210
+ node_metrics ,
216
211
..
217
212
} => {
218
213
let receiver_res = {
@@ -232,7 +227,7 @@ impl ChainSource {
232
227
// If this is our first sync, do a full scan with the configured gap limit.
233
228
// Otherwise just do an incremental sync.
234
229
let incremental_sync =
235
- latest_onchain_wallet_sync_timestamp . read ( ) . unwrap ( ) . is_some ( ) ;
230
+ node_metrics . read ( ) . unwrap ( ) . latest_onchain_wallet_sync_timestamp . is_some ( ) ;
236
231
237
232
macro_rules! get_and_apply_wallet_update {
238
233
( $sync_future: expr) => { {
@@ -251,8 +246,11 @@ impl ChainSource {
251
246
. duration_since( UNIX_EPOCH )
252
247
. ok( )
253
248
. map( |d| d. as_secs( ) ) ;
254
- * latest_onchain_wallet_sync_timestamp. write( ) . unwrap( ) =
255
- unix_time_secs_opt;
249
+ {
250
+ let mut locked_node_metrics = node_metrics. write( ) . unwrap( ) ;
251
+ locked_node_metrics. latest_onchain_wallet_sync_timestamp = unix_time_secs_opt;
252
+ write_node_metrics( & * locked_node_metrics, Arc :: clone( & kv_store) , Arc :: clone( & logger) ) ?;
253
+ }
256
254
Ok ( ( ) )
257
255
} ,
258
256
Err ( e) => Err ( e) ,
@@ -327,9 +325,9 @@ impl ChainSource {
327
325
Self :: Esplora {
328
326
tx_sync,
329
327
lightning_wallet_sync_status,
328
+ kv_store,
330
329
logger,
331
- latest_wallet_sync_timestamp,
332
- latest_channel_monitor_archival_height,
330
+ node_metrics,
333
331
..
334
332
} => {
335
333
let sync_cman = Arc :: clone ( & channel_manager) ;
@@ -372,13 +370,24 @@ impl ChainSource {
372
370
. duration_since ( UNIX_EPOCH )
373
371
. ok ( )
374
372
. map ( |d| d. as_secs ( ) ) ;
375
- * latest_wallet_sync_timestamp. write ( ) . unwrap ( ) = unix_time_secs_opt;
373
+ {
374
+ let mut locked_node_metrics = node_metrics. write ( ) . unwrap ( ) ;
375
+ locked_node_metrics. latest_lightning_wallet_sync_timestamp =
376
+ unix_time_secs_opt;
377
+ write_node_metrics (
378
+ & * locked_node_metrics,
379
+ Arc :: clone ( & kv_store) ,
380
+ Arc :: clone ( & logger) ,
381
+ ) ?;
382
+ }
376
383
377
384
periodically_archive_fully_resolved_monitors (
378
385
Arc :: clone ( & channel_manager) ,
379
386
Arc :: clone ( & chain_monitor) ,
380
- Arc :: clone ( & latest_channel_monitor_archival_height) ,
381
- ) ;
387
+ Arc :: clone ( & kv_store) ,
388
+ Arc :: clone ( & logger) ,
389
+ Arc :: clone ( & node_metrics) ,
390
+ ) ?;
382
391
Ok ( ( ) )
383
392
} ,
384
393
Err ( e) => {
@@ -406,8 +415,9 @@ impl ChainSource {
406
415
esplora_client,
407
416
fee_estimator,
408
417
config,
418
+ kv_store,
409
419
logger,
410
- latest_fee_rate_cache_update_timestamp ,
420
+ node_metrics ,
411
421
..
412
422
} => {
413
423
let now = Instant :: now ( ) ;
@@ -479,7 +489,15 @@ impl ChainSource {
479
489
) ;
480
490
let unix_time_secs_opt =
481
491
SystemTime :: now ( ) . duration_since ( UNIX_EPOCH ) . ok ( ) . map ( |d| d. as_secs ( ) ) ;
482
- * latest_fee_rate_cache_update_timestamp. write ( ) . unwrap ( ) = unix_time_secs_opt;
492
+ {
493
+ let mut locked_node_metrics = node_metrics. write ( ) . unwrap ( ) ;
494
+ locked_node_metrics. latest_fee_rate_cache_update_timestamp = unix_time_secs_opt;
495
+ write_node_metrics (
496
+ & * locked_node_metrics,
497
+ Arc :: clone ( & kv_store) ,
498
+ Arc :: clone ( & logger) ,
499
+ ) ?;
500
+ }
483
501
484
502
Ok ( ( ) )
485
503
} ,
@@ -580,16 +598,19 @@ impl Filter for ChainSource {
580
598
581
599
fn periodically_archive_fully_resolved_monitors (
582
600
channel_manager : Arc < ChannelManager > , chain_monitor : Arc < ChainMonitor > ,
583
- latest_channel_monitor_archival_height : Arc < RwLock < Option < u32 > > > ,
584
- ) {
585
- let mut latest_archival_height_lock = latest_channel_monitor_archival_height . write ( ) . unwrap ( ) ;
601
+ kv_store : Arc < DynStore > , logger : Arc < FilesystemLogger > , node_metrics : Arc < RwLock < NodeMetrics > > ,
602
+ ) -> Result < ( ) , Error > {
603
+ let mut locked_node_metrics = node_metrics . write ( ) . unwrap ( ) ;
586
604
let cur_height = channel_manager. current_best_block ( ) . height ;
587
- let should_archive = latest_archival_height_lock
605
+ let should_archive = locked_node_metrics
606
+ . latest_channel_monitor_archival_height
588
607
. as_ref ( )
589
608
. map_or ( true , |h| cur_height >= h + RESOLVED_CHANNEL_MONITOR_ARCHIVAL_INTERVAL ) ;
590
609
591
610
if should_archive {
592
611
chain_monitor. archive_fully_resolved_channel_monitors ( ) ;
593
- * latest_archival_height_lock = Some ( cur_height) ;
612
+ locked_node_metrics. latest_channel_monitor_archival_height = Some ( cur_height) ;
613
+ write_node_metrics ( & * locked_node_metrics, kv_store, logger) ?;
594
614
}
615
+ Ok ( ( ) )
595
616
}
0 commit comments