From: Jeffrey Czyz Date: Thu, 20 Jan 2022 06:41:04 +0000 (-0600) Subject: f - Use FixedPenaltyScorer in tests X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=25725caf4131739003d24abc9d03add049f1fe56;p=rust-lightning f - Use FixedPenaltyScorer in tests --- diff --git a/fuzz/src/full_stack.rs b/fuzz/src/full_stack.rs index 34c6f554c..e8e40cd29 100644 --- a/fuzz/src/full_stack.rs +++ b/fuzz/src/full_stack.rs @@ -39,7 +39,7 @@ use lightning::ln::msgs::DecodeError; use lightning::ln::script::ShutdownScript; use lightning::routing::network_graph::{NetGraphMsgHandler, NetworkGraph}; use lightning::routing::router::{find_route, Payee, RouteParameters}; -use lightning::routing::scoring::Scorer; +use lightning::routing::scoring::FixedPenaltyScorer; use lightning::util::config::UserConfig; use lightning::util::errors::APIError; use lightning::util::events::Event; @@ -390,7 +390,7 @@ pub fn do_test(data: &[u8], logger: &Arc) { let our_id = PublicKey::from_secret_key(&Secp256k1::signing_only(), &keys_manager.get_node_secret()); let network_graph = Arc::new(NetworkGraph::new(genesis_block(network).block_hash())); let net_graph_msg_handler = Arc::new(NetGraphMsgHandler::new(Arc::clone(&network_graph), None, Arc::clone(&logger))); - let scorer = Scorer::with_fixed_penalty(0); + let scorer = FixedPenaltyScorer::with_penalty(0); let peers = RefCell::new([false; 256]); let mut loss_detector = MoneyLossDetector::new(&peers, channelmanager.clone(), monitor.clone(), PeerManager::new(MessageHandler { diff --git a/fuzz/src/router.rs b/fuzz/src/router.rs index d6aa97e42..45f562dab 100644 --- a/fuzz/src/router.rs +++ b/fuzz/src/router.rs @@ -17,7 +17,7 @@ use lightning::ln::channelmanager::{ChannelDetails, ChannelCounterparty}; use lightning::ln::features::InitFeatures; use lightning::ln::msgs; use lightning::routing::router::{find_route, Payee, RouteHint, RouteHintHop, RouteParameters}; -use lightning::routing::scoring::Scorer; +use lightning::routing::scoring::FixedPenaltyScorer; use lightning::util::logger::Logger; use lightning::util::ser::Readable; use lightning::routing::network_graph::{NetworkGraph, RoutingFees}; @@ -249,7 +249,7 @@ pub fn do_test(data: &[u8], out: Out) { }])); } } - let scorer = Scorer::with_fixed_penalty(0); + let scorer = FixedPenaltyScorer::with_penalty(0); for target in node_pks.iter() { let params = RouteParameters { payee: Payee::from_node_id(*target).with_route_hints(last_hops.clone()), diff --git a/lightning-background-processor/src/lib.rs b/lightning-background-processor/src/lib.rs index 2dbc8053b..0aaa43d7e 100644 --- a/lightning-background-processor/src/lib.rs +++ b/lightning-background-processor/src/lib.rs @@ -658,7 +658,7 @@ mod tests { let data_dir = nodes[0].persister.get_data_dir(); let persister = move |node: &ChannelManager, Arc, Arc, Arc, Arc>| FilesystemPersister::persist_manager(data_dir.clone(), node); let router = DefaultRouter::new(Arc::clone(&nodes[0].network_graph), Arc::clone(&nodes[0].logger)); - let scorer = Arc::new(Mutex::new(test_utils::TestScorer::default())); + let scorer = Arc::new(Mutex::new(test_utils::TestScorer::with_penalty(0))); let invoice_payer = Arc::new(InvoicePayer::new(Arc::clone(&nodes[0].node), router, scorer, Arc::clone(&nodes[0].logger), |_: &_| {}, RetryAttempts(2))); let event_handler = Arc::clone(&invoice_payer); let bg_processor = BackgroundProcessor::start(persister, event_handler, nodes[0].chain_monitor.clone(), nodes[0].node.clone(), nodes[0].net_graph_msg_handler.clone(), nodes[0].peer_manager.clone(), nodes[0].logger.clone()); diff --git a/lightning-invoice/src/utils.rs b/lightning-invoice/src/utils.rs index ffa84c98e..e4267b44c 100644 --- a/lightning-invoice/src/utils.rs +++ b/lightning-invoice/src/utils.rs @@ -228,7 +228,7 @@ mod test { let first_hops = nodes[0].node.list_usable_channels(); let network_graph = node_cfgs[0].network_graph; let logger = test_utils::TestLogger::new(); - let scorer = test_utils::TestScorer::with_fixed_penalty(0); + let scorer = test_utils::TestScorer::with_penalty(0); let route = find_route( &nodes[0].node.get_our_node_id(), ¶ms, network_graph, Some(&first_hops.iter().collect::>()), &logger, &scorer, diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index 77e9152d4..3e55fe913 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -6895,7 +6895,7 @@ mod tests { let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]); let nodes = create_network(2, &node_cfgs, &node_chanmgrs); create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()); - let scorer = test_utils::TestScorer::with_fixed_penalty(0); + let scorer = test_utils::TestScorer::with_penalty(0); // To start (1), send a regular payment but don't claim it. let expected_route = [&nodes[1]]; @@ -7000,7 +7000,7 @@ mod tests { }; let network_graph = nodes[0].network_graph; let first_hops = nodes[0].node.list_usable_channels(); - let scorer = test_utils::TestScorer::with_fixed_penalty(0); + let scorer = test_utils::TestScorer::with_penalty(0); let route = find_route( &payer_pubkey, ¶ms, network_graph, Some(&first_hops.iter().collect::>()), nodes[0].logger, &scorer @@ -7043,7 +7043,7 @@ mod tests { }; let network_graph = nodes[0].network_graph; let first_hops = nodes[0].node.list_usable_channels(); - let scorer = test_utils::TestScorer::with_fixed_penalty(0); + let scorer = test_utils::TestScorer::with_penalty(0); let route = find_route( &payer_pubkey, ¶ms, network_graph, Some(&first_hops.iter().collect::>()), nodes[0].logger, &scorer @@ -7137,7 +7137,6 @@ pub mod bench { use ln::msgs::{ChannelMessageHandler, Init}; use routing::network_graph::NetworkGraph; use routing::router::{Payee, get_route}; - use routing::scoring::Scorer; use util::test_utils; use util::config::UserConfig; use util::events::{Event, MessageSendEvent, MessageSendEventsProvider, PaymentPurpose}; @@ -7247,7 +7246,7 @@ pub mod bench { let usable_channels = $node_a.list_usable_channels(); let payee = Payee::from_node_id($node_b.get_our_node_id()) .with_features(InvoiceFeatures::known()); - let scorer = Scorer::with_fixed_penalty(0); + let scorer = test_utils::TestScorer::with_penalty(0); let route = get_route(&$node_a.get_our_node_id(), &payee, &dummy_graph, Some(&usable_channels.iter().map(|r| r).collect::>()), 10_000, TEST_FINAL_CLTV, &logger_a, &scorer).unwrap(); diff --git a/lightning/src/ln/functional_test_utils.rs b/lightning/src/ln/functional_test_utils.rs index 2e8994b58..b544936ce 100644 --- a/lightning/src/ln/functional_test_utils.rs +++ b/lightning/src/ln/functional_test_utils.rs @@ -1078,7 +1078,7 @@ macro_rules! get_route_and_payment_hash { let payee = $crate::routing::router::Payee::from_node_id($recv_node.node.get_our_node_id()) .with_features($crate::ln::features::InvoiceFeatures::known()) .with_route_hints($last_hops); - let scorer = $crate::util::test_utils::TestScorer::with_fixed_penalty(0); + let scorer = $crate::util::test_utils::TestScorer::with_penalty(0); let route = $crate::routing::router::get_route( &$send_node.node.get_our_node_id(), &payee, $send_node.network_graph, Some(&$send_node.node.list_usable_channels().iter().collect::>()), @@ -1533,7 +1533,7 @@ pub const TEST_FINAL_CLTV: u32 = 70; pub fn route_payment<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_route: &[&Node<'a, 'b, 'c>], recv_value: u64) -> (PaymentPreimage, PaymentHash, PaymentSecret) { let payee = Payee::from_node_id(expected_route.last().unwrap().node.get_our_node_id()) .with_features(InvoiceFeatures::known()); - let scorer = test_utils::TestScorer::with_fixed_penalty(0); + let scorer = test_utils::TestScorer::with_penalty(0); let route = get_route( &origin_node.node.get_our_node_id(), &payee, &origin_node.network_graph, Some(&origin_node.node.list_usable_channels().iter().collect::>()), @@ -1551,7 +1551,7 @@ pub fn route_payment<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_route: pub fn route_over_limit<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_route: &[&Node<'a, 'b, 'c>], recv_value: u64) { let payee = Payee::from_node_id(expected_route.last().unwrap().node.get_our_node_id()) .with_features(InvoiceFeatures::known()); - let scorer = test_utils::TestScorer::with_fixed_penalty(0); + let scorer = test_utils::TestScorer::with_penalty(0); let route = get_route(&origin_node.node.get_our_node_id(), &payee, origin_node.network_graph, None, recv_value, TEST_FINAL_CLTV, origin_node.logger, &scorer).unwrap(); assert_eq!(route.paths.len(), 1); assert_eq!(route.paths[0].len(), expected_route.len()); diff --git a/lightning/src/ln/functional_tests.rs b/lightning/src/ln/functional_tests.rs index 0993caf4c..fc5260fe9 100644 --- a/lightning/src/ln/functional_tests.rs +++ b/lightning/src/ln/functional_tests.rs @@ -7350,7 +7350,7 @@ fn test_check_htlc_underpaying() { // Create some initial channels create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()); - let scorer = test_utils::TestScorer::with_fixed_penalty(0); + let scorer = test_utils::TestScorer::with_penalty(0); let payee = Payee::from_node_id(nodes[1].node.get_our_node_id()).with_features(InvoiceFeatures::known()); let route = get_route(&nodes[0].node.get_our_node_id(), &payee, nodes[0].network_graph, None, 10_000, TEST_FINAL_CLTV, nodes[0].logger, &scorer).unwrap(); let (_, our_payment_hash, _) = get_payment_preimage_hash!(nodes[0]); @@ -7753,7 +7753,7 @@ fn test_bump_penalty_txn_on_revoked_htlcs() { let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 59000000, InitFeatures::known(), InitFeatures::known()); // Lock HTLC in both directions (using a slightly lower CLTV delay to provide timely RBF bumps) let payee = Payee::from_node_id(nodes[1].node.get_our_node_id()).with_features(InvoiceFeatures::known()); - let scorer = test_utils::TestScorer::with_fixed_penalty(0); + let scorer = test_utils::TestScorer::with_penalty(0); let route = get_route(&nodes[0].node.get_our_node_id(), &payee, &nodes[0].network_graph, None, 3_000_000, 50, nodes[0].logger, &scorer).unwrap(); let payment_preimage = send_along_route(&nodes[0], route, &[&nodes[1]], 3_000_000).0; @@ -9290,7 +9290,7 @@ fn test_keysend_payments_to_public_node() { final_value_msat: 10000, final_cltv_expiry_delta: 40, }; - let scorer = test_utils::TestScorer::with_fixed_penalty(0); + let scorer = test_utils::TestScorer::with_penalty(0); let route = find_route(&payer_pubkey, ¶ms, network_graph, None, nodes[0].logger, &scorer).unwrap(); let test_preimage = PaymentPreimage([42; 32]); @@ -9324,7 +9324,7 @@ fn test_keysend_payments_to_private_node() { }; let network_graph = nodes[0].network_graph; let first_hops = nodes[0].node.list_usable_channels(); - let scorer = test_utils::TestScorer::with_fixed_penalty(0); + let scorer = test_utils::TestScorer::with_penalty(0); let route = find_route( &payer_pubkey, ¶ms, network_graph, Some(&first_hops.iter().collect::>()), nodes[0].logger, &scorer diff --git a/lightning/src/ln/payment_tests.rs b/lightning/src/ln/payment_tests.rs index 960a255e9..a3130af40 100644 --- a/lightning/src/ln/payment_tests.rs +++ b/lightning/src/ln/payment_tests.rs @@ -723,7 +723,7 @@ fn get_ldk_payment_preimage() { let payee = Payee::from_node_id(nodes[1].node.get_our_node_id()) .with_features(InvoiceFeatures::known()); - let scorer = test_utils::TestScorer::with_fixed_penalty(0); + let scorer = test_utils::TestScorer::with_penalty(0); let route = get_route( &nodes[0].node.get_our_node_id(), &payee, &nodes[0].network_graph, Some(&nodes[0].node.list_usable_channels().iter().collect::>()), diff --git a/lightning/src/ln/shutdown_tests.rs b/lightning/src/ln/shutdown_tests.rs index 7afc982fa..f3a07f99c 100644 --- a/lightning/src/ln/shutdown_tests.rs +++ b/lightning/src/ln/shutdown_tests.rs @@ -76,7 +76,7 @@ fn updates_shutdown_wait() { let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()); let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known()); let logger = test_utils::TestLogger::new(); - let scorer = test_utils::TestScorer::with_fixed_penalty(0); + let scorer = test_utils::TestScorer::with_penalty(0); let (payment_preimage, _, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 100000); diff --git a/lightning/src/routing/router.rs b/lightning/src/routing/router.rs index 4955ecc30..9b308ee00 100644 --- a/lightning/src/routing/router.rs +++ b/lightning/src/routing/router.rs @@ -2003,7 +2003,7 @@ mod tests { let (secp_ctx, network_graph, _, _, logger) = build_graph(); let (_, our_id, _, nodes) = get_nodes(&secp_ctx); let payee = Payee::from_node_id(nodes[2]); - let scorer = test_utils::TestScorer::with_fixed_penalty(0); + let scorer = test_utils::TestScorer::with_penalty(0); // Simple route to 2 via 1 @@ -2034,7 +2034,7 @@ mod tests { let (secp_ctx, network_graph, _, _, logger) = build_graph(); let (_, our_id, _, nodes) = get_nodes(&secp_ctx); let payee = Payee::from_node_id(nodes[2]); - let scorer = test_utils::TestScorer::with_fixed_penalty(0); + let scorer = test_utils::TestScorer::with_penalty(0); // Simple route to 2 via 1 @@ -2053,7 +2053,7 @@ mod tests { let (secp_ctx, network_graph, net_graph_msg_handler, _, logger) = build_graph(); let (our_privkey, our_id, privkeys, nodes) = get_nodes(&secp_ctx); let payee = Payee::from_node_id(nodes[2]); - let scorer = test_utils::TestScorer::with_fixed_penalty(0); + let scorer = test_utils::TestScorer::with_penalty(0); // Simple route to 2 via 1 @@ -2178,7 +2178,7 @@ mod tests { let (secp_ctx, network_graph, net_graph_msg_handler, _, logger) = build_graph(); let (our_privkey, our_id, privkeys, nodes) = get_nodes(&secp_ctx); let payee = Payee::from_node_id(nodes[2]).with_features(InvoiceFeatures::known()); - let scorer = test_utils::TestScorer::with_fixed_penalty(0); + let scorer = test_utils::TestScorer::with_penalty(0); // A route to node#2 via two paths. // One path allows transferring 35-40 sats, another one also allows 35-40 sats. @@ -2314,7 +2314,7 @@ mod tests { let (secp_ctx, network_graph, net_graph_msg_handler, _, logger) = build_graph(); let (our_privkey, our_id, privkeys, nodes) = get_nodes(&secp_ctx); let payee = Payee::from_node_id(nodes[2]); - let scorer = test_utils::TestScorer::with_fixed_penalty(0); + let scorer = test_utils::TestScorer::with_penalty(0); // // Disable channels 4 and 12 by flags=2 update_channel(&net_graph_msg_handler, &secp_ctx, &privkeys[1], UnsignedChannelUpdate { @@ -2372,7 +2372,7 @@ mod tests { let (secp_ctx, network_graph, net_graph_msg_handler, _, logger) = build_graph(); let (_, our_id, privkeys, nodes) = get_nodes(&secp_ctx); let payee = Payee::from_node_id(nodes[2]); - let scorer = test_utils::TestScorer::with_fixed_penalty(0); + let scorer = test_utils::TestScorer::with_penalty(0); // Disable nodes 1, 2, and 8 by requiring unknown feature bits let unknown_features = NodeFeatures::known().set_unknown_feature_required(); @@ -2413,7 +2413,7 @@ mod tests { fn our_chans_test() { let (secp_ctx, network_graph, _, _, logger) = build_graph(); let (_, our_id, _, nodes) = get_nodes(&secp_ctx); - let scorer = test_utils::TestScorer::with_fixed_penalty(0); + let scorer = test_utils::TestScorer::with_penalty(0); // Route to 1 via 2 and 3 because our channel to 1 is disabled let payee = Payee::from_node_id(nodes[0]); @@ -2542,7 +2542,7 @@ mod tests { fn partial_route_hint_test() { let (secp_ctx, network_graph, _, _, logger) = build_graph(); let (_, our_id, _, nodes) = get_nodes(&secp_ctx); - let scorer = test_utils::TestScorer::with_fixed_penalty(0); + let scorer = test_utils::TestScorer::with_penalty(0); // Simple test across 2, 3, 5, and 4 via a last_hop channel // Tests the behaviour when the RouteHint contains a suboptimal hop. @@ -2641,7 +2641,7 @@ mod tests { let (secp_ctx, network_graph, _, _, logger) = build_graph(); let (_, our_id, _, nodes) = get_nodes(&secp_ctx); let payee = Payee::from_node_id(nodes[6]).with_route_hints(empty_last_hop(&nodes)); - let scorer = test_utils::TestScorer::with_fixed_penalty(0); + let scorer = test_utils::TestScorer::with_penalty(0); // Test handling of an empty RouteHint passed in Invoice. @@ -2723,7 +2723,7 @@ mod tests { let (secp_ctx, network_graph, net_graph_msg_handler, _, logger) = build_graph(); let (_, our_id, privkeys, nodes) = get_nodes(&secp_ctx); let payee = Payee::from_node_id(nodes[6]).with_route_hints(multi_hint_last_hops(&nodes)); - let scorer = test_utils::TestScorer::with_fixed_penalty(0); + let scorer = test_utils::TestScorer::with_penalty(0); // Test through channels 2, 3, 5, 8. // Test shows that multiple hop hints are considered. @@ -2829,7 +2829,7 @@ mod tests { let (secp_ctx, network_graph, _, _, logger) = build_graph(); let (_, our_id, _, nodes) = get_nodes(&secp_ctx); let payee = Payee::from_node_id(nodes[6]).with_route_hints(last_hops_with_public_channel(&nodes)); - let scorer = test_utils::TestScorer::with_fixed_penalty(0); + let scorer = test_utils::TestScorer::with_penalty(0); // This test shows that public routes can be present in the invoice // which would be handled in the same manner. @@ -2878,7 +2878,7 @@ mod tests { fn our_chans_last_hop_connect_test() { let (secp_ctx, network_graph, _, _, logger) = build_graph(); let (_, our_id, _, nodes) = get_nodes(&secp_ctx); - let scorer = test_utils::TestScorer::with_fixed_penalty(0); + let scorer = test_utils::TestScorer::with_penalty(0); // Simple test with outbound channel to 4 to test that last_hops and first_hops connect let our_chans = vec![get_channel_details(Some(42), nodes[3].clone(), InitFeatures::from_le_bytes(vec![0b11]), 250_000_000)]; @@ -2999,7 +2999,7 @@ mod tests { }]); let payee = Payee::from_node_id(target_node_id).with_route_hints(vec![last_hops]); let our_chans = vec![get_channel_details(Some(42), middle_node_id, InitFeatures::from_le_bytes(vec![0b11]), outbound_capacity_msat)]; - let scorer = test_utils::TestScorer::with_fixed_penalty(0); + let scorer = test_utils::TestScorer::with_penalty(0); get_route(&source_node_id, &payee, &NetworkGraph::new(genesis_block(Network::Testnet).header.block_hash()), Some(&our_chans.iter().collect::>()), route_val, 42, &test_utils::TestLogger::new(), &scorer) } @@ -3053,7 +3053,7 @@ mod tests { let (secp_ctx, network_graph, mut net_graph_msg_handler, chain_monitor, logger) = build_graph(); let (our_privkey, our_id, privkeys, nodes) = get_nodes(&secp_ctx); - let scorer = test_utils::TestScorer::with_fixed_penalty(0); + let scorer = test_utils::TestScorer::with_penalty(0); let payee = Payee::from_node_id(nodes[2]).with_features(InvoiceFeatures::known()); // We will use a simple single-path route from @@ -3325,7 +3325,7 @@ mod tests { // one of the latter hops is limited. let (secp_ctx, network_graph, net_graph_msg_handler, _, logger) = build_graph(); let (our_privkey, our_id, privkeys, nodes) = get_nodes(&secp_ctx); - let scorer = test_utils::TestScorer::with_fixed_penalty(0); + let scorer = test_utils::TestScorer::with_penalty(0); let payee = Payee::from_node_id(nodes[3]).with_features(InvoiceFeatures::known()); // Path via {node7, node2, node4} is channels {12, 13, 6, 11}. @@ -3448,7 +3448,7 @@ mod tests { fn ignore_fee_first_hop_test() { let (secp_ctx, network_graph, net_graph_msg_handler, _, logger) = build_graph(); let (our_privkey, our_id, privkeys, nodes) = get_nodes(&secp_ctx); - let scorer = test_utils::TestScorer::with_fixed_penalty(0); + let scorer = test_utils::TestScorer::with_penalty(0); let payee = Payee::from_node_id(nodes[2]); // Path via node0 is channels {1, 3}. Limit them to 100 and 50 sats (total limit 50). @@ -3494,7 +3494,7 @@ mod tests { fn simple_mpp_route_test() { let (secp_ctx, network_graph, net_graph_msg_handler, _, logger) = build_graph(); let (our_privkey, our_id, privkeys, nodes) = get_nodes(&secp_ctx); - let scorer = test_utils::TestScorer::with_fixed_penalty(0); + let scorer = test_utils::TestScorer::with_penalty(0); let payee = Payee::from_node_id(nodes[2]).with_features(InvoiceFeatures::known()); // We need a route consisting of 3 paths: @@ -3625,7 +3625,7 @@ mod tests { fn long_mpp_route_test() { let (secp_ctx, network_graph, net_graph_msg_handler, _, logger) = build_graph(); let (our_privkey, our_id, privkeys, nodes) = get_nodes(&secp_ctx); - let scorer = test_utils::TestScorer::with_fixed_penalty(0); + let scorer = test_utils::TestScorer::with_penalty(0); let payee = Payee::from_node_id(nodes[3]).with_features(InvoiceFeatures::known()); // We need a route consisting of 3 paths: @@ -3787,7 +3787,7 @@ mod tests { fn mpp_cheaper_route_test() { let (secp_ctx, network_graph, net_graph_msg_handler, _, logger) = build_graph(); let (our_privkey, our_id, privkeys, nodes) = get_nodes(&secp_ctx); - let scorer = test_utils::TestScorer::with_fixed_penalty(0); + let scorer = test_utils::TestScorer::with_penalty(0); let payee = Payee::from_node_id(nodes[3]).with_features(InvoiceFeatures::known()); // This test checks that if we have two cheaper paths and one more expensive path, @@ -3954,7 +3954,7 @@ mod tests { // if the fee is not properly accounted for, the behavior is different. let (secp_ctx, network_graph, net_graph_msg_handler, _, logger) = build_graph(); let (our_privkey, our_id, privkeys, nodes) = get_nodes(&secp_ctx); - let scorer = test_utils::TestScorer::with_fixed_penalty(0); + let scorer = test_utils::TestScorer::with_penalty(0); let payee = Payee::from_node_id(nodes[3]).with_features(InvoiceFeatures::known()); // We need a route consisting of 2 paths: @@ -4133,7 +4133,7 @@ mod tests { // This bug appeared in production in some specific channel configurations. let (secp_ctx, network_graph, net_graph_msg_handler, _, logger) = build_graph(); let (our_privkey, our_id, privkeys, nodes) = get_nodes(&secp_ctx); - let scorer = test_utils::TestScorer::with_fixed_penalty(0); + let scorer = test_utils::TestScorer::with_penalty(0); let payee = Payee::from_node_id(PublicKey::from_slice(&[02; 33]).unwrap()).with_features(InvoiceFeatures::known()) .with_route_hints(vec![RouteHint(vec![RouteHintHop { src_node_id: nodes[2], @@ -4221,7 +4221,7 @@ mod tests { // path finding we realize that we found more capacity than we need. let (secp_ctx, network_graph, net_graph_msg_handler, _, logger) = build_graph(); let (our_privkey, our_id, privkeys, nodes) = get_nodes(&secp_ctx); - let scorer = test_utils::TestScorer::with_fixed_penalty(0); + let scorer = test_utils::TestScorer::with_penalty(0); let payee = Payee::from_node_id(nodes[2]).with_features(InvoiceFeatures::known()); // We need a route consisting of 3 paths: @@ -4378,7 +4378,7 @@ mod tests { let network_graph = Arc::new(NetworkGraph::new(genesis_block(Network::Testnet).header.block_hash())); let net_graph_msg_handler = NetGraphMsgHandler::new(Arc::clone(&network_graph), None, Arc::clone(&logger)); let (our_privkey, our_id, privkeys, nodes) = get_nodes(&secp_ctx); - let scorer = test_utils::TestScorer::with_fixed_penalty(0); + let scorer = test_utils::TestScorer::with_penalty(0); let payee = Payee::from_node_id(nodes[6]); add_channel(&net_graph_msg_handler, &secp_ctx, &our_privkey, &privkeys[1], ChannelFeatures::from_le_bytes(id_to_feature_flags(6)), 6); @@ -4507,7 +4507,7 @@ mod tests { // we calculated fees on a higher value, resulting in us ignoring such paths. let (secp_ctx, network_graph, net_graph_msg_handler, _, logger) = build_graph(); let (our_privkey, our_id, _, nodes) = get_nodes(&secp_ctx); - let scorer = test_utils::TestScorer::with_fixed_penalty(0); + let scorer = test_utils::TestScorer::with_penalty(0); let payee = Payee::from_node_id(nodes[2]); // We modify the graph to set the htlc_maximum of channel 2 to below the value we wish to @@ -4569,7 +4569,7 @@ mod tests { // resulting in us thinking there is no possible path, even if other paths exist. let (secp_ctx, network_graph, net_graph_msg_handler, _, logger) = build_graph(); let (our_privkey, our_id, privkeys, nodes) = get_nodes(&secp_ctx); - let scorer = test_utils::TestScorer::with_fixed_penalty(0); + let scorer = test_utils::TestScorer::with_penalty(0); let payee = Payee::from_node_id(nodes[2]).with_features(InvoiceFeatures::known()); // We modify the graph to set the htlc_minimum of channel 2 and 4 as needed - channel 2 @@ -4636,7 +4636,7 @@ mod tests { let (_, our_id, _, nodes) = get_nodes(&secp_ctx); let logger = Arc::new(test_utils::TestLogger::new()); let network_graph = NetworkGraph::new(genesis_block(Network::Testnet).header.block_hash()); - let scorer = test_utils::TestScorer::with_fixed_penalty(0); + let scorer = test_utils::TestScorer::with_penalty(0); let payee = Payee::from_node_id(nodes[0]).with_features(InvoiceFeatures::known()); { @@ -4677,7 +4677,7 @@ mod tests { let payee = Payee::from_node_id(nodes[6]).with_route_hints(last_hops(&nodes)); // Without penalizing each hop 100 msats, a longer path with lower fees is chosen. - let scorer = test_utils::TestScorer::with_fixed_penalty(0); + let scorer = test_utils::TestScorer::with_penalty(0); let route = get_route( &our_id, &payee, &network_graph, None, 100, 42, Arc::clone(&logger), &scorer @@ -4690,7 +4690,7 @@ mod tests { // Applying a 100 msat penalty to each hop results in taking channels 7 and 10 to nodes[6] // from nodes[2] rather than channel 6, 11, and 8, even though the longer path is cheaper. - let scorer = test_utils::TestScorer::with_fixed_penalty(100); + let scorer = test_utils::TestScorer::with_penalty(100); let route = get_route( &our_id, &payee, &network_graph, None, 100, 42, Arc::clone(&logger), &scorer @@ -4744,7 +4744,7 @@ mod tests { let payee = Payee::from_node_id(nodes[6]).with_route_hints(last_hops(&nodes)); // A path to nodes[6] exists when no penalties are applied to any channel. - let scorer = test_utils::TestScorer::with_fixed_penalty(0); + let scorer = test_utils::TestScorer::with_penalty(0); let route = get_route( &our_id, &payee, &network_graph, None, 100, 42, Arc::clone(&logger), &scorer @@ -4856,7 +4856,7 @@ mod tests { let (secp_ctx, network_graph, _, _, logger) = build_graph(); let (_, our_id, _, nodes) = get_nodes(&secp_ctx); - let scorer = test_utils::TestScorer::with_fixed_penalty(0); + let scorer = test_utils::TestScorer::with_penalty(0); // Make sure that generally there is at least one route available let feasible_max_total_cltv_delta = 1008; @@ -4990,7 +4990,7 @@ mod benches { use chain::transaction::OutPoint; use ln::channelmanager::{ChannelCounterparty, ChannelDetails}; use ln::features::{InitFeatures, InvoiceFeatures}; - use routing::scoring::Scorer; + use routing::scoring::{FixedPenaltyScorer, Scorer}; use util::logger::{Logger, Record}; use test::Bencher; @@ -5000,15 +5000,6 @@ mod benches { fn log(&self, _record: &Record) {} } - struct ZeroPenaltyScorer; - impl Score for ZeroPenaltyScorer { - fn channel_penalty_msat( - &self, _short_channel_id: u64, _send_amt: u64, _capacity_msat: u64, _source: &NodeId, _target: &NodeId - ) -> u64 { 0 } - fn payment_path_failed(&mut self, _path: &[&RouteHop], _short_channel_id: u64) {} - fn payment_path_successful(&mut self, _path: &[&RouteHop]) {} - } - fn read_network_graph() -> NetworkGraph { let mut d = test_utils::get_route_file().unwrap(); NetworkGraph::read(&mut d).unwrap() @@ -5051,14 +5042,14 @@ mod benches { #[bench] fn generate_routes_with_zero_penalty_scorer(bench: &mut Bencher) { let network_graph = read_network_graph(); - let scorer = ZeroPenaltyScorer; + let scorer = FixedPenaltyScorer::with_penalty(0); generate_routes(bench, &network_graph, scorer, InvoiceFeatures::empty()); } #[bench] fn generate_mpp_routes_with_zero_penalty_scorer(bench: &mut Bencher) { let network_graph = read_network_graph(); - let scorer = ZeroPenaltyScorer; + let scorer = FixedPenaltyScorer::with_penalty(0); generate_routes(bench, &network_graph, scorer, InvoiceFeatures::known()); } diff --git a/lightning/src/routing/scoring.rs b/lightning/src/routing/scoring.rs index 311ddadac..acac35ab3 100644 --- a/lightning/src/routing/scoring.rs +++ b/lightning/src/routing/scoring.rs @@ -190,6 +190,28 @@ impl<'a, S: Writeable> Writeable for MutexGuard<'a, S> { } } +/// [`Score`] implementation that uses a fixed penalty. +pub struct FixedPenaltyScorer { + penalty_msat: u64, +} + +impl FixedPenaltyScorer { + /// Creates a new scorer using `penalty_msat`. + pub fn with_penalty(penalty_msat: u64) -> Self { + Self { penalty_msat } + } +} + +impl Score for FixedPenaltyScorer { + fn channel_penalty_msat( &self, _: u64, _: u64, _: u64, _: &NodeId, _: &NodeId) -> u64 { + self.penalty_msat + } + + fn payment_path_failed(&mut self, _path: &[&RouteHop], _short_channel_id: u64) {} + + fn payment_path_successful(&mut self, _path: &[&RouteHop]) {} +} + /// [`Score`] implementation that provides reasonable default behavior. /// /// Used to apply a fixed penalty to each channel, thus avoiding long paths when shorter paths with @@ -306,18 +328,6 @@ impl ScorerUsingTime { channel_failures: HashMap::new(), } } - - /// Creates a new scorer using `penalty_msat` as a fixed channel penalty. - #[cfg(any(test, feature = "fuzztarget", feature = "_test_utils"))] - pub fn with_fixed_penalty(penalty_msat: u64) -> Self { - Self::new(ScoringParameters { - base_penalty_msat: penalty_msat, - failure_penalty_msat: 0, - failure_penalty_half_life: Duration::from_secs(0), - overuse_penalty_start_1024th: 1024, - overuse_penalty_msat_per_1024th: 0, - }) - } } impl ChannelFailure { diff --git a/lightning/src/util/test_utils.rs b/lightning/src/util/test_utils.rs index 5b3865eda..59e7c70c0 100644 --- a/lightning/src/util/test_utils.rs +++ b/lightning/src/util/test_utils.rs @@ -21,8 +21,7 @@ use ln::features::{ChannelFeatures, InitFeatures}; use ln::msgs; use ln::msgs::OptionalField; use ln::script::ShutdownScript; -use routing::scoring::ScorerUsingTime; -use routing::scoring::time::Eternity; +use routing::scoring::FixedPenaltyScorer; use util::enforcing_trait_impls::{EnforcingSigner, EnforcementState}; use util::events; use util::logger::{Logger, Level, Record}; @@ -722,4 +721,4 @@ impl core::fmt::Debug for OnRegisterOutput { } /// A scorer useful in testing, when the passage of time isn't a concern. -pub type TestScorer = ScorerUsingTime; +pub type TestScorer = FixedPenaltyScorer;