X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Futil%2Ftest_utils.rs;h=a96711d144c1441f5aa0a846990d13c5060d4f54;hb=361079d842444496cf52b2125f010f61a4ca5539;hp=c1692f9e3eda199713c8de1c3201a446170d26ef;hpb=3b6d039f89b75758dda7c5aeedce5e79bbc0d5fa;p=rust-lightning diff --git a/lightning/src/util/test_utils.rs b/lightning/src/util/test_utils.rs index c1692f9e..a96711d1 100644 --- a/lightning/src/util/test_utils.rs +++ b/lightning/src/util/test_utils.rs @@ -35,7 +35,7 @@ use crate::offers::invoice_request::UnsignedInvoiceRequest; use crate::onion_message::messenger::{DefaultMessageRouter, Destination, MessageRouter, OnionMessagePath}; use crate::routing::gossip::{EffectiveCapacity, NetworkGraph, NodeId, RoutingFees}; use crate::routing::utxo::{UtxoLookup, UtxoLookupError, UtxoResult}; -use crate::routing::router::{find_route, InFlightHtlcs, Path, Route, RouteParameters, RouteHintHop, Router, ScorerAccountingForInFlightHtlcs}; +use crate::routing::router::{DefaultRouter, InFlightHtlcs, Path, Route, RouteParameters, RouteHintHop, Router, ScorerAccountingForInFlightHtlcs}; use crate::routing::scoring::{ChannelUsage, ScoreUpdate, ScoreLookUp}; use crate::sync::RwLock; use crate::util::config::UserConfig; @@ -104,14 +104,29 @@ impl chaininterface::FeeEstimator for TestFeeEstimator { } pub struct TestRouter<'a> { + pub router: DefaultRouter< + Arc>, + &'a TestLogger, + &'a RwLock, + (), + TestScorer, + >, pub network_graph: Arc>, pub next_routes: Mutex)>>, pub scorer: &'a RwLock, } impl<'a> TestRouter<'a> { - pub fn new(network_graph: Arc>, scorer: &'a RwLock) -> Self { - Self { network_graph, next_routes: Mutex::new(VecDeque::new()), scorer } + pub fn new( + network_graph: Arc>, logger: &'a TestLogger, + scorer: &'a RwLock + ) -> Self { + Self { + router: DefaultRouter::new(network_graph.clone(), logger, [42u8; 32], scorer, ()), + network_graph, + next_routes: Mutex::new(VecDeque::new()), + scorer, + } } pub fn expect_find_route(&self, query: RouteParameters, result: Result) { @@ -185,38 +200,36 @@ impl<'a> Router for TestRouter<'a> { } return find_route_res; } - let logger = TestLogger::new(); - find_route( - payer, params, &self.network_graph, first_hops, &logger, - &ScorerAccountingForInFlightHtlcs::new(self.scorer.read().unwrap(), &inflight_htlcs), &Default::default(), - &[42; 32] - ) + + self.router.find_route(payer, params, first_hops, inflight_htlcs) } fn create_blinded_payment_paths< ES: EntropySource + ?Sized, T: secp256k1::Signing + secp256k1::Verification >( - &self, _recipient: PublicKey, _first_hops: Vec, _tlvs: ReceiveTlvs, - _amount_msats: u64, _entropy_source: &ES, _secp_ctx: &Secp256k1 + &self, recipient: PublicKey, first_hops: Vec, tlvs: ReceiveTlvs, + amount_msats: u64, entropy_source: &ES, secp_ctx: &Secp256k1 ) -> Result, ()> { - unreachable!() + self.router.create_blinded_payment_paths( + recipient, first_hops, tlvs, amount_msats, entropy_source, secp_ctx + ) } } impl<'a> MessageRouter for TestRouter<'a> { fn find_path( - &self, _sender: PublicKey, _peers: Vec, _destination: Destination + &self, sender: PublicKey, peers: Vec, destination: Destination ) -> Result { - unreachable!() + self.router.find_path(sender, peers, destination) } fn create_blinded_paths< ES: EntropySource + ?Sized, T: secp256k1::Signing + secp256k1::Verification >( - &self, _recipient: PublicKey, _peers: Vec, _entropy_source: &ES, - _secp_ctx: &Secp256k1 + &self, recipient: PublicKey, peers: Vec, entropy_source: &ES, + secp_ctx: &Secp256k1 ) -> Result, ()> { - unreachable!() + self.router.create_blinded_paths(recipient, peers, entropy_source, secp_ctx) } } @@ -328,7 +341,7 @@ impl<'a> chain::Watch for TestChainMonitor<'a> { let new_monitor = <(BlockHash, channelmonitor::ChannelMonitor)>::read( &mut io::Cursor::new(&w.0), (self.keys_manager, self.keys_manager)).unwrap().1; assert!(new_monitor == monitor); - self.latest_monitor_update_id.lock().unwrap().insert(funding_txo.to_channel_id(), + self.latest_monitor_update_id.lock().unwrap().insert(monitor.channel_id(), (funding_txo, monitor.get_latest_update_id(), MonitorUpdateId::from_new_monitor(&monitor))); self.added_monitors.lock().unwrap().push((funding_txo, monitor)); self.chain_monitor.watch_channel(funding_txo, new_monitor) @@ -340,18 +353,19 @@ impl<'a> chain::Watch for TestChainMonitor<'a> { update.write(&mut w).unwrap(); assert!(channelmonitor::ChannelMonitorUpdate::read( &mut io::Cursor::new(&w.0)).unwrap() == *update); + let channel_id = update.channel_id.unwrap_or(ChannelId::v1_from_funding_outpoint(funding_txo)); - self.monitor_updates.lock().unwrap().entry(funding_txo.to_channel_id()).or_insert(Vec::new()).push(update.clone()); + self.monitor_updates.lock().unwrap().entry(channel_id).or_insert(Vec::new()).push(update.clone()); if let Some(exp) = self.expect_channel_force_closed.lock().unwrap().take() { - assert_eq!(funding_txo.to_channel_id(), exp.0); + assert_eq!(channel_id, exp.0); assert_eq!(update.updates.len(), 1); if let channelmonitor::ChannelMonitorUpdateStep::ChannelForceClosed { should_broadcast } = update.updates[0] { assert_eq!(should_broadcast, exp.1); } else { panic!(); } } - self.latest_monitor_update_id.lock().unwrap().insert(funding_txo.to_channel_id(), + self.latest_monitor_update_id.lock().unwrap().insert(channel_id, (funding_txo, update.update_id, MonitorUpdateId::from_monitor_update(update))); let update_res = self.chain_monitor.update_channel(funding_txo, update); // At every point where we get a monitor update, we should be able to send a useful monitor @@ -362,7 +376,7 @@ impl<'a> chain::Watch for TestChainMonitor<'a> { let new_monitor = <(BlockHash, channelmonitor::ChannelMonitor)>::read( &mut io::Cursor::new(&w.0), (self.keys_manager, self.keys_manager)).unwrap().1; if let Some(chan_id) = self.expect_monitor_round_trip_fail.lock().unwrap().take() { - assert_eq!(chan_id, funding_txo.to_channel_id()); + assert_eq!(chan_id, channel_id); assert!(new_monitor != *monitor); } else { assert!(new_monitor == *monitor); @@ -371,7 +385,7 @@ impl<'a> chain::Watch for TestChainMonitor<'a> { update_res } - fn release_pending_monitor_events(&self) -> Vec<(OutPoint, Vec, Option)> { + fn release_pending_monitor_events(&self) -> Vec<(OutPoint, ChannelId, Vec, Option)> { return self.chain_monitor.release_pending_monitor_events(); } }