Skip to content

Commit 98eb104

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 fa2c4c5 commit 98eb104

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
@@ -7923,56 +7923,82 @@ where
79237923
&payment_hash,
79247924
onion_error
79257925
);
7926-
let failure = match blinded_failure {
7927-
Some(BlindedFailure::FromIntroductionNode) => {
7928-
let blinded_onion_error = HTLCFailReason::reason(
7929-
LocalHTLCFailureReason::InvalidOnionBlinding,
7930-
vec![0; 32],
7931-
);
7932-
let err_packet = blinded_onion_error.get_encrypted_failure_packet(
7933-
incoming_packet_shared_secret,
7934-
phantom_shared_secret,
7935-
);
7936-
HTLCForwardInfo::FailHTLC { htlc_id: *htlc_id, err_packet }
7937-
},
7938-
Some(BlindedFailure::FromBlindedNode) => HTLCForwardInfo::FailMalformedHTLC {
7939-
htlc_id: *htlc_id,
7940-
failure_code: LocalHTLCFailureReason::InvalidOnionBlinding.failure_code(),
7941-
sha256_of_onion: [0; 32],
7942-
},
7943-
None => {
7944-
let err_packet = onion_error.get_encrypted_failure_packet(
7945-
incoming_packet_shared_secret,
7946-
phantom_shared_secret,
7947-
);
7948-
HTLCForwardInfo::FailHTLC { htlc_id: *htlc_id, err_packet }
7949-
},
7950-
};
7951-
7952-
let mut forward_htlcs = self.forward_htlcs.lock().unwrap();
7953-
match forward_htlcs.entry(*short_channel_id) {
7954-
hash_map::Entry::Occupied(mut entry) => {
7955-
entry.get_mut().push(failure);
7956-
},
7957-
hash_map::Entry::Vacant(entry) => {
7958-
entry.insert(vec![failure]);
7959-
},
7960-
}
7961-
mem::drop(forward_htlcs);
7962-
let mut pending_events = self.pending_events.lock().unwrap();
7963-
pending_events.push_back((
7964-
events::Event::HTLCHandlingFailed {
7965-
prev_channel_id: *channel_id,
7966-
failure_type,
7967-
failure_reason: Some(onion_error.into()),
7968-
},
7969-
None,
7970-
));
7926+
let htlc_failure_reason = self.get_htlc_failure_from_blinded_failure_forward(
7927+
blinded_failure,
7928+
onion_error,
7929+
incoming_packet_shared_secret,
7930+
phantom_shared_secret,
7931+
htlc_id,
7932+
);
7933+
self.fail_htlc_backwards_from_forward(
7934+
&onion_error,
7935+
failure_type,
7936+
short_channel_id,
7937+
channel_id,
7938+
htlc_failure_reason,
7939+
);
79717940
},
79727941
HTLCSource::TrampolineForward { .. } => todo!(),
79737942
}
79747943
}
79757944

7945+
fn get_htlc_failure_from_blinded_failure_forward(
7946+
&self, blinded_failure: &Option<BlindedFailure>, onion_error: &HTLCFailReason,
7947+
incoming_packet_shared_secret: &[u8; 32], secondary_shared_secret: &Option<[u8; 32]>,
7948+
htlc_id: &u64,
7949+
) -> HTLCForwardInfo {
7950+
match blinded_failure {
7951+
Some(BlindedFailure::FromIntroductionNode) => {
7952+
let blinded_onion_error = HTLCFailReason::reason(
7953+
LocalHTLCFailureReason::InvalidOnionBlinding,
7954+
vec![0; 32],
7955+
);
7956+
let err_packet = blinded_onion_error.get_encrypted_failure_packet(
7957+
incoming_packet_shared_secret,
7958+
secondary_shared_secret,
7959+
);
7960+
HTLCForwardInfo::FailHTLC { htlc_id: *htlc_id, err_packet }
7961+
},
7962+
Some(BlindedFailure::FromBlindedNode) => HTLCForwardInfo::FailMalformedHTLC {
7963+
htlc_id: *htlc_id,
7964+
failure_code: LocalHTLCFailureReason::InvalidOnionBlinding.failure_code(),
7965+
sha256_of_onion: [0; 32],
7966+
},
7967+
None => {
7968+
let err_packet = onion_error.get_encrypted_failure_packet(
7969+
incoming_packet_shared_secret,
7970+
secondary_shared_secret,
7971+
);
7972+
HTLCForwardInfo::FailHTLC { htlc_id: *htlc_id, err_packet }
7973+
},
7974+
}
7975+
}
7976+
7977+
fn fail_htlc_backwards_from_forward(
7978+
&self, onion_error: &HTLCFailReason, failure_type: HTLCHandlingFailureType,
7979+
short_channel_id: &u64, channel_id: &ChannelId, failure: HTLCForwardInfo,
7980+
) {
7981+
let mut forward_htlcs = self.forward_htlcs.lock().unwrap();
7982+
match forward_htlcs.entry(*short_channel_id) {
7983+
hash_map::Entry::Occupied(mut entry) => {
7984+
entry.get_mut().push(failure);
7985+
},
7986+
hash_map::Entry::Vacant(entry) => {
7987+
entry.insert(vec![failure]);
7988+
},
7989+
}
7990+
mem::drop(forward_htlcs);
7991+
let mut pending_events = self.pending_events.lock().unwrap();
7992+
pending_events.push_back((
7993+
events::Event::HTLCHandlingFailed {
7994+
prev_channel_id: *channel_id,
7995+
failure_type,
7996+
failure_reason: Some(onion_error.into()),
7997+
},
7998+
None,
7999+
));
8000+
}
8001+
79768002
/// Provides a payment preimage in response to [`Event::PaymentClaimable`], generating any
79778003
/// [`MessageSendEvent`]s needed to claim the payment.
79788004
///

0 commit comments

Comments
 (0)