Skip to content

Commit db81c65

Browse files
authored
Merge pull request #2791 from valentinewallace/2023-12-multihop-recv-followups
Follow-ups to #2688
2 parents ccf710d + 3ec4d52 commit db81c65

File tree

2 files changed

+58
-71
lines changed

2 files changed

+58
-71
lines changed

lightning/src/ln/channel.rs

Lines changed: 37 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2555,26 +2555,24 @@ impl FailHTLCContents for msgs::OnionErrorPacket {
25552555
HTLCUpdateAwaitingACK::FailHTLC { htlc_id, err_packet: self }
25562556
}
25572557
}
2558-
impl FailHTLCContents for (u16, [u8; 32]) {
2559-
type Message = msgs::UpdateFailMalformedHTLC; // (failure_code, sha256_of_onion)
2558+
impl FailHTLCContents for ([u8; 32], u16) {
2559+
type Message = msgs::UpdateFailMalformedHTLC;
25602560
fn to_message(self, htlc_id: u64, channel_id: ChannelId) -> Self::Message {
25612561
msgs::UpdateFailMalformedHTLC {
25622562
htlc_id,
25632563
channel_id,
2564-
failure_code: self.0,
2565-
sha256_of_onion: self.1
2564+
sha256_of_onion: self.0,
2565+
failure_code: self.1
25662566
}
25672567
}
25682568
fn to_inbound_htlc_state(self) -> InboundHTLCState {
2569-
InboundHTLCState::LocalRemoved(
2570-
InboundHTLCRemovalReason::FailMalformed((self.1, self.0))
2571-
)
2569+
InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::FailMalformed(self))
25722570
}
25732571
fn to_htlc_update_awaiting_ack(self, htlc_id: u64) -> HTLCUpdateAwaitingACK {
25742572
HTLCUpdateAwaitingACK::FailMalformedHTLC {
25752573
htlc_id,
2576-
failure_code: self.0,
2577-
sha256_of_onion: self.1
2574+
sha256_of_onion: self.0,
2575+
failure_code: self.1
25782576
}
25792577
}
25802578
}
@@ -2901,7 +2899,7 @@ impl<SP: Deref> Channel<SP> where
29012899
pub fn queue_fail_malformed_htlc<L: Deref>(
29022900
&mut self, htlc_id_arg: u64, failure_code: u16, sha256_of_onion: [u8; 32], logger: &L
29032901
) -> Result<(), ChannelError> where L::Target: Logger {
2904-
self.fail_htlc(htlc_id_arg, (failure_code, sha256_of_onion), true, logger)
2902+
self.fail_htlc(htlc_id_arg, (sha256_of_onion, failure_code), true, logger)
29052903
.map(|msg_opt| assert!(msg_opt.is_none(), "We forced holding cell?"))
29062904
}
29072905

@@ -2914,7 +2912,7 @@ impl<SP: Deref> Channel<SP> where
29142912
/// return `Ok(_)` if preconditions are met. In any case, `Err`s will only be
29152913
/// [`ChannelError::Ignore`].
29162914
fn fail_htlc<L: Deref, E: FailHTLCContents + Clone>(
2917-
&mut self, htlc_id_arg: u64, err_packet: E, mut force_holding_cell: bool,
2915+
&mut self, htlc_id_arg: u64, err_contents: E, mut force_holding_cell: bool,
29182916
logger: &L
29192917
) -> Result<Option<E::Message>, ChannelError> where L::Target: Logger {
29202918
if !matches!(self.context.channel_state, ChannelState::ChannelReady(_)) {
@@ -2981,18 +2979,18 @@ impl<SP: Deref> Channel<SP> where
29812979
}
29822980
}
29832981
log_trace!(logger, "Placing failure for HTLC ID {} in holding cell in channel {}.", htlc_id_arg, &self.context.channel_id());
2984-
self.context.holding_cell_htlc_updates.push(err_packet.to_htlc_update_awaiting_ack(htlc_id_arg));
2982+
self.context.holding_cell_htlc_updates.push(err_contents.to_htlc_update_awaiting_ack(htlc_id_arg));
29852983
return Ok(None);
29862984
}
29872985

29882986
log_trace!(logger, "Failing HTLC ID {} back with {} message in channel {}.", htlc_id_arg,
29892987
E::Message::name(), &self.context.channel_id());
29902988
{
29912989
let htlc = &mut self.context.pending_inbound_htlcs[pending_idx];
2992-
htlc.state = err_packet.clone().to_inbound_htlc_state();
2990+
htlc.state = err_contents.clone().to_inbound_htlc_state();
29932991
}
29942992

