Skip to content

Commit c1c37bc

Browse files
fixup! Limit phantom invoice hints to 3
1 parent 53b5f0b commit c1c37bc

File tree

1 file changed

+35
-11
lines changed

1 file changed

+35
-11
lines changed

lightning-invoice/src/utils.rs

+35-11
Original file line numberDiff line numberDiff line change
@@ -274,13 +274,17 @@ where
274274
let mut remaining_hints = false;
275275

276276
for hints in phantom_hints.iter() {
277+
if invoice_hints.len() == 3 {
278+
return invoice_hints
279+
}
280+
277281
if hint_idx < hints.len() {
278282
invoice_hints.push(hints[hint_idx].clone());
279283
remaining_hints = true
280284
}
281285
}
282286

283-
if !remaining_hints || invoice_hints.len() == 3 {
287+
if !remaining_hints {
284288
return invoice_hints
285289
}
286290

@@ -1751,38 +1755,58 @@ mod test {
17511755

17521756
#[test]
17531757
fn test_multi_node_hints_limited_to_3() {
1754-
let mut chanmon_cfgs = create_chanmon_cfgs(5);
1758+
let mut chanmon_cfgs = create_chanmon_cfgs(6);
17551759
let seed_1 = [42 as u8; 32];
17561760
let seed_2 = [43 as u8; 32];
17571761
let seed_3 = [44 as u8; 32];
1762+
let seed_4 = [45 as u8; 32];
17581763
let cross_node_seed = [44 as u8; 32];
17591764
chanmon_cfgs[2].keys_manager.backing = PhantomKeysManager::new(&seed_1, 43, 44, &cross_node_seed);
17601765
chanmon_cfgs[3].keys_manager.backing = PhantomKeysManager::new(&seed_2, 43, 44, &cross_node_seed);
17611766
chanmon_cfgs[4].keys_manager.backing = PhantomKeysManager::new(&seed_3, 43, 44, &cross_node_seed);
1762-
let node_cfgs = create_node_cfgs(5, &chanmon_cfgs);
1763-
let node_chanmgrs = create_node_chanmgrs(5, &node_cfgs, &[None, None, None, None, None]);
1764-
let nodes = create_network(5, &node_cfgs, &node_chanmgrs);
1767+
chanmon_cfgs[5].keys_manager.backing = PhantomKeysManager::new(&seed_4, 43, 44, &cross_node_seed);
1768+
let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
1769+
let node_chanmgrs = create_node_chanmgrs(6, &node_cfgs, &[None, None, None, None, None, None]);
1770+
let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
17651771

17661772
// Setup each phantom node with two channels from distinct peers.
1767-
let _chan_0_2 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 2, 10_000, 0);
1773+
let chan_0_2 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 2, 10_000, 0);
17681774
let chan_1_2 = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 2, 20_000, 0);
17691775
let chan_0_3 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 3, 20_000, 0);
17701776
let _chan_1_3 = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 3, 10_000, 0);
17711777
let chan_0_4 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 4, 20_000, 0);
17721778
let _chan_1_4 = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000, 0);
1779+
let _chan_0_5 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 5, 20_000, 0);
1780+
let _chan_1_5 = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000, 0);
1781+
1782+
// Set invoice amount > all channels inbound so that every one is eligible for inclusion
1783+
// and hints will be sorted by largest inbound capacity.
1784+
let invoice_amt = Some(100_000_000);
17731785

1786+
// With 4 phantom nodes, assert that we include 1 hint per node, up to 3 nodes.
17741787
let mut scid_aliases = HashSet::new();
17751788
scid_aliases.insert(chan_1_2.0.short_channel_id_alias.unwrap());
17761789
scid_aliases.insert(chan_0_3.0.short_channel_id_alias.unwrap());
17771790
scid_aliases.insert(chan_0_4.0.short_channel_id_alias.unwrap());
17781791

1779-
// Since the invoice amount is above all channels inbound, every one is eligible and we'll
1780-
// sort by largest inbound capacity. Test that we include one channel from each of our
1781-
// three nodes.
17821792
match_multi_node_invoice_routes(
1783-
Some(100_000_000),
1793+
invoice_amt,
1794+
&nodes[3],
1795+
vec![&nodes[2], &nodes[3], &nodes[4], &nodes[5]],
1796+
scid_aliases,
1797+
false,
1798+
);
1799+
1800+
// With 2 phantom nodes, assert that we include no more than 3 hints.
1801+
let mut scid_aliases = HashSet::new();
1802+
scid_aliases.insert(chan_1_2.0.short_channel_id_alias.unwrap());
1803+
scid_aliases.insert(chan_0_3.0.short_channel_id_alias.unwrap());
1804+
scid_aliases.insert(chan_0_2.0.short_channel_id_alias.unwrap());
1805+
1806+
match_multi_node_invoice_routes(
1807+
invoice_amt,
17841808
&nodes[3],
1785-
vec![&nodes[2], &nodes[3], &nodes[4],],
1809+
vec![&nodes[2], &nodes[3],],
17861810
scid_aliases,
17871811
false,
17881812
);

0 commit comments

Comments
 (0)