Skip to content

Commit 49fc649

Browse files
Ack monitor events on check_free_peer_holding_cells
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.
1 parent b972c47 commit 49fc649

2 files changed

Lines changed: 50 additions & 23 deletions

File tree

lightning/src/ln/channel.rs

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8345,7 +8345,7 @@ where
83458345
/// returns `(None, Vec::new())`.
83468346
pub fn maybe_free_holding_cell_htlcs<F: FeeEstimator, L: Logger>(
83478347
&mut self, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L,
8348-
) -> (Option<ChannelMonitorUpdate>, Vec<(HTLCSource, PaymentHash)>) {
8348+
) -> (Option<(ChannelMonitorUpdate, Vec<MonitorEventSource>)>, Vec<(HTLCSource, PaymentHash)>) {
83498349
if matches!(self.context.channel_state, ChannelState::ChannelReady(_))
83508350
&& self.context.channel_state.can_generate_new_commitment()
83518351
{
@@ -8359,7 +8359,7 @@ where
83598359
/// for our counterparty.
83608360
fn free_holding_cell_htlcs<F: FeeEstimator, L: Logger>(
83618361
&mut self, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L,
8362-
) -> (Option<ChannelMonitorUpdate>, Vec<(HTLCSource, PaymentHash)>) {
8362+
) -> (Option<(ChannelMonitorUpdate, Vec<MonitorEventSource>)>, Vec<(HTLCSource, PaymentHash)>) {
83638363
assert!(matches!(self.context.channel_state, ChannelState::ChannelReady(_)));
83648364
assert!(!self.context.channel_state.is_monitor_update_in_progress());
83658365
assert!(!self.context.channel_state.is_quiescent());
@@ -8389,6 +8389,7 @@ where
83898389
let mut update_fulfill_count = 0;
83908390
let mut update_fail_count = 0;
83918391
let mut htlcs_to_fail = Vec::new();
8392+
let mut monitor_events_to_ack = Vec::new();
83928393
for htlc_update in htlc_updates.drain(..) {
83938394
// Note that this *can* fail, though it should be due to rather-rare conditions on
83948395
// fee races with adding too many outputs which push our total payments just over
@@ -8484,31 +8485,41 @@ where
84848485
htlc_id,
84858486
ref err_packet,
84868487
monitor_event_source,
8487-
} => Some(
8488-
self.fail_htlc(
8489-
htlc_id,
8490-
err_packet.clone(),
8491-
false,
8492-
monitor_event_source,
8493-
logger,
8488+
} => {
8489+
if let Some(source) = monitor_event_source {
8490+
monitor_events_to_ack.push(source);
8491+
}
8492+
Some(
8493+
self.fail_htlc(
8494+
htlc_id,
8495+
err_packet.clone(),
8496+
false,
8497+
monitor_event_source,
8498+
logger,
8499+
)
8500+
.map(|fail_msg_opt| fail_msg_opt.map(|_| ())),
84948501
)
8495-
.map(|fail_msg_opt| fail_msg_opt.map(|_| ())),
8496-
),
8502+
},
84978503
&HTLCUpdateAwaitingACK::FailMalformedHTLC {
84988504
htlc_id,
84998505
failure_code,
85008506
sha256_of_onion,
85018507
monitor_event_source,
8502-
} => Some(
8503-
self.fail_htlc(
8504-
htlc_id,
8505-
(sha256_of_onion, failure_code),
8506-
false,
8507-
monitor_event_source,
8508-
logger,
8508+
} => {
8509+
if let Some(source) = monitor_event_source {
8510+
monitor_events_to_ack.push(source);
8511+
}
8512+
Some(
8513+
self.fail_htlc(
8514+
htlc_id,
8515+
(sha256_of_onion, failure_code),
8516+
false,
8517+
monitor_event_source,
8518+
logger,
8519+
)
8520+
.map(|fail_msg_opt| fail_msg_opt.map(|_| ())),
85098521
)
8510-
.map(|fail_msg_opt| fail_msg_opt.map(|_| ())),
8511-
),
8522+
},
85128523
};
85138524
if let Some(res) = fail_htlc_res {
85148525
match res {
@@ -8560,7 +8571,11 @@ where
85608571
Vec::new(),
85618572
logger,
85628573
);
8563-
(self.push_ret_blockable_mon_update(monitor_update), htlcs_to_fail)
8574+
(
8575+
self.push_ret_blockable_mon_update(monitor_update)
8576+
.map(|upd| (upd, monitor_events_to_ack)),
8577+
htlcs_to_fail,
8578+
)
85648579
} else {
85658580
(None, Vec::new())
85668581
}
@@ -8919,7 +8934,10 @@ where
89198934
self.context.monitor_pending_update_adds.append(&mut pending_update_adds);
89208935

89218936
match self.maybe_free_holding_cell_htlcs(fee_estimator, logger) {
8922-
(Some(mut additional_update), htlcs_to_fail) => {
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) => {
89238941
// free_holding_cell_htlcs may bump latest_monitor_id multiple times but we want them to be
89248942
// strictly increasing by one, so decrement it here.
89258943
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
@@ -13732,7 +13732,16 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1373213732
);
1373313733
if monitor_opt.is_some() || !holding_cell_failed_htlcs.is_empty() {
1373413734
let update_res = monitor_opt
13735-
.map(|monitor_update| {
13735+
.map(|(monitor_update, monitor_events_to_ack)| {
13736+
if !monitor_events_to_ack.is_empty() {
13737+
peer_state
13738+
.monitor_update_blocked_actions
13739+
.entry(*chan_id)
13740+
.or_default()
13741+
.push(MonitorUpdateCompletionAction::AckMonitorEvents {
13742+
monitor_events_to_ack,
13743+
});
13744+
}
1373613745
self.handle_new_monitor_update(
1373713746
&mut peer_state.in_flight_monitor_updates,
1373813747
&mut peer_state.monitor_update_blocked_actions,

0 commit comments

Comments
 (0)