Skip to content

Commit 01e3742

Browse files
committed
Extract common HTLC failure handling into helper methods
In order to reduce code duplication for trampoline routing support, this commit extracts the following heper methods. - `get_htlc_failure_from_blinded_failure_forward`: builds htlc forward info with error details. - `fail_htlc_backwards_from_forward`: handles failure propagation.
1 parent df49139 commit 01e3742

File tree

1 file changed

+71
-45
lines changed

1 file changed

+71
-45
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 71 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8029,56 +8029,82 @@ where
80298029
&payment_hash,
80308030
onion_error
80318031
);
8032-
let failure = match blinded_failure {
8033-
Some(BlindedFailure::FromIntroductionNode) => {
8034-
let blinded_onion_error = HTLCFailReason::reason(
8035-
LocalHTLCFailureReason::InvalidOnionBlinding,
8036-
vec![0; 32],
8037-
);
8038-
let err_packet = blinded_onion_error.get_encrypted_failure_packet(
8039-
incoming_packet_shared_secret,
8040-
phantom_shared_secret,
8041-
);
8042-
HTLCForwardInfo::FailHTLC { htlc_id: *htlc_id, err_packet }
8043-
},
8044-
Some(BlindedFailure::FromBlindedNode) => HTLCForwardInfo::FailMalformedHTLC {
8045-
htlc_id: *htlc_id,
8046-
failure_code: LocalHTLCFailureReason::InvalidOnionBlinding.failure_code(),
8047-
sha256_of_onion: [0; 32],
8048-
},
8049-
None => {
8050-
let err_packet = onion_error.get_encrypted_failure_packet(
8051-
incoming_packet_shared_secret,
8052-
phantom_shared_secret,
8053-
);
8054-
HTLCForwardInfo::FailHTLC { htlc_id: *htlc_id, err_packet }
8055-
},
8056-
};
8057-
8058-
let mut forward_htlcs = self.forward_htlcs.lock().unwrap();
8059-
match forward_htlcs.entry(*short_channel_id) {
8060-
hash_map::Entry::Occupied(mut entry) => {
8061-
entry.get_mut().push(failure);
8062-
},
8063-
hash_map::Entry::Vacant(entry) => {
8064-
entry.insert(vec![failure]);
8065-
},
8066-
}
8067-
mem::drop(forward_htlcs);
8068-
let mut pending_events = self.pending_events.lock().unwrap();
8069-
pending_events.push_back((
8070-
events::Event::HTLCHandlingFailed {
8071-
prev_channel_id: *channel_id,
8072-
failure_type,
8073-
failure_reason: Some(onion_error.into()),
8074-
},
8075-
None,
8076-
));
8032+
let htlc_failure_reason = self.get_htlc_failure_from_blinded_failure_forward(
8033+
blinded_failure,
8034+
onion_error,
8035+
incoming_packet_shared_secret,
8036+
phantom_shared_secret,
8037+
htlc_id,
8038+
);
8039+
self.fail_htlc_backwards_from_forward(
8040+
&onion_error,
8041+
failure_type,
8042+
short_channel_id,
8043+
channel_id,
8044+
htlc_failure_reason,
8045+
);
80778046
},
80788047
HTLCSource::TrampolineForward { .. } => todo!(),
80798048
}
80808049
}
80818050

8051+
fn get_htlc_failure_from_blinded_failure_forward(
8052+
&self, blinded_failure: &Option<BlindedFailure>, onion_error: &HTLCFailReason,
8053+
incoming_packet_shared_secret: &[u8; 32], secondary_shared_secret: &Option<[u8; 32]>,
8054+
htlc_id: &u64,
8055+
) -> HTLCForwardInfo {
8056+
match blinded_failure {
8057+
Some(BlindedFailure::FromIntroductionNode) => {
8058+
let blinded_onion_error = HTLCFailReason::reason(
8059+
LocalHTLCFailureReason::InvalidOnionBlinding,
8060+
vec![0; 32],
8061+
);
8062+
let err_packet = blinded_onion_error.get_encrypted_failure_packet(
8063+
incoming_packet_shared_secret,
8064+
secondary_shared_secret,
8065+
);
8066+
HTLCForwardInfo::FailHTLC { htlc_id: *htlc_id, err_packet }
8067+
},
8068+
Some(BlindedFailure::FromBlindedNode) => HTLCForwardInfo::FailMalformedHTLC {
8069+
htlc_id: *htlc_id,
8070+
failure_code: LocalHTLCFailureReason::InvalidOnionBlinding.failure_code(),
8071+
sha256_of_onion: [0; 32],
8072+
},
8073+
None => {
8074+
let err_packet = onion_error.get_encrypted_failure_packet(
8075+
incoming_packet_shared_secret,
8076+
secondary_shared_secret,
8077+
);
8078+
HTLCForwardInfo::FailHTLC { htlc_id: *htlc_id, err_packet }
8079+
},
8080+
}
8081+
}
8082+
8083+
fn fail_htlc_backwards_from_forward(
8084+
&self, onion_error: &HTLCFailReason, failure_type: HTLCHandlingFailureType,
8085+
short_channel_id: &u64, channel_id: &ChannelId, failure: HTLCForwardInfo,
8086+
) {
8087+
let mut forward_htlcs = self.forward_htlcs.lock().unwrap();
8088+
match forward_htlcs.entry(*short_channel_id) {
8089+
hash_map::Entry::Occupied(mut entry) => {
8090+
entry.get_mut().push(failure);
8091+
},
8092+
hash_map::Entry::Vacant(entry) => {
8093+
entry.insert(vec![failure]);
8094+
},
8095+
}
8096+
mem::drop(forward_htlcs);
8097+
let mut pending_events = self.pending_events.lock().unwrap();
8098+
pending_events.push_back((
8099+
events::Event::HTLCHandlingFailed {
8100+
prev_channel_id: *channel_id,
8101+
failure_type,
8102+
failure_reason: Some(onion_error.into()),
8103+
},
8104+
None,
8105+
));
8106+
}
8107+
80828108
/// Provides a payment preimage in response to [`Event::PaymentClaimable`], generating any
80838109
/// [`MessageSendEvent`]s needed to claim the payment.
80848110
///

0 commit comments

Comments
 (0)