@@ -78,6 +78,7 @@ enum HTLCFailReason<'a> {
78
78
} ,
79
79
Reason {
80
80
failure_code : u16 ,
81
+ data : & ' a [ u8 ] ,
81
82
}
82
83
}
83
84
@@ -571,6 +572,7 @@ impl ChannelManager {
571
572
572
573
pub fn process_pending_htlc_forward ( & self ) {
573
574
let mut new_events = Vec :: new ( ) ;
575
+ let mut failed_forwards = Vec :: new ( ) ;
574
576
{
575
577
let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
576
578
let channel_state = channel_state_lock. borrow_parts ( ) ;
@@ -584,6 +586,10 @@ impl ChannelManager {
584
586
let forward_chan_id = match channel_state. short_to_id . get ( & short_chan_id) {
585
587
Some ( chan_id) => chan_id. clone ( ) ,
586
588
None => {
589
+ failed_forwards. reserve ( pending_forwards. len ( ) ) ;
590
+ for forward_info in pending_forwards {
591
+ failed_forwards. push ( ( forward_info. payment_hash , 0x4000 | 10 , None ) ) ;
592
+ }
587
593
// TODO: Send a failure packet back on each pending_forward
588
594
continue ;
589
595
}
@@ -594,7 +600,8 @@ impl ChannelManager {
594
600
for forward_info in pending_forwards {
595
601
match forward_chan. send_htlc ( forward_info. amt_to_forward , forward_info. payment_hash , forward_info. outgoing_cltv_value , forward_info. onion_packet . unwrap ( ) ) {
596
602
Err ( _e) => {
597
- // TODO: Send a failure packet back
603
+ let chan_update = self . get_channel_update ( forward_chan) . unwrap ( ) ;
604
+ failed_forwards. push ( ( forward_info. payment_hash , 0x4000 | 7 , Some ( chan_update) ) ) ;
598
605
continue ;
599
606
} ,
600
607
Ok ( update_add) => {
@@ -639,6 +646,13 @@ impl ChannelManager {
639
646
}
640
647
}
641
648
649
+ for failed_forward in failed_forwards. drain ( ..) {
650
+ match failed_forward. 2 {
651
+ None => self . fail_htlc_backwards_internal ( self . channel_state . lock ( ) . unwrap ( ) , & failed_forward. 0 , HTLCFailReason :: Reason { failure_code : failed_forward. 1 , data : & [ 0 ; 0 ] } ) ,
652
+ Some ( chan_update) => self . fail_htlc_backwards_internal ( self . channel_state . lock ( ) . unwrap ( ) , & failed_forward. 0 , HTLCFailReason :: Reason { failure_code : failed_forward. 1 , data : & chan_update. encode ( ) [ ..] } ) ,
653
+ } ;
654
+ }
655
+
642
656
if new_events. is_empty ( ) { return }
643
657
644
658
let mut events = self . pending_events . lock ( ) . unwrap ( ) ;
@@ -650,7 +664,7 @@ impl ChannelManager {
650
664
651
665
/// Indicates that the preimage for payment_hash is unknown after a PaymentReceived event.
652
666
pub fn fail_htlc_backwards ( & self , payment_hash : & [ u8 ; 32 ] ) -> bool {
653
- self . fail_htlc_backwards_internal ( self . channel_state . lock ( ) . unwrap ( ) , payment_hash, HTLCFailReason :: Reason { failure_code : 0x4000 | 15 } )
667
+ self . fail_htlc_backwards_internal ( self . channel_state . lock ( ) . unwrap ( ) , payment_hash, HTLCFailReason :: Reason { failure_code : 0x4000 | 15 , data : & [ 0 ; 0 ] } )
654
668
}
655
669
656
670
fn fail_htlc_backwards_internal ( & self , mut channel_state : MutexGuard < ChannelHolder > , payment_hash : & [ u8 ; 32 ] , onion_error : HTLCFailReason ) -> bool {
@@ -681,8 +695,8 @@ impl ChannelManager {
681
695
} ,
682
696
PendingOutboundHTLC :: IntermediaryHopData { source_short_channel_id, incoming_packet_shared_secret } => {
683
697
let err_packet = match onion_error {
684
- HTLCFailReason :: Reason { failure_code } => {
685
- let packet = ChannelManager :: build_failure_packet ( & incoming_packet_shared_secret, failure_code, & [ 0 ; 0 ] ) . encode ( ) ;
698
+ HTLCFailReason :: Reason { failure_code, data } => {
699
+ let packet = ChannelManager :: build_failure_packet ( & incoming_packet_shared_secret, failure_code, data ) . encode ( ) ;
686
700
ChannelManager :: encrypt_failure_packet ( & incoming_packet_shared_secret, & packet)
687
701
} ,
688
702
HTLCFailReason :: ErrorPacket { err } => {
@@ -1244,7 +1258,7 @@ impl ChannelMessageHandler for ChannelManager {
1244
1258
} ,
1245
1259
None => return Err ( HandleError { err : "Failed to find corresponding channel" , msg : None } )
1246
1260
}
1247
- self . fail_htlc_backwards_internal ( channel_state, & res. 0 , HTLCFailReason :: Reason { failure_code : msg. failure_code } ) ;
1261
+ self . fail_htlc_backwards_internal ( channel_state, & res. 0 , HTLCFailReason :: Reason { failure_code : msg. failure_code , data : & [ 0 ; 0 ] } ) ;
1248
1262
Ok ( res. 1 )
1249
1263
}
1250
1264
0 commit comments