@@ -287,6 +287,19 @@ pub(super) enum HTLCFailReason {
287
287
}
288
288
}
289
289
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
+
290
303
impl HTLCFailReason {
291
304
pub ( super ) fn reason ( failure_code : u16 , data : Vec < u8 > ) -> Self {
292
305
Self :: Reason { failure_code, data }
@@ -295,6 +308,24 @@ impl HTLCFailReason {
295
308
pub ( super ) fn from_failure_code ( failure_code : u16 ) -> Self {
296
309
Self :: Reason { failure_code, data : Vec :: new ( ) }
297
310
}
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
+ }
298
329
}
299
330
300
331
struct ReceiveError {
@@ -4140,23 +4171,8 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4140
4171
if let Some ( ev) = full_failure_ev { pending_events. push ( ev) ; }
4141
4172
} ,
4142
4173
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) ;
4160
4176
4161
4177
let mut forward_event = None ;
4162
4178
let mut forward_htlcs = self . forward_htlcs . lock ( ) . unwrap ( ) ;
0 commit comments