+
+ #[test]
+ fn test_payment_path_scoring() {
+ // Ensure that we update the scorer when relevant events are processed. In this case, we ensure
+ // that we update the scorer upon a payment path succeeding (note that the channel must be
+ // public or else we won't score it).
+ // Set up a background event handler for FundingGenerationReady events.
+ let (sender, receiver) = std::sync::mpsc::sync_channel(1);
+ let event_handler = move |event: Event| match event {
+ Event::PaymentPathFailed { .. } => sender.send(event).unwrap(),
+ Event::PaymentPathSuccessful { .. } => sender.send(event).unwrap(),
+ Event::ProbeSuccessful { .. } => sender.send(event).unwrap(),
+ Event::ProbeFailed { .. } => sender.send(event).unwrap(),
+ _ => panic!("Unexpected event: {:?}", event),
+ };
+
+ let nodes = create_nodes(1, "test_payment_path_scoring".to_string());
+ let data_dir = nodes[0].persister.get_data_dir();
+ let persister = Arc::new(Persister::new(data_dir.clone()));
+ let bg_processor = BackgroundProcessor::start(persister, event_handler, nodes[0].chain_monitor.clone(), nodes[0].node.clone(), nodes[0].no_gossip_sync(), nodes[0].peer_manager.clone(), nodes[0].logger.clone(), Some(nodes[0].scorer.clone()));
+
+ let scored_scid = 4242;
+ let secp_ctx = Secp256k1::new();
+ let node_1_privkey = SecretKey::from_slice(&[42; 32]).unwrap();
+ let node_1_id = PublicKey::from_secret_key(&secp_ctx, &node_1_privkey);
+
+ let path = vec![RouteHop {
+ pubkey: node_1_id,
+ node_features: NodeFeatures::empty(),
+ short_channel_id: scored_scid,
+ channel_features: ChannelFeatures::empty(),
+ fee_msat: 0,
+ cltv_expiry_delta: MIN_CLTV_EXPIRY_DELTA as u32,
+ }];
+
+ nodes[0].scorer.lock().unwrap().expect(TestResult::PaymentFailure { path: path.clone(), short_channel_id: scored_scid });
+ nodes[0].node.push_pending_event(Event::PaymentPathFailed {
+ payment_id: None,
+ payment_hash: PaymentHash([42; 32]),
+ payment_failed_permanently: false,
+ network_update: None,
+ all_paths_failed: true,
+ path: path.clone(),
+ short_channel_id: Some(scored_scid),
+ retry: None,
+ });
+ let event = receiver
+ .recv_timeout(Duration::from_secs(EVENT_DEADLINE))
+ .expect("PaymentPathFailed not handled within deadline");
+ match event {
+ Event::PaymentPathFailed { .. } => {},
+ _ => panic!("Unexpected event"),
+ }
+
+ // Ensure we'll score payments that were explicitly failed back by the destination as
+ // ProbeSuccess.
+ nodes[0].scorer.lock().unwrap().expect(TestResult::ProbeSuccess { path: path.clone() });
+ nodes[0].node.push_pending_event(Event::PaymentPathFailed {
+ payment_id: None,
+ payment_hash: PaymentHash([42; 32]),
+ payment_failed_permanently: true,
+ network_update: None,
+ all_paths_failed: true,
+ path: path.clone(),
+ short_channel_id: None,
+ retry: None,
+ });
+ let event = receiver
+ .recv_timeout(Duration::from_secs(EVENT_DEADLINE))
+ .expect("PaymentPathFailed not handled within deadline");
+ match event {
+ Event::PaymentPathFailed { .. } => {},
+ _ => panic!("Unexpected event"),
+ }
+
+ nodes[0].scorer.lock().unwrap().expect(TestResult::PaymentSuccess { path: path.clone() });
+ nodes[0].node.push_pending_event(Event::PaymentPathSuccessful {
+ payment_id: PaymentId([42; 32]),
+ payment_hash: None,
+ path: path.clone(),
+ });
+ let event = receiver
+ .recv_timeout(Duration::from_secs(EVENT_DEADLINE))
+ .expect("PaymentPathSuccessful not handled within deadline");
+ match event {
+ Event::PaymentPathSuccessful { .. } => {},
+ _ => panic!("Unexpected event"),
+ }
+
+ nodes[0].scorer.lock().unwrap().expect(TestResult::ProbeSuccess { path: path.clone() });
+ nodes[0].node.push_pending_event(Event::ProbeSuccessful {
+ payment_id: PaymentId([42; 32]),
+ payment_hash: PaymentHash([42; 32]),
+ path: path.clone(),
+ });
+ let event = receiver
+ .recv_timeout(Duration::from_secs(EVENT_DEADLINE))
+ .expect("ProbeSuccessful not handled within deadline");
+ match event {
+ Event::ProbeSuccessful { .. } => {},
+ _ => panic!("Unexpected event"),
+ }
+
+ nodes[0].scorer.lock().unwrap().expect(TestResult::ProbeFailure { path: path.clone() });
+ nodes[0].node.push_pending_event(Event::ProbeFailed {
+ payment_id: PaymentId([42; 32]),
+ payment_hash: PaymentHash([42; 32]),
+ path: path.clone(),
+ short_channel_id: Some(scored_scid),
+ });
+ let event = receiver
+ .recv_timeout(Duration::from_secs(EVENT_DEADLINE))
+ .expect("ProbeFailure not handled within deadline");
+ match event {
+ Event::ProbeFailed { .. } => {},
+ _ => panic!("Unexpected event"),
+ }
+
+ assert!(bg_processor.stop().is_ok());
+ }