@@ -126,8 +126,9 @@ pub trait Persist<ChannelSigner: EcdsaChannelSigner> {
126
126
///
127
127
/// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
128
128
/// [`Writeable::write`]: crate::util::ser::Writeable::write
129
- #[ rustfmt:: skip]
130
- fn persist_new_channel ( & self , monitor_name : MonitorName , monitor : & ChannelMonitor < ChannelSigner > ) -> ChannelMonitorUpdateStatus ;
129
+ fn persist_new_channel (
130
+ & self , monitor_name : MonitorName , monitor : & ChannelMonitor < ChannelSigner > ,
131
+ ) -> ChannelMonitorUpdateStatus ;
131
132
132
133
/// Update one channel's data. The provided [`ChannelMonitor`] has already applied the given
133
134
/// update.
@@ -166,8 +167,10 @@ pub trait Persist<ChannelSigner: EcdsaChannelSigner> {
166
167
/// [`ChannelMonitorUpdateStatus`] for requirements when returning errors.
167
168
///
168
169
/// [`Writeable::write`]: crate::util::ser::Writeable::write
169
- #[ rustfmt:: skip]
170
- fn update_persisted_channel ( & self , monitor_name : MonitorName , monitor_update : Option < & ChannelMonitorUpdate > , monitor : & ChannelMonitor < ChannelSigner > ) -> ChannelMonitorUpdateStatus ;
170
+ fn update_persisted_channel (
171
+ & self , monitor_name : MonitorName , monitor_update : Option < & ChannelMonitorUpdate > ,
172
+ monitor : & ChannelMonitor < ChannelSigner > ,
173
+ ) -> ChannelMonitorUpdateStatus ;
171
174
/// Prevents the channel monitor from being loaded on startup.
172
175
///
173
176
/// Archiving the data in a backup location (rather than deleting it fully) is useful for
@@ -350,19 +353,26 @@ where
350
353
}
351
354
}
352
355
353
- #[ rustfmt:: skip]
354
356
fn update_monitor_with_chain_data < FN > (
355
- & self , header : & Header , best_height : Option < u32 > , txdata : & TransactionData , process : FN , channel_id : & ChannelId ,
356
- monitor_state : & MonitorHolder < ChannelSigner > , channel_count : usize ,
357
- ) -> Result < ( ) , ( ) > where FN : Fn ( & ChannelMonitor < ChannelSigner > , & TransactionData ) -> Vec < TransactionOutputs > {
357
+ & self , header : & Header , best_height : Option < u32 > , txdata : & TransactionData , process : FN ,
358
+ channel_id : & ChannelId , monitor_state : & MonitorHolder < ChannelSigner > , channel_count : usize ,
359
+ ) -> Result < ( ) , ( ) >
360
+ where
361
+ FN : Fn ( & ChannelMonitor < ChannelSigner > , & TransactionData ) -> Vec < TransactionOutputs > ,
362
+ {
358
363
let monitor = & monitor_state. monitor ;
359
364
let logger = WithChannelMonitor :: from ( & self . logger , & monitor, None ) ;
360
365
361
366
let mut txn_outputs = process ( monitor, txdata) ;
362
367
363
368
let get_partition_key = |channel_id : & ChannelId | {
364
369
let channel_id_bytes = channel_id. 0 ;
365
- let channel_id_u32 = u32:: from_be_bytes ( [ channel_id_bytes[ 0 ] , channel_id_bytes[ 1 ] , channel_id_bytes[ 2 ] , channel_id_bytes[ 3 ] ] ) ;
370
+ let channel_id_u32 = u32:: from_be_bytes ( [
371
+ channel_id_bytes[ 0 ] ,
372
+ channel_id_bytes[ 1 ] ,
373
+ channel_id_bytes[ 2 ] ,
374
+ channel_id_bytes[ 3 ] ,
375
+ ] ) ;
366
376
channel_id_u32. wrapping_add ( best_height. unwrap_or_default ( ) )
367
377
} ;
368
378
@@ -374,23 +384,33 @@ where
374
384
375
385
let has_pending_claims = monitor_state. monitor . has_pending_claims ( ) ;
376
386
if has_pending_claims || get_partition_key ( channel_id) % partition_factor == 0 {
377
- log_trace ! ( logger, "Syncing Channel Monitor for channel {}" , log_funding_info!( monitor) ) ;
387
+ log_trace ! (
388
+ logger,
389
+ "Syncing Channel Monitor for channel {}" ,
390
+ log_funding_info!( monitor)
391
+ ) ;
378
392
// Even though we don't track monitor updates from chain-sync as pending, we still want
379
393
// updates per-channel to be well-ordered so that users don't see a
380
394
// `ChannelMonitorUpdate` after a channel persist for a channel with the same
381
395
// `latest_update_id`.
382
396
let _pending_monitor_updates = monitor_state. pending_monitor_updates . lock ( ) . unwrap ( ) ;
383
- match self . persister . update_persisted_channel ( monitor. persistence_key ( ) , None , monitor) {
384
- ChannelMonitorUpdateStatus :: Completed =>
385
- log_trace ! ( logger, "Finished syncing Channel Monitor for channel {} for block-data" ,
386
- log_funding_info!( monitor)
387
- ) ,
397
+ match self . persister . update_persisted_channel ( monitor. persistence_key ( ) , None , monitor)
398
+ {
399
+ ChannelMonitorUpdateStatus :: Completed => log_trace ! (
400
+ logger,
401
+ "Finished syncing Channel Monitor for channel {} for block-data" ,
402
+ log_funding_info!( monitor)
403
+ ) ,
388
404
ChannelMonitorUpdateStatus :: InProgress => {
389
- log_trace ! ( logger, "Channel Monitor sync for channel {} in progress." , log_funding_info!( monitor) ) ;
390
- }
405
+ log_trace ! (
406
+ logger,
407
+ "Channel Monitor sync for channel {} in progress." ,
408
+ log_funding_info!( monitor)
409
+ ) ;
410
+ } ,
391
411
ChannelMonitorUpdateStatus :: UnrecoverableError => {
392
412
return Err ( ( ) ) ;
393
- }
413
+ } ,
394
414
}
395
415
}
396
416
@@ -406,7 +426,11 @@ where
406
426
outpoint : OutPoint { txid, index : idx as u16 } ,
407
427
script_pubkey : output. script_pubkey ,
408
428
} ;
409
- log_trace ! ( logger, "Adding monitoring for spends of outpoint {} to the filter" , output. outpoint) ;
429
+ log_trace ! (
430
+ logger,
431
+ "Adding monitoring for spends of outpoint {} to the filter" ,
432
+ output. outpoint
433
+ ) ;
410
434
chain_source. register_output ( output) ;
411
435
}
412
436
}
@@ -433,8 +457,10 @@ where
433
457
/// [`NodeSigner`]: crate::sign::NodeSigner
434
458
/// [`NodeSigner::get_peer_storage_key`]: crate::sign::NodeSigner::get_peer_storage_key
435
459
/// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
436
- #[ rustfmt:: skip]
437
- pub fn new ( chain_source : Option < C > , broadcaster : T , logger : L , feeest : F , persister : P , entropy_source : ES , our_peerstorage_encryption_key : PeerStorageKey ) -> Self {
460
+ pub fn new (
461
+ chain_source : Option < C > , broadcaster : T , logger : L , feeest : F , persister : P ,
462
+ entropy_source : ES , our_peerstorage_encryption_key : PeerStorageKey ,
463
+ ) -> Self {
438
464
Self {
439
465
monitors : RwLock :: new ( new_hash_map ( ) ) ,
440
466
chain_source,
@@ -447,7 +473,7 @@ where
447
473
highest_chain_height : AtomicUsize :: new ( 0 ) ,
448
474
event_notifier : Notifier :: new ( ) ,
449
475
pending_send_only_events : Mutex :: new ( Vec :: new ( ) ) ,
450
- our_peerstorage_encryption_key
476
+ our_peerstorage_encryption_key,
451
477
}
452
478
}
453
479
@@ -547,37 +573,50 @@ where
547
573
///
548
574
/// Returns an [`APIError::APIMisuseError`] if `funding_txo` does not match any currently
549
575
/// registered [`ChannelMonitor`]s.
550
- #[ rustfmt:: skip]
551
- pub fn channel_monitor_updated ( & self , channel_id : ChannelId , completed_update_id : u64 ) -> Result < ( ) , APIError > {
576
+ pub fn channel_monitor_updated (
577
+ & self , channel_id : ChannelId , completed_update_id : u64 ,
578
+ ) -> Result < ( ) , APIError > {
552
579
let monitors = self . monitors . read ( ) . unwrap ( ) ;
553
- let monitor_data = if let Some ( mon) = monitors. get ( & channel_id) { mon } else {
554
- return Err ( APIError :: APIMisuseError { err : format ! ( "No ChannelMonitor matching channel ID {} found" , channel_id) } ) ;
580
+ let monitor_data = if let Some ( mon) = monitors. get ( & channel_id) {
581
+ mon
582
+ } else {
583
+ return Err ( APIError :: APIMisuseError {
584
+ err : format ! ( "No ChannelMonitor matching channel ID {} found" , channel_id) ,
585
+ } ) ;
555
586
} ;
556
587
let mut pending_monitor_updates = monitor_data. pending_monitor_updates . lock ( ) . unwrap ( ) ;
557
588
pending_monitor_updates. retain ( |update_id| * update_id != completed_update_id) ;
558
589
559
590
// Note that we only check for pending non-chainsync monitor updates and we don't track monitor
560
591
// updates resulting from chainsync in `pending_monitor_updates`.
561
592
let monitor_is_pending_updates = monitor_data. has_pending_updates ( & pending_monitor_updates) ;
562
- log_debug ! ( self . logger, "Completed off-chain monitor update {} for channel with channel ID {}, {}" ,
593
+ log_debug ! (
594
+ self . logger,
595
+ "Completed off-chain monitor update {} for channel with channel ID {}, {}" ,
563
596
completed_update_id,
564
597
channel_id,
565
598
if monitor_is_pending_updates {
566
599
"still have pending off-chain updates"
567
600
} else {
568
601
"all off-chain updates complete, returning a MonitorEvent"
569
- } ) ;
602
+ }
603
+ ) ;
570
604
if monitor_is_pending_updates {
571
605
// If there are still monitor updates pending, we cannot yet construct a
572
606
// Completed event.
573
607
return Ok ( ( ) ) ;
574
608
}
575
609
let funding_txo = monitor_data. monitor . get_funding_txo ( ) ;
576
- self . pending_monitor_events . lock ( ) . unwrap ( ) . push ( ( funding_txo , channel_id , vec ! [ MonitorEvent :: Completed {
610
+ self . pending_monitor_events . lock ( ) . unwrap ( ) . push ( (
577
611
funding_txo,
578
612
channel_id,
579
- monitor_update_id: monitor_data. monitor. get_latest_update_id( ) ,
580
- } ] , monitor_data. monitor . get_counterparty_node_id ( ) ) ) ;
613
+ vec ! [ MonitorEvent :: Completed {
614
+ funding_txo,
615
+ channel_id,
616
+ monitor_update_id: monitor_data. monitor. get_latest_update_id( ) ,
617
+ } ] ,
618
+ monitor_data. monitor . get_counterparty_node_id ( ) ,
619
+ ) ) ;
581
620
582
621
self . event_notifier . notify ( ) ;
583
622
Ok ( ( ) )
@@ -693,30 +732,37 @@ where
693
732
///
694
733
/// Depending on the implementation of [`Persist::archive_persisted_channel`] the monitor
695
734
/// data could be moved to an archive location or removed entirely.
696
- #[ rustfmt:: skip]
697
735
pub fn archive_fully_resolved_channel_monitors ( & self ) {
698
736
let mut have_monitors_to_prune = false ;
699
737
for monitor_holder in self . monitors . read ( ) . unwrap ( ) . values ( ) {
700
738
let logger = WithChannelMonitor :: from ( & self . logger , & monitor_holder. monitor , None ) ;
701
- let ( is_fully_resolved, needs_persistence) = monitor_holder. monitor . check_and_update_full_resolution_status ( & logger) ;
739
+ let ( is_fully_resolved, needs_persistence) =
740
+ monitor_holder. monitor . check_and_update_full_resolution_status ( & logger) ;
702
741
if is_fully_resolved {
703
742
have_monitors_to_prune = true ;
704
743
}
705
744
if needs_persistence {
706
- self . persister . update_persisted_channel ( monitor_holder. monitor . persistence_key ( ) , None , & monitor_holder. monitor ) ;
745
+ self . persister . update_persisted_channel (
746
+ monitor_holder. monitor . persistence_key ( ) ,
747
+ None ,
748
+ & monitor_holder. monitor ,
749
+ ) ;
707
750
}
708
751
}
709
752
if have_monitors_to_prune {
710
753
let mut monitors = self . monitors . write ( ) . unwrap ( ) ;
711
754
monitors. retain ( |channel_id, monitor_holder| {
712
755
let logger = WithChannelMonitor :: from ( & self . logger , & monitor_holder. monitor , None ) ;
713
- let ( is_fully_resolved, _) = monitor_holder. monitor . check_and_update_full_resolution_status ( & logger) ;
756
+ let ( is_fully_resolved, _) =
757
+ monitor_holder. monitor . check_and_update_full_resolution_status ( & logger) ;
714
758
if is_fully_resolved {
715
- log_info ! ( logger,
759
+ log_info ! (
760
+ logger,
716
761
"Archiving fully resolved ChannelMonitor for channel ID {}" ,
717
762
channel_id
718
763
) ;
719
- self . persister . archive_persisted_channel ( monitor_holder. monitor . persistence_key ( ) ) ;
764
+ self . persister
765
+ . archive_persisted_channel ( monitor_holder. monitor . persistence_key ( ) ) ;
720
766
false
721
767
} else {
722
768
true
@@ -786,8 +832,11 @@ where
786
832
InitFeatures :: empty ( )
787
833
}
788
834
789
- #[ rustfmt:: skip]
790
- fn peer_connected ( & self , _their_node_id : PublicKey , _msg : & Init , _inbound : bool ) -> Result < ( ) , ( ) > { Ok ( ( ) ) }
835
+ fn peer_connected (
836
+ & self , _their_node_id : PublicKey , _msg : & Init , _inbound : bool ,
837
+ ) -> Result < ( ) , ( ) > {
838
+ Ok ( ( ) )
839
+ }
791
840
}
792
841
793
842
impl <
@@ -923,8 +972,9 @@ where
923
972
P :: Target : Persist < ChannelSigner > ,
924
973
ES :: Target : EntropySource ,
925
974
{
926
- #[ rustfmt:: skip]
927
- fn watch_channel ( & self , channel_id : ChannelId , monitor : ChannelMonitor < ChannelSigner > ) -> Result < ChannelMonitorUpdateStatus , ( ) > {
975
+ fn watch_channel (
976
+ & self , channel_id : ChannelId , monitor : ChannelMonitor < ChannelSigner > ,
977
+ ) -> Result < ChannelMonitorUpdateStatus , ( ) > {
928
978
let logger = WithChannelMonitor :: from ( & self . logger , & monitor, None ) ;
929
979
let mut monitors = self . monitors . write ( ) . unwrap ( ) ;
930
980
let entry = match monitors. entry ( channel_id) {
@@ -940,11 +990,19 @@ where
940
990
let persist_res = self . persister . persist_new_channel ( monitor. persistence_key ( ) , & monitor) ;
941
991
match persist_res {
942
992
ChannelMonitorUpdateStatus :: InProgress => {
943
- log_info ! ( logger, "Persistence of new ChannelMonitor for channel {} in progress" , log_funding_info!( monitor) ) ;
993
+ log_info ! (
994
+ logger,
995
+ "Persistence of new ChannelMonitor for channel {} in progress" ,
996
+ log_funding_info!( monitor)
997
+ ) ;
944
998
pending_monitor_updates. push ( update_id) ;
945
999
} ,
946
1000
ChannelMonitorUpdateStatus :: Completed => {
947
- log_info ! ( logger, "Persistence of new ChannelMonitor for channel {} completed" , log_funding_info!( monitor) ) ;
1001
+ log_info ! (
1002
+ logger,
1003
+ "Persistence of new ChannelMonitor for channel {} completed" ,
1004
+ log_funding_info!( monitor)
1005
+ ) ;
948
1006
} ,
949
1007
ChannelMonitorUpdateStatus :: UnrecoverableError => {
950
1008
let err_str = "ChannelMonitor[Update] persistence failed unrecoverably. This indicates we cannot continue normal operation and must shut down." ;
@@ -953,7 +1011,7 @@ where
953
1011
} ,
954
1012
}
955
1013
if let Some ( ref chain_source) = self . chain_source {
956
- monitor. load_outputs_to_watch ( chain_source , & self . logger ) ;
1014
+ monitor. load_outputs_to_watch ( chain_source, & self . logger ) ;
957
1015
}
958
1016
entry. insert ( MonitorHolder {
959
1017
monitor,
@@ -962,8 +1020,9 @@ where
962
1020
Ok ( persist_res)
963
1021
}
964
1022
965
- #[ rustfmt:: skip]
966
- fn update_channel ( & self , channel_id : ChannelId , update : & ChannelMonitorUpdate ) -> ChannelMonitorUpdateStatus {
1023
+ fn update_channel (
1024
+ & self , channel_id : ChannelId , update : & ChannelMonitorUpdate ,
1025
+ ) -> ChannelMonitorUpdateStatus {
967
1026
// `ChannelMonitorUpdate`'s `channel_id` is `None` prior to 0.0.121 and all channels in those
968
1027
// versions are V1-established. For 0.0.121+ the `channel_id` fields is always `Some`.
969
1028
debug_assert_eq ! ( update. channel_id. unwrap( ) , channel_id) ;
@@ -985,13 +1044,24 @@ where
985
1044
Some ( monitor_state) => {
986
1045
let monitor = & monitor_state. monitor ;
987
1046
let logger = WithChannelMonitor :: from ( & self . logger , & monitor, None ) ;
988
- log_trace ! ( logger, "Updating ChannelMonitor to id {} for channel {}" , update. update_id, log_funding_info!( monitor) ) ;
1047
+ log_trace ! (
1048
+ logger,
1049
+ "Updating ChannelMonitor to id {} for channel {}" ,
1050
+ update. update_id,
1051
+ log_funding_info!( monitor)
1052
+ ) ;
989
1053
990
1054
// We hold a `pending_monitor_updates` lock through `update_monitor` to ensure we
991
1055
// have well-ordered updates from the users' point of view. See the
992
1056
// `pending_monitor_updates` docs for more.
993
- let mut pending_monitor_updates = monitor_state. pending_monitor_updates . lock ( ) . unwrap ( ) ;
994
- let update_res = monitor. update_monitor ( update, & self . broadcaster , & self . fee_estimator , & self . logger ) ;
1057
+ let mut pending_monitor_updates =
1058
+ monitor_state. pending_monitor_updates . lock ( ) . unwrap ( ) ;
1059
+ let update_res = monitor. update_monitor (
1060
+ update,
1061
+ & self . broadcaster ,
1062
+ & self . fee_estimator ,
1063
+ & self . logger ,
1064
+ ) ;
995
1065
996
1066
let update_id = update. update_id ;
997
1067
let persist_res = if update_res. is_err ( ) {
@@ -1001,9 +1071,17 @@ where
1001
1071
// while reading `channel_monitor` with updates from storage. Instead, we should persist
1002
1072
// the entire `channel_monitor` here.
1003
1073
log_warn ! ( logger, "Failed to update ChannelMonitor for channel {}. Going ahead and persisting the entire ChannelMonitor" , log_funding_info!( monitor) ) ;
1004
- self . persister . update_persisted_channel ( monitor. persistence_key ( ) , None , monitor)
1074
+ self . persister . update_persisted_channel (
1075
+ monitor. persistence_key ( ) ,
1076
+ None ,
1077
+ monitor,
1078
+ )
1005
1079
} else {
1006
- self . persister . update_persisted_channel ( monitor. persistence_key ( ) , Some ( update) , monitor)
1080
+ self . persister . update_persisted_channel (
1081
+ monitor. persistence_key ( ) ,
1082
+ Some ( update) ,
1083
+ monitor,
1084
+ )
1007
1085
} ;
1008
1086
match persist_res {
1009
1087
ChannelMonitorUpdateStatus :: InProgress => {
@@ -1015,7 +1093,8 @@ where
1015
1093
) ;
1016
1094
} ,
1017
1095
ChannelMonitorUpdateStatus :: Completed => {
1018
- log_debug ! ( logger,
1096
+ log_debug ! (
1097
+ logger,
1019
1098
"Persistence of ChannelMonitorUpdate id {:?} for channel {} completed" ,
1020
1099
update_id,
1021
1100
log_funding_info!( monitor)
@@ -1037,7 +1116,7 @@ where
1037
1116
} else {
1038
1117
persist_res
1039
1118
}
1040
- }
1119
+ } ,
1041
1120
}
1042
1121
}
1043
1122
@@ -1087,14 +1166,16 @@ where
1087
1166
///
1088
1167
/// [`SpendableOutputs`]: events::Event::SpendableOutputs
1089
1168
/// [`BumpTransaction`]: events::Event::BumpTransaction
1090
- #[ rustfmt:: skip]
1091
- fn process_pending_events < H : Deref > ( & self , handler : H ) where H :: Target : EventHandler {
1169
+ fn process_pending_events < H : Deref > ( & self , handler : H )
1170
+ where
1171
+ H :: Target : EventHandler ,
1172
+ {
1092
1173
for monitor_state in self . monitors . read ( ) . unwrap ( ) . values ( ) {
1093
1174
match monitor_state. monitor . process_pending_events ( & handler, & self . logger ) {
1094
1175
Ok ( ( ) ) => { } ,
1095
- Err ( ReplayEvent ( ) ) => {
1176
+ Err ( ReplayEvent ( ) ) => {
1096
1177
self . event_notifier . notify ( ) ;
1097
- }
1178
+ } ,
1098
1179
}
1099
1180
}
1100
1181
}
0 commit comments