Skip to content

Commit 946f2de

Browse files
Ack monitor events on revoke_and_ack
Currently, the resolution of HTLCs (and decisions on when HTLCs can be forwarded) is the responsibility of Channel objects (a part of ChannelManager) until the channel is closed, and then the ChannelMonitor thereafter. This leads to some complexity around race conditions for HTLCs right around channel closure. Additionally, there is lots of complexity reconstructing the state of all HTLCs in the ChannelManager deserialization/loading logic. Instead, we want to do all resolution in ChannelMonitors (in response to ChannelMonitorUpdates) and pass them back to ChannelManager in the form of MonitorEvents (similar to how HTLCs are resolved after channels are closed). In order to have reliable resolution, we'll need to keep MonitorEvents around in the ChannelMonitor until the ChannelManager has finished processing them. This will simplify things - on restart instead of examining the set of HTLCs in monitors we can simply replay all the pending MonitorEvents. Here we build on recent commits by ACK'ing monitor events for forward failures once the monitor update that marks them as failed on the inbound edge is complete, when the HTLC was irrevocably failed via revoke_and_ack.
1 parent 49fc649 commit 946f2de

2 files changed

Lines changed: 20 additions & 7 deletions

File tree

lightning/src/ln/channel.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8601,7 +8601,7 @@ where
86018601
(
86028602
Vec<(HTLCSource, PaymentHash)>,
86038603
Vec<(StaticInvoice, BlindedMessagePath)>,
8604-
Option<ChannelMonitorUpdate>,
8604+
Option<(ChannelMonitorUpdate, Vec<MonitorEventSource>)>,
86058605
),
86068606
ChannelError,
86078607
> {
@@ -8918,6 +8918,7 @@ where
89188918
} else {
89198919
"Blocked"
89208920
};
8921+
let mut monitor_events_to_ack = Vec::new();
89218922
macro_rules! return_with_htlcs_to_fail {
89228923
($htlcs_to_fail: expr) => {
89238924
if !release_monitor {
@@ -8926,18 +8927,21 @@ where
89268927
.push(PendingChannelMonitorUpdate { update: monitor_update });
89278928
return Ok(($htlcs_to_fail, static_invoices, None));
89288929
} else {
8929-
return Ok(($htlcs_to_fail, static_invoices, Some(monitor_update)));
8930+
let events_to_ack = core::mem::take(&mut monitor_events_to_ack);
8931+
return Ok((
8932+
$htlcs_to_fail,
8933+
static_invoices,
8934+
Some((monitor_update, events_to_ack)),
8935+
));
89308936
}
89318937
};
89328938
}
89338939

89348940
self.context.monitor_pending_update_adds.append(&mut pending_update_adds);
89358941

89368942
match self.maybe_free_holding_cell_htlcs(fee_estimator, logger) {
8937-
// TODO: Thread monitor_events_to_ack through the revoke_and_ack return
8938-
// value so the ChannelManager can attach an AckMonitorEvents completion
8939-
// action to this monitor update.
8940-
(Some((mut additional_update, _monitor_events_to_ack)), htlcs_to_fail) => {
8943+
(Some((mut additional_update, holding_cell_monitor_events_to_ack)), htlcs_to_fail) => {
8944+
monitor_events_to_ack = holding_cell_monitor_events_to_ack;
89418945
// free_holding_cell_htlcs may bump latest_monitor_id multiple times but we want them to be
89428946
// strictly increasing by one, so decrement it here.
89438947
self.context.latest_monitor_update_id = monitor_update.update_id;

lightning/src/ln/channelmanager.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12935,7 +12935,16 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1293512935
*counterparty_node_id);
1293612936
let (htlcs_to_fail, static_invoices, monitor_update_opt) = try_channel_entry!(self, peer_state,
1293712937
chan.revoke_and_ack(&msg, &self.fee_estimator, &&logger, mon_update_blocked), chan_entry);
12938-
if let Some(monitor_update) = monitor_update_opt {
12938+
if let Some((monitor_update, monitor_events_to_ack)) = monitor_update_opt {
12939+
if !monitor_events_to_ack.is_empty() {
12940+
peer_state
12941+
.monitor_update_blocked_actions
12942+
.entry(msg.channel_id)
12943+
.or_default()
12944+
.push(MonitorUpdateCompletionAction::AckMonitorEvents {
12945+
monitor_events_to_ack,
12946+
});
12947+
}
1293912948
let funding_txo = funding_txo_opt
1294012949
.expect("Funding outpoint must have been set for RAA handling to succeed");
1294112950
if let Some(data) = self.handle_new_monitor_update(

0 commit comments

Comments
 (0)