Skip to content

Commit 1c84014

Browse files
authored
Merge pull request #798 from TheBlueMatt/2021-02-route-bench
2 parents 879e309 + 454e01d commit 1c84014

File tree

6 files changed

+89
-7
lines changed

6 files changed

+89
-7
lines changed

.github/workflows/build.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,39 @@ jobs:
126126
token: f421b687-4dc2-4387-ac3d-dc3b2528af57
127127
fail_ci_if_error: true
128128

129+
benchmark:
130+
runs-on: ubuntu-latest
131+
env:
132+
TOOLCHAIN: nightly
133+
steps:
134+
- name: Checkout source code
135+
uses: actions/checkout@v2
136+
- name: Install Rust ${{ env.TOOLCHAIN }} toolchain
137+
uses: actions-rs/toolchain@v1
138+
with:
139+
toolchain: ${{ env.TOOLCHAIN }}
140+
override: true
141+
profile: minimal
142+
- name: Cache routing graph snapshot
143+
id: cache-graph
144+
uses: actions/cache@v2
145+
with:
146+
path: lightning/net_graph-2021-02-12.bin
147+
key: net_graph-2021-02-12
148+
- name: Fetch routing graph snapshot
149+
if: steps.cache-graph.outputs.cache-hit != 'true'
150+
run: |
151+
wget -O lightning/net_graph-2021-02-12.bin https://bitcoin.ninja/ldk-net_graph-879e309c128-2020-02-12.bin
152+
if [ "$(sha256sum lightning/net_graph-2021-02-12.bin | awk '{ print $1 }')" != "890a1f80dfb6ef674a1e4ff0f23cd73d740731c395f99d85abbede0cfbb701ab" ]; then
153+
echo "Bad hash"
154+
exit 1
155+
fi
156+
- name: Run benchmarks on Rust ${{ matrix.toolchain }}
157+
run: |
158+
cd lightning
159+
cargo bench --features unstable
160+
cd ..
161+
129162
check_commits:
130163
runs-on: ubuntu-latest
131164
env:

lightning/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ max_level_debug = []
2323
# Allow signing of local transactions that may have been revoked or will be revoked, for functional testing (e.g. justice tx handling).
2424
# This is unsafe to use in production because it may result in the counterparty publishing taking our funds.
2525
unsafe_revoked_tx_signing = []
26+
unstable = []
2627

2728
[dependencies]
2829
bitcoin = "0.24"

lightning/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
#![allow(bare_trait_objects)]
2828
#![allow(ellipsis_inclusive_range_patterns)]
2929

30+
#![cfg_attr(all(test, feature = "unstable"), feature(test))]
31+
#[cfg(all(test, feature = "unstable"))] extern crate test;
32+
3033
extern crate bitcoin;
3134
#[cfg(any(test, feature = "_test_utils"))] extern crate hex;
3235
#[cfg(any(test, feature = "fuzztarget", feature = "_test_utils"))] extern crate regex;

lightning/src/ln/functional_tests.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,7 +1583,7 @@ fn test_fee_spike_violation_fails_htlc() {
15831583
let route = get_route(&nodes[0].node.get_our_node_id(), net_graph_msg_handler, &nodes.last().unwrap().node.get_our_node_id(), None, &Vec::new(), $recv_value, TEST_FINAL_CLTV, &logger).unwrap();
15841584
(route, payment_hash, payment_preimage)
15851585
}}
1586-
};
1586+
}
15871587

15881588
let (route, payment_hash, _) = get_route_and_payment_hash!(3460001);
15891589
// Need to manually create the update_add_htlc message to go around the channel reserve check in send_htlc()
@@ -1722,7 +1722,7 @@ fn test_chan_reserve_violation_outbound_htlc_inbound_chan() {
17221722
let route = get_route(&nodes[1].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes.first().unwrap().node.get_our_node_id(), None, &Vec::new(), $recv_value, TEST_FINAL_CLTV, &logger).unwrap();
17231723
(route, payment_hash, payment_preimage)
17241724
}}
1725-
};
1725+
}
17261726

17271727
let (route, our_payment_hash, _) = get_route_and_payment_hash!(1000);
17281728
unwrap_send_err!(nodes[1].node.send_payment(&route, our_payment_hash, &None), true, APIError::ChannelUnavailable { ref err },
@@ -1754,7 +1754,7 @@ fn test_chan_reserve_violation_inbound_htlc_outbound_channel() {
17541754
let route = get_route(&nodes[1].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes.first().unwrap().node.get_our_node_id(), None, &Vec::new(), $recv_value, TEST_FINAL_CLTV, &logger).unwrap();
17551755
(route, payment_hash, payment_preimage)
17561756
}}
1757-
};
1757+
}
17581758

