Skip to content

Commit f99be6b

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

2 files changed

Lines changed: 34 additions & 5 deletions

File tree

lightning/src/ln/channel.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8175,7 +8175,7 @@ where
81758175
/// returns `(None, Vec::new())`.
81768176
pub fn maybe_free_holding_cell_htlcs<F: FeeEstimator, L: Logger>(
81778177
&mut self, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L,
8178-
) -> (Option<ChannelMonitorUpdate>, Vec<(HTLCSource, PaymentHash)>) {
8178+
) -> (Option<(ChannelMonitorUpdate, Vec<MonitorEventSource>)>, Vec<(HTLCSource, PaymentHash)>) {
81798179
if matches!(self.context.channel_state, ChannelState::ChannelReady(_))
81808180
&& self.context.channel_state.can_generate_new_commitment()
81818181
{
@@ -8189,7 +8189,7 @@ where
81898189
/// for our counterparty.
81908190
fn free_holding_cell_htlcs<F: FeeEstimator, L: Logger>(
81918191
&mut self, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L,
8192-
) -> (Option<ChannelMonitorUpdate>, Vec<(HTLCSource, PaymentHash)>) {
8192+
) -> (Option<(ChannelMonitorUpdate, Vec<MonitorEventSource>)>, Vec<(HTLCSource, PaymentHash)>) {
81938193
assert!(matches!(self.context.channel_state, ChannelState::ChannelReady(_)));
81948194
assert!(!self.context.channel_state.is_monitor_update_in_progress());
81958195
assert!(!self.context.channel_state.is_quiescent());
@@ -8219,7 +8219,20 @@ where
82198219
let mut update_fulfill_count = 0;
82208220
let mut update_fail_count = 0;
82218221
let mut htlcs_to_fail = Vec::new();
8222+
let mut monitor_events_to_ack = Vec::new();
82228223
for htlc_update in htlc_updates.drain(..) {
8224+
match &htlc_update {
8225+
HTLCUpdateAwaitingACK::FailHTLC {
8226+
monitor_event_source: Some(source), ..
8227+
}
8228+
| HTLCUpdateAwaitingACK::FailMalformedHTLC {
8229+
monitor_event_source: Some(source),
8230+
..
8231+
} => {
8232+
monitor_events_to_ack.push(*source);
8233+
},
8234+
_ => {},
8235+
}
82238236
// Note that this *can* fail, though it should be due to rather-rare conditions on
82248237
// fee races with adding too many outputs which push our total payments just over
82258238
// the limit. In case it's less rare than I anticipate, we may want to revisit
@@ -8380,7 +8393,11 @@ where
83808393
Vec::new(),
83818394
logger,
83828395
);
8383-
(self.push_ret_blockable_mon_update(monitor_update), htlcs_to_fail)
8396+
(
8397+
self.push_ret_blockable_mon_update(monitor_update)
8398+
.map(|upd| (upd, monitor_events_to_ack)),
8399+
htlcs_to_fail,
8400+
)
83848401
} else {
83858402
(None, Vec::new())
83868403
}
@@ -8739,7 +8756,10 @@ where
87398756
self.context.monitor_pending_update_adds.append(&mut pending_update_adds);
87408757

87418758
match self.maybe_free_holding_cell_htlcs(fee_estimator, logger) {
8742-
(Some(mut additional_update), htlcs_to_fail) => {
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) => {
87438763
// free_holding_cell_htlcs may bump latest_monitor_id multiple times but we want them to be
87448764
// strictly increasing by one, so decrement it here.
87458765
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
@@ -13537,7 +13537,16 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1353713537
);
1353813538
if monitor_opt.is_some() || !holding_cell_failed_htlcs.is_empty() {
1353913539
let update_res = monitor_opt
13540-
.map(|monitor_update| {
13540+
.map(|(monitor_update, monitor_events_to_ack)| {
13541+
if !monitor_events_to_ack.is_empty() {
13542+
peer_state
13543+
.monitor_update_blocked_actions
13544+
.entry(*chan_id)
13545+
.or_default()
13546+
.push(MonitorUpdateCompletionAction::AckMonitorEvents {
13547+
monitor_events_to_ack,
13548+
});
13549+
}
1354113550
self.handle_new_monitor_update(
1354213551
&mut peer_state.in_flight_monitor_updates,
1354313552
&mut peer_state.monitor_update_blocked_actions,

0 commit comments

Comments
 (0)