Skip to content

Commit 9a2e26b

Browse files
committed
Encode HTLC failure packets in a util method on HTLCFailReason
1 parent 4dafa43 commit 9a2e26b

File tree

1 file changed

+33
-17
lines changed

1 file changed

+33
-17
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,19 @@ pub(super) enum HTLCFailReason {
287287
}
288288
}
289289

290+
impl core::fmt::Debug for HTLCFailReason {
291+
fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
292+
match self {
293+
HTLCFailReason::Reason { ref failure_code, .. } => {
294+
write!(f, "HTLC error code {}", failure_code)
295+
},
296+
HTLCFailReason::LightningError { .. } => {
297+
write!(f, "pre-built LightningError")
298+
}
299+
}
300+
}
301+
}
302+
290303
impl HTLCFailReason {
291304
pub(super) fn reason(failure_code: u16, data: Vec<u8>) -> Self {
292305
Self::Reason { failure_code, data }
@@ -295,6 +308,24 @@ impl HTLCFailReason {
295308
pub(super) fn from_failure_code(failure_code: u16) -> Self {
296309
Self::Reason { failure_code, data: Vec::new() }
297310
}
311+
312+
fn get_encrypted_failure_packet(&self, incoming_packet_shared_secret: &[u8; 32], phantom_shared_secret: &Option<[u8; 32]>) -> msgs::OnionErrorPacket {
313+
match self {
314+
HTLCFailReason::Reason { ref failure_code, ref data } => {
315+
if let Some(phantom_ss) = phantom_shared_secret {
316+
let phantom_packet = onion_utils::build_failure_packet(phantom_ss, *failure_code, &data[..]).encode();
317+
let encrypted_phantom_packet = onion_utils::encrypt_failure_packet(phantom_ss, &phantom_packet);
318+
onion_utils::encrypt_failure_packet(incoming_packet_shared_secret, &encrypted_phantom_packet.data[..])
319+
} else {
320+
let packet = onion_utils::build_failure_packet(incoming_packet_shared_secret, *failure_code, &data[..]).encode();
321+
onion_utils::encrypt_failure_packet(incoming_packet_shared_secret, &packet)
322+
}
323+
},
324+
HTLCFailReason::LightningError { err } => {
325+
onion_utils::encrypt_failure_packet(incoming_packet_shared_secret, &err.data)
326+
}
327+
}
328+
}
298329
}
299330

300331
struct ReceiveError {
@@ -4140,23 +4171,8 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
41404171
if let Some(ev) = full_failure_ev { pending_events.push(ev); }
41414172
},
41424173
HTLCSource::PreviousHopData(HTLCPreviousHopData { ref short_channel_id, ref htlc_id, ref incoming_packet_shared_secret, ref phantom_shared_secret, ref outpoint }) => {
4143-
let err_packet = match onion_error {
4144-
HTLCFailReason::Reason { ref failure_code, ref data } => {
4145-
log_trace!(self.logger, "Failing HTLC with payment_hash {} backwards from us with code {}", log_bytes!(payment_hash.0), failure_code);
4146-
if let Some(phantom_ss) = phantom_shared_secret {
4147-
let phantom_packet = onion_utils::build_failure_packet(phantom_ss, *failure_code, &data[..]).encode();
4148-
let encrypted_phantom_packet = onion_utils::encrypt_failure_packet(phantom_ss, &phantom_packet);
4149-
onion_utils::encrypt_failure_packet(incoming_packet_shared_secret, &encrypted_phantom_packet.data[..])
4150-
} else {
4151-
let packet = onion_utils::build_failure_packet(incoming_packet_shared_secret, *failure_code, &data[..]).encode();
4152-
onion_utils::encrypt_failure_packet(incoming_packet_shared_secret, &packet)
4153-
}
4154-
},
4155-
HTLCFailReason::LightningError { err } => {
4156-
log_trace!(self.logger, "Failing HTLC with payment_hash {} backwards with pre-built LightningError", log_bytes!(payment_hash.0));
4157-
onion_utils::encrypt_failure_packet(incoming_packet_shared_secret, &err.data)
4158-
}
4159-
};
4174+
log_trace!(self.logger, "Failing HTLC with payment_hash {} backwards from us with {:?}", log_bytes!(payment_hash.0), onion_error);
4175+
let err_packet = onion_error.get_encrypted_failure_packet(incoming_packet_shared_secret, phantom_shared_secret);
41604176

41614177
let mut forward_event = None;
41624178
let mut forward_htlcs = self.forward_htlcs.lock().unwrap();

0 commit comments

Comments
 (0)