@@ -1175,3 +1175,105 @@ fn high_prop_fees() {
11751175 let expected_fee = pass_claimed_payment_along_route ( args) ;
11761176 expect_payment_sent ( & nodes[ 0 ] , payment_preimage, Some ( Some ( expected_fee) ) , true , true ) ;
11771177}
1178+
1179+ #[ cfg( feature = "std" ) ]
1180+ #[ test]
1181+ fn prop_fees_rng ( ) {
1182+ do_prop_fees_rng ( true ) ;
1183+ do_prop_fees_rng ( false ) ;
1184+ }
1185+
1186+ #[ cfg( feature = "std" ) ]
1187+ fn do_prop_fees_rng ( send_min : bool ) {
1188+ use std:: hash:: { BuildHasher , Hasher } ;
1189+ // Ensure the proportional fees are calculated correctly for `BlindedPayInfo`.
1190+ let chanmon_cfgs = create_chanmon_cfgs ( 5 ) ;
1191+ const PROP_LIMIT : u64 = 1_000_000 ;
1192+ let base_limit: u64 = if send_min { 1_000_000 } else { 15_000_000 } ;
1193+ const MIN_HTLC_LIMIT : u64 = 15_000_000 ;
1194+ let node_cfgs = create_node_cfgs ( 5 , & chanmon_cfgs) ;
1195+
1196+ let mut node_1_cfg = test_default_channel_config ( ) ;
1197+ node_1_cfg. channel_config . forwarding_fee_base_msat =
1198+ ( std:: collections:: hash_map:: RandomState :: new ( ) . build_hasher ( ) . finish ( ) % base_limit) as u32 ;
1199+ node_1_cfg. channel_config . forwarding_fee_proportional_millionths =
1200+ ( std:: collections:: hash_map:: RandomState :: new ( ) . build_hasher ( ) . finish ( ) % PROP_LIMIT ) as u32 ;
1201+ if send_min {
1202+ node_1_cfg. channel_handshake_config . our_htlc_minimum_msat =
1203+ ( std:: collections:: hash_map:: RandomState :: new ( ) . build_hasher ( ) . finish ( ) % MIN_HTLC_LIMIT ) as u64 ;
1204+ }
1205+
1206+ let mut node_2_cfg = test_default_channel_config ( ) ;
1207+ node_2_cfg. channel_config . forwarding_fee_base_msat =
1208+ ( std:: collections:: hash_map:: RandomState :: new ( ) . build_hasher ( ) . finish ( ) % base_limit) as u32 ;
1209+ node_2_cfg. channel_config . forwarding_fee_proportional_millionths =
1210+ ( std:: collections:: hash_map:: RandomState :: new ( ) . build_hasher ( ) . finish ( ) % PROP_LIMIT ) as u32 ;
1211+ if send_min {
1212+ node_2_cfg. channel_handshake_config . our_htlc_minimum_msat =
1213+ ( std:: collections:: hash_map:: RandomState :: new ( ) . build_hasher ( ) . finish ( ) % MIN_HTLC_LIMIT ) as u64 ;
1214+ }
1215+
1216+ let mut node_3_cfg = test_default_channel_config ( ) ;
1217+ node_3_cfg. channel_config . forwarding_fee_base_msat =
1218+ ( std:: collections:: hash_map:: RandomState :: new ( ) . build_hasher ( ) . finish ( ) % base_limit) as u32 ;
1219+ node_3_cfg. channel_config . forwarding_fee_proportional_millionths =
1220+ ( std:: collections:: hash_map:: RandomState :: new ( ) . build_hasher ( ) . finish ( ) % PROP_LIMIT ) as u32 ;
1221+ if send_min {
1222+ node_3_cfg. channel_handshake_config . our_htlc_minimum_msat =
1223+ ( std:: collections:: hash_map:: RandomState :: new ( ) . build_hasher ( ) . finish ( ) % MIN_HTLC_LIMIT ) as u64 ;
1224+ }
1225+
1226+ println ! ( "Randomly selected node parameters:" ) ;
1227+ for ( idx, node) in [ node_1_cfg, node_2_cfg, node_3_cfg] . iter ( ) . enumerate ( ) {
1228+ println ! ( " node {}: forwarding_fee_base_msat: {}, forwarding_fee_proportional_millionths: {}, our_htlc_minimum_msat: {}" , idx, node. channel_config. forwarding_fee_base_msat, node. channel_config. forwarding_fee_proportional_millionths, node. channel_handshake_config. our_htlc_minimum_msat) ;
1229+ }
1230+
1231+ let node_chanmgrs = create_node_chanmgrs ( 5 , & node_cfgs, & [ None , Some ( node_1_cfg) , Some ( node_2_cfg) , Some ( node_3_cfg) , None ] ) ;
1232+ let nodes = create_network ( 5 , & node_cfgs, & node_chanmgrs) ;
1233+ create_announced_chan_between_nodes_with_value ( & nodes, 0 , 1 , 1_000_000 , 0 ) ;
1234+ let chan_1_2 = create_announced_chan_between_nodes_with_value ( & nodes, 1 , 2 , 1_000_000 , 0 ) ;
1235+ let chan_2_3 = create_announced_chan_between_nodes_with_value ( & nodes, 2 , 3 , 1_000_000 , 0 ) ;
1236+ let chan_3_4 = create_announced_chan_between_nodes_with_value ( & nodes, 3 , 4 , 1_000_000 , 0 ) ;
1237+
1238+ let amt_msat = if send_min {
1239+ get_blinded_route_parameters (
1240+ 42 , PaymentSecret ( [ 0 ; 32 ] ) , chan_1_2. 1 . contents . htlc_minimum_msat ,
1241+ chan_1_2. 1 . contents . htlc_maximum_msat ,
1242+ nodes. iter ( ) . skip ( 1 ) . map ( |node| node. node . get_our_node_id ( ) ) . collect ( ) ,
1243+ & [ & chan_1_2. 0 . contents , & chan_2_3. 0 . contents , & chan_3_4. 0 . contents ] ,
1244+ & chanmon_cfgs[ 4 ] . keys_manager
1245+ )
1246+ . payment_params . payee . blinded_route_hints ( ) [ 0 ] . 0 . htlc_minimum_msat
1247+ } else { 100_000 } ;
1248+ let ( payment_preimage, payment_hash, payment_secret) = get_payment_preimage_hash ( & nodes[ 4 ] , Some ( amt_msat) , None ) ;
1249+
1250+ let mut route_params = get_blinded_route_parameters ( amt_msat, payment_secret,
1251+ chan_1_2. 1 . contents . htlc_minimum_msat , chan_1_2. 1 . contents . htlc_maximum_msat ,
1252+ nodes. iter ( ) . skip ( 1 ) . map ( |node| node. node . get_our_node_id ( ) ) . collect ( ) ,
1253+ & [ & chan_1_2. 0 . contents , & chan_2_3. 0 . contents , & chan_3_4. 0 . contents ] ,
1254+ & chanmon_cfgs[ 4 ] . keys_manager ) ;
1255+ route_params. max_total_routing_fee_msat = None ;
1256+
1257+ nodes[ 0 ] . node . send_payment ( payment_hash, RecipientOnionFields :: spontaneous_empty ( ) , PaymentId ( payment_hash. 0 ) , route_params. clone ( ) , Retry :: Attempts ( 0 ) ) . unwrap ( ) ;
1258+ check_added_monitors ( & nodes[ 0 ] , 1 ) ;
1259+ let mut events = nodes[ 0 ] . node . get_and_clear_pending_msg_events ( ) ;
1260+ assert_eq ! ( events. len( ) , 1 ) ;
1261+ let event = remove_first_msg_event_to_node ( & nodes[ 1 ] . node . get_our_node_id ( ) , & mut events) ;
1262+ let expected_path = & [ & nodes[ 1 ] , & nodes[ 2 ] , & nodes[ 3 ] , & nodes[ 4 ] ] ;
1263+
1264+ let args = PassAlongPathArgs :: new ( & nodes[ 0 ] , expected_path, amt_msat, payment_hash, event)
1265+ . with_payment_secret ( payment_secret)
1266+ . with_overpay_limit ( if send_min { 40 } else { 3 } ) ;
1267+ let claimable_ev = do_pass_along_path ( args) ;
1268+ let claim_amt = if let Some ( crate :: events:: Event :: PaymentClaimable { amount_msat, .. } ) = claimable_ev {
1269+ amount_msat
1270+ } else { panic ! ( ) } ;
1271+ let overpayment_amt = claim_amt. checked_sub ( amt_msat) . unwrap ( ) ;
1272+
1273+ nodes[ 4 ] . node . claim_funds ( payment_preimage) ;
1274+ let expected_route = & [ & expected_path[ ..] ] ;
1275+ let mut claim_args = ClaimAlongRouteArgs :: new ( & nodes[ 0 ] , & expected_route[ ..] , payment_preimage)
1276+ . allow_1_msat_fee_overpay ( ) ;
1277+ let expected_fee = pass_claimed_payment_along_route ( claim_args) ;
1278+ expect_payment_sent ( & nodes[ 0 ] , payment_preimage, Some ( Some ( expected_fee + overpayment_amt) ) , true , true ) ;
1279+ }
0 commit comments