Skip to content

Commit b1395b3

Browse files
committed
Handle EventCompletionActions after events complete
This adds handling of the new `EventCompletionAction`s after `Event`s are handled, letting `ChannelMonitorUpdate`s which were blocked fly after a relevant `Event`.
1 parent 32d3dbe commit b1395b3

File tree

1 file changed

+72
-1
lines changed

1 file changed

+72
-1
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1744,9 +1744,14 @@ macro_rules! process_events_body {
17441744
result = NotifyOption::DoPersist;
17451745
}
17461746

1747-
for (event, _action) in pending_events {
1747+
let mut post_event_actions = Vec::new();
1748+
1749+
for (event, action_opt) in pending_events {
17481750
$event_to_handle = event;
17491751
$handle_event;
1752+
if let Some(action) = action_opt {
1753+
post_event_actions.push(action);
1754+
}
17501755
}
17511756

17521757
{
@@ -1756,6 +1761,12 @@ macro_rules! process_events_body {
17561761
$self.pending_events_processor.store(false, Ordering::Release);
17571762
}
17581763

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+
17591770
if result == NotifyOption::DoPersist {
17601771
$self.persistence_notifier.notify();
17611772
}
@@ -5926,6 +5937,66 @@ where
59265937
self.pending_outbound_payments.clear_pending_payments()
59275938
}
59285939

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+
59296000
/// Processes any events asynchronously in the order they were generated since the last call
59306001
/// using the given event handler.
59316002
///

0 commit comments

Comments
 (0)