Skip to content

Commit 4438f86

Browse files
author
cdmoss
committed
limit amount of hints resulting from filter_channels to 20 and shuffles hints
1 parent d4dc05b commit 4438f86

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

lightning-invoice/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ num-traits = { version = "0.2.8", default-features = false }
2727
bitcoin_hashes = { version = "0.11", default-features = false }
2828
hashbrown = { version = "0.8", optional = true }
2929
serde = { version = "1.0.118", optional = true }
30+
rand = "0.8.5"
3031

3132
[dev-dependencies]
3233
lightning = { version = "0.0.112", path = "../lightning", default-features = false, features = ["_test_utils"] }

lightning-invoice/src/utils.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ use lightning::util::logger::Logger;
2020
use secp256k1::PublicKey;
2121
use core::ops::Deref;
2222
use core::time::Duration;
23+
use rand::thread_rng;
24+
use rand::seq::SliceRandom;
2325

2426
#[cfg(feature = "std")]
2527
/// Utility to create an invoice that can be paid to one of multiple nodes, or a "phantom invoice."
@@ -529,7 +531,7 @@ fn filter_channels<L: Deref>(
529531
// the payment value and where we're currently connected to the channel counterparty.
530532
// Even if we cannot satisfy both goals, always ensure we include *some* hints, preferring
531533
// those which meet at least one criteria.
532-
filtered_channels
534+
let mut final_channels = filtered_channels
533535
.into_iter()
534536
.map(|(_, channel)| channel)
535537
.filter(|channel| {
@@ -562,7 +564,13 @@ fn filter_channels<L: Deref>(
562564
include_channel
563565
})
564566
.map(route_hint_from_channel)
565-
.collect::<Vec<RouteHint>>()
567+
// Following LND's lead by taking max 20 hints
568+
.take(20)
569+
.collect::<Vec<RouteHint>>();
570+
571+
// Shuffle selected channels to prevent channel exhaustion
572+
final_channels.shuffle(&mut thread_rng());
573+
final_channels
566574
}
567575

568576
impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> Payer for ChannelManager<M, T, K, F, L>

0 commit comments

Comments
 (0)