Skip to content

Commit a56f00c

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 05448f5 commit a56f00c

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
@@ -16666,51 +16666,16 @@ where
1666616666
);
1666716667
match htlc_source {
1666816668
HTLCSource::PreviousHopData(prev_hop_data) => {
16669-
let pending_forward_matches_htlc = |info: &PendingAddHTLCInfo| {
16670-
info.prev_funding_outpoint == prev_hop_data.outpoint
16671-
&& info.prev_htlc_id == prev_hop_data.htlc_id
16672-
};
16673-
// The ChannelMonitor is now responsible for this HTLC's
16674-
// failure/success and will let us know what its outcome is. If we
16675-
// still have an entry for this HTLC in `forward_htlcs` or
16676-
// `pending_intercepted_htlcs`, we were apparently not persisted after
16677-
// the monitor was when forwarding the payment.
16678-
decode_update_add_htlcs.retain(|scid, update_add_htlcs| {
16679-
update_add_htlcs.retain(|update_add_htlc| {
16680-
let matches = *scid == prev_hop_data.short_channel_id &&
16681-
update_add_htlc.htlc_id == prev_hop_data.htlc_id;
16682-
if matches {
16683-
log_info!(logger, "Removing pending to-decode HTLC with hash {} as it was forwarded to the closed channel {}",
16684-
&htlc.payment_hash, &monitor.channel_id());
16685-
}
16686-
!matches
16687-
});
16688-
!update_add_htlcs.is_empty()
16689-
});
16690-
forward_htlcs.retain(|_, forwards| {
16691-
forwards.retain(|forward| {
16692-
if let HTLCForwardInfo::AddHTLC(htlc_info) = forward {
16693-
if pending_forward_matches_htlc(&htlc_info) {
16694-
log_info!(logger, "Removing pending to-forward HTLC with hash {} as it was forwarded to the closed channel {}",
16695-
&htlc.payment_hash, &monitor.channel_id());
16696-
false
16697-
} else { true }
16698-
} else { true }
16699-
});
16700-
!forwards.is_empty()
16701-
});
16702-
pending_intercepted_htlcs.as_mut().unwrap().retain(|intercepted_id, htlc_info| {
16703-
if pending_forward_matches_htlc(&htlc_info) {
16704-
log_info!(logger, "Removing pending intercepted HTLC with hash {} as it was forwarded to the closed channel {}",
16705-
&htlc.payment_hash, &monitor.channel_id());
16706-
pending_events_read.retain(|(event, _)| {
16707-
if let Event::HTLCIntercepted { intercept_id: ev_id, .. } = event {
16708-
intercepted_id != ev_id
16709-
} else { true }
16710-
});
16711-
false
16712-
} else { true }
16713-
});
16669+
channel_monitor_recovery_internal(
16670+
&mut forward_htlcs,
16671+
&mut pending_events_read,
16672+
&mut pending_intercepted_htlcs,
16673+
&mut decode_update_add_htlcs,
16674+
prev_hop_data,
16675+
&logger,
16676+
htlc.payment_hash,
16677+
monitor.channel_id(),
16678+
);
1671416679
},
1671516680
HTLCSource::TrampolineForward { .. } => todo!(),
1671616681
HTLCSource::OutboundRoute {
@@ -17482,6 +17447,60 @@ where
1748217447
}
1748317448
}
1748417449

17450+
fn channel_monitor_recovery_internal(
17451+
forward_htlcs: &mut HashMap<u64, Vec<HTLCForwardInfo>>,
17452+
pending_events_read: &mut VecDeque<(Event, Option<EventCompletionAction>)>,
17453+
pending_intercepted_htlcs: &mut Option<HashMap<InterceptId, PendingAddHTLCInfo>>,
17454+
decode_update_add_htlcs: &mut HashMap<u64, Vec<msgs::UpdateAddHTLC>>,
17455+
prev_hop_data: HTLCPreviousHopData, logger: &impl Logger, payment_hash: PaymentHash,
17456+
channel_id: ChannelId,
17457+
) {
17458+
let pending_forward_matches_htlc = |info: &PendingAddHTLCInfo| {
17459+
info.prev_funding_outpoint == prev_hop_data.outpoint
17460+
&& info.prev_htlc_id == prev_hop_data.htlc_id
17461+
};
17462+
// The ChannelMonitor is now responsible for this HTLC's
17463+
// failure/success and will let us know what its outcome is. If we
17464+
// still have an entry for this HTLC in `forward_htlcs` or
17465+
// `pending_intercepted_htlcs`, we were apparently not persisted after
17466+
// the monitor was when forwarding the payment.
17467+
decode_update_add_htlcs.retain(|scid, update_add_htlcs| {
17468+
update_add_htlcs.retain(|update_add_htlc| {
17469+
let matches = *scid == prev_hop_data.short_channel_id && update_add_htlc.htlc_id == prev_hop_data.htlc_id;
17470+
if matches {
17471+
log_info!(logger, "Removing pending to-decode HTLC with hash {} as it was forwarded to the closed channel {}",
17472+
payment_hash, channel_id);
17473+
}
17474+
!matches
17475+
});
17476+
!update_add_htlcs.is_empty()
17477+
});
17478+
forward_htlcs.retain(|_, forwards| {
17479+
forwards.retain(|forward| {
17480+
if let HTLCForwardInfo::AddHTLC(htlc_info) = forward {
17481+
if pending_forward_matches_htlc(&htlc_info) {
17482+
log_info!(logger, "Removing pending to-forward HTLC with hash {} as it was forwarded to the closed channel {}",
17483+
payment_hash, channel_id);
17484+
false
17485+
} else { true }
17486+
} else { true }
17487+
});
17488+
!forwards.is_empty()
17489+
});
17490+
pending_intercepted_htlcs.as_mut().unwrap().retain(|intercepted_id, htlc_info| {
17491+
if pending_forward_matches_htlc(&htlc_info) {
17492+
log_info!(logger, "Removing pending intercepted HTLC with hash {} as it was forwarded to the closed channel {}",
17493+
payment_hash, channel_id);
17494+
pending_events_read.retain(|(event, _)| {
17495+
if let Event::HTLCIntercepted { intercept_id: ev_id, .. } = event {
17496+
intercepted_id != ev_id
17497+
} else { true }
17498+
});
17499+
false
17500+
} else { true }
17501+
});
17502+
}
17503+
1748517504
#[cfg(test)]
1748617505
mod tests {
1748717506
use crate::events::{ClosureReason, Event, HTLCHandlingFailureType};

0 commit comments

Comments
 (0)