2995-
Ok(Some(err_packet.to_message(htlc_id_arg, self.context.channel_id())))
2993+
Ok(Some(err_contents.to_message(htlc_id_arg, self.context.channel_id())))
29962994
}
29972995

29982996
// Message handlers:
@@ -3605,7 +3603,7 @@ impl<SP: Deref> Channel<SP> where
36053603
// the limit. In case it's less rare than I anticipate, we may want to revisit
36063604
// handling this case better and maybe fulfilling some of the HTLCs while attempting
36073605
// to rebalance channels.
3608-
match &htlc_update {
3606+
let fail_htlc_res = match &htlc_update {
36093607
&HTLCUpdateAwaitingACK::AddHTLC {
36103608
amount_msat, cltv_expiry, ref payment_hash, ref source, ref onion_routing_packet,
36113609
skimmed_fee_msat, blinding_point, ..
@@ -3633,6 +3631,7 @@ impl<SP: Deref> Channel<SP> where
36333631
}
36343632
}
36353633
}
3634+
None
36363635
},
36373636
&HTLCUpdateAwaitingACK::ClaimHTLC { ref payment_preimage, htlc_id, .. } => {
36383637
// If an HTLC claim was previously added to the holding cell (via
@@ -3646,40 +3645,33 @@ impl<SP: Deref> Channel<SP> where
36463645
{ monitor_update } else { unreachable!() };
36473646
update_fulfill_count += 1;
36483647
monitor_update.updates.append(&mut additional_monitor_update.updates);
3648+
None
36493649
},
36503650
&HTLCUpdateAwaitingACK::FailHTLC { htlc_id, ref err_packet } => {
3651-
match self.fail_htlc(htlc_id, err_packet.clone(), false, logger) {
3652-
Ok(update_fail_msg_option) => {
3653-
// If an HTLC failure was previously added to the holding cell (via
3654-
// `queue_fail_htlc`) then generating the fail message itself must
3655-
// not fail - we should never end up in a state where we double-fail
3656-
// an HTLC or fail-then-claim an HTLC as it indicates we didn't wait
3657-
// for a full revocation before failing.
3658-
debug_assert!(update_fail_msg_option.is_some());
3659-
update_fail_count += 1;
3660-
},
3661-
Err(e) => {
3662-
if let ChannelError::Ignore(_) = e {}
3663-
else {
3664-
panic!("Got a non-IgnoreError action trying to fail holding cell HTLC");
3665-
}
3666-
}
3667-
}
3651+
Some(self.fail_htlc(htlc_id, err_packet.clone(), false, logger)
3652+
.map(|fail_msg_opt| fail_msg_opt.map(|_| ())))
36683653
},
36693654
&HTLCUpdateAwaitingACK::FailMalformedHTLC { htlc_id, failure_code, sha256_of_onion } => {
3670-
match self.fail_htlc(htlc_id, (failure_code, sha256_of_onion), false, logger) {
3671-
Ok(update_fail_malformed_opt) => {
3672-
debug_assert!(update_fail_malformed_opt.is_some()); // See above comment
3673-
update_fail_count += 1;
3674-
},
3675-
Err(e) => {
3676-
if let ChannelError::Ignore(_) = e {}
3677-
else {
3678-
panic!("Got a non-IgnoreError action trying to fail holding cell HTLC");
3679-
}
3680-
}
3681-
}
3682-
},
3655+
Some(self.fail_htlc(htlc_id, (sha256_of_onion, failure_code), false, logger)
3656+
.map(|fail_msg_opt| fail_msg_opt.map(|_| ())))
3657+
}
3658+
};
3659+
if let Some(res) = fail_htlc_res {
3660+
match res {
3661+
Ok(fail_msg_opt) => {
3662+
// If an HTLC failure was previously added to the holding cell (via
3663+
// `queue_fail_{malformed_}htlc`) then generating the fail message itself must
3664+
// not fail - we should never end up in a state where we double-fail
3665+
// an HTLC or fail-then-claim an HTLC as it indicates we didn't wait
3666+
// for a full revocation before failing.
3667+
debug_assert!(fail_msg_opt.is_some());
3668+
update_fail_count += 1;
3669+
},
3670+
Err(ChannelError::Ignore(_)) => {},
3671+
Err(_) => {
3672+
panic!("Got a non-IgnoreError action trying to fail holding cell HTLC");
3673+
},
3674+
}
36833675
}
36843676
}
36853677
if update_add_count == 0 && update_fulfill_count == 0 && update_fail_count == 0 && self.context.holding_cell_update_fee.is_none() {

lightning/src/ln/channelmanager.rs

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4350,7 +4350,7 @@ where
43504350
if let Some(ChannelPhase::Funded(ref mut chan)) = peer_state.channel_by_id.get_mut(&forward_chan_id) {
43514351
let logger = WithChannelContext::from(&self.logger, &chan.context);
43524352
for forward_info in pending_forwards.drain(..) {
4353-
match forward_info {
4353+
let queue_fail_htlc_res = match forward_info {
43544354
HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
43554355
prev_short_channel_id, prev_htlc_id, prev_funding_outpoint, prev_user_channel_id,
43564356
forward_info: PendingHTLCInfo {
@@ -4396,40 +4396,35 @@ where
43964396
));
43974397
continue;
43984398
}
4399+
None
43994400
},
44004401
HTLCForwardInfo::AddHTLC { .. } => {
44014402
panic!("short_channel_id != 0 should imply any pending_forward entries are of type Forward");
44024403
},
44034404
HTLCForwardInfo::FailHTLC { htlc_id, err_packet } => {
44044405
log_trace!(logger, "Failing HTLC back to channel with short id {} (backward HTLC ID {}) after delay", short_chan_id, htlc_id);
4405-
if let Err(e) = chan.queue_fail_htlc(
4406-
htlc_id, err_packet, &&logger
4407-
) {
4408-
if let ChannelError::Ignore(msg) = e {
4409-
log_trace!(logger, "Failed to fail HTLC with ID {} backwards to short_id {}: {}", htlc_id, short_chan_id, msg);
4410-
} else {
4411-
panic!("Stated return value requirements in queue_fail_htlc() were not met");
4412-
}
4413-
// fail-backs are best-effort, we probably already have one
4414-
// pending, and if not that's OK, if not, the channel is on
4415-
// the chain and sending the HTLC-Timeout is their problem.
4416-
continue;
4417-
}
4406+
Some((chan.queue_fail_htlc(htlc_id, err_packet, &&logger), htlc_id))
44184407
},
44194408
HTLCForwardInfo::FailMalformedHTLC { htlc_id, failure_code, sha256_of_onion } => {
4420-
log_trace!(self.logger, "Failing malformed HTLC back to channel with short id {} (backward HTLC ID {}) after delay", short_chan_id, htlc_id);
4421-
if let Err(e) = chan.queue_fail_malformed_htlc(htlc_id, failure_code, sha256_of_onion, &self.logger) {
4422-
if let ChannelError::Ignore(msg) = e {
4423-
log_trace!(self.logger, "Failed to fail HTLC with ID {} backwards to short_id {}: {}", htlc_id, short_chan_id, msg);
4424-
} else {
4425-
panic!("Stated return value requirements in queue_fail_malformed_htlc() were not met");
4426-
}
4427-
// fail-backs are best-effort, we probably already have one
4428-
// pending, and if not that's OK, if not, the channel is on
4429-
// the chain and sending the HTLC-Timeout is their problem.
4430-
continue;
4431-
}
4409+
log_trace!(logger, "Failing malformed HTLC back to channel with short id {} (backward HTLC ID {}) after delay", short_chan_id, htlc_id);
4410+
let res = chan.queue_fail_malformed_htlc(
4411+
htlc_id, failure_code, sha256_of_onion, &&logger
4412+
);
4413+
Some((res, htlc_id))
44324414
},
4415+
};
4416+
if let Some((queue_fail_htlc_res, htlc_id)) = queue_fail_htlc_res {
4417+
if let Err(e) = queue_fail_htlc_res {
4418+
if let ChannelError::Ignore(msg) = e {
4419+
log_trace!(logger, "Failed to fail HTLC with ID {} backwards to short_id {}: {}", htlc_id, short_chan_id, msg);
4420+
} else {
4421+
panic!("Stated return value requirements in queue_fail_{{malformed_}}htlc() were not met");
4422+
}
4423+
// fail-backs are best-effort, we probably already have one
4424+
// pending, and if not that's OK, if not, the channel is on
4425+
// the chain and sending the HTLC-Timeout is their problem.
4426+
continue;
4427+
}
44334428
}
44344429
}
44354430
} else {

0 commit comments

Comments
 (0)