From 5aea2cf02b93c0d8f21897259935ee4d822c2e46 Mon Sep 17 00:00:00 2001 From: Valentine Wallace Date: Fri, 3 Feb 2023 11:14:53 -0500 Subject: [PATCH] Use TestScorer in BackgroundProcessor testing --- lightning-background-processor/src/lib.rs | 141 ++++++++++++++++++++-- 1 file changed, 133 insertions(+), 8 deletions(-) diff --git a/lightning-background-processor/src/lib.rs b/lightning-background-processor/src/lib.rs index 4bd5e463f..9cf46a166 100644 --- a/lightning-background-processor/src/lib.rs +++ b/lightning-background-processor/src/lib.rs @@ -618,13 +618,14 @@ mod tests { use lightning::chain::keysinterface::{InMemorySigner, EntropySource, KeysManager}; use lightning::chain::transaction::OutPoint; use lightning::get_event_msg; - use lightning::ln::channelmanager::{BREAKDOWN_TIMEOUT, ChainParameters, ChannelManager, SimpleArcChannelManager}; + use lightning::ln::channelmanager; + use lightning::ln::channelmanager::{BREAKDOWN_TIMEOUT, ChainParameters}; use lightning::ln::features::ChannelFeatures; use lightning::ln::msgs::{ChannelMessageHandler, Init}; use lightning::ln::peer_handler::{PeerManager, MessageHandler, SocketDescriptor, IgnoringMessageHandler}; - use lightning::routing::gossip::{NetworkGraph, P2PGossipSync}; - use lightning::routing::router::DefaultRouter; - use lightning::routing::scoring::{ProbabilisticScoringParameters, ProbabilisticScorer}; + use lightning::routing::gossip::{NetworkGraph, NodeId, P2PGossipSync}; + use lightning::routing::router::{DefaultRouter, RouteHop}; + use lightning::routing::scoring::{ChannelUsage, Score}; use lightning::util::config::UserConfig; use lightning::util::events::{Event, MessageSendEventsProvider, MessageSendEvent}; use lightning::util::ser::Writeable; @@ -632,6 +633,7 @@ mod tests { use lightning::util::persist::KVStorePersister; use lightning_invoice::payment::{InvoicePayer, Retry}; use lightning_persister::FilesystemPersister; + use std::collections::VecDeque; use std::fs; use std::path::PathBuf; use std::sync::{Arc, Mutex}; @@ -654,13 +656,15 @@ mod tests { fn disconnect_socket(&mut self) {} } + type ChannelManager = channelmanager::ChannelManager, Arc, Arc, Arc, Arc, Arc, Arc>>, Arc, Arc>>>, Arc>; + type ChainMonitor = chainmonitor::ChainMonitor, Arc, Arc, Arc, Arc>; type PGS = Arc>>, Arc, Arc>>; type RGS = Arc>>, Arc>>; struct Node { - node: Arc>, + node: Arc, p2p_gossip_sync: PGS, rapid_gossip_sync: RGS, peer_manager: Arc, Arc, IgnoringMessageHandler, Arc, IgnoringMessageHandler, Arc>>, @@ -670,7 +674,7 @@ mod tests { network_graph: Arc>>, logger: Arc, best_block: BestBlock, - scorer: Arc>>, Arc>>>, + scorer: Arc>, } impl Node { @@ -756,6 +760,128 @@ mod tests { } } + struct TestScorer { + event_expectations: Option>, + } + + #[derive(Debug)] + enum TestResult { + PaymentFailure { path: Vec, short_channel_id: u64 }, + PaymentSuccess { path: Vec }, + ProbeFailure { path: Vec }, + ProbeSuccess { path: Vec }, + } + + impl TestScorer { + fn new() -> Self { + Self { event_expectations: None } + } + + fn expect(&mut self, expectation: TestResult) { + self.event_expectations.get_or_insert_with(|| VecDeque::new()).push_back(expectation); + } + } + + impl lightning::util::ser::Writeable for TestScorer { + fn write(&self, _: &mut W) -> Result<(), lightning::io::Error> { Ok(()) } + } + + impl Score for TestScorer { + fn channel_penalty_msat( + &self, _short_channel_id: u64, _source: &NodeId, _target: &NodeId, _usage: ChannelUsage + ) -> u64 { unimplemented!(); } + + fn payment_path_failed(&mut self, actual_path: &[&RouteHop], actual_short_channel_id: u64) { + if let Some(expectations) = &mut self.event_expectations { + match expectations.pop_front().unwrap() { + TestResult::PaymentFailure { path, short_channel_id } => { + assert_eq!(actual_path, &path.iter().collect::>()[..]); + assert_eq!(actual_short_channel_id, short_channel_id); + }, + TestResult::PaymentSuccess { path } => { + panic!("Unexpected successful payment path: {:?}", path) + }, + TestResult::ProbeFailure { path } => { + panic!("Unexpected probe failure: {:?}", path) + }, + TestResult::ProbeSuccess { path } => { + panic!("Unexpected probe success: {:?}", path) + } + } + } + } + + fn payment_path_successful(&mut self, actual_path: &[&RouteHop]) { + if let Some(expectations) = &mut self.event_expectations { + match expectations.pop_front().unwrap() { + TestResult::PaymentFailure { path, .. } => { + panic!("Unexpected payment path failure: {:?}", path) + }, + TestResult::PaymentSuccess { path } => { + assert_eq!(actual_path, &path.iter().collect::>()[..]); + }, + TestResult::ProbeFailure { path } => { + panic!("Unexpected probe failure: {:?}", path) + }, + TestResult::ProbeSuccess { path } => { + panic!("Unexpected probe success: {:?}", path) + } + } + } + } + + fn probe_failed(&mut self, actual_path: &[&RouteHop], _: u64) { + if let Some(expectations) = &mut self.event_expectations { + match expectations.pop_front().unwrap() { + TestResult::PaymentFailure { path, .. } => { + panic!("Unexpected payment path failure: {:?}", path) + }, + TestResult::PaymentSuccess { path } => { + panic!("Unexpected payment path success: {:?}", path) + }, + TestResult::ProbeFailure { path } => { + assert_eq!(actual_path, &path.iter().collect::>()[..]); + }, + TestResult::ProbeSuccess { path } => { + panic!("Unexpected probe success: {:?}", path) + } + } + } + } + fn probe_successful(&mut self, actual_path: &[&RouteHop]) { + if let Some(expectations) = &mut self.event_expectations { + match expectations.pop_front().unwrap() { + TestResult::PaymentFailure { path, .. } => { + panic!("Unexpected payment path failure: {:?}", path) + }, + TestResult::PaymentSuccess { path } => { + panic!("Unexpected payment path success: {:?}", path) + }, + TestResult::ProbeFailure { path } => { + panic!("Unexpected probe failure: {:?}", path) + }, + TestResult::ProbeSuccess { path } => { + assert_eq!(actual_path, &path.iter().collect::>()[..]); + } + } + } + } + } + + impl Drop for TestScorer { + fn drop(&mut self) { + if std::thread::panicking() { + return; + } + + if let Some(event_expectations) = &self.event_expectations { + if !event_expectations.is_empty() { + panic!("Unsatisfied event expectations: {:?}", event_expectations); + } + } + } + } + fn get_full_filepath(filepath: String, filename: String) -> String { let mut path = PathBuf::from(filepath); path.push(filename); @@ -771,8 +897,7 @@ mod tests { let network = Network::Testnet; let genesis_block = genesis_block(network); let network_graph = Arc::new(NetworkGraph::new(genesis_block.header.block_hash(), logger.clone())); - let params = ProbabilisticScoringParameters::default(); - let scorer = Arc::new(Mutex::new(ProbabilisticScorer::new(params, network_graph.clone(), logger.clone()))); + let scorer = Arc::new(Mutex::new(TestScorer::new())); let seed = [i as u8; 32]; let router = Arc::new(DefaultRouter::new(network_graph.clone(), logger.clone(), seed, scorer.clone())); let chain_source = Arc::new(test_utils::TestChainSource::new(Network::Testnet)); -- 2.39.5