@@ -1744,9 +1744,14 @@ macro_rules! process_events_body {
1744
1744
result = NotifyOption :: DoPersist ;
1745
1745
}
1746
1746
1747
- for ( event, _action) in pending_events {
1747
+ let mut post_event_actions = Vec :: new( ) ;
1748
+
1749
+ for ( event, action_opt) in pending_events {
1748
1750
$event_to_handle = event;
1749
1751
$handle_event;
1752
+ if let Some ( action) = action_opt {
1753
+ post_event_actions. push( action) ;
1754
+ }
1750
1755
}
1751
1756
1752
1757
{
@@ -1756,6 +1761,12 @@ macro_rules! process_events_body {
1756
1761
$self. pending_events_processor. store( false , Ordering :: Release ) ;
1757
1762
}
1758
1763
1764
+ if !post_event_actions. is_empty( ) {
1765
+ $self. handle_post_event_actions( post_event_actions) ;
1766
+ // If we had some actions, go around again as we may have more events now
1767
+ processed_all_events = false ;
1768
+ }
1769
+
1759
1770
if result == NotifyOption :: DoPersist {
1760
1771
$self. persistence_notifier. notify( ) ;
1761
1772
}
@@ -5926,6 +5937,66 @@ where
5926
5937
self . pending_outbound_payments . clear_pending_payments ( )
5927
5938
}
5928
5939
5940
+ fn handle_monitor_update_release ( & self , counterparty_node_id : PublicKey , channel_funding_outpoint : OutPoint ) {
5941
+ loop {
5942
+ let per_peer_state = self . per_peer_state . read ( ) . unwrap ( ) ;
5943
+ if let Some ( peer_state_mtx) = per_peer_state. get ( & counterparty_node_id) {
5944
+ let mut peer_state_lck = peer_state_mtx. lock ( ) . unwrap ( ) ;
5945
+ let peer_state = & mut * peer_state_lck;
5946
+ if self . pending_events . lock ( ) . unwrap ( ) . iter ( )
5947
+ . any ( |( _ev, action_opt) | action_opt == & Some ( EventCompletionAction :: ReleaseRAAChannelMonitorUpdate {
5948
+ channel_funding_outpoint, counterparty_node_id
5949
+ } ) )
5950
+ {
5951
+ // Check that, while holding the peer lock, we don't have another event
5952
+ // blocking any monitor updates for this channel. If we do, let those
5953
+ // events be the ones that ultimately release the monitor update(s).
5954
+ log_trace ! ( self . logger, "Delaying monitor unlock for channel {} as another event is pending" ,
5955
+ log_bytes!( & channel_funding_outpoint. to_channel_id( ) [ ..] ) ) ;
5956
+ return ;
5957
+ }
5958
+ if let hash_map:: Entry :: Occupied ( mut chan) = peer_state. channel_by_id . entry ( channel_funding_outpoint. to_channel_id ( ) ) {
5959
+ debug_assert_eq ! ( chan. get( ) . get_funding_txo( ) . unwrap( ) , channel_funding_outpoint) ;
5960
+ if let Some ( ( monitor_update, further_update_exists) ) = chan. get_mut ( ) . unblock_next_blocked_monitor_update ( ) {
5961
+ log_debug ! ( self . logger, "Unlocking monitor updating for channel {} and updating monitor" ,
5962
+ log_bytes!( & channel_funding_outpoint. to_channel_id( ) [ ..] ) ) ;
5963
+ let update_res = self . chain_monitor . update_channel ( channel_funding_outpoint, monitor_update) ;
5964
+ let update_id = monitor_update. update_id ;
5965
+ let _ = handle_error ! ( self ,
5966
+ handle_new_monitor_update!( self , update_res, update_id,
5967
+ peer_state_lck, peer_state, per_peer_state, chan) ,
5968
+ counterparty_node_id) ;
5969
+ if further_update_exists {
5970
+ // If there are more `ChannelMonitorUpdate`s to process, restart at the
5971
+ // top of the loop.
5972
+ continue ;
5973
+ }
5974
+ } else {
5975
+ log_trace ! ( self . logger, "Unlocked monitor updating for channel {} without monitors to update" ,
5976
+ log_bytes!( & channel_funding_outpoint. to_channel_id( ) [ ..] ) ) ;
5977
+ }
5978
+ }
5979
+ } else {
5980
+ log_debug ! ( self . logger,
5981
+ "Got a release post-RAA monitor update for peer {} but the channel is gone" ,
5982
+ log_pubkey!( counterparty_node_id) ) ;
5983
+ }
5984
+ break ;
5985
+ }
5986
+ }
5987
+
5988
+ fn handle_post_event_actions ( & self , actions : Vec < EventCompletionAction > ) {
5989
+ for action in actions {
5990
+ match action {
5991
+ EventCompletionAction :: ReleaseRAAChannelMonitorUpdate {
5992
+ channel_funding_outpoint, counterparty_node_id
5993
+ } => {
5994
+ self . handle_monitor_update_release ( counterparty_node_id, channel_funding_outpoint) ;
5995
+ }
5996
+ }
5997
+ }
5998
+ }
5999
+
5929
6000
/// Processes any events asynchronously in the order they were generated since the last call
5930
6001
/// using the given event handler.
5931
6002
///
0 commit comments