Skip to content

Commit e0677b0

Browse files
committed
Extract channelmonitor recovery to external helper
Move recovery logic for `HTLCSource::PreviousHopData` into `channel_monitor_recovery_internal` to prepare for trampoline forward reuse.
1 parent ee95116 commit e0677b0

File tree

1 file changed

+64
-45
lines changed

1 file changed

+64
-45
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 64 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -16554,51 +16554,16 @@ where
1655416554
);
1655516555
match htlc_source {
1655616556
HTLCSource::PreviousHopData(prev_hop_data) => {
16557-
let pending_forward_matches_htlc = |info: &PendingAddHTLCInfo| {
16558-
info.prev_funding_outpoint == prev_hop_data.outpoint
16559-
&& info.prev_htlc_id == prev_hop_data.htlc_id
16560-
};
16561-
// The ChannelMonitor is now responsible for this HTLC's
16562-
// failure/success and will let us know what its outcome is. If we
16563-
// still have an entry for this HTLC in `forward_htlcs` or
16564-
// `pending_intercepted_htlcs`, we were apparently not persisted after
16565-
// the monitor was when forwarding the payment.
16566-
decode_update_add_htlcs.retain(|scid, update_add_htlcs| {
16567-
update_add_htlcs.retain(|update_add_htlc| {
16568-
let matches = *scid == prev_hop_data.short_channel_id &&
16569-
update_add_htlc.htlc_id == prev_hop_data.htlc_id;
16570-
if matches {
16571-
log_info!(logger, "Removing pending to-decode HTLC with hash {} as it was forwarded to the closed channel {}",
16572-
&htlc.payment_hash, &monitor.channel_id());
16573-
}
16574-
!matches
16575-
});
16576-
!update_add_htlcs.is_empty()
16577-
});
16578-
forward_htlcs.retain(|_, forwards| {
16579-
forwards.retain(|forward| {
16580-
if let HTLCForwardInfo::AddHTLC(htlc_info) = forward {
16581-
if pending_forward_matches_htlc(&htlc_info) {
16582-
log_info!(logger, "Removing pending to-forward HTLC with hash {} as it was forwarded to the closed channel {}",
16583-
&htlc.payment_hash, &monitor.channel_id());
16584-
false
16585-
} else { true }
16586-
} else { true }
16587-
});
16588-
!forwards.is_empty()
16589-
});
16590-
pending_intercepted_htlcs.as_mut().unwrap().retain(|intercepted_id, htlc_info| {
16591-
if pending_forward_matches_htlc(&htlc_info) {
16592-
log_info!(logger, "Removing pending intercepted HTLC with hash {} as it was forwarded to the closed channel {}",
16593-
&htlc.payment_hash, &monitor.channel_id());
16594-
pending_events_read.retain(|(event, _)| {
16595-
if let Event::HTLCIntercepted { intercept_id: ev_id, .. } = event {
16596-
intercepted_id != ev_id
16597-
} else { true }
16598-
});
16599-
false
16600-
} else { true }
16601-
});
16557+
channel_monitor_recovery_internal(
16558+
&mut forward_htlcs,
16559+
&mut pending_events_read,
16560+
&mut pending_intercepted_htlcs,
16561+
&mut decode_update_add_htlcs,
16562+
prev_hop_data,
16563+
&logger,
16564+
htlc.payment_hash,
16565+
monitor.channel_id(),
16566+
);
1660216567
},
1660316568
HTLCSource::TrampolineForward { .. } => todo!(),
1660416569
HTLCSource::OutboundRoute {
@@ -17370,6 +17335,60 @@ where
1737017335
}
1737117336
}
1737217337

17338+
fn channel_monitor_recovery_internal(
17339+
forward_htlcs: &mut HashMap<u64, Vec<HTLCForwardInfo>>,
17340+
pending_events_read: &mut VecDeque<(Event, Option<EventCompletionAction>)>,
17341+
pending_intercepted_htlcs: &mut Option<HashMap<InterceptId, PendingAddHTLCInfo>>,
17342+
decode_update_add_htlcs: &mut HashMap<u64, Vec<msgs::UpdateAddHTLC>>,
17343+
prev_hop_data: HTLCPreviousHopData, logger: &impl Logger, payment_hash: PaymentHash,
17344+
channel_id: ChannelId,
17345+
) {
17346+
let pending_forward_matches_htlc = |info: &PendingAddHTLCInfo| {
17347+
info.prev_funding_outpoint == prev_hop_data.outpoint
17348+
&& info.prev_htlc_id == prev_hop_data.htlc_id
17349+
};
17350+
// The ChannelMonitor is now responsible for this HTLC's
17351+
// failure/success and will let us know what its outcome is. If we
17352+
// still have an entry for this HTLC in `forward_htlcs` or
17353+
// `pending_intercepted_htlcs`, we were apparently not persisted after
17354+
// the monitor was when forwarding the payment.
17355+
decode_update_add_htlcs.retain(|scid, update_add_htlcs| {
17356+
update_add_htlcs.retain(|update_add_htlc| {
17357+
let matches = *scid == prev_hop_data.short_channel_id && update_add_htlc.htlc_id == prev_hop_data.htlc_id;
17358+
if matches {
17359+
log_info!(logger, "Removing pending to-decode HTLC with hash {} as it was forwarded to the closed channel {}",
17360+
payment_hash, channel_id);
17361+
}
17362+
!matches
17363+
});
17364+
!update_add_htlcs.is_empty()
17365+
});
17366+
forward_htlcs.retain(|_, forwards| {
17367+
forwards.retain(|forward| {
17368+
if let HTLCForwardInfo::AddHTLC(htlc_info) = forward {
17369+
if pending_forward_matches_htlc(&htlc_info) {
17370+
log_info!(logger, "Removing pending to-forward HTLC with hash {} as it was forwarded to the closed channel {}",
17371+
payment_hash, channel_id);
17372+
false
17373+
} else { true }
17374+
} else { true }
17375+
});
17376+
!forwards.is_empty()
17377+
});
17378+
pending_intercepted_htlcs.as_mut().unwrap().retain(|intercepted_id, htlc_info| {
17379+
if pending_forward_matches_htlc(&htlc_info) {
17380+
log_info!(logger, "Removing pending intercepted HTLC with hash {} as it was forwarded to the closed channel {}",
17381+
payment_hash, channel_id);
17382+
pending_events_read.retain(|(event, _)| {
17383+
if let Event::HTLCIntercepted { intercept_id: ev_id, .. } = event {
17384+
intercepted_id != ev_id
17385+
} else { true }
17386+
});
17387+
false
17388+
} else { true }
17389+
});
17390+
}
17391+
1737317392
#[cfg(test)]
1737417393
mod tests {
1737517394
use crate::events::{ClosureReason, Event, HTLCHandlingFailureType};

0 commit comments

Comments
 (0)