Skip to content

Commit 9b33f06

Browse files
Track monitor event id in HTLCForwardInfo::Fail*
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. To ensure we can resolve HTLC monitor events for forward failures, we need to pipe the event id through the HTLC failure pipeline, starting from when we first handle the monitor event and call fail_htlc_backwards. To get it from there to the next stage, here we store the monitor event id in the manager's HTLCForwardInfo::Fail*. In upcoming commits we will eventually get to the point of acking the monitor event when the HTLC is irrevocably removed from the inbound edge via monitor update.
1 parent f2f3f26 commit 9b33f06

1 file changed

Lines changed: 52 additions & 9 deletions

File tree

lightning/src/ln/channelmanager.rs

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -497,8 +497,21 @@ impl PendingAddHTLCInfo {
497497
#[cfg_attr(test, derive(Clone, Debug, PartialEq))]
498498
pub(super) enum HTLCForwardInfo {
499499
AddHTLC(PendingAddHTLCInfo),
500-
FailHTLC { htlc_id: u64, err_packet: msgs::OnionErrorPacket },
501-
FailMalformedHTLC { htlc_id: u64, failure_code: u16, sha256_of_onion: [u8; 32] },
500+
FailHTLC {
501+
htlc_id: u64,
502+
err_packet: msgs::OnionErrorPacket,
503+
/// If this fail originated from a [`MonitorEvent`], the event ID and channel to acknowledge
504+
/// once the fail is durably applied to the inbound channel.
505+
monitor_event_source: Option<MonitorEventSource>,
506+
},
507+
FailMalformedHTLC {
508+
htlc_id: u64,
509+
failure_code: u16,
510+
sha256_of_onion: [u8; 32],
511+
/// If this fail originated from a [`MonitorEvent`], the event ID and channel to acknowledge
512+
/// once the fail is durably applied to the inbound channel.
513+
monitor_event_source: Option<MonitorEventSource>,
514+
},
502515
}
503516

504517
/// Whether this blinded HTLC is being failed backwards by the introduction node or a blinded node,
@@ -7529,12 +7542,14 @@ impl<
75297542
HTLCFailureMsg::Relay(fail_htlc) => HTLCForwardInfo::FailHTLC {
75307543
htlc_id: fail_htlc.htlc_id,
75317544
err_packet: fail_htlc.into(),
7545+
monitor_event_source: None,
75327546
},
75337547
HTLCFailureMsg::Malformed(fail_malformed_htlc) => {
75347548
HTLCForwardInfo::FailMalformedHTLC {
75357549
htlc_id: fail_malformed_htlc.htlc_id,
75367550
sha256_of_onion: fail_malformed_htlc.sha256_of_onion,
75377551
failure_code: fail_malformed_htlc.failure_code.into(),
7552+
monitor_event_source: None,
75387553
}
75397554
},
75407555
};
@@ -8064,7 +8079,7 @@ impl<
80648079
}
80658080
None
80668081
},
8067-
HTLCForwardInfo::FailHTLC { htlc_id, ref err_packet } => {
8082+
HTLCForwardInfo::FailHTLC { htlc_id, ref err_packet, .. } => {
80688083
if let Some(chan) = peer_state
80698084
.channel_by_id
80708085
.get_mut(&forward_chan_id)
@@ -8084,7 +8099,12 @@ impl<
80848099
break;
80858100
}
80868101
},
8087-
HTLCForwardInfo::FailMalformedHTLC { htlc_id, failure_code, sha256_of_onion } => {
8102+
HTLCForwardInfo::FailMalformedHTLC {
8103+
htlc_id,
8104+
failure_code,
8105+
sha256_of_onion,
8106+
..
8107+
} => {
80888108
if let Some(chan) = peer_state
80898109
.channel_by_id
80908110
.get_mut(&forward_chan_id)
@@ -9122,6 +9142,10 @@ impl<
91229142
onion_error
91239143
);
91249144

9145+
let monitor_event_source = from_monitor_update_completion.as_ref().and_then(|u| {
9146+
u.monitor_event_id
9147+
.map(|id| MonitorEventSource { event_id: id, channel_id: u.channel_id })
9148+
});
91259149
push_forward_htlcs_failure(
91269150
*prev_outbound_scid_alias,
91279151
get_htlc_forward_failure(
@@ -9131,6 +9155,7 @@ impl<
91319155
trampoline_shared_secret,
91329156
phantom_shared_secret,
91339157
*htlc_id,
9158+
monitor_event_source,
91349159
),
91359160
);
91369161

@@ -9166,7 +9191,12 @@ impl<
91669191
// necessarily want to fail all of our incoming HTLCs back yet. We may have other
91679192
// outgoing HTLCs that need to resolve first. This will be tracked in our
91689193
// pending_outbound_payments in a followup.
9169-
for current_hop_data in previous_hop_data {
9194+
let trampoline_monitor_event_source =
9195+
from_monitor_update_completion.as_ref().and_then(|u| {
9196+
u.monitor_event_id
9197+
.map(|id| MonitorEventSource { event_id: id, channel_id: u.channel_id })
9198+
});
9199+
for (i, current_hop_data) in previous_hop_data.iter().enumerate() {
91709200
let HTLCPreviousHopData {
91719201
prev_outbound_scid_alias,
91729202
htlc_id,
@@ -9193,6 +9223,7 @@ impl<
91939223
&incoming_trampoline_shared_secret,
91949224
&None,
91959225
*htlc_id,
9226+
if i == 0 { trampoline_monitor_event_source } else { None },
91969227
),
91979228
);
91989229
}
@@ -14028,6 +14059,7 @@ fn get_htlc_forward_failure(
1402814059
blinded_failure: &Option<BlindedFailure>, onion_error: &HTLCFailReason,
1402914060
incoming_packet_shared_secret: &[u8; 32], trampoline_shared_secret: &Option<[u8; 32]>,
1403014061
phantom_shared_secret: &Option<[u8; 32]>, htlc_id: u64,
14062+
monitor_event_source: Option<MonitorEventSource>,
1403114063
) -> HTLCForwardInfo {
1403214064
// TODO: Correctly wrap the error packet twice if failing back a trampoline + phantom HTLC.
1403314065
let secondary_shared_secret = trampoline_shared_secret.or(*phantom_shared_secret);
@@ -14039,19 +14071,20 @@ fn get_htlc_forward_failure(
1403914071
incoming_packet_shared_secret,
1404014072
&secondary_shared_secret,
1404114073
);
14042-
HTLCForwardInfo::FailHTLC { htlc_id, err_packet }
14074+
HTLCForwardInfo::FailHTLC { htlc_id, err_packet, monitor_event_source }
1404314075
},
1404414076
Some(BlindedFailure::FromBlindedNode) => HTLCForwardInfo::FailMalformedHTLC {
1404514077
htlc_id,
1404614078
failure_code: LocalHTLCFailureReason::InvalidOnionBlinding.failure_code(),
1404714079
sha256_of_onion: [0; 32],
14080+
monitor_event_source,
1404814081
},
1404914082
None => {
1405014083
let err_packet = onion_error.get_encrypted_failure_packet(
1405114084
incoming_packet_shared_secret,
1405214085
&secondary_shared_secret,
1405314086
);
14054-
HTLCForwardInfo::FailHTLC { htlc_id, err_packet }
14087+
HTLCForwardInfo::FailHTLC { htlc_id, err_packet, monitor_event_source }
1405514088
},
1405614089
}
1405714090
}
@@ -17690,15 +17723,21 @@ impl Writeable for HTLCForwardInfo {
1769017723
0u8.write(w)?;
1769117724
info.write(w)?;
1769217725
},
17693-
Self::FailHTLC { htlc_id, err_packet } => {
17726+
Self::FailHTLC { htlc_id, err_packet, monitor_event_source } => {
1769417727
FAIL_HTLC_VARIANT_ID.write(w)?;
1769517728
write_tlv_fields!(w, {
1769617729
(0, htlc_id, required),
1769717730
(2, err_packet.data, required),
1769817731
(5, err_packet.attribution_data, option),
17732+
(7, monitor_event_source, option),
1769917733
});
1770017734
},
17701-
Self::FailMalformedHTLC { htlc_id, failure_code, sha256_of_onion } => {
17735+
Self::FailMalformedHTLC {
17736+
htlc_id,
17737+
failure_code,
17738+
sha256_of_onion,
17739+
monitor_event_source,
17740+
} => {
1770217741
// Since this variant was added in 0.0.119, write this as `::FailHTLC` with an empty error
1770317742
// packet so older versions have something to fail back with, but serialize the real data as
1770417743
// optional TLVs for the benefit of newer versions.
@@ -17708,6 +17747,7 @@ impl Writeable for HTLCForwardInfo {
1770817747
(1, failure_code, required),
1770917748
(2, Vec::<u8>::new(), required),
1771017749
(3, sha256_of_onion, required),
17750+
(7, monitor_event_source, option),
1771117751
});
1771217752
},
1771317753
}
@@ -17728,6 +17768,7 @@ impl Readable for HTLCForwardInfo {
1772817768
(2, err_packet, required),
1772917769
(3, sha256_of_onion, option),
1773017770
(5, attribution_data, option),
17771+
(7, monitor_event_source, option),
1773117772
});
1773217773
if let Some(failure_code) = malformed_htlc_failure_code {
1773317774
if attribution_data.is_some() {
@@ -17737,6 +17778,7 @@ impl Readable for HTLCForwardInfo {
1773717778
htlc_id: _init_tlv_based_struct_field!(htlc_id, required),
1773817779
failure_code,
1773917780
sha256_of_onion: sha256_of_onion.ok_or(DecodeError::InvalidValue)?,
17781+
monitor_event_source,
1774017782
}
1774117783
} else {
1774217784
Self::FailHTLC {
@@ -17745,6 +17787,7 @@ impl Readable for HTLCForwardInfo {
1774517787
data: _init_tlv_based_struct_field!(err_packet, required),
1774617788
attribution_data: _init_tlv_based_struct_field!(attribution_data, option),
1774717789
},
17790+
monitor_event_source,
1774817791
}
1774917792
}
1775017793
},

0 commit comments

Comments
 (0)