Add first_hops to generate_routes benchmarks
authorJeffrey Czyz <jkczyz@gmail.com>
Sun, 16 Jan 2022 16:04:11 +0000 (10:04 -0600)
committerJeffrey Czyz <jkczyz@gmail.com>
Tue, 25 Jan 2022 01:05:49 +0000 (19:05 -0600)
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.

lightning/src/routing/router.rs

index 397042f8e1248923ff4bf24edd844a4eba6ddc55..49d82f344ad44d313052bc148127abf90cde4ee7 100644 (file)
@@ -4963,8 +4963,11 @@ pub(crate) mod test_utils {
 #[cfg(all(test, feature = "unstable", not(feature = "no-std")))]
 mod benches {
        use super::*;
-       use bitcoin::secp256k1::PublicKey;
-       use ln::features::InvoiceFeatures;
+       use bitcoin::hashes::Hash;
+       use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
+       use chain::transaction::OutPoint;
+       use ln::channelmanager::{ChannelCounterparty, ChannelDetails};
+       use ln::features::{InitFeatures, InvoiceFeatures};
        use routing::scoring::Scorer;
        use util::logger::{Logger, Record};
 
@@ -4980,6 +4983,40 @@ mod benches {
                NetworkGraph::read(&mut d).unwrap()
        }
 
+       fn payer_pubkey() -> PublicKey {
+               let secp_ctx = Secp256k1::new();
+               PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap())
+       }
+
+       #[inline]
+       fn first_hop(node_id: PublicKey) -> ChannelDetails {
+               ChannelDetails {
+                       channel_id: [0; 32],
+                       counterparty: ChannelCounterparty {
+                               features: InitFeatures::known(),
+                               node_id,
+                               unspendable_punishment_reserve: 0,
+                               forwarding_info: None,
+                       },
+                       funding_txo: Some(OutPoint {
+                               txid: bitcoin::Txid::from_slice(&[0; 32]).unwrap(), index: 0
+                       }),
+                       short_channel_id: Some(1),
+                       channel_value_satoshis: 10_000_000,
+                       user_channel_id: 0,
+                       balance_msat: 10_000_000,
+                       outbound_capacity_msat: 10_000_000,
+                       inbound_capacity_msat: 0,
+                       unspendable_punishment_reserve: None,
+                       confirmations_required: None,
+                       force_close_spend_delay: None,
+                       is_outbound: true,
+                       is_funding_locked: true,
+                       is_usable: true,
+                       is_public: true,
+               }
+       }
+
        #[bench]
        fn generate_routes_with_default_scorer(bench: &mut Bencher) {
                let network_graph = read_network_graph();
@@ -4998,6 +5035,7 @@ mod benches {
                bench: &mut Bencher, graph: &NetworkGraph, scorer: S, features: InvoiceFeatures
        ) {
                let nodes = graph.read_only().nodes().clone();
+               let payer = payer_pubkey();
 
                // First, get 100 (source, destination) pairs for which route-getting actually succeeds...
                let mut path_endpoints = Vec::new();
@@ -5009,9 +5047,10 @@ mod benches {
                                seed *= 0xdeadbeef;
                                let dst = PublicKey::from_slice(nodes.keys().skip(seed % nodes.len()).next().unwrap().as_slice()).unwrap();
                                let params = PaymentParameters::from_node_id(dst).with_features(features.clone());
+                               let first_hop = first_hop(src);
                                let amt = seed as u64 % 1_000_000;
-                               if get_route(&src, &params, &graph, None, amt, 42, &DummyLogger{}, &scorer).is_ok() {
-                                       path_endpoints.push((src, dst, amt));
+                               if get_route(&payer, &params, &graph, Some(&[&first_hop]), amt, 42, &DummyLogger{}, &scorer).is_ok() {
+                                       path_endpoints.push((first_hop, params, amt));
                                        continue 'load_endpoints;
                                }
                        }
@@ -5020,9 +5059,8 @@ mod benches {
                // ...then benchmark finding paths between the nodes we learned.
                let mut idx = 0;
                bench.iter(|| {
-                       let (src, dst, amt) = path_endpoints[idx % path_endpoints.len()];
-                       let params = PaymentParameters::from_node_id(dst).with_features(features.clone());
-                       assert!(get_route(&src, &params, &graph, None, amt, 42, &DummyLogger{}, &scorer).is_ok());
+                       let (first_hop, params, amt) = &path_endpoints[idx % path_endpoints.len()];
+                       assert!(get_route(&payer, params, &graph, Some(&[first_hop]), *amt, 42, &DummyLogger{}, &scorer).is_ok());
                        idx += 1;
                });
        }