X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Futil%2Ftest_utils.rs;h=6dd775ea30630e3211efe0e89fc092cfcdfbf9b1;hb=1fd95496d1038352535a74065d2b06559e2192fb;hp=7b4037bec10969e572311b1438252b6402cfc5d4;hpb=8bb661449e6b990a1b86af36b323e639decf9eb1;p=rust-lightning diff --git a/lightning/src/util/test_utils.rs b/lightning/src/util/test_utils.rs index 7b4037be..6dd775ea 100644 --- a/lightning/src/util/test_utils.rs +++ b/lightning/src/util/test_utils.rs @@ -20,8 +20,10 @@ use crate::chain::keysinterface; use crate::ln::channelmanager; use crate::ln::features::{ChannelFeatures, InitFeatures, NodeFeatures}; use crate::ln::{msgs, wire}; +use crate::ln::msgs::LightningError; use crate::ln::script::ShutdownScript; use crate::routing::gossip::NetworkGraph; +use crate::routing::gossip::NodeId; use crate::routing::router::{find_route, InFlightHtlcs, Route, RouteHop, RouteParameters, Router, ScorerAccountingForInFlightHtlcs}; use crate::routing::scoring::FixedPenaltyScorer; use crate::util::config::UserConfig; @@ -76,11 +78,17 @@ impl chaininterface::FeeEstimator for TestFeeEstimator { pub struct TestRouter<'a> { pub network_graph: Arc>, + pub next_routes: Mutex)>>, } impl<'a> TestRouter<'a> { pub fn new(network_graph: Arc>) -> Self { - Self { network_graph } + Self { network_graph, next_routes: Mutex::new(VecDeque::new()), } + } + + pub fn expect_find_route(&self, query: RouteParameters, result: Result) { + let mut expected_routes = self.next_routes.lock().unwrap(); + expected_routes.push_back((query, result)); } } @@ -89,6 +97,10 @@ impl<'a> Router for TestRouter<'a> { &self, payer: &PublicKey, params: &RouteParameters, first_hops: Option<&[&channelmanager::ChannelDetails]>, inflight_htlcs: &InFlightHtlcs ) -> Result { + if let Some((find_route_query, find_route_res)) = self.next_routes.lock().unwrap().pop_front() { + assert_eq!(find_route_query, *params); + return find_route_res; + } let logger = TestLogger::new(); find_route( payer, params, &self.network_graph, first_hops, &logger, @@ -102,6 +114,16 @@ impl<'a> Router for TestRouter<'a> { fn notify_payment_probe_failed(&self, _path: &[&RouteHop], _short_channel_id: u64) {} } +#[cfg(feature = "std")] // If we put this on the `if`, we get "attributes are not yet allowed on `if` expressions" on 1.41.1 +impl<'a> Drop for TestRouter<'a> { + fn drop(&mut self) { + if std::thread::panicking() { + return; + } + assert!(self.next_routes.lock().unwrap().is_empty()); + } +} + pub struct OnlyReadsKeysInterface {} impl EntropySource for OnlyReadsKeysInterface { @@ -414,10 +436,10 @@ fn get_dummy_channel_announcement(short_chan_id: u64) -> msgs::ChannelAnnounceme features: ChannelFeatures::empty(), chain_hash: genesis_block(network).header.block_hash(), short_channel_id: short_chan_id, - node_id_1: PublicKey::from_secret_key(&secp_ctx, &node_1_privkey), - node_id_2: PublicKey::from_secret_key(&secp_ctx, &node_2_privkey), - bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, &node_1_btckey), - bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, &node_2_btckey), + node_id_1: NodeId::from_pubkey(&PublicKey::from_secret_key(&secp_ctx, &node_1_privkey)), + node_id_2: NodeId::from_pubkey(&PublicKey::from_secret_key(&secp_ctx, &node_2_privkey)), + bitcoin_key_1: NodeId::from_pubkey(&PublicKey::from_secret_key(&secp_ctx, &node_1_btckey)), + bitcoin_key_2: NodeId::from_pubkey(&PublicKey::from_secret_key(&secp_ctx, &node_2_btckey)), excess_data: Vec::new(), };