Skip to content

Commit 486df78

Browse files
committed
Add first_hops to generate_routes benchmarks
Passing first_hops to get_route increases the coverage of the benchmark test. For scorers needing the sending node, it allows for using a single scorer in the benchmark rather than re-initializing on each iteration. As a consequence, the scorer can be seeded with success and failure data.
1 parent 06053fc commit 486df78

File tree

1 file changed

+45
-7
lines changed

1 file changed

+45
-7
lines changed

lightning/src/routing/router.rs

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4963,8 +4963,11 @@ pub(crate) mod test_utils {
49634963
#[cfg(all(test, feature = "unstable", not(feature = "no-std")))]
49644964
mod benches {
49654965
use super::*;
4966-
use bitcoin::secp256k1::PublicKey;
4967-
use ln::features::InvoiceFeatures;
4966+
use bitcoin::hashes::Hash;
4967+
use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
4968+
use chain::transaction::OutPoint;
4969+
use ln::channelmanager::{ChannelCounterparty, ChannelDetails};
4970+
use ln::features::{InitFeatures, InvoiceFeatures};
49684971
use routing::scoring::Scorer;
49694972
use util::logger::{Logger, Record};
49704973

@@ -4980,6 +4983,40 @@ mod benches {
49804983
NetworkGraph::read(&mut d).unwrap()
49814984
}
49824985

4986+
fn payer_pubkey() -> PublicKey {
4987+
let secp_ctx = Secp256k1::new();
4988+
PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap())
4989+
}
4990+
4991+
#[inline]
4992+
fn first_hop(node_id: PublicKey) -> ChannelDetails {
4993+
ChannelDetails {
4994+
channel_id: [0; 32],
4995+
counterparty: ChannelCounterparty {
4996+
features: InitFeatures::known(),
4997+
node_id,
4998+
unspendable_punishment_reserve: 0,
4999+
forwarding_info: None,
5000+
},
5001+
funding_txo: Some(OutPoint {
5002+
txid: bitcoin::Txid::from_slice(&[0; 32]).unwrap(), index: 0
5003+
}),
5004+
short_channel_id: Some(1),
5005+
channel_value_satoshis: 10_000_000,
5006+
user_channel_id: 0,
5007+
balance_msat: 10_000_000,
5008+
outbound_capacity_msat: 10_000_000,
5009+
inbound_capacity_msat: 0,
5010+
unspendable_punishment_reserve: None,
5011+
confirmations_required: None,
5012+
force_close_spend_delay: None,
5013+
is_outbound: true,
5014+
is_funding_locked: true,
5015+
is_usable: true,
5016+
is_public: true,
5017+
}
5018+
}
5019+
49835020
#[bench]
49845021
fn generate_routes_with_default_scorer(bench: &mut Bencher) {
49855022
let network_graph = read_network_graph();
@@ -4998,6 +5035,7 @@ mod benches {
49985035
bench: &mut Bencher, graph: &NetworkGraph, scorer: S, features: InvoiceFeatures
49995036
) {
50005037
let nodes = graph.read_only().nodes().clone();
5038+
let payer = payer_pubkey();
50015039

50025040
// First, get 100 (source, destination) pairs for which route-getting actually succeeds...
50035041
let mut path_endpoints = Vec::new();
@@ -5009,9 +5047,10 @@ mod benches {
50095047
seed *= 0xdeadbeef;
50105048
let dst = PublicKey::from_slice(nodes.keys().skip(seed % nodes.len()).next().unwrap().as_slice()).unwrap();
50115049
let params = PaymentParameters::from_node_id(dst).with_features(features.clone());
5050+
let first_hop = first_hop(src);
50125051
let amt = seed as u64 % 1_000_000;
5013-
if get_route(&src, &params, &graph, None, amt, 42, &DummyLogger{}, &scorer).is_ok() {
5014-
path_endpoints.push((src, dst, amt));
5052+
if get_route(&payer, &params, &graph, Some(&[&first_hop]), amt, 42, &DummyLogger{}, &scorer).is_ok() {
5053+
path_endpoints.push((first_hop, params, amt));
50155054
continue 'load_endpoints;
50165055
}
50175056
}
@@ -5020,9 +5059,8 @@ mod benches {
50205059
// ...then benchmark finding paths between the nodes we learned.
50215060
let mut idx = 0;
50225061
bench.iter(|| {
5023-
let (src, dst, amt) = path_endpoints[idx % path_endpoints.len()];
5024-
let params = PaymentParameters::from_node_id(dst).with_features(features.clone());
5025-
assert!(get_route(&src, &params, &graph, None, amt, 42, &DummyLogger{}, &scorer).is_ok());
5062+
let (first_hop, params, amt) = &path_endpoints[idx % path_endpoints.len()];
5063+
assert!(get_route(&payer, params, &graph, Some(&[first_hop]), *amt, 42, &DummyLogger{}, &scorer).is_ok());
50265064
idx += 1;
50275065
});
50285066
}

0 commit comments

Comments
 (0)