11
11
//! These tests work by standing up full nodes and route payments across the network, checking the
12
12
//! returned errors decode to the correct thing.
13
13
14
- use chain:: channelmonitor:: { CLTV_CLAIM_BUFFER , LATENCY_GRACE_PERIOD_BLOCKS } ;
14
+ use chain:: channelmonitor:: { ChannelMonitor , CLTV_CLAIM_BUFFER , LATENCY_GRACE_PERIOD_BLOCKS } ;
15
15
use chain:: keysinterface:: { KeysInterface , Recipient } ;
16
16
use ln:: { PaymentHash , PaymentSecret } ;
17
- use ln:: channelmanager:: { HTLCForwardInfo , CLTV_FAR_FAR_AWAY , MIN_CLTV_EXPIRY_DELTA , PendingHTLCInfo , PendingHTLCRouting } ;
17
+ use ln:: channelmanager:: { ChannelManager , ChannelManagerReadArgs , HTLCForwardInfo , CLTV_FAR_FAR_AWAY , MIN_CLTV_EXPIRY_DELTA , PendingHTLCInfo , PendingHTLCRouting } ;
18
18
use ln:: onion_utils;
19
19
use routing:: gossip:: { NetworkUpdate , RoutingFees , NodeId } ;
20
20
use routing:: router:: { get_route, PaymentParameters , Route , RouteHint , RouteHintHop } ;
@@ -23,9 +23,10 @@ use ln::msgs;
23
23
use ln:: msgs:: { ChannelMessageHandler , ChannelUpdate , OptionalField } ;
24
24
use ln:: wire:: Encode ;
25
25
use util:: events:: { Event , MessageSendEvent , MessageSendEventsProvider } ;
26
- use util:: ser:: { Writeable , Writer } ;
26
+ use util:: ser:: { ReadableArgs , Writeable , Writer } ;
27
27
use util:: { byte_utils, test_utils} ;
28
- use util:: config:: UserConfig ;
28
+ use util:: config:: { UserConfig , ChannelConfig } ;
29
+ use util:: errors:: APIError ;
29
30
30
31
use bitcoin:: hash_types:: BlockHash ;
31
32
@@ -506,8 +507,6 @@ fn test_onion_failure() {
506
507
let preimage = send_along_route ( & nodes[ 0 ] , bogus_route, & [ & nodes[ 1 ] , & nodes[ 2 ] ] , amt_to_forward+1 ) . 0 ;
507
508
claim_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] , & nodes[ 2 ] ] , preimage) ;
508
509
509
- //TODO: with new config API, we will be able to generate both valid and
510
- //invalid channel_update cases.
511
510
let short_channel_id = channels[ 0 ] . 0 . contents . short_channel_id ;
512
511
run_onion_failure_test ( "fee_insufficient" , 0 , & nodes, & route, & payment_hash, & payment_secret, |msg| {
513
512
msg. amount_msat -= 1 ;
@@ -594,6 +593,183 @@ fn test_onion_failure() {
594
593
} , true , Some ( 23 ) , None , None ) ;
595
594
}
596
595
596
+ fn do_test_onion_failure_stale_channel_update ( announced_channel : bool ) {
597
+ // Create a network of three nodes and two channels connecting them. We'll be updating the
598
+ // HTLC relay policy of the second channel, causing forwarding failures at the first hop.
599
+ let mut config = UserConfig :: default ( ) ;
600
+ config. channel_handshake_config . announced_channel = announced_channel;
601
+ config. channel_handshake_limits . force_announced_channel_preference = false ;
602
+ config. accept_forwards_to_priv_channels = !announced_channel;
603
+ let chanmon_cfgs = create_chanmon_cfgs ( 3 ) ;
604
+ let node_cfgs = create_node_cfgs ( 3 , & chanmon_cfgs) ;
605
+ let node_chanmgrs = create_node_chanmgrs ( 3 , & node_cfgs, & [ None , Some ( config) , None ] ) ;
606
+ let mut nodes = create_network ( 3 , & node_cfgs, & node_chanmgrs) ;
607
+
608
+ let other_channel = create_chan_between_nodes (
609
+ & nodes[ 0 ] , & nodes[ 1 ] , InitFeatures :: known ( ) , InitFeatures :: known ( ) ,
610
+ ) ;
611
+ let channel_to_update = if announced_channel {
612
+ let channel = create_announced_chan_between_nodes (
613
+ & nodes, 1 , 2 , InitFeatures :: known ( ) , InitFeatures :: known ( ) ,
614
+ ) ;
615
+ ( channel. 2 , channel. 0 . contents . short_channel_id )
616
+ } else {
617
+ let channel = create_unannounced_chan_between_nodes_with_value (
618
+ & nodes, 1 , 2 , 100000 , 10001 , InitFeatures :: known ( ) , InitFeatures :: known ( ) ,
619
+ ) ;
620
+ ( channel. 0 . channel_id , channel. 0 . short_channel_id_alias . unwrap ( ) )
621
+ } ;
622
+ let channel_to_update_counterparty = & nodes[ 2 ] . node . get_our_node_id ( ) ;
623
+
624
+ let default_config = ChannelConfig :: default ( ) ;
625
+
626
+ // A test payment should succeed as the ChannelConfig has not been changed yet.
627
+ const PAYMENT_AMT : u64 = 40000 ;
628
+ let ( route, payment_hash, payment_preimage, payment_secret) = if announced_channel {
629
+ get_route_and_payment_hash ! ( nodes[ 0 ] , nodes[ 2 ] , PAYMENT_AMT )
630
+ } else {
631
+ let hop_hints = vec ! [ RouteHint ( vec![ RouteHintHop {
632
+ src_node_id: nodes[ 1 ] . node. get_our_node_id( ) ,
633
+ short_channel_id: channel_to_update. 1 ,
634
+ fees: RoutingFees {
635
+ base_msat: default_config. forwarding_fee_base_msat,
636
+ proportional_millionths: default_config. forwarding_fee_proportional_millionths,
637
+ } ,
638
+ cltv_expiry_delta: default_config. cltv_expiry_delta,
639
+ htlc_maximum_msat: None ,
640
+ htlc_minimum_msat: None ,
641
+ } ] ) ] ;
642
+ let payment_params = PaymentParameters :: from_node_id ( * channel_to_update_counterparty)
643
+ . with_features ( InvoiceFeatures :: known ( ) )
644
+ . with_route_hints ( hop_hints) ;
645
+ get_route_and_payment_hash ! ( nodes[ 0 ] , nodes[ 2 ] , payment_params, PAYMENT_AMT , TEST_FINAL_CLTV )
646
+ } ;
647
+ send_along_route_with_secret ( & nodes[ 0 ] , route. clone ( ) , & [ & [ & nodes[ 1 ] , & nodes[ 2 ] ] ] , PAYMENT_AMT ,
648
+ payment_hash, payment_secret) ;
649
+ claim_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] , & nodes[ 2 ] ] , payment_preimage) ;
650
+
651
+ // Closure to update and retrieve the latest ChannelUpdate.
652
+ let update_and_get_channel_update = |config : & ChannelConfig , expect_new_update : bool ,
653
+ prev_update : Option < & msgs:: ChannelUpdate > | -> Option < msgs:: ChannelUpdate > {
654
+ nodes[ 1 ] . node . update_channel_config (
655
+ channel_to_update_counterparty, & [ channel_to_update. 0 ] , config,
656
+ ) . unwrap ( ) ;
657
+ let events = nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
658
+ assert_eq ! ( events. len( ) , expect_new_update as usize ) ;
659
+ if !expect_new_update {
660
+ return None ;
661
+ }
662
+ let new_update = match & events[ 0 ] {
663
+ MessageSendEvent :: BroadcastChannelUpdate { msg } => {
664
+ assert ! ( announced_channel) ;
665
+ msg. clone ( )
666
+ } ,
667
+ MessageSendEvent :: SendChannelUpdate { node_id, msg } => {
668
+ assert_eq ! ( node_id, channel_to_update_counterparty) ;
669
+ assert ! ( !announced_channel) ;
670
+ msg. clone ( )
671
+ } ,
672
+ _ => panic ! ( "expected Broadcast/SendChannelUpdate event" ) ,
673
+ } ;
674
+ if prev_update. is_some ( ) {
675
+ assert ! ( new_update. contents. timestamp > prev_update. unwrap( ) . contents. timestamp)
676
+ }
677
+ Some ( new_update)
678
+ } ;
679
+
680
+ // We'll be attempting to route payments using the default ChannelUpdate for channels. This will
681
+ // lead to onion failures at the first hop once we update the ChannelConfig for the
682
+ // second hop.
683
+ let expect_onion_failure = |name : & str , error_code : u16 , channel_update : & msgs:: ChannelUpdate | {
684
+ let short_channel_id = channel_to_update. 1 ;
685
+ let network_update = NetworkUpdate :: ChannelUpdateMessage { msg : channel_update. clone ( ) } ;
686
+ run_onion_failure_test (
687
+ name, 0 , & nodes, & route, & payment_hash, & payment_secret, |_| { } , || { } , true ,
688
+ Some ( error_code) , Some ( network_update) , Some ( short_channel_id) ,
689
+ ) ;
690
+ } ;
691
+
692
+ // Updates to cltv_expiry_delta below MIN_CLTV_EXPIRY_DELTA should fail with APIMisuseError.
693
+ let mut invalid_config = default_config. clone ( ) ;
694
+ invalid_config. cltv_expiry_delta = 0 ;
695
+ match nodes[ 1 ] . node . update_channel_config (
696
+ channel_to_update_counterparty, & [ channel_to_update. 0 ] , & invalid_config,
697
+ ) {
698
+ Err ( APIError :: APIMisuseError { .. } ) => { } ,
699
+ _ => panic ! ( "unexpected result applying invalid cltv_expiry_delta" ) ,
700
+ }
701
+
702
+ // Increase the base fee which should trigger a new ChannelUpdate.
703
+ let mut config = nodes[ 1 ] . node . list_usable_channels ( ) . iter ( )
704
+ . find ( |channel| channel. channel_id == channel_to_update. 0 ) . unwrap ( )
705
+ . config . unwrap ( ) ;
706
+ config. forwarding_fee_base_msat = u32:: max_value ( ) ;
707
+ let msg = update_and_get_channel_update ( & config, true , None ) . unwrap ( ) ;
708
+ expect_onion_failure ( "fee_insufficient" , UPDATE |12 , & msg) ;
709
+
710
+ // Redundant updates should not trigger a new ChannelUpdate.
711
+ assert ! ( update_and_get_channel_update( & config, false , None ) . is_none( ) ) ;
712
+
713
+ // Similarly, updates that do not have an affect on ChannelUpdate should not trigger a new one.
714
+ config. force_close_avoidance_max_fee_satoshis *= 2 ;
715
+ assert ! ( update_and_get_channel_update( & config, false , None ) . is_none( ) ) ;
716
+
717
+ // Reset the base fee to the default and increase the proportional fee which should trigger a
718
+ // new ChannelUpdate.
719
+ config. forwarding_fee_base_msat = default_config. forwarding_fee_base_msat ;
720
+ config. cltv_expiry_delta = u16:: max_value ( ) ;
721
+ let msg = update_and_get_channel_update ( & config, true , Some ( & msg) ) . unwrap ( ) ;
722
+ expect_onion_failure ( "incorrect_cltv_expiry" , UPDATE |13 , & msg) ;
723
+
724
+ // Reset the proportional fee and increase the CLTV expiry delta which should trigger a new
725
+ // ChannelUpdate.
726
+ config. cltv_expiry_delta = default_config. cltv_expiry_delta ;
727
+ config. forwarding_fee_proportional_millionths = u32:: max_value ( ) ;
728
+ let msg = update_and_get_channel_update ( & config, true , Some ( & msg) ) . unwrap ( ) ;
729
+ expect_onion_failure ( "fee_insufficient" , UPDATE |12 , & msg) ;
730
+
731
+ // To test persistence of the updated config, we'll re-initialize the ChannelManager.
732
+ let config_after_restart = {
733
+ let persister = test_utils:: TestPersister :: new ( ) ;
734
+ let chain_monitor = test_utils:: TestChainMonitor :: new (
735
+ Some ( nodes[ 1 ] . chain_source ) , nodes[ 1 ] . tx_broadcaster . clone ( ) , nodes[ 1 ] . logger ,
736
+ node_cfgs[ 1 ] . fee_estimator , & persister, nodes[ 1 ] . keys_manager ,
737
+ ) ;
738
+
739
+ let mut chanmon_1 = <( _ , ChannelMonitor < _ > ) >:: read (
740
+ & mut & get_monitor ! ( nodes[ 1 ] , other_channel. 3 ) . encode ( ) [ ..] , nodes[ 1 ] . keys_manager ,
741
+ ) . unwrap ( ) . 1 ;
742
+ let mut chanmon_2 = <( _ , ChannelMonitor < _ > ) >:: read (
743
+ & mut & get_monitor ! ( nodes[ 1 ] , channel_to_update. 0 ) . encode ( ) [ ..] , nodes[ 1 ] . keys_manager ,
744
+ ) . unwrap ( ) . 1 ;
745
+ let mut channel_monitors = HashMap :: new ( ) ;
746
+ channel_monitors. insert ( chanmon_1. get_funding_txo ( ) . 0 , & mut chanmon_1) ;
747
+ channel_monitors. insert ( chanmon_2. get_funding_txo ( ) . 0 , & mut chanmon_2) ;
748
+
749
+ let chanmgr = <( _ , ChannelManager < _ , _ , _ , _ , _ , _ > ) >:: read (
750
+ & mut & nodes[ 1 ] . node . encode ( ) [ ..] , ChannelManagerReadArgs {
751
+ default_config : * nodes[ 1 ] . node . get_current_default_configuration ( ) ,
752
+ keys_manager : nodes[ 1 ] . keys_manager ,
753
+ fee_estimator : node_cfgs[ 1 ] . fee_estimator ,
754
+ chain_monitor : & chain_monitor,
755
+ tx_broadcaster : nodes[ 1 ] . tx_broadcaster . clone ( ) ,
756
+ logger : nodes[ 1 ] . logger ,
757
+ channel_monitors : channel_monitors,
758
+ } ,
759
+ ) . unwrap ( ) . 1 ;
760
+ chanmgr. list_channels ( ) . iter ( )
761
+ . find ( |channel| channel. channel_id == channel_to_update. 0 ) . unwrap ( )
762
+ . config . unwrap ( )
763
+ } ;
764
+ assert_eq ! ( config, config_after_restart) ;
765
+ }
766
+
767
+ #[ test]
768
+ fn test_onion_failure_stale_channel_update ( ) {
769
+ do_test_onion_failure_stale_channel_update ( false ) ;
770
+ do_test_onion_failure_stale_channel_update ( true ) ;
771
+ }
772
+
597
773
#[ test]
598
774
fn test_default_to_onion_payload_tlv_format ( ) {
599
775
// Tests that we default to creating tlv format onion payloads when no `NodeAnnouncementInfo`
0 commit comments