Fix followups to PR2797
[rust-lightning] / lightning / src / util / test_utils.rs
index c1692f9e3eda199713c8de1c3201a446170d26ef..a96711d144c1441f5aa0a846990d13c5060d4f54 100644 (file)
@@ -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<NetworkGraph<&'a TestLogger>>,
+               &'a TestLogger,
+               &'a RwLock<TestScorer>,
+               (),
+               TestScorer,
+       >,
        pub network_graph: Arc<NetworkGraph<&'a TestLogger>>,
        pub next_routes: Mutex<VecDeque<(RouteParameters, Result<Route, LightningError>)>>,
        pub scorer: &'a RwLock<TestScorer>,
 }
 
 impl<'a> TestRouter<'a> {
-       pub fn new(network_graph: Arc<NetworkGraph<&'a TestLogger>>, scorer: &'a RwLock<TestScorer>) -> Self {
-               Self { network_graph, next_routes: Mutex::new(VecDeque::new()), scorer }
+       pub fn new(
+               network_graph: Arc<NetworkGraph<&'a TestLogger>>, logger: &'a TestLogger,
+               scorer: &'a RwLock<TestScorer>
+       ) -> 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<Route, LightningError>) {
@@ -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<ChannelDetails>, _tlvs: ReceiveTlvs,
-               _amount_msats: u64, _entropy_source: &ES, _secp_ctx: &Secp256k1<T>
+               &self, recipient: PublicKey, first_hops: Vec<ChannelDetails>, tlvs: ReceiveTlvs,
+               amount_msats: u64, entropy_source: &ES, secp_ctx: &Secp256k1<T>
        ) -> Result<Vec<(BlindedPayInfo, BlindedPath)>, ()> {
-               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<PublicKey>, _destination: Destination
+               &self, sender: PublicKey, peers: Vec<PublicKey>, destination: Destination
        ) -> Result<OnionMessagePath, ()> {
-               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<PublicKey>, _entropy_source: &ES,
-               _secp_ctx: &Secp256k1<T>
+               &self, recipient: PublicKey, peers: Vec<PublicKey>, entropy_source: &ES,
+               secp_ctx: &Secp256k1<T>
        ) -> Result<Vec<BlindedPath>, ()> {
-               unreachable!()
+               self.router.create_blinded_paths(recipient, peers, entropy_source, secp_ctx)
        }
 }
 
@@ -328,7 +341,7 @@ impl<'a> chain::Watch<TestChannelSigner> for TestChainMonitor<'a> {
                let new_monitor = <(BlockHash, channelmonitor::ChannelMonitor<TestChannelSigner>)>::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<TestChannelSigner> 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<TestChannelSigner> for TestChainMonitor<'a> {
                let new_monitor = <(BlockHash, channelmonitor::ChannelMonitor<TestChannelSigner>)>::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<TestChannelSigner> for TestChainMonitor<'a> {
                update_res
        }
 
-       fn release_pending_monitor_events(&self) -> Vec<(OutPoint, Vec<MonitorEvent>, Option<PublicKey>)> {
+       fn release_pending_monitor_events(&self) -> Vec<(OutPoint, ChannelId, Vec<MonitorEvent>, Option<PublicKey>)> {
                return self.chain_monitor.release_pending_monitor_events();
        }
 }