@@ -530,6 +530,13 @@ enum BackgroundEvent {
530
530
funding_txo : OutPoint ,
531
531
update : ChannelMonitorUpdate
532
532
} ,
533
+ /// Some [`ChannelMonitorUpdate`] (s) completed before we were serialized but we still have
534
+ /// them marked pending, thus we need to run any [`MonitorUpdateCompletionAction`] (s) pending
535
+ /// on a channel.
536
+ MonitorUpdatesComplete {
537
+ counterparty_node_id : PublicKey ,
538
+ channel_id : [ u8 ; 32 ] ,
539
+ } ,
533
540
}
534
541
535
542
#[ derive( Debug ) ]
@@ -4194,6 +4201,22 @@ where
4194
4201
}
4195
4202
let _ = handle_error ! ( self , res, counterparty_node_id) ;
4196
4203
} ,
4204
+ BackgroundEvent :: MonitorUpdatesComplete { counterparty_node_id, channel_id } => {
4205
+ let per_peer_state = self . per_peer_state . read ( ) . unwrap ( ) ;
4206
+ if let Some ( peer_state_mutex) = per_peer_state. get ( & counterparty_node_id) {
4207
+ let mut peer_state_lock = peer_state_mutex. lock ( ) . unwrap ( ) ;
4208
+ let peer_state = & mut * peer_state_lock;
4209
+ if let Some ( chan) = peer_state. channel_by_id . get_mut ( & channel_id) {
4210
+ handle_monitor_update_completion ! ( self , peer_state_lock, peer_state, per_peer_state, chan) ;
4211
+ } else {
4212
+ let update_actions = peer_state. monitor_update_blocked_actions
4213
+ . remove ( & channel_id) . unwrap_or ( Vec :: new ( ) ) ;
4214
+ mem:: drop ( peer_state_lock) ;
4215
+ mem:: drop ( per_peer_state) ;
4216
+ self . handle_monitor_update_completion_actions ( update_actions) ;
4217
+ }
4218
+ }
4219
+ } ,
4197
4220
}
4198
4221
}
4199
4222
NotifyOption :: DoPersist
@@ -5004,24 +5027,29 @@ where
5004
5027
if peer_state_mutex_opt. is_none ( ) { return }
5005
5028
peer_state_lock = peer_state_mutex_opt. unwrap ( ) . lock ( ) . unwrap ( ) ;
5006
5029
let peer_state = & mut * peer_state_lock;
5007
- let mut channel = {
5008
- match peer_state. channel_by_id . entry ( funding_txo. to_channel_id ( ) ) {
5009
- hash_map:: Entry :: Occupied ( chan) => chan,
5010
- hash_map:: Entry :: Vacant ( _) => return ,
5011
- }
5012
- } ;
5030
+ let channel =
5031
+ if let Some ( chan) = peer_state. channel_by_id . get_mut ( & funding_txo. to_channel_id ( ) ) {
5032
+ chan
5033
+ } else {
5034
+ let update_actions = peer_state. monitor_update_blocked_actions
5035
+ . remove ( & funding_txo. to_channel_id ( ) ) . unwrap_or ( Vec :: new ( ) ) ;
5036
+ mem:: drop ( peer_state_lock) ;
5037
+ mem:: drop ( per_peer_state) ;
5038
+ self . handle_monitor_update_completion_actions ( update_actions) ;
5039
+ return ;
5040
+ } ;
5013
5041
let remaining_in_flight =
5014
5042
if let Some ( pending) = peer_state. in_flight_monitor_updates . get_mut ( funding_txo) {
5015
5043
pending. retain ( |upd| upd. update_id > highest_applied_update_id) ;
5016
5044
pending. len ( )
5017
5045
} else { 0 } ;
5018
5046
log_trace ! ( self . logger, "ChannelMonitor updated to {}. Current highest is {}. {} pending in-flight updates." ,
5019
- highest_applied_update_id, channel. get ( ) . context. get_latest_monitor_update_id( ) ,
5047
+ highest_applied_update_id, channel. context. get_latest_monitor_update_id( ) ,
5020
5048
remaining_in_flight) ;
5021
- if !channel. get ( ) . is_awaiting_monitor_update ( ) || channel. get ( ) . context . get_latest_monitor_update_id ( ) != highest_applied_update_id {
5049
+ if !channel. is_awaiting_monitor_update ( ) || channel. context . get_latest_monitor_update_id ( ) != highest_applied_update_id {
5022
5050
return ;
5023
5051
}
5024
- handle_monitor_update_completion ! ( self , peer_state_lock, peer_state, per_peer_state, channel. get_mut ( ) ) ;
5052
+ handle_monitor_update_completion ! ( self , peer_state_lock, peer_state, per_peer_state, channel) ;
5025
5053
}
5026
5054
5027
5055
/// Accepts a request to open a channel after a [`Event::OpenChannelRequest`].
@@ -8521,6 +8549,16 @@ where
8521
8549
update: update. clone( ) ,
8522
8550
} ) ;
8523
8551
}
8552
+ if $chan_in_flight_upds. is_empty( ) {
8553
+ // We had some updates to apply, but it turns out they had completed before we
8554
+ // were serialized, we just weren't notified of that. Thus, we may have to run
8555
+ // the completion actions for any monitor updates, but otherwise are done.
8556
+ pending_background_events. push(
8557
+ BackgroundEvent :: MonitorUpdatesComplete {
8558
+ counterparty_node_id: $counterparty_node_id,
8559
+ channel_id: $funding_txo. to_channel_id( ) ,
8560
+ } ) ;
8561
+ }
8524
8562
if $peer_state. in_flight_monitor_updates. insert( $funding_txo, $chan_in_flight_upds) . is_some( ) {
8525
8563
log_error!( args. logger, "Duplicate in-flight monitor update set for the same channel!" ) ;
8526
8564
return Err ( DecodeError :: InvalidValue ) ;
@@ -8913,6 +8951,12 @@ where
8913
8951
blocked_peer_state. lock ( ) . unwrap ( ) . actions_blocking_raa_monitor_updates
8914
8952
. entry ( blocked_channel_outpoint. to_channel_id ( ) )
8915
8953
. or_insert_with ( Vec :: new) . push ( blocking_action. clone ( ) ) ;
8954
+ } else {
8955
+ // If the channel we were blocking has closed, we don't need to
8956
+ // worry about it - the blocked monitor update should never have
8957
+ // been released from the `Channel` object so it can't have
8958
+ // completed, and if the channel closed there's no reason to bother
8959
+ // anymore.
8916
8960
}
8917
8961
}
8918
8962
}
0 commit comments