@@ -509,7 +509,11 @@ fn _create_invoice_from_channelmanager_and_duration_since_epoch_with_payment_has
509509///
510510/// The filtering is based on the following criteria:
511511/// * Only one channel per counterparty node
512- /// * Always select the channel with the highest inbound capacity per counterparty node
512+ /// * If the counterparty has a channel that is above the `min_inbound_capacity_msat` + 10% scaling
513+ /// factor (to allow some margin for change in inbound), select the channel with the lowest
514+ /// inbound capacity that is above this threshold.
515+ /// * If no `min_inbound_capacity_msat` is specified, or the counterparty has no channels above the
516+ /// minimum + 10% scaling factor, select the channel with the highest inbound capacity per counterparty.
513517/// * Prefer channels with capacity at least `min_inbound_capacity_msat` and where the channel
514518/// `is_usable` (i.e. the peer is connected).
515519/// * If any public channel exists, only public [`RouteHint`]s will be returned.
@@ -570,12 +574,17 @@ fn filter_channels<L: Deref>(
570574 // If this channel is public and the previous channel is not, ensure we replace the
571575 // previous channel to avoid announcing non-public channels.
572576 let new_now_public = channel. is_public && !entry. get ( ) . is_public ;
577+ // Decide whether we prefer the currently selected channel with the node to the new one,
578+ // based on their inbound capacity. A scaling factor of 10% to pad our minimum inbound
579+ // capacity to make this decision.
580+ let prefer_current = prefer_current_channel ( min_inbound_capacity_msat, current_max_capacity,
581+ channel. inbound_capacity_msat ) ;
573582 // If the public-ness of the channel has not changed (in which case simply defer to
574- // `new_now_public), and this channel has a greater capacity, prefer to announce
575- // this channel.
576- let new_higher_capacity = channel. is_public == entry. get ( ) . is_public &&
577- channel . inbound_capacity_msat > current_max_capacity ;
578- if new_now_public || new_higher_capacity {
583+ // `new_now_public), and this channel has more desirable inbound than the incumbent,
584+ // prefer to announce this channel.
585+ let new_channel_preferable = channel. is_public == entry. get ( ) . is_public && !prefer_current ;
586+
587+ if new_now_public || new_channel_preferable {
579588 log_trace ! ( logger,
580589 "Preferring counterparty {} channel {} (SCID {:?}, {} msats) over {} (SCID {:?}, {} msats) for invoice route hints" ,
581590 log_pubkey!( channel. counterparty. node_id) ,
@@ -658,6 +667,41 @@ fn filter_channels<L: Deref>(
658667 . collect :: < Vec < RouteHint > > ( )
659668}
660669
670+ /// prefer_current_channel chooses a channel to use for route hints between a currently selected and candidate
671+ /// channel based on the inbound capacity of each channel and the minimum inbound capacity requested for the hints,
672+ /// returning true if the current channel should be preferred over the candidate channel.
673+ /// * If no minimum amount is requested, the channel with the most inbound is chosen to maximize the chances that a
674+ /// payment of any size will succeed.
675+ /// * If we have channels with inbound above our minimum requested inbound (plus a 10% scaling factor, expressed as a
676+ /// percentage)
677+ /// then we choose the lowest inbound channel with above this amount. If we have sufficient inbound channels, we don't
678+ /// want to deplete our larger channels with small payments (the off-chain version of "grinding our change").
679+ /// * If no channel above our minimum amount exists, then we just prefer the channel with the most inbound to give
680+ /// payments the best chance of succeeding in multiple parts.
681+ fn prefer_current_channel ( min_inbound_capacity_msat : Option < u64 > , current_channel : u64 ,
682+ candidate_channel : u64 ) -> bool {
683+
684+ // If no min amount is given for the hints, err of the side of caution and choose the largest channel inbound to
685+ // maximize chances of any payment succeeding.
686+ if min_inbound_capacity_msat. is_none ( ) {
687+ return current_channel > candidate_channel
688+ }
689+
690+ let scaled_min_inbound = min_inbound_capacity_msat. unwrap ( ) * 110 ;
691+ let current_sufficient = current_channel * 100 >= scaled_min_inbound;
692+ let candidate_sufficient = candidate_channel * 100 >= scaled_min_inbound;
693+
694+ if current_sufficient && candidate_sufficient {
695+ return current_channel < candidate_channel
696+ } else if current_sufficient {
697+ return true
698+ } else if candidate_sufficient {
699+ return false
700+ }
701+
702+ current_channel > candidate_channel
703+ }
704+
661705#[ cfg( test) ]
662706mod test {
663707 use core:: time:: Duration ;
@@ -676,6 +720,33 @@ mod test {
676720 use crate :: utils:: create_invoice_from_channelmanager_and_duration_since_epoch;
677721 use std:: collections:: HashSet ;
678722
723+ #[ test]
724+ fn test_prefer_current_channel ( ) {
725+ let test_cases = vec ! [
726+ // No minimum, prefer larger candidate channel.
727+ ( None , 100 , 200 , false ) ,
728+ // No minimum, prefer larger current channel.
729+ ( None , 200 , 100 , true ) ,
730+ // Minimum set, prefer current channel over minimum + buffer.
731+ ( Some ( 100 ) , 115 , 100 , true ) ,
732+ // Minimum set, prefer candidate channel over minimum + buffer.
733+ ( Some ( 100 ) , 105 , 125 , false ) ,
734+ // Minimum set, both channels sufficient, prefer smaller current channel.
735+ ( Some ( 100 ) , 115 , 125 , true ) ,
736+ // Minimum set, both channels sufficient, prefer smaller candidate channel.
737+ ( Some ( 100 ) , 200 , 160 , false ) ,
738+ // Minimum set, neither sufficient, prefer larger current channel.
739+ ( Some ( 200 ) , 100 , 50 , true ) ,
740+ // Minimum set, neither sufficient, prefer larger candidate channel.
741+ ( Some ( 200 ) , 100 , 150 , false ) ,
742+ ] ;
743+
744+ for test_case in test_cases {
745+ assert_eq ! ( crate :: utils:: prefer_current_channel( test_case. 0 , test_case. 1 , test_case. 2 ) , test_case. 3 ) ;
746+ }
747+ }
748+
749+
679750 #[ test]
680751 fn test_from_channelmanager ( ) {
681752 let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
@@ -891,17 +962,19 @@ mod test {
891962 }
892963
893964 #[ test]
894- fn test_hints_has_only_highest_inbound_capacity_channel ( ) {
965+ fn test_hints_has_only_lowest_inbound_capacity_channel_above_minimum ( ) {
895966 let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
896967 let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
897968 let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ None , None ] ) ;
898969 let nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
899- let _chan_1_0_low_inbound_capacity = create_unannounced_chan_between_nodes_with_value ( & nodes, 1 , 0 , 100_000 , 0 ) ;
900- let chan_1_0_high_inbound_capacity = create_unannounced_chan_between_nodes_with_value ( & nodes, 1 , 0 , 10_000_000 , 0 ) ;
901- let _chan_1_0_medium_inbound_capacity = create_unannounced_chan_between_nodes_with_value ( & nodes, 1 , 0 , 1_000_000 , 0 ) ;
970+
971+ let _chan_1_0_inbound_below_amt = create_unannounced_chan_between_nodes_with_value ( & nodes, 1 , 0 , 10_000 , 0 ) ;
972+ let _chan_1_0_large_inbound_above_amt = create_unannounced_chan_between_nodes_with_value ( & nodes, 1 , 0 , 500_000 , 0 ) ;
973+ let chan_1_0_low_inbound_above_amt = create_unannounced_chan_between_nodes_with_value ( & nodes, 1 , 0 , 200_000 , 0 ) ;
974+
902975 let mut scid_aliases = HashSet :: new ( ) ;
903- scid_aliases. insert ( chan_1_0_high_inbound_capacity . 0 . short_channel_id_alias . unwrap ( ) ) ;
904- match_invoice_routes ( Some ( 5000 ) , & nodes[ 0 ] , scid_aliases) ;
976+ scid_aliases. insert ( chan_1_0_low_inbound_above_amt . 0 . short_channel_id_alias . unwrap ( ) ) ;
977+ match_invoice_routes ( Some ( 100_000_000 ) , & nodes[ 0 ] , scid_aliases) ;
905978 }
906979
907980 #[ test]
@@ -1474,7 +1547,7 @@ mod test {
14741547
14751548 #[ test]
14761549 #[ cfg( feature = "std" ) ]
1477- fn test_multi_node_hints_has_only_highest_inbound_capacity_channel ( ) {
1550+ fn test_multi_node_hints_has_only_lowest_inbound_channel_above_minimum ( ) {
14781551 let mut chanmon_cfgs = create_chanmon_cfgs ( 3 ) ;
14791552 let seed_1 = [ 42u8 ; 32 ] ;
14801553 let seed_2 = [ 43u8 ; 32 ] ;
@@ -1485,17 +1558,17 @@ mod test {
14851558 let node_chanmgrs = create_node_chanmgrs ( 3 , & node_cfgs, & [ None , None , None ] ) ;
14861559 let nodes = create_network ( 3 , & node_cfgs, & node_chanmgrs) ;
14871560
1488- let _chan_0_1_low_inbound_capacity = create_unannounced_chan_between_nodes_with_value ( & nodes, 0 , 1 , 100_000 , 0 ) ;
1489- let chan_0_1_high_inbound_capacity = create_unannounced_chan_between_nodes_with_value ( & nodes, 0 , 1 , 10_000_000 , 0 ) ;
1490- let _chan_0_1_medium_inbound_capacity = create_unannounced_chan_between_nodes_with_value ( & nodes, 0 , 1 , 1_000_000 , 0 ) ;
1561+ let _chan_0_1_below_amt = create_unannounced_chan_between_nodes_with_value ( & nodes, 0 , 1 , 100_000 , 0 ) ;
1562+ let _chan_0_1_above_amt_high_inbound = create_unannounced_chan_between_nodes_with_value ( & nodes, 0 , 1 , 500_000 , 0 ) ;
1563+ let chan_0_1_above_amt_low_inbound = create_unannounced_chan_between_nodes_with_value ( & nodes, 0 , 1 , 180_000 , 0 ) ;
14911564 let chan_0_2 = create_unannounced_chan_between_nodes_with_value ( & nodes, 0 , 2 , 100000 , 10001 ) ;
14921565
14931566 let mut scid_aliases = HashSet :: new ( ) ;
1494- scid_aliases. insert ( chan_0_1_high_inbound_capacity . 0 . short_channel_id_alias . unwrap ( ) ) ;
1567+ scid_aliases. insert ( chan_0_1_above_amt_low_inbound . 0 . short_channel_id_alias . unwrap ( ) ) ;
14951568 scid_aliases. insert ( chan_0_2. 0 . short_channel_id_alias . unwrap ( ) ) ;
14961569
14971570 match_multi_node_invoice_routes (
1498- Some ( 10_000 ) ,
1571+ Some ( 100_000_000 ) ,
14991572 & nodes[ 1 ] ,
15001573 vec ! [ & nodes[ 1 ] , & nodes[ 2 ] , ] ,
15011574 scid_aliases,
0 commit comments