@@ -28,7 +28,7 @@ use crate::ln::msgs::{
28
28
OutboundOnionPayload , OutboundTrampolinePayload , MessageSendEvent ,
29
29
} ;
30
30
use crate :: ln:: wire:: Encode ;
31
- use crate :: util:: ser:: { Writeable , Writer , BigSize } ;
31
+ use crate :: util:: ser:: { BigSize , Writeable , Writer } ;
32
32
use crate :: util:: test_utils;
33
33
use crate :: util:: config:: { UserConfig , ChannelConfig , MaxDustHTLCExposure } ;
34
34
use crate :: util:: errors:: APIError ;
@@ -49,6 +49,8 @@ use crate::blinded_path::BlindedHop;
49
49
use crate :: ln:: functional_test_utils:: * ;
50
50
use crate :: ln:: onion_utils:: { construct_trampoline_onion_keys, construct_trampoline_onion_packet} ;
51
51
52
+ use super :: msgs:: OnionErrorPacket ;
53
+
52
54
fn run_onion_failure_test < F1 , F2 > ( _name : & str , test_case : u8 , nodes : & Vec < Node > , route : & Route , payment_hash : & PaymentHash , payment_secret : & PaymentSecret , callback_msg : F1 , callback_node : F2 , expected_retryable : bool , expected_error_code : Option < u16 > , expected_channel_update : Option < NetworkUpdate > , expected_short_channel_id : Option < u64 > , expected_htlc_destination : Option < HTLCDestination > )
53
55
where F1 : for < ' a > FnMut ( & ' a mut msgs:: UpdateAddHTLC ) ,
54
56
F2 : FnMut ( ) ,
@@ -409,15 +411,15 @@ fn test_onion_failure() {
409
411
// and tamper returning error message
410
412
let session_priv = SecretKey :: from_slice ( & [ 3 ; 32 ] ) . unwrap ( ) ;
411
413
let onion_keys = onion_utils:: construct_onion_keys ( & Secp256k1 :: new ( ) , & route. paths [ 0 ] , & session_priv) . unwrap ( ) ;
412
- msg. reason = onion_utils:: build_first_hop_failure_packet ( onion_keys[ 0 ] . shared_secret . as_ref ( ) , NODE |2 , & [ 0 ; 0 ] ) ;
414
+ msg. reason = onion_utils:: build_failure_packet ( onion_keys[ 0 ] . shared_secret . as_ref ( ) , NODE |2 , & [ 0 ; 0 ] ) ;
413
415
} , ||{ } , true , Some ( NODE |2 ) , Some ( NetworkUpdate :: NodeFailure { node_id : route. paths [ 0 ] . hops [ 0 ] . pubkey , is_permanent : false } ) , Some ( route. paths [ 0 ] . hops [ 0 ] . short_channel_id ) , Some ( next_hop_failure. clone ( ) ) ) ;
414
416
415
417
// final node failure
416
418
run_onion_failure_test_with_fail_intercept ( "temporary_node_failure" , 200 , & nodes, & route, & payment_hash, & payment_secret, |_msg| { } , |msg| {
417
419
// and tamper returning error message
418
420
let session_priv = SecretKey :: from_slice ( & [ 3 ; 32 ] ) . unwrap ( ) ;
419
421
let onion_keys = onion_utils:: construct_onion_keys ( & Secp256k1 :: new ( ) , & route. paths [ 0 ] , & session_priv) . unwrap ( ) ;
420
- msg. reason = onion_utils:: build_first_hop_failure_packet ( onion_keys[ 1 ] . shared_secret . as_ref ( ) , NODE |2 , & [ 0 ; 0 ] ) ;
422
+ msg. reason = onion_utils:: build_failure_packet ( onion_keys[ 1 ] . shared_secret . as_ref ( ) , NODE |2 , & [ 0 ; 0 ] ) ;
421
423
} , ||{
422
424
nodes[ 2 ] . node . fail_htlc_backwards ( & payment_hash) ;
423
425
} , true , Some ( NODE |2 ) , Some ( NetworkUpdate :: NodeFailure { node_id : route. paths [ 0 ] . hops [ 1 ] . pubkey , is_permanent : false } ) , Some ( route. paths [ 0 ] . hops [ 1 ] . short_channel_id ) , None ) ;
@@ -429,14 +431,14 @@ fn test_onion_failure() {
429
431
} , |msg| {
430
432
let session_priv = SecretKey :: from_slice ( & [ 3 ; 32 ] ) . unwrap ( ) ;
431
433
let onion_keys = onion_utils:: construct_onion_keys ( & Secp256k1 :: new ( ) , & route. paths [ 0 ] , & session_priv) . unwrap ( ) ;
432
- msg. reason = onion_utils:: build_first_hop_failure_packet ( onion_keys[ 0 ] . shared_secret . as_ref ( ) , PERM |NODE |2 , & [ 0 ; 0 ] ) ;
434
+ msg. reason = onion_utils:: build_failure_packet ( onion_keys[ 0 ] . shared_secret . as_ref ( ) , PERM |NODE |2 , & [ 0 ; 0 ] ) ;
433
435
} , ||{ } , true , Some ( PERM |NODE |2 ) , Some ( NetworkUpdate :: NodeFailure { node_id : route. paths [ 0 ] . hops [ 0 ] . pubkey , is_permanent : true } ) , Some ( route. paths [ 0 ] . hops [ 0 ] . short_channel_id ) , Some ( next_hop_failure. clone ( ) ) ) ;
434
436
435
437
// final node failure
436
438
run_onion_failure_test_with_fail_intercept ( "permanent_node_failure" , 200 , & nodes, & route, & payment_hash, & payment_secret, |_msg| { } , |msg| {
437
439
let session_priv = SecretKey :: from_slice ( & [ 3 ; 32 ] ) . unwrap ( ) ;
438
440
let onion_keys = onion_utils:: construct_onion_keys ( & Secp256k1 :: new ( ) , & route. paths [ 0 ] , & session_priv) . unwrap ( ) ;
439
- msg. reason = onion_utils:: build_first_hop_failure_packet ( onion_keys[ 1 ] . shared_secret . as_ref ( ) , PERM |NODE |2 , & [ 0 ; 0 ] ) ;
441
+ msg. reason = onion_utils:: build_failure_packet ( onion_keys[ 1 ] . shared_secret . as_ref ( ) , PERM |NODE |2 , & [ 0 ; 0 ] ) ;
440
442
} , ||{
441
443
nodes[ 2 ] . node . fail_htlc_backwards ( & payment_hash) ;
442
444
} , false , Some ( PERM |NODE |2 ) , Some ( NetworkUpdate :: NodeFailure { node_id : route. paths [ 0 ] . hops [ 1 ] . pubkey , is_permanent : true } ) , Some ( route. paths [ 0 ] . hops [ 1 ] . short_channel_id ) , None ) ;
@@ -448,7 +450,7 @@ fn test_onion_failure() {
448
450
} , |msg| {
449
451
let session_priv = SecretKey :: from_slice ( & [ 3 ; 32 ] ) . unwrap ( ) ;
450
452
let onion_keys = onion_utils:: construct_onion_keys ( & Secp256k1 :: new ( ) , & route. paths [ 0 ] , & session_priv) . unwrap ( ) ;
451
- msg. reason = onion_utils:: build_first_hop_failure_packet ( onion_keys[ 0 ] . shared_secret . as_ref ( ) , PERM |NODE |3 , & [ 0 ; 0 ] ) ;
453
+ msg. reason = onion_utils:: build_failure_packet ( onion_keys[ 0 ] . shared_secret . as_ref ( ) , PERM |NODE |3 , & [ 0 ; 0 ] ) ;
452
454
} , ||{
453
455
nodes[ 2 ] . node . fail_htlc_backwards ( & payment_hash) ;
454
456
} , true , Some ( PERM |NODE |3 ) , Some ( NetworkUpdate :: NodeFailure { node_id : route. paths [ 0 ] . hops [ 0 ] . pubkey , is_permanent : true } ) , Some ( route. paths [ 0 ] . hops [ 0 ] . short_channel_id ) , Some ( next_hop_failure. clone ( ) ) ) ;
@@ -457,7 +459,7 @@ fn test_onion_failure() {
457
459
run_onion_failure_test_with_fail_intercept ( "required_node_feature_missing" , 200 , & nodes, & route, & payment_hash, & payment_secret, |_msg| { } , |msg| {
458
460
let session_priv = SecretKey :: from_slice ( & [ 3 ; 32 ] ) . unwrap ( ) ;
459
461
let onion_keys = onion_utils:: construct_onion_keys ( & Secp256k1 :: new ( ) , & route. paths [ 0 ] , & session_priv) . unwrap ( ) ;
460
- msg. reason = onion_utils:: build_first_hop_failure_packet ( onion_keys[ 1 ] . shared_secret . as_ref ( ) , PERM |NODE |3 , & [ 0 ; 0 ] ) ;
462
+ msg. reason = onion_utils:: build_failure_packet ( onion_keys[ 1 ] . shared_secret . as_ref ( ) , PERM |NODE |3 , & [ 0 ; 0 ] ) ;
461
463
} , ||{
462
464
nodes[ 2 ] . node . fail_htlc_backwards ( & payment_hash) ;
463
465
} , false , Some ( PERM |NODE |3 ) , Some ( NetworkUpdate :: NodeFailure { node_id : route. paths [ 0 ] . hops [ 1 ] . pubkey , is_permanent : true } ) , Some ( route. paths [ 0 ] . hops [ 1 ] . short_channel_id ) , None ) ;
@@ -487,7 +489,7 @@ fn test_onion_failure() {
487
489
} , |msg| {
488
490
let session_priv = SecretKey :: from_slice ( & [ 3 ; 32 ] ) . unwrap ( ) ;
489
491
let onion_keys = onion_utils:: construct_onion_keys ( & Secp256k1 :: new ( ) , & route. paths [ 0 ] , & session_priv) . unwrap ( ) ;
490
- msg. reason = onion_utils:: build_first_hop_failure_packet ( onion_keys[ 0 ] . shared_secret . as_ref ( ) , UPDATE |7 , & err_data) ;
492
+ msg. reason = onion_utils:: build_failure_packet ( onion_keys[ 0 ] . shared_secret . as_ref ( ) , UPDATE |7 , & err_data) ;
491
493
} , ||{ } , true , Some ( UPDATE |7 ) ,
492
494
Some ( NetworkUpdate :: ChannelFailure { short_channel_id, is_permanent : false } ) ,
493
495
Some ( short_channel_id) , Some ( next_hop_failure. clone ( ) ) ) ;
@@ -499,7 +501,7 @@ fn test_onion_failure() {
499
501
} , |msg| {
500
502
let session_priv = SecretKey :: from_slice ( & [ 3 ; 32 ] ) . unwrap ( ) ;
501
503
let onion_keys = onion_utils:: construct_onion_keys ( & Secp256k1 :: new ( ) , & route. paths [ 0 ] , & session_priv) . unwrap ( ) ;
502
- msg. reason = onion_utils:: build_first_hop_failure_packet ( onion_keys[ 0 ] . shared_secret . as_ref ( ) , UPDATE |7 , & err_data_without_type) ;
504
+ msg. reason = onion_utils:: build_failure_packet ( onion_keys[ 0 ] . shared_secret . as_ref ( ) , UPDATE |7 , & err_data_without_type) ;
503
505
} , ||{ } , true , Some ( UPDATE |7 ) ,
504
506
Some ( NetworkUpdate :: ChannelFailure { short_channel_id, is_permanent : false } ) ,
505
507
Some ( short_channel_id) , Some ( next_hop_failure. clone ( ) ) ) ;
@@ -510,7 +512,7 @@ fn test_onion_failure() {
510
512
} , |msg| {
511
513
let session_priv = SecretKey :: from_slice ( & [ 3 ; 32 ] ) . unwrap ( ) ;
512
514
let onion_keys = onion_utils:: construct_onion_keys ( & Secp256k1 :: new ( ) , & route. paths [ 0 ] , & session_priv) . unwrap ( ) ;
513
- msg. reason = onion_utils:: build_first_hop_failure_packet ( onion_keys[ 0 ] . shared_secret . as_ref ( ) , PERM |8 , & [ 0 ; 0 ] ) ;
515
+ msg. reason = onion_utils:: build_failure_packet ( onion_keys[ 0 ] . shared_secret . as_ref ( ) , PERM |8 , & [ 0 ; 0 ] ) ;
514
516
// short_channel_id from the processing node
515
517
} , ||{ } , true , Some ( PERM |8 ) , Some ( NetworkUpdate :: ChannelFailure { short_channel_id, is_permanent : true } ) , Some ( short_channel_id) , Some ( next_hop_failure. clone ( ) ) ) ;
516
518
@@ -520,7 +522,7 @@ fn test_onion_failure() {
520
522
} , |msg| {
521
523
let session_priv = SecretKey :: from_slice ( & [ 3 ; 32 ] ) . unwrap ( ) ;
522
524
let onion_keys = onion_utils:: construct_onion_keys ( & Secp256k1 :: new ( ) , & route. paths [ 0 ] , & session_priv) . unwrap ( ) ;
523
- msg. reason = onion_utils:: build_first_hop_failure_packet ( onion_keys[ 0 ] . shared_secret . as_ref ( ) , PERM |9 , & [ 0 ; 0 ] ) ;
525
+ msg. reason = onion_utils:: build_failure_packet ( onion_keys[ 0 ] . shared_secret . as_ref ( ) , PERM |9 , & [ 0 ; 0 ] ) ;
524
526
// short_channel_id from the processing node
525
527
} , ||{ } , true , Some ( PERM |9 ) , Some ( NetworkUpdate :: ChannelFailure { short_channel_id, is_permanent : true } ) , Some ( short_channel_id) , Some ( next_hop_failure. clone ( ) ) ) ;
526
528
@@ -652,7 +654,7 @@ fn test_onion_failure() {
652
654
// Tamper returning error message
653
655
let session_priv = SecretKey :: from_slice ( & [ 3 ; 32 ] ) . unwrap ( ) ;
654
656
let onion_keys = onion_utils:: construct_onion_keys ( & Secp256k1 :: new ( ) , & route. paths [ 0 ] , & session_priv) . unwrap ( ) ;
655
- msg. reason = onion_utils:: build_first_hop_failure_packet ( onion_keys[ 1 ] . shared_secret . as_ref ( ) , 23 , & [ 0 ; 0 ] ) ;
657
+ msg. reason = onion_utils:: build_failure_packet ( onion_keys[ 1 ] . shared_secret . as_ref ( ) , 23 , & [ 0 ; 0 ] ) ;
656
658
} , ||{
657
659
nodes[ 2 ] . node . fail_htlc_backwards ( & payment_hash) ;
658
660
} , true , Some ( 23 ) , None , None , None ) ;
@@ -670,8 +672,12 @@ fn test_onion_failure() {
670
672
let mut hmac = HmacEngine :: < Sha256 > :: new ( & um) ;
671
673
hmac. input ( & decoded_err_packet. encode ( ) [ 32 ..] ) ;
672
674
decoded_err_packet. hmac = Hmac :: from_engine ( hmac) . to_byte_array ( ) ;
673
- msg. reason = onion_utils:: encrypt_failure_packet (
674
- & onion_keys[ 1 ] . shared_secret . as_ref ( ) , & decoded_err_packet. encode ( ) [ ..] )
675
+ let mut onion_error = OnionErrorPacket {
676
+ data : decoded_err_packet. encode ( ) ,
677
+ } ;
678
+ onion_utils:: test_crypt_failure_packet (
679
+ & onion_keys[ 1 ] . shared_secret . as_ref ( ) , & mut onion_error) ;
680
+ msg. reason = onion_error;
675
681
} , || nodes[ 2 ] . node . fail_htlc_backwards ( & payment_hash) , false , None ,
676
682
Some ( NetworkUpdate :: NodeFailure { node_id : route. paths [ 0 ] . hops [ 1 ] . pubkey , is_permanent : true } ) ,
677
683
Some ( channels[ 1 ] . 0 . contents . short_channel_id ) , None ) ;
@@ -693,8 +699,12 @@ fn test_onion_failure() {
693
699
let mut hmac = HmacEngine :: < Sha256 > :: new ( & um) ;
694
700
hmac. input ( & decoded_err_packet. encode ( ) [ 32 ..] ) ;
695
701
decoded_err_packet. hmac = Hmac :: from_engine ( hmac) . to_byte_array ( ) ;
696
- msg. reason = onion_utils:: encrypt_failure_packet (
697
- & onion_keys[ 0 ] . shared_secret . as_ref ( ) , & decoded_err_packet. encode ( ) [ ..] )
702
+ let mut onion_error = OnionErrorPacket {
703
+ data : decoded_err_packet. encode ( ) ,
704
+ } ;
705
+ onion_utils:: test_crypt_failure_packet (
706
+ & onion_keys[ 0 ] . shared_secret . as_ref ( ) , & mut onion_error) ;
707
+ msg. reason = onion_error;
698
708
} , || { } , true , Some ( 0x1000 |7 ) ,
699
709
Some ( NetworkUpdate :: ChannelFailure {
700
710
short_channel_id : channels[ 1 ] . 0 . contents . short_channel_id ,
@@ -717,8 +727,12 @@ fn test_onion_failure() {
717
727
let mut hmac = HmacEngine :: < Sha256 > :: new ( & um) ;
718
728
hmac. input ( & decoded_err_packet. encode ( ) [ 32 ..] ) ;
719
729
decoded_err_packet. hmac = Hmac :: from_engine ( hmac) . to_byte_array ( ) ;
720
- msg. reason = onion_utils:: encrypt_failure_packet (
721
- & onion_keys[ 1 ] . shared_secret . as_ref ( ) , & decoded_err_packet. encode ( ) [ ..] )
730
+ let mut onion_error = OnionErrorPacket {
731
+ data : decoded_err_packet. encode ( ) ,
732
+ } ;
733
+ onion_utils:: test_crypt_failure_packet (
734
+ & onion_keys[ 1 ] . shared_secret . as_ref ( ) , & mut onion_error) ;
735
+ msg. reason = onion_error;
722
736
} , || nodes[ 2 ] . node . fail_htlc_backwards ( & payment_hash) , true , Some ( 0x1000 |7 ) ,
723
737
Some ( NetworkUpdate :: ChannelFailure {
724
738
short_channel_id : channels[ 1 ] . 0 . contents . short_channel_id ,
0 commit comments