From: Matt Corallo Date: Sat, 27 Mar 2021 01:50:54 +0000 (-0400) Subject: Add test X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=27ea7be0cd9a62a36bde4def93dcdd08bcc28411;p=rust-lightning Add test --- diff --git a/lightning/src/routing/router.rs b/lightning/src/routing/router.rs index eee8aff42..2ae3218e1 100644 --- a/lightning/src/routing/router.rs +++ b/lightning/src/routing/router.rs @@ -3741,6 +3741,77 @@ mod tests { assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags(13)); } } + + /// Tries to open a network graph file, or panics with a URL to fetch it. + pub(super) fn get_route_file() -> std::fs::File { + File::open("net_graph-2021-02-12.bin") // By default we're run in RL/lightning + .or_else(|_| File::open("lightning/net_graph-2021-02-12.bin")) // We may be run manually in RL/ + .or_else(|_| { // Fall back to guessing based on the binary location + // path is likely something like .../rust-lightning/target/debug/deps/lightning-... + let mut path = std::env::current_exe().unwrap(); + path.pop(); // lightning-... + path.pop(); // deps + path.pop(); // debug + path.pop(); // target + path.push("lightning"); + path.push("net_graph-2021-02-12.bin"); + eprintln!("{}", path.to_str().unwrap()); + File::open(path) + }) + .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") + } + + pub(super) fn random_init_seed() -> u64 { + // Because the default HashMap in std pulls OS randomness, we can use it as a (bad) RNG. + use std::hash::{BuildHasher, Hasher}; + let seed = std::collections::hash_map::RandomState::new().build_hasher().finish(); + println!("Using seed of {}", seed); + seed + } + + use std::fs::File; + use util::ser::Readable; + #[test] + fn generate_routes() { + let mut d = get_route_file(); + let graph = NetworkGraph::read(&mut d).unwrap(); + + // First, get 100 (source, destination) pairs for which route-getting actually succeeds... + let mut seed = random_init_seed() as usize; + 'load_endpoints: for _ in 0..10 { + loop { + seed = seed.overflowing_mul(0xdeadbeef).0; + let src = graph.get_nodes().keys().skip(seed % graph.get_nodes().len()).next().unwrap(); + seed = seed.overflowing_mul(0xdeadbeef).0; + let dst = graph.get_nodes().keys().skip(seed % graph.get_nodes().len()).next().unwrap(); + let amt = seed as u64 % 200_000_000; + if get_route(src, &graph, dst, None, None, &[], amt, 42, &test_utils::TestLogger::new()).is_ok() { + continue 'load_endpoints; + } + } + } + } + + #[test] + fn generate_routes_mpp() { + let mut d = get_route_file(); + let graph = NetworkGraph::read(&mut d).unwrap(); + + // First, get 100 (source, destination) pairs for which route-getting actually succeeds... + let mut seed = random_init_seed() as usize; + 'load_endpoints: for _ in 0..10 { + loop { + seed = seed.overflowing_mul(0xdeadbeef).0; + let src = graph.get_nodes().keys().skip(seed % graph.get_nodes().len()).next().unwrap(); + seed = seed.overflowing_mul(0xdeadbeef).0; + let dst = graph.get_nodes().keys().skip(seed % graph.get_nodes().len()).next().unwrap(); + let amt = seed as u64 % 200_000_000; + if get_route(src, &graph, dst, Some(InvoiceFeatures::known()), None, &[], amt, 42, &test_utils::TestLogger::new()).is_ok() { + continue 'load_endpoints; + } + } + } + } } #[cfg(all(test, feature = "unstable"))] @@ -3758,7 +3829,7 @@ mod benches { #[bench] fn generate_routes(bench: &mut Bencher) { - 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"); + let mut d = tests::get_route_file(); let graph = NetworkGraph::read(&mut d).unwrap(); // First, get 100 (source, destination) pairs for which route-getting actually succeeds... @@ -3789,7 +3860,7 @@ mod benches { #[bench] fn generate_mpp_routes(bench: &mut Bencher) { - 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"); + let mut d = tests::get_route_file(); let graph = NetworkGraph::read(&mut d).unwrap(); // First, get 100 (source, destination) pairs for which route-getting actually succeeds...