@@ -14,7 +14,7 @@ use crate::ln::functional_test_utils::*;
1414use crate :: ln:: msgs:: { self , BaseMessageHandler , ChannelMessageHandler , MessageSendEvent } ;
1515use crate :: ln:: onion_utils:: { self , AttributionData } ;
1616use crate :: ln:: outbound_payment:: RecipientOnionFields ;
17- use crate :: routing:: router:: PaymentParameters ;
17+ use crate :: routing:: router:: { PaymentParameters , RouteParameters } ;
1818use crate :: sign:: ecdsa:: EcdsaChannelSigner ;
1919use crate :: sign:: tx_builder:: { SpecTxBuilder , TxBuilder } ;
2020use crate :: types:: features:: ChannelTypeFeatures ;
@@ -2431,3 +2431,170 @@ pub fn do_test_dust_limit_fee_accounting(can_afford: bool) {
24312431 check_added_monitors ( & nodes[ 1 ] , 3 ) ;
24322432 }
24332433}
2434+
2435+ #[ test]
2436+ fn test_create_channel_to_trusted_peer ( ) {
2437+ let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
2438+ let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
2439+ let mut config = test_default_channel_config ( ) ;
2440+ config. channel_handshake_config . max_inbound_htlc_value_in_flight_percent_of_channel = 100 ;
2441+ let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ Some ( config) , None ] ) ;
2442+ let nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
2443+
2444+ let node_a_id = nodes[ 0 ] . node . get_our_node_id ( ) ;
2445+ let node_b_id = nodes[ 1 ] . node . get_our_node_id ( ) ;
2446+
2447+ let feerate_per_kw = 253 ;
2448+ let channel_type_features = ChannelTypeFeatures :: only_static_remote_key ( ) ;
2449+
2450+ let default_config = UserConfig :: default ( ) ;
2451+
2452+ let mut push_amt = 100_000_000 ;
2453+ push_amt -= chan_utils:: commit_tx_fee_sat (
2454+ feerate_per_kw,
2455+ MIN_AFFORDABLE_HTLC_COUNT ,
2456+ & channel_type_features,
2457+ ) * 1000 ;
2458+ push_amt -= get_holder_selected_channel_reserve_satoshis ( 100_000 , & default_config) * 1000 ;
2459+
2460+ let temp_channel_id = nodes[ 0 ]
2461+ . node
2462+ . create_channel_to_trusted_peer ( node_b_id, 100_000 , push_amt, 42 , None , None )
2463+ . unwrap ( ) ;
2464+ let mut open_channel_message =
2465+ get_event_msg ! ( nodes[ 0 ] , MessageSendEvent :: SendOpenChannel , node_b_id) ;
2466+
2467+ nodes[ 1 ] . node . handle_open_channel ( node_a_id, & open_channel_message) ;
2468+
2469+ let mut accept_channel_message =
2470+ get_event_msg ! ( nodes[ 1 ] , MessageSendEvent :: SendAcceptChannel , node_a_id) ;
2471+
2472+ nodes[ 0 ] . node . handle_accept_channel ( node_b_id, & accept_channel_message) ;
2473+
2474+ let funding_tx = sign_funding_transaction ( & nodes[ 0 ] , & nodes[ 1 ] , 100_000 , temp_channel_id) ;
2475+ let funding_msgs =
2476+ create_chan_between_nodes_with_value_confirm ( & nodes[ 0 ] , & nodes[ 1 ] , & funding_tx) ;
2477+ create_chan_between_nodes_with_value_b ( & nodes[ 0 ] , & nodes[ 1 ] , & funding_msgs. 0 ) ;
2478+
2479+ send_payment ( & nodes[ 1 ] , & [ & nodes[ 0 ] ] , push_amt) ;
2480+ }
2481+
2482+ #[ test]
2483+ fn test_accept_inbound_channel_from_trusted_peer_0reserve ( ) {
2484+ let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
2485+ let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
2486+ let mut config = test_default_channel_config ( ) ;
2487+ config. channel_handshake_config . max_inbound_htlc_value_in_flight_percent_of_channel = 100 ;
2488+ config. manually_accept_inbound_channels = true ;
2489+ let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ None , Some ( config) ] ) ;
2490+ let nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
2491+
2492+ let initial_channel_value_sat = 100_000 ;
2493+ let ( _, _, chan_id, _) = create_announced_zero_reserve_chan_between_nodes_with_value (
2494+ & nodes,
2495+ 0 ,
2496+ 1 ,
2497+ initial_channel_value_sat,
2498+ 0 ,
2499+ ) ;
2500+
2501+ let channel_type = ChannelTypeFeatures :: only_static_remote_key ( ) ;
2502+
2503+ let delta = chan_utils:: commit_tx_fee_sat (
2504+ 253 * FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE as u32 ,
2505+ 2 ,
2506+ & channel_type,
2507+ ) ;
2508+
2509+ let _ = send_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] ] , ( 100_000 - delta) * 1000 ) ;
2510+
2511+ {
2512+ let per_peer_state_lock;
2513+ let mut peer_state_lock;
2514+ let chan =
2515+ get_channel_ref ! ( nodes[ 1 ] , nodes[ 0 ] , per_peer_state_lock, peer_state_lock, chan_id) ;
2516+ assert_eq ! ( chan. funding( ) . holder_selected_channel_reserve_satoshis, 0 ) ;
2517+ }
2518+ }
2519+
2520+ #[ test]
2521+ fn test_zero_reserve_no_outputs ( ) {
2522+ let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
2523+ let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
2524+ let mut config = test_default_channel_config ( ) ;
2525+ config. channel_handshake_config . max_inbound_htlc_value_in_flight_percent_of_channel = 100 ;
2526+ config. manually_accept_inbound_channels = true ;
2527+ let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ None , Some ( config) ] ) ;
2528+ let nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
2529+ let channel_type = ChannelTypeFeatures :: only_static_remote_key ( ) ;
2530+
2531+ let node_a_id = nodes[ 0 ] . node . get_our_node_id ( ) ;
2532+ let node_b_id = nodes[ 1 ] . node . get_our_node_id ( ) ;
2533+
2534+ nodes[ 0 ] . node . create_channel ( node_b_id, 1000 + 2 , 0 , 42 , None , None ) . unwrap ( ) ;
2535+ let mut open_channel = get_event_msg ! ( nodes[ 0 ] , MessageSendEvent :: SendOpenChannel , node_b_id) ;
2536+ open_channel. common_fields . max_htlc_value_in_flight_msat = 1_000_000 ;
2537+ open_channel. common_fields . dust_limit_satoshis = 546 ;
2538+ nodes[ 1 ] . node . handle_open_channel ( node_a_id, & open_channel) ;
2539+ let events = nodes[ 1 ] . node . get_and_clear_pending_events ( ) ;
2540+ assert_eq ! ( events. len( ) , 1 ) ;
2541+ match events[ 0 ] {
2542+ Event :: OpenChannelRequest { temporary_channel_id : chan_id, .. } => {
2543+ nodes[ 1 ]
2544+ . node
2545+ . accept_inbound_channel_from_trusted_peer (
2546+ & chan_id, & node_a_id, 0 , false , true , None ,
2547+ )
2548+ . unwrap ( ) ;
2549+ } ,
2550+ _ => panic ! ( "Unexpected event" ) ,
2551+ } ;
2552+
2553+ nodes[ 0 ] . node . handle_accept_channel (
2554+ node_b_id,
2555+ & get_event_msg ! ( nodes[ 1 ] , MessageSendEvent :: SendAcceptChannel , node_a_id) ,
2556+ ) ;
2557+
2558+ let ( chan_id, tx, _) = create_funding_transaction ( & nodes[ 0 ] , & node_b_id, 1000 + 2 , 42 ) ;
2559+
2560+ {
2561+ let mut per_peer_lock;
2562+ let mut peer_state_lock;
2563+ let channel = get_channel_ref ! ( nodes[ 0 ] , nodes[ 1 ] , per_peer_lock, peer_state_lock, chan_id) ;
2564+ if let Some ( mut chan) = channel. as_unfunded_outbound_v1_mut ( ) {
2565+ chan. context . holder_dust_limit_satoshis = 546 ;
2566+ } else {
2567+ panic ! ( "Unexpected Channel phase" ) ;
2568+ }
2569+ }
2570+
2571+ nodes[ 0 ] . node . funding_transaction_generated ( chan_id, node_b_id, tx. clone ( ) ) . unwrap ( ) ;
2572+ nodes[ 1 ] . node . handle_funding_created (
2573+ node_a_id,
2574+ & get_event_msg ! ( nodes[ 0 ] , MessageSendEvent :: SendFundingCreated , node_b_id) ,
2575+ ) ;
2576+ check_added_monitors ( & nodes[ 1 ] , 1 ) ;
2577+ expect_channel_pending_event ( & nodes[ 1 ] , & node_a_id) ;
2578+
2579+ nodes[ 0 ] . node . handle_funding_signed (
2580+ node_b_id,
2581+ & get_event_msg ! ( nodes[ 1 ] , MessageSendEvent :: SendFundingSigned , node_a_id) ,
2582+ ) ;
2583+ check_added_monitors ( & nodes[ 0 ] , 1 ) ;
2584+ expect_channel_pending_event ( & nodes[ 0 ] , & node_b_id) ;
2585+
2586+ let ( channel_ready, _channel_id) =
2587+ create_chan_between_nodes_with_value_confirm ( & nodes[ 0 ] , & nodes[ 1 ] , & tx) ;
2588+ let ( announcement, as_update, bs_update) =
2589+ create_chan_between_nodes_with_value_b ( & nodes[ 0 ] , & nodes[ 1 ] , & channel_ready) ;
2590+ update_nodes_with_chan_announce ( & nodes, 0 , 1 , & announcement, & as_update, & bs_update) ;
2591+
2592+ let delta = chan_utils:: commit_tx_fee_sat ( 253 * 2 , 2 , & channel_type) ;
2593+ //let _ = send_payment(&nodes[0], &[&nodes[1]], delta * 1000);
2594+
2595+ let payment_params =
2596+ PaymentParameters :: from_node_id ( nodes[ 1 ] . node . get_our_node_id ( ) , TEST_FINAL_CLTV ) ;
2597+
2598+ let route_params = RouteParameters :: from_payment_params_and_value ( payment_params, delta * 1000 ) ;
2599+ assert ! ( get_route( & nodes[ 0 ] , & route_params) . is_err( ) ) ;
2600+ }
0 commit comments