17591759
let (route, payment_hash, _) = get_route_and_payment_hash!(1000);
17601760
// Need to manually create the update_add_htlc message to go around the channel reserve check in send_htlc()
@@ -1799,7 +1799,7 @@ fn test_chan_reserve_violation_inbound_htlc_inbound_chan() {
17991799
let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes.last().unwrap().node.get_our_node_id(), None, &Vec::new(), $recv_value, TEST_FINAL_CLTV, &logger).unwrap();
18001800
(route, payment_hash, payment_preimage)
18011801
}}
1802-
};
1802+
}
18031803

18041804
let feemsat = 239;
18051805
let total_routing_fee_msat = (nodes.len() - 2) as u64 * feemsat;
@@ -1900,7 +1900,7 @@ fn test_channel_reserve_holding_cell_htlcs() {
19001900
let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes.last().unwrap().node.get_our_node_id(), None, &Vec::new(), $recv_value, TEST_FINAL_CLTV, &logger).unwrap();
19011901
(route, payment_hash, payment_preimage)
19021902
}}
1903-
};
1903+
}
19041904

19051905
macro_rules! expect_forward {
19061906
($node: expr) => {{

lightning/src/routing/router.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,3 +1279,48 @@ mod tests {
12791279
assert_eq!(route.paths[0][1].channel_features.le_flags(), &[0; 0]); // We can't learn any flags from invoices, sadly
12801280
}
12811281
}
1282+
1283+
#[cfg(all(test, feature = "unstable"))]
1284+
mod benches {
1285+
use super::*;
1286+
use util::logger::{Logger, Record};
1287+
1288+
use std::fs::File;
1289+
use test::Bencher;
1290+
1291+
struct DummyLogger {}
1292+
impl Logger for DummyLogger {
1293+
fn log(&self, _record: &Record) {}
1294+
}
1295+
1296+
#[bench]
1297+
fn generate_routes(bench: &mut Bencher) {
1298+
let mut d = File::open("net_graph-2021-02-12.bin").expect("Please fetch https://bitcoin.ninja/ldk-net_graph-879e309c128-2020-02-12.bin and place it at lightning/net_graph-2021-02-12.bin");
1299+
let graph = NetworkGraph::read(&mut d).unwrap();
1300+
1301+
// First, get 100 (source, destination) pairs for which route-getting actually succeeds...
1302+
let mut path_endpoints = Vec::new();
1303+
let mut seed: usize = 0xdeadbeef;
1304+
'load_endpoints: for _ in 0..100 {
1305+
loop {
1306+
seed *= 0xdeadbeef;
1307+
let src = graph.get_nodes().keys().skip(seed % graph.get_nodes().len()).next().unwrap();
1308+
seed *= 0xdeadbeef;
1309+
let dst = graph.get_nodes().keys().skip(seed % graph.get_nodes().len()).next().unwrap();
1310+
let amt = seed as u64 % 1_000_000;
1311+
if get_route(src, &graph, dst, None, &[], amt, 42, &DummyLogger{}).is_ok() {
1312+
path_endpoints.push((src, dst, amt));
1313+
continue 'load_endpoints;
1314+
}
1315+
}
1316+
}
1317+
1318+
// ...then benchmark finding paths between the nodes we learned.
1319+
let mut idx = 0;
1320+
bench.iter(|| {
1321+
let (src, dst, amt) = path_endpoints[idx % path_endpoints.len()];
1322+
assert!(get_route(src, &graph, dst, None, &[], amt, 42, &DummyLogger{}).is_ok());
1323+
idx += 1;
1324+
});
1325+
}
1326+
}

lightning/src/util/chacha20.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ mod test {
328328
key: [u8; 32],
329329
nonce: [u8; 8],
330330
keystream: Vec<u8>,
331-
};
331+
}
332332
// taken from http://tools.ietf.org/html/draft-agl-tls-chacha20poly1305-04
333333
let test_vectors = vec!(
334334
TestVector{
@@ -463,7 +463,7 @@ mod test {
463463
key: [u8; 32],
464464
nonce: [u8; 12],
465465
keystream: Vec<u8>,
466-
};
466+
}
467467
// taken from http://tools.ietf.org/html/draft-agl-tls-chacha20poly1305-04
468468
let test_vectors = vec!(
469469
TestVector{

0 commit comments

Comments
 (0)