@@ -3511,31 +3511,48 @@ where
3511
3511
for htlc in sources. drain ( ..) {
3512
3512
let source = HTLCSource :: PreviousHopData ( htlc. prev_hop . clone ( ) ) ;
3513
3513
let receiver = HTLCDestination :: FailedPayment { payment_hash : * payment_hash } ;
3514
+ let reason = self . get_htlc_fail_reason_from_failure_code ( failure_code, & htlc) ;
3515
+ self . fail_htlc_backwards_internal ( & source, & payment_hash, & reason, receiver) ;
3516
+ }
3517
+ }
3518
+ }
3514
3519
3515
- let reason = match failure_code {
3516
- FailureCode :: TemporaryNodeFailure => HTLCFailReason :: from_failure_code ( * failure_code as u16 ) ,
3517
- FailureCode :: RequiredNodeFeatureMissing => HTLCFailReason :: from_failure_code ( * failure_code as u16 ) ,
3518
- FailureCode :: ExpiryTooSoon => {
3519
- let short_channel_id = htlc. prev_hop . short_channel_id ;
3520
- let ( counterparty_node_id, chan_id) = self . short_to_chan_info . read ( ) . unwrap ( ) . get ( & short_channel_id) . cloned ( ) . unwrap ( ) ;
3521
- let per_peer_state = self . per_peer_state . read ( ) . unwrap ( ) ;
3522
- let peer_state = per_peer_state. get ( & counterparty_node_id) . unwrap ( ) . lock ( ) . unwrap ( ) ;
3523
- let chan = peer_state. channel_by_id . get ( & chan_id) . unwrap ( ) ;
3524
- let channel_update = self . get_channel_update_for_onion ( short_channel_id, chan) . unwrap ( ) ;
3525
- let mut channel_update_data = Vec :: new ( ) ;
3526
- ( channel_update. serialized_length ( ) as u16 + 2 ) . write ( & mut channel_update_data) . expect ( "Writes cannot fail" ) ;
3527
- msgs:: ChannelUpdate :: TYPE . write ( & mut channel_update_data) . expect ( "Writes cannot fail" ) ;
3528
- channel_update. write ( & mut channel_update_data) . expect ( "Writes cannot fail" ) ;
3529
- HTLCFailReason :: reason ( * failure_code as u16 , channel_update_data)
3530
- } ,
3531
- FailureCode :: IncorrectOrUnknownPaymentDetails => {
3532
- let mut htlc_msat_height_data = htlc. value . to_be_bytes ( ) . to_vec ( ) ;
3533
- htlc_msat_height_data. extend_from_slice ( & self . best_block . read ( ) . unwrap ( ) . height ( ) . to_be_bytes ( ) ) ;
3534
- HTLCFailReason :: reason ( * failure_code as u16 , htlc_msat_height_data)
3520
+ /// Gets error data to form an [`HTLCFailReason`] given a [`FailureCode`] and [`ClaimableHTLC`].
3521
+ /// If something goes wrong while finding the necessary error data, this defaults to getting
3522
+ /// error data for [`FailureCode::IncorrectOrUnknownPaymentDetails`].
3523
+ fn get_htlc_fail_reason_from_failure_code ( & self , failure_code : & FailureCode , htlc : & ClaimableHTLC ) -> HTLCFailReason {
3524
+ match failure_code {
3525
+ FailureCode :: TemporaryNodeFailure => HTLCFailReason :: from_failure_code ( * failure_code as u16 ) ,
3526
+ FailureCode :: RequiredNodeFeatureMissing => HTLCFailReason :: from_failure_code ( * failure_code as u16 ) ,
3527
+ FailureCode :: ExpiryTooSoon => {
3528
+ let mut expiry_fail_reason = None ;
3529
+ let short_channel_id = htlc. prev_hop . short_channel_id ;
3530
+ let cp_node_chan_id = self . short_to_chan_info . read ( ) . unwrap ( ) . get ( & short_channel_id) . cloned ( ) ;
3531
+ if let Some ( ( counterparty_node_id, chan_id) ) = cp_node_chan_id {
3532
+ let per_peer_state = self . per_peer_state . read ( ) . unwrap ( ) ;
3533
+ if let Some ( peer_state) = per_peer_state. get ( & counterparty_node_id) {
3534
+ let peer_state = peer_state. lock ( ) . unwrap ( ) ;
3535
+ if let Some ( chan) = peer_state. channel_by_id . get ( & chan_id) {
3536
+ if let Ok ( channel_update) = self . get_channel_update_for_onion ( short_channel_id, chan) {
3537
+ let mut channel_update_data = Vec :: new ( ) ;
3538
+ ( channel_update. serialized_length ( ) as u16 + 2 ) . write ( & mut channel_update_data) . expect ( "Writes cannot fail" ) ;
3539
+ msgs:: ChannelUpdate :: TYPE . write ( & mut channel_update_data) . expect ( "Writes cannot fail" ) ;
3540
+ channel_update. write ( & mut channel_update_data) . expect ( "Writes cannot fail" ) ;
3541
+ expiry_fail_reason = Some ( HTLCFailReason :: reason ( * failure_code as u16 , channel_update_data) ) ;
3542
+ }
3543
+ }
3535
3544
}
3536
- } ;
3537
-
3538
- self . fail_htlc_backwards_internal ( & source, & payment_hash, & reason, receiver) ;
3545
+ }
3546
+ if let Some ( expiry_fail_reason) = expiry_fail_reason {
3547
+ expiry_fail_reason
3548
+ } else {
3549
+ self . get_htlc_fail_reason_from_failure_code ( & FailureCode :: IncorrectOrUnknownPaymentDetails , htlc)
3550
+ }
3551
+ } ,
3552
+ FailureCode :: IncorrectOrUnknownPaymentDetails => {
3553
+ let mut htlc_msat_height_data = htlc. value . to_be_bytes ( ) . to_vec ( ) ;
3554
+ htlc_msat_height_data. extend_from_slice ( & self . best_block . read ( ) . unwrap ( ) . height ( ) . to_be_bytes ( ) ) ;
3555
+ HTLCFailReason :: reason ( * failure_code as u16 , htlc_msat_height_data)
3539
3556
}
3540
3557
}
3541
3558
}
0 commit comments