Extract util for HTLCIntercepted event creation
What changed, and why it matters
This commit is a small internal code cleanup in the Lightning Dev Kit's channel manager. It pulls out duplicated code for creating an 'HTLC intercepted' event into a shared helper function. The change does not fix a security bug and does not alter user-facing behavior; it is preparation for a future reliability improvement around restarting the channel manager without losing track of intercepted payments.
No security action required. Treat as normal maintenance/refactoring. Review the follow-up commit that regenerates HTLCIntercepted events from rebuilt ChannelManager state when it lands, as that is where any actual security or reliability implications would reside.
Security signals we found
No security-relevant behavior change: pure refactor/DRY extraction
New helper returns `Result<Event, ()>` instead of unwrapping, adding defensive checks for missing fields
Failure path now fails the intercepted HTLC rather than panicking
Commit message mentions future fix for ChannelManager/ChannelMonitor state resynchronization after restart
Evidence from the diff
The patch extracts a new private utility create_htlc_intercepted_event in lightning/src/ln/channelmanager.rs. The helper builds an Event::HTLCIntercepted from a PendingAddHTLCInfo, returning Err(()) if the required forward-routing variant or inbound amount is missing. The existing call site that previously constructed the event inline now uses this helper and, on failure, calls fail_intercepted_htlc(pending_add) with a debug_assert!(false). The commit message frames this as groundwork for later rebuilding ChannelManager state from ChannelMonitors and regenerating lost HTLCIntercepted events after restart.
Changed components
lightning/src/ln/channelmanager.rsHTLC interception event generationPendingAddHTLCInfo handlingInspect captured patch +28 / −16
diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs
index 72585d6..aa78710 100644
--- a/lightning/src/ln/channelmanager.rs
+++ b/lightning/src/ln/channelmanager.rs
@@ -3911,6 +3911,25 @@ macro_rules! process_events_body {
}
}
+/// Creates an [`Event::HTLCIntercepted`] from a [`PendingAddHTLCInfo`]. We generate this event in a
+/// few places so this DRYs the code.
+fn create_htlc_intercepted_event(
+ intercept_id: InterceptId, pending_add: &PendingAddHTLCInfo,
+) -> Result<Event, ()> {
+ let inbound_amount_msat = pending_add.forward_info.incoming_amt_msat.ok_or(())?;
+ let requested_next_hop_scid = match pending_add.forward_info.routing {
+ PendingHTLCRouting::Forward { short_channel_id, .. } => short_channel_id,
+ _ => return Err(()),
+ };
+ Ok(Event::HTLCIntercepted {
+ requested_next_hop_scid,
+ payment_hash: pending_add.forward_info.payment_hash,
+ inbound_amount_msat,
+ expected_outbound_amount_msat: pending_add.forward_info.outgoing_amt_msat,
+ intercept_id,
+ })
+}
+
impl<
M: Deref,
T: Deref,
@@ -11486,22 +11505,15 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
let mut pending_intercepts = self.pending_intercepted_htlcs.lock().unwrap();
match pending_intercepts.entry(intercept_id) {
hash_map::Entry::Vacant(entry) => {
- new_intercept_events.push_back((
- events::Event::HTLCIntercepted {
- requested_next_hop_scid: scid,
- payment_hash,
- inbound_amount_msat: pending_add
- .forward_info
- .incoming_amt_msat
- .unwrap(),
- expected_outbound_amount_msat: pending_add
- .forward_info
- .outgoing_amt_msat,
- intercept_id,
- },
- None,
- ));
- entry.insert(pending_add);
+ if let Ok(intercept_ev) =
+ create_htlc_intercepted_event(intercept_id, &pending_add)
+ {
+ new_intercept_events.push_back((intercept_ev, None));
+ entry.insert(pending_add);
+ } else {
+ debug_assert!(false);
+ fail_intercepted_htlc(pending_add);
+ }
},
hash_map::Entry::Occupied(_) => {
log_info!(
Why this scored 12/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.