13
13
14
14
use chain:: Watch ;
15
15
use chain:: channelmonitor:: ChannelMonitor ;
16
+ use chain:: keysinterface:: { Recipient , KeysInterface } ;
16
17
use ln:: channelmanager:: { ChannelManager , ChannelManagerReadArgs , MIN_CLTV_EXPIRY_DELTA } ;
17
18
use routing:: network_graph:: RoutingFees ;
18
19
use routing:: router:: { RouteHint , RouteHintHop } ;
19
20
use ln:: features:: InitFeatures ;
20
21
use ln:: msgs;
21
- use ln:: msgs:: { ChannelMessageHandler , RoutingMessageHandler } ;
22
+ use ln:: msgs:: { ChannelMessageHandler , RoutingMessageHandler , OptionalField } ;
22
23
use util:: enforcing_trait_impls:: EnforcingSigner ;
23
24
use util:: events:: { Event , MessageSendEvent , MessageSendEventsProvider } ;
24
25
use util:: config:: UserConfig ;
@@ -30,7 +31,12 @@ use core::default::Default;
30
31
31
32
use ln:: functional_test_utils:: * ;
32
33
34
+ use bitcoin:: blockdata:: constants:: genesis_block;
33
35
use bitcoin:: hash_types:: BlockHash ;
36
+ use bitcoin:: hashes:: Hash ;
37
+ use bitcoin:: hashes:: sha256d:: Hash as Sha256dHash ;
38
+ use bitcoin:: network:: constants:: Network ;
39
+ use bitcoin:: secp256k1:: Secp256k1 ;
34
40
35
41
#[ test]
36
42
fn test_priv_forwarding_rejection ( ) {
@@ -445,3 +451,93 @@ fn test_inbound_scid_privacy() {
445
451
PaymentFailedConditions :: new( ) . blamed_scid( last_hop[ 0 ] . short_channel_id. unwrap( ) )
446
452
. blamed_chan_closed( true ) . expected_htlc_error_data( 0x4000 |10 , & [ 0 ; 0 ] ) ) ;
447
453
}
454
+
455
+ #[ test]
456
+ fn test_scid_alias_returned ( ) {
457
+ // Tests that when we fail an HTLC (in this case due to attempting to forward more than the
458
+ // channel's available balance) we use the correct (in this case the aliased) SCID in the
459
+ // channel_update which is returned in the onion to the sender.
460
+ let chanmon_cfgs = create_chanmon_cfgs ( 3 ) ;
461
+ let node_cfgs = create_node_cfgs ( 3 , & chanmon_cfgs) ;
462
+ let mut accept_forward_cfg = test_default_channel_config ( ) ;
463
+ accept_forward_cfg. accept_forwards_to_priv_channels = true ;
464
+ let node_chanmgrs = create_node_chanmgrs ( 3 , & node_cfgs, & [ None , Some ( accept_forward_cfg) , None ] ) ;
465
+ let nodes = create_network ( 3 , & node_cfgs, & node_chanmgrs) ;
466
+
467
+ create_announced_chan_between_nodes_with_value ( & nodes, 0 , 1 , 10_000_000 , 0 , InitFeatures :: known ( ) , InitFeatures :: known ( ) ) ;
468
+ create_unannounced_chan_between_nodes_with_value ( & nodes, 1 , 2 , 10_000 , 0 , InitFeatures :: known ( ) , InitFeatures :: known ( ) ) ;
469
+
470
+ let last_hop = nodes[ 2 ] . node . list_usable_channels ( ) ;
471
+ let mut hop_hints = vec ! [ RouteHint ( vec![ RouteHintHop {
472
+ src_node_id: nodes[ 1 ] . node. get_our_node_id( ) ,
473
+ short_channel_id: last_hop[ 0 ] . inbound_scid_alias. unwrap( ) ,
474
+ fees: RoutingFees {
475
+ base_msat: last_hop[ 0 ] . counterparty. forwarding_info. as_ref( ) . unwrap( ) . fee_base_msat,
476
+ proportional_millionths: last_hop[ 0 ] . counterparty. forwarding_info. as_ref( ) . unwrap( ) . fee_proportional_millionths,
477
+ } ,
478
+ cltv_expiry_delta: last_hop[ 0 ] . counterparty. forwarding_info. as_ref( ) . unwrap( ) . cltv_expiry_delta,
479
+ htlc_maximum_msat: None ,
480
+ htlc_minimum_msat: None ,
481
+ } ] ) ] ;
482
+ let ( mut route, payment_hash, _, payment_secret) = get_route_and_payment_hash ! ( nodes[ 0 ] , nodes[ 2 ] , hop_hints, 10_000 , 42 ) ;
483
+ assert_eq ! ( route. paths[ 0 ] [ 1 ] . short_channel_id, nodes[ 2 ] . node. list_usable_channels( ) [ 0 ] . inbound_scid_alias. unwrap( ) ) ;
484
+
485
+ route. paths [ 0 ] [ 1 ] . fee_msat = 10_000_000 ; // Overshoot the last channel's value
486
+
487
+ // Route the HTLC through to the destination.
488
+ nodes[ 0 ] . node . send_payment ( & route, payment_hash, & Some ( payment_secret) ) . unwrap ( ) ;
489
+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
490
+ let as_updates = get_htlc_update_msgs ! ( nodes[ 0 ] , nodes[ 1 ] . node. get_our_node_id( ) ) ;
491
+ nodes[ 1 ] . node . handle_update_add_htlc ( & nodes[ 0 ] . node . get_our_node_id ( ) , & as_updates. update_add_htlcs [ 0 ] ) ;
492
+ commitment_signed_dance ! ( nodes[ 1 ] , nodes[ 0 ] , & as_updates. commitment_signed, false , true ) ;
493
+
494
+ expect_pending_htlcs_forwardable ! ( nodes[ 1 ] ) ;
495
+ expect_pending_htlcs_forwardable ! ( nodes[ 1 ] ) ;
496
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
497
+
498
+ let bs_updates = get_htlc_update_msgs ! ( nodes[ 1 ] , nodes[ 0 ] . node. get_our_node_id( ) ) ;
499
+ nodes[ 0 ] . node . handle_update_fail_htlc ( & nodes[ 1 ] . node . get_our_node_id ( ) , & bs_updates. update_fail_htlcs [ 0 ] ) ;
500
+ commitment_signed_dance ! ( nodes[ 0 ] , nodes[ 1 ] , bs_updates. commitment_signed, false , true ) ;
501
+
502
+ // Build the expected channel update
503
+ let contents = msgs:: UnsignedChannelUpdate {
504
+ chain_hash : genesis_block ( Network :: Testnet ) . header . block_hash ( ) ,
505
+ short_channel_id : last_hop[ 0 ] . inbound_scid_alias . unwrap ( ) ,
506
+ timestamp : 21 ,
507
+ flags : 1 ,
508
+ cltv_expiry_delta : accept_forward_cfg. channel_options . cltv_expiry_delta ,
509
+ htlc_minimum_msat : 1_000 ,
510
+ htlc_maximum_msat : OptionalField :: Present ( 1_000_000 ) , // Defaults to 10% of the channel value
511
+ fee_base_msat : last_hop[ 0 ] . counterparty . forwarding_info . as_ref ( ) . unwrap ( ) . fee_base_msat ,
512
+ fee_proportional_millionths : last_hop[ 0 ] . counterparty . forwarding_info . as_ref ( ) . unwrap ( ) . fee_proportional_millionths ,
513
+ excess_data : Vec :: new ( ) ,
514
+ } ;
515
+ let msg_hash = Sha256dHash :: hash ( & contents. encode ( ) [ ..] ) ;
516
+ let signature = Secp256k1 :: new ( ) . sign ( & hash_to_message ! ( & msg_hash[ ..] ) , & nodes[ 1 ] . keys_manager . get_node_secret ( Recipient :: Node ) . unwrap ( ) ) ;
517
+ let msg = msgs:: ChannelUpdate { signature, contents } ;
518
+
519
+ expect_payment_failed_conditions ! ( nodes[ 0 ] , payment_hash, false ,
520
+ PaymentFailedConditions :: new( ) . blamed_scid( last_hop[ 0 ] . inbound_scid_alias. unwrap( ) )
521
+ . blamed_chan_closed( false ) . expected_htlc_error_data( 0x1000 |7 , & msg. encode_with_len( ) ) ) ;
522
+
523
+ route. paths [ 0 ] [ 1 ] . fee_msat = 10_000 ; // Reset to the correct payment amount
524
+ route. paths [ 0 ] [ 0 ] . fee_msat = 0 ; // But set fee paid to the middle hop to 0
525
+
526
+ // Route the HTLC through to the destination.
527
+ nodes[ 0 ] . node . send_payment ( & route, payment_hash, & Some ( payment_secret) ) . unwrap ( ) ;
528
+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
529
+ let as_updates = get_htlc_update_msgs ! ( nodes[ 0 ] , nodes[ 1 ] . node. get_our_node_id( ) ) ;
530
+ nodes[ 1 ] . node . handle_update_add_htlc ( & nodes[ 0 ] . node . get_our_node_id ( ) , & as_updates. update_add_htlcs [ 0 ] ) ;
531
+ commitment_signed_dance ! ( nodes[ 1 ] , nodes[ 0 ] , & as_updates. commitment_signed, false , true ) ;
532
+
533
+ let bs_updates = get_htlc_update_msgs ! ( nodes[ 1 ] , nodes[ 0 ] . node. get_our_node_id( ) ) ;
534
+ nodes[ 0 ] . node . handle_update_fail_htlc ( & nodes[ 1 ] . node . get_our_node_id ( ) , & bs_updates. update_fail_htlcs [ 0 ] ) ;
535
+ commitment_signed_dance ! ( nodes[ 0 ] , nodes[ 1 ] , bs_updates. commitment_signed, false , true ) ;
536
+
537
+ let mut err_data = Vec :: new ( ) ;
538
+ err_data. extend_from_slice ( & 10_000u64 . to_be_bytes ( ) ) ;
539
+ err_data. extend_from_slice ( & msg. encode_with_len ( ) ) ;
540
+ expect_payment_failed_conditions ! ( nodes[ 0 ] , payment_hash, false ,
541
+ PaymentFailedConditions :: new( ) . blamed_scid( last_hop[ 0 ] . inbound_scid_alias. unwrap( ) )
542
+ . blamed_chan_closed( false ) . expected_htlc_error_data( 0x1000 |12 , & err_data) ) ;
543
+ }
0 commit comments