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