Skip to content

Commit f2a5aea

Browse files
Update filter channels to limit to 3 hints per invoice
1 parent 22d1bab commit f2a5aea

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

lightning-invoice/src/utils.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,7 @@ 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+
/// * Limited to three channels in total
512513
/// * Always select the channel with the highest inbound capacity per counterparty node
513514
/// * Prefer channels with capacity at least `min_inbound_capacity_msat` and where the channel
514515
/// `is_usable` (i.e. the peer is connected).
@@ -629,6 +630,7 @@ fn filter_channels<L: Deref>(
629630

630631
include_channel
631632
})
633+
.take(3)
632634
.map(route_hint_from_channel)
633635
.collect::<Vec<RouteHint>>()
634636
}
@@ -852,6 +854,28 @@ mod test {
852854
match_invoice_routes(Some(1_000_000_000), &nodes[0], scid_aliases);
853855
}
854856

857+
#[test]
858+
fn test_hints_limited_to_3() {
859+
let chanmon_cfgs = create_chanmon_cfgs(5);
860+
let node_cfgs = create_node_cfgs(5, &chanmon_cfgs);
861+
let node_chanmgrs = create_node_chanmgrs(5, &node_cfgs, &[None,None,None, None, None]);
862+
let nodes = create_network(5, &node_cfgs, &node_chanmgrs);
863+
864+
let _chan_1_0 = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 0, 100_004, 0);
865+
let _chan_2_0 = create_unannounced_chan_between_nodes_with_value(&nodes, 2, 0, 100_003, 0);
866+
let _chan_3_0 = create_unannounced_chan_between_nodes_with_value(&nodes, 3, 0, 100_002, 0);
867+
let _chan_4_0 = create_unannounced_chan_between_nodes_with_value(&nodes, 4, 0, 100_001, 0);
868+
869+
// Hints are randomly selected, so we can only assert that we limit our number of hints, not the
870+
// actual hints that are included.
871+
let invoice = create_invoice_from_channelmanager_and_duration_since_epoch(
872+
&nodes[0].node, nodes[0].keys_manager, nodes[0].logger,
873+
Currency::BitcoinTestnet, Some(500_000), "test".to_string(), Duration::from_secs(1234567),
874+
3600, None).unwrap();
875+
876+
assert_eq!(invoice.private_routes().len(), 3, "expected 3 route hints");
877+
}
878+
855879
#[test]
856880
fn test_forwarding_info_not_assigned_channel_excluded_from_hints() {
857881
let chanmon_cfgs = create_chanmon_cfgs(3);

0 commit comments

Comments
 (0)