@@ -28,7 +28,7 @@ use crate::ln::msgs::{
2828 OutboundOnionPayload , OutboundTrampolinePayload , MessageSendEvent ,
2929} ;
3030use crate :: ln:: wire:: Encode ;
31- use crate :: util:: ser:: { Writeable , Writer , BigSize } ;
31+ use crate :: util:: ser:: { BigSize , Writeable , Writer } ;
3232use crate :: util:: test_utils;
3333use crate :: util:: config:: { UserConfig , ChannelConfig , MaxDustHTLCExposure } ;
3434use crate :: util:: errors:: APIError ;
@@ -49,6 +49,8 @@ use crate::blinded_path::BlindedHop;
4949use crate :: ln:: functional_test_utils:: * ;
5050use crate :: ln:: onion_utils:: { construct_trampoline_onion_keys, construct_trampoline_onion_packet} ;
5151
52+ use super :: msgs:: OnionErrorPacket ;
53+
5254fn 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 > )
5355 where F1 : for < ' a > FnMut ( & ' a mut msgs:: UpdateAddHTLC ) ,
5456 F2 : FnMut ( ) ,
@@ -409,15 +411,15 @@ fn test_onion_failure() {
409411 // and tamper returning error message
410412 let session_priv = SecretKey :: from_slice ( & [ 3 ; 32 ] ) . unwrap ( ) ;
411413 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 ] ) ;
413415 } , ||{ } , 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 ( ) ) ) ;
414416
415417 // final node failure
416418 run_onion_failure_test_with_fail_intercept ( "temporary_node_failure" , 200 , & nodes, & route, & payment_hash, & payment_secret, |_msg| { } , |msg| {
417419 // and tamper returning error message
418420 let session_priv = SecretKey :: from_slice ( & [ 3 ; 32 ] ) . unwrap ( ) ;
419421 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 ] ) ;
421423 } , ||{
422424 nodes[ 2 ] . node . fail_htlc_backwards ( & payment_hash) ;
423425 } , 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() {
429431 } , |msg| {
430432 let session_priv = SecretKey :: from_slice ( & [ 3 ; 32 ] ) . unwrap ( ) ;
431433 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 ] ) ;
433435 } , ||{ } , 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 ( ) ) ) ;
434436
435437 // final node failure
436438 run_onion_failure_test_with_fail_intercept ( "permanent_node_failure" , 200 , & nodes, & route, & payment_hash, & payment_secret, |_msg| { } , |msg| {
437439 let session_priv = SecretKey :: from_slice ( & [ 3 ; 32 ] ) . unwrap ( ) ;
438440 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 ] ) ;
440442 } , ||{
441443 nodes[ 2 ] . node . fail_htlc_backwards ( & payment_hash) ;
442444 } , 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() {
448450 } , |msg| {
449451 let session_priv = SecretKey :: from_slice ( & [ 3 ; 32 ] ) . unwrap ( ) ;
450452 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 ] ) ;
452454 } , ||{
453455 nodes[ 2 ] . node . fail_htlc_backwards ( & payment_hash) ;
454456 } , 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() {
457459 run_onion_failure_test_with_fail_intercept ( "required_node_feature_missing" , 200 , & nodes, & route, & payment_hash, & payment_secret, |_msg| { } , |msg| {
458460 let session_priv = SecretKey :: from_slice ( & [ 3 ; 32 ] ) . unwrap ( ) ;
459461 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 ] ) ;
461463 } , ||{
462464 nodes[ 2 ] . node . fail_htlc_backwards ( & payment_hash) ;
463465 } , 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() {
487489 } , |msg| {
488490 let session_priv = SecretKey :: from_slice ( & [ 3 ; 32 ] ) . unwrap ( ) ;
489491 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) ;
491493 } , ||{ } , true , Some ( UPDATE |7 ) ,
492494 Some ( NetworkUpdate :: ChannelFailure { short_channel_id, is_permanent : false } ) ,
493495 Some ( short_channel_id) , Some ( next_hop_failure. clone ( ) ) ) ;
@@ -499,7 +501,7 @@ fn test_onion_failure() {
499501 } , |msg| {
500502 let session_priv = SecretKey :: from_slice ( & [ 3 ; 32 ] ) . unwrap ( ) ;
501503 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) ;
503505 } , ||{ } , true , Some ( UPDATE |7 ) ,
504506 Some ( NetworkUpdate :: ChannelFailure { short_channel_id, is_permanent : false } ) ,
505507 Some ( short_channel_id) , Some ( next_hop_failure. clone ( ) ) ) ;
@@ -510,7 +512,7 @@ fn test_onion_failure() {
510512 } , |msg| {
511513 let session_priv = SecretKey :: from_slice ( & [ 3 ; 32 ] ) . unwrap ( ) ;
512514 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 ] ) ;
514516 // short_channel_id from the processing node
515517 } , ||{ } , true , Some ( PERM |8 ) , Some ( NetworkUpdate :: ChannelFailure { short_channel_id, is_permanent : true } ) , Some ( short_channel_id) , Some ( next_hop_failure. clone ( ) ) ) ;
516518
@@ -520,7 +522,7 @@ fn test_onion_failure() {
520522 } , |msg| {
521523 let session_priv = SecretKey :: from_slice ( & [ 3 ; 32 ] ) . unwrap ( ) ;
522524 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 ] ) ;
524526 // short_channel_id from the processing node
525527 } , ||{ } , true , Some ( PERM |9 ) , Some ( NetworkUpdate :: ChannelFailure { short_channel_id, is_permanent : true } ) , Some ( short_channel_id) , Some ( next_hop_failure. clone ( ) ) ) ;
526528
@@ -652,7 +654,7 @@ fn test_onion_failure() {
652654 // Tamper returning error message
653655 let session_priv = SecretKey :: from_slice ( & [ 3 ; 32 ] ) . unwrap ( ) ;
654656 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 ] ) ;
656658 } , ||{
657659 nodes[ 2 ] . node . fail_htlc_backwards ( & payment_hash) ;
658660 } , true , Some ( 23 ) , None , None , None ) ;
@@ -670,8 +672,12 @@ fn test_onion_failure() {
670672 let mut hmac = HmacEngine :: < Sha256 > :: new ( & um) ;
671673 hmac. input ( & decoded_err_packet. encode ( ) [ 32 ..] ) ;
672674 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;
675681 } , || nodes[ 2 ] . node . fail_htlc_backwards ( & payment_hash) , false , None ,
676682 Some ( NetworkUpdate :: NodeFailure { node_id : route. paths [ 0 ] . hops [ 1 ] . pubkey , is_permanent : true } ) ,
677683 Some ( channels[ 1 ] . 0 . contents . short_channel_id ) , None ) ;
@@ -693,8 +699,12 @@ fn test_onion_failure() {
693699 let mut hmac = HmacEngine :: < Sha256 > :: new ( & um) ;
694700 hmac. input ( & decoded_err_packet. encode ( ) [ 32 ..] ) ;
695701 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;
698708 } , || { } , true , Some ( 0x1000 |7 ) ,
699709 Some ( NetworkUpdate :: ChannelFailure {
700710 short_channel_id : channels[ 1 ] . 0 . contents . short_channel_id ,
@@ -717,8 +727,12 @@ fn test_onion_failure() {
717727 let mut hmac = HmacEngine :: < Sha256 > :: new ( & um) ;
718728 hmac. input ( & decoded_err_packet. encode ( ) [ 32 ..] ) ;
719729 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;
722736 } , || nodes[ 2 ] . node . fail_htlc_backwards ( & payment_hash) , true , Some ( 0x1000 |7 ) ,
723737 Some ( NetworkUpdate :: ChannelFailure {
724738 short_channel_id : channels[ 1 ] . 0 . contents . short_channel_id ,
0 commit comments