Skip to content

Commit 5aea2cf

Browse files
Use TestScorer in BackgroundProcessor testing
1 parent d2bf407 commit 5aea2cf

File tree

1 file changed

+133
-8
lines changed
  • lightning-background-processor/src

1 file changed

+133
-8
lines changed

lightning-background-processor/src/lib.rs

+133-8
Original file line numberDiff line numberDiff line change
@@ -618,20 +618,22 @@ mod tests {
618618
use lightning::chain::keysinterface::{InMemorySigner, EntropySource, KeysManager};
619619
use lightning::chain::transaction::OutPoint;
620620
use lightning::get_event_msg;
621-
use lightning::ln::channelmanager::{BREAKDOWN_TIMEOUT, ChainParameters, ChannelManager, SimpleArcChannelManager};
621+
use lightning::ln::channelmanager;
622+
use lightning::ln::channelmanager::{BREAKDOWN_TIMEOUT, ChainParameters};
622623
use lightning::ln::features::ChannelFeatures;
623624
use lightning::ln::msgs::{ChannelMessageHandler, Init};
624625
use lightning::ln::peer_handler::{PeerManager, MessageHandler, SocketDescriptor, IgnoringMessageHandler};
625-
use lightning::routing::gossip::{NetworkGraph, P2PGossipSync};
626-
use lightning::routing::router::DefaultRouter;
627-
use lightning::routing::scoring::{ProbabilisticScoringParameters, ProbabilisticScorer};
626+
use lightning::routing::gossip::{NetworkGraph, NodeId, P2PGossipSync};
627+
use lightning::routing::router::{DefaultRouter, RouteHop};
628+
use lightning::routing::scoring::{ChannelUsage, Score};
628629
use lightning::util::config::UserConfig;
629630
use lightning::util::events::{Event, MessageSendEventsProvider, MessageSendEvent};
630631
use lightning::util::ser::Writeable;
631632
use lightning::util::test_utils;
632633
use lightning::util::persist::KVStorePersister;
633634
use lightning_invoice::payment::{InvoicePayer, Retry};
634635
use lightning_persister::FilesystemPersister;
636+
use std::collections::VecDeque;
635637
use std::fs;
636638
use std::path::PathBuf;
637639
use std::sync::{Arc, Mutex};
@@ -654,13 +656,15 @@ mod tests {
654656
fn disconnect_socket(&mut self) {}
655657
}
656658

659+
type ChannelManager = channelmanager::ChannelManager<Arc<ChainMonitor>, Arc<test_utils::TestBroadcaster>, Arc<KeysManager>, Arc<KeysManager>, Arc<KeysManager>, Arc<test_utils::TestFeeEstimator>, Arc<DefaultRouter< Arc<NetworkGraph<Arc<test_utils::TestLogger>>>, Arc<test_utils::TestLogger>, Arc<Mutex<TestScorer>>>>, Arc<test_utils::TestLogger>>;
660+
657661
type ChainMonitor = chainmonitor::ChainMonitor<InMemorySigner, Arc<test_utils::TestChainSource>, Arc<test_utils::TestBroadcaster>, Arc<test_utils::TestFeeEstimator>, Arc<test_utils::TestLogger>, Arc<FilesystemPersister>>;
658662

659663
type PGS = Arc<P2PGossipSync<Arc<NetworkGraph<Arc<test_utils::TestLogger>>>, Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>>>;
660664
type RGS = Arc<RapidGossipSync<Arc<NetworkGraph<Arc<test_utils::TestLogger>>>, Arc<test_utils::TestLogger>>>;
661665

662666
struct Node {
663-
node: Arc<SimpleArcChannelManager<ChainMonitor, test_utils::TestBroadcaster, test_utils::TestFeeEstimator, test_utils::TestLogger>>,
667+
node: Arc<ChannelManager>,
664668
p2p_gossip_sync: PGS,
665669
rapid_gossip_sync: RGS,
666670
peer_manager: Arc<PeerManager<TestDescriptor, Arc<test_utils::TestChannelMessageHandler>, Arc<test_utils::TestRoutingMessageHandler>, IgnoringMessageHandler, Arc<test_utils::TestLogger>, IgnoringMessageHandler, Arc<KeysManager>>>,
@@ -670,7 +674,7 @@ mod tests {
670674
network_graph: Arc<NetworkGraph<Arc<test_utils::TestLogger>>>,
671675
logger: Arc<test_utils::TestLogger>,
672676
best_block: BestBlock,
673-
scorer: Arc<Mutex<ProbabilisticScorer<Arc<NetworkGraph<Arc<test_utils::TestLogger>>>, Arc<test_utils::TestLogger>>>>,
677+
scorer: Arc<Mutex<TestScorer>>,
674678
}
675679

676680
impl Node {
@@ -756,6 +760,128 @@ mod tests {
756760
}
757761
}
758762

763+
struct TestScorer {
764+
event_expectations: Option<VecDeque<TestResult>>,
765+
}
766+
767+
#[derive(Debug)]
768+
enum TestResult {
769+
PaymentFailure { path: Vec<RouteHop>, short_channel_id: u64 },
770+
PaymentSuccess { path: Vec<RouteHop> },
771+
ProbeFailure { path: Vec<RouteHop> },
772+
ProbeSuccess { path: Vec<RouteHop> },
773+
}
774+
775+
impl TestScorer {
776+
fn new() -> Self {
777+
Self { event_expectations: None }
778+
}
779+
780+
fn expect(&mut self, expectation: TestResult) {
781+
self.event_expectations.get_or_insert_with(|| VecDeque::new()).push_back(expectation);
782+
}
783+
}
784+
785+
impl lightning::util::ser::Writeable for TestScorer {
786+
fn write<W: lightning::util::ser::Writer>(&self, _: &mut W) -> Result<(), lightning::io::Error> { Ok(()) }
787+
}
788+
789+
impl Score for TestScorer {
790+
fn channel_penalty_msat(
791+
&self, _short_channel_id: u64, _source: &NodeId, _target: &NodeId, _usage: ChannelUsage
792+
) -> u64 { unimplemented!(); }
793+
794+
fn payment_path_failed(&mut self, actual_path: &[&RouteHop], actual_short_channel_id: u64) {
795+
if let Some(expectations) = &mut self.event_expectations {
796+
match expectations.pop_front().unwrap() {
797+
TestResult::PaymentFailure { path, short_channel_id } => {
798+
assert_eq!(actual_path, &path.iter().collect::<Vec<_>>()[..]);
799+
assert_eq!(actual_short_channel_id, short_channel_id);
800+
},
801+
TestResult::PaymentSuccess { path } => {
802+
panic!("Unexpected successful payment path: {:?}", path)
803+
},
804+
TestResult::ProbeFailure { path } => {
805+
panic!("Unexpected probe failure: {:?}", path)
806+
},
807+
TestResult::ProbeSuccess { path } => {
808+
panic!("Unexpected probe success: {:?}", path)
809+
}
810+
}
811+
}
812+
}
813+
814+
fn payment_path_successful(&mut self, actual_path: &[&RouteHop]) {
815+
if let Some(expectations) = &mut self.event_expectations {
816+
match expectations.pop_front().unwrap() {
817+
TestResult::PaymentFailure { path, .. } => {
818+
panic!("Unexpected payment path failure: {:?}", path)
819+
},
820+
TestResult::PaymentSuccess { path } => {
821+
assert_eq!(actual_path, &path.iter().collect::<Vec<_>>()[..]);
822+
},
823+
TestResult::ProbeFailure { path } => {
824+
panic!("Unexpected probe failure: {:?}", path)
825+
},
826+
TestResult::ProbeSuccess { path } => {
827+
panic!("Unexpected probe success: {:?}", path)
828+
}
829+
}
830+
}
831+
}
832+
833+
fn probe_failed(&mut self, actual_path: &[&RouteHop], _: u64) {
834+
if let Some(expectations) = &mut self.event_expectations {
835+
match expectations.pop_front().unwrap() {
836+
TestResult::PaymentFailure { path, .. } => {
837+
panic!("Unexpected payment path failure: {:?}", path)
838+
},
839+
TestResult::PaymentSuccess { path } => {
840+
panic!("Unexpected payment path success: {:?}", path)
841+
},
842+
TestResult::ProbeFailure { path } => {
843+
assert_eq!(actual_path, &path.iter().collect::<Vec<_>>()[..]);
844+
},
845+
TestResult::ProbeSuccess { path } => {
846+
panic!("Unexpected probe success: {:?}", path)
847+
}
848+
}
849+
}
850+
}
851+
fn probe_successful(&mut self, actual_path: &[&RouteHop]) {
852+
if let Some(expectations) = &mut self.event_expectations {
853+
match expectations.pop_front().unwrap() {
854+
TestResult::PaymentFailure { path, .. } => {
855+
panic!("Unexpected payment path failure: {:?}", path)
856+
},
857+
TestResult::PaymentSuccess { path } => {
858+
panic!("Unexpected payment path success: {:?}", path)
859+
},
860+
TestResult::ProbeFailure { path } => {
861+
panic!("Unexpected probe failure: {:?}", path)
862+
},
863+
TestResult::ProbeSuccess { path } => {
864+
assert_eq!(actual_path, &path.iter().collect::<Vec<_>>()[..]);
865+
}
866+
}
867+
}
868+
}
869+
}
870+
871+
impl Drop for TestScorer {
872+
fn drop(&mut self) {
873+
if std::thread::panicking() {
874+
return;
875+
}
876+
877+
if let Some(event_expectations) = &self.event_expectations {
878+
if !event_expectations.is_empty() {
879+
panic!("Unsatisfied event expectations: {:?}", event_expectations);
880+
}
881+
}
882+
}
883+
}
884+
759885
fn get_full_filepath(filepath: String, filename: String) -> String {
760886
let mut path = PathBuf::from(filepath);
761887
path.push(filename);
@@ -771,8 +897,7 @@ mod tests {
771897
let network = Network::Testnet;
772898
let genesis_block = genesis_block(network);
773899
let network_graph = Arc::new(NetworkGraph::new(genesis_block.header.block_hash(), logger.clone()));
774-
let params = ProbabilisticScoringParameters::default();
775-
let scorer = Arc::new(Mutex::new(ProbabilisticScorer::new(params, network_graph.clone(), logger.clone())));
900+
let scorer = Arc::new(Mutex::new(TestScorer::new()));
776901
let seed = [i as u8; 32];
777902
let router = Arc::new(DefaultRouter::new(network_graph.clone(), logger.clone(), seed, scorer.clone()));
778903
let chain_source = Arc::new(test_utils::TestChainSource::new(Network::Testnet));

0 commit comments

Comments
 (0)