Skip to content

Commit e5b0df6

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 f99be6b commit e5b0df6

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
@@ -8423,7 +8423,7 @@ where
84238423
(
84248424
Vec<(HTLCSource, PaymentHash)>,
84258425
Vec<(StaticInvoice, BlindedMessagePath)>,
8426-
Option<ChannelMonitorUpdate>,
8426+
Option<(ChannelMonitorUpdate, Vec<MonitorEventSource>)>,
84278427
),
84288428
ChannelError,
84298429
> {
@@ -8740,6 +8740,7 @@ where
87408740
} else {
87418741
"Blocked"
87428742
};
8743+
let mut monitor_events_to_ack = Vec::new();
87438744
macro_rules! return_with_htlcs_to_fail {
87448745
($htlcs_to_fail: expr) => {
87458746
if !release_monitor {
@@ -8748,18 +8749,21 @@ where
87488749
.push(PendingChannelMonitorUpdate { update: monitor_update });
87498750
return Ok(($htlcs_to_fail, static_invoices, None));
87508751
} else {
8751-
return Ok(($htlcs_to_fail, static_invoices, Some(monitor_update)));
8752+
let events_to_ack = core::mem::take(&mut monitor_events_to_ack);
8753+
return Ok((
8754+
$htlcs_to_fail,
8755+
static_invoices,
8756+
Some((monitor_update, events_to_ack)),
8757+
));
87528758
}
87538759
};
87548760
}
87558761

87568762
self.context.monitor_pending_update_adds.append(&mut pending_update_adds);
87578763

87588764
match self.maybe_free_holding_cell_htlcs(fee_estimator, logger) {
8759-
// TODO: Thread monitor_events_to_ack through the revoke_and_ack return
8760-
// value so the ChannelManager can attach an AckMonitorEvents completion
8761-
// action to this monitor update.
8762-
(Some((mut additional_update, _monitor_events_to_ack)), htlcs_to_fail) => {
8765+
(Some((mut additional_update, holding_cell_monitor_events_to_ack)), htlcs_to_fail) => {
8766+
monitor_events_to_ack = holding_cell_monitor_events_to_ack;
87638767
// free_holding_cell_htlcs may bump latest_monitor_id multiple times but we want them to be
87648768
// strictly increasing by one, so decrement it here.
87658769
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
@@ -12838,7 +12838,16 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1283812838
*counterparty_node_id);
1283912839
let (htlcs_to_fail, static_invoices, monitor_update_opt) = try_channel_entry!(self, peer_state,
1284012840
chan.revoke_and_ack(&msg, &self.fee_estimator, &&logger, mon_update_blocked), chan_entry);
12841-
if let Some(monitor_update) = monitor_update_opt {
12841+
if let Some((monitor_update, monitor_events_to_ack)) = monitor_update_opt {
12842+
if !monitor_events_to_ack.is_empty() {
12843+
peer_state
12844+
.monitor_update_blocked_actions
12845+
.entry(msg.channel_id)
12846+
.or_default()
12847+
.push(MonitorUpdateCompletionAction::AckMonitorEvents {
12848+
monitor_events_to_ack,
12849+
});
12850+
}
1284212851
let funding_txo = funding_txo_opt
1284312852
.expect("Funding outpoint must have been set for RAA handling to succeed");
1284412853
if let Some(data) = self.handle_new_monitor_update(

0 commit comments

Comments
 (0)