Skip to content

Commit 756b5aa

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 9ede794 commit 756b5aa

File tree

1 file changed

+78
-1
lines changed

1 file changed

+78
-1
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 78 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,72 @@ 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+
let mut errors = Vec::new();
5942+
loop {
5943+
let per_peer_state = self.per_peer_state.read().unwrap();
5944+
if let Some(peer_state_mtx) = per_peer_state.get(&counterparty_node_id) {
5945+
let mut peer_state_lck = peer_state_mtx.lock().unwrap();
5946+
let peer_state = &mut *peer_state_lck;
5947+
if self.pending_events.lock().unwrap().iter()
5948+
.any(|(_ev, action_opt)| action_opt == &Some(EventCompletionAction::ReleaseRAAChannelMonitorUpdate {
5949+
channel_funding_outpoint, counterparty_node_id
5950+
}))
5951+
{
5952+
// Check that, while holding the peer lock, we don't have another event
5953+
// blocking any monitor updates for this channel. If we do, let those
5954+
// events be the ones that ultimately release the monitor update(s).
5955+
log_trace!(self.logger, "Delaying monitor unlock for channel {} as another event is pending",
5956+
log_bytes!(&channel_funding_outpoint.to_channel_id()[..]));
5957+
break;
5958+
}
5959+
if let hash_map::Entry::Occupied(mut chan) = peer_state.channel_by_id.entry(channel_funding_outpoint.to_channel_id()) {
5960+
debug_assert_eq!(chan.get().get_funding_txo().unwrap(), channel_funding_outpoint);
5961+
if let Some((monitor_update, further_update_exists)) = chan.get_mut().unblock_next_blocked_monitor_update() {
5962+
log_debug!(self.logger, "Unlocking monitor updating for channel {} and updating monitor",
5963+
log_bytes!(&channel_funding_outpoint.to_channel_id()[..]));
5964+
let update_res = self.chain_monitor.update_channel(channel_funding_outpoint, monitor_update);
5965+
let update_id = monitor_update.update_id;
5966+
if let Err(e) = handle_new_monitor_update!(self, update_res, update_id,
5967+
peer_state_lck, peer_state, per_peer_state, chan)
5968+
{
5969+
errors.push((e, counterparty_node_id));
5970+
}
5971+
if further_update_exists {
5972+
// If there are more `ChannelMonitorUpdate`s to process, restart at the
5973+
// top of the loop.
5974+
continue;
5975+
}
5976+
} else {
5977+
log_trace!(self.logger, "Unlocked monitor updating for channel {} without monitors to update",
5978+
log_bytes!(&channel_funding_outpoint.to_channel_id()[..]));
5979+
}
5980+
}
5981+
} else {
5982+
log_debug!(self.logger,
5983+
"Got a release post-RAA monitor update for peer {} but the channel is gone",
5984+
log_pubkey!(counterparty_node_id));
5985+
}
5986+
break;
5987+
}
5988+
for (err, counterparty_node_id) in errors {
5989+
let res = Err::<(), _>(err);
5990+
let _ = handle_error!(self, res, counterparty_node_id);
5991+
}
5992+
}
5993+
5994+
fn handle_post_event_actions(&self, actions: Vec<EventCompletionAction>) {
5995+
for action in actions {
5996+
match action {
5997+
EventCompletionAction::ReleaseRAAChannelMonitorUpdate {
5998+
channel_funding_outpoint, counterparty_node_id
5999+
} => {
6000+
self.handle_monitor_update_release(counterparty_node_id, channel_funding_outpoint);
6001+
}
6002+
}
6003+
}
6004+
}
6005+
59296006
/// Processes any events asynchronously in the order they were generated since the last call
59306007
/// using the given event handler.
59316008
///

0 commit comments

Comments
 (0)