X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Futil%2Ftest_utils.rs;h=be09c8f0065b29e1fff3eaed9c54380d11751d27;hb=41a6c674f9c06c89b0890364ac75b57b648e538e;hp=9fb5178ad9d00b7a34f5cc99226aac62d6db8041;hpb=db0d6ecdbbde48e77ae458e904099a4a973d1511;p=rust-lightning diff --git a/lightning/src/util/test_utils.rs b/lightning/src/util/test_utils.rs index 9fb5178a..be09c8f0 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 { @@ -115,8 +137,7 @@ impl SignerProvider for OnlyReadsKeysInterface { fn derive_channel_signer(&self, _channel_value_satoshis: u64, _channel_keys_id: [u8; 32]) -> Self::Signer { unreachable!(); } fn read_chan_signer(&self, mut reader: &[u8]) -> Result { - let dummy_sk = SecretKey::from_slice(&[42; 32]).unwrap(); - let inner: InMemorySigner = ReadableArgs::read(&mut reader, dummy_sk)?; + let inner: InMemorySigner = Readable::read(&mut reader)?; let state = Arc::new(Mutex::new(EnforcementState::new())); Ok(EnforcingSigner::new_with_revoked( @@ -235,7 +256,7 @@ impl TestPersister { self.update_rets.lock().unwrap().push_back(next_ret); } } -impl chainmonitor::Persist for TestPersister { +impl chainmonitor::Persist for TestPersister { fn persist_new_channel(&self, _funding_txo: OutPoint, _data: &channelmonitor::ChannelMonitor, _id: MonitorUpdateId) -> chain::ChannelMonitorUpdateStatus { if let Some(update_ret) = self.update_rets.lock().unwrap().pop_front() { return update_ret @@ -415,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(), }; @@ -490,7 +511,7 @@ impl msgs::RoutingMessageHandler for TestRoutingMessageHandler { Some((chan_ann, Some(chan_upd_1), Some(chan_upd_2))) } - fn get_next_node_announcement(&self, _starting_point: Option<&PublicKey>) -> Option { + fn get_next_node_announcement(&self, _starting_point: Option<&NodeId>) -> Option { None } @@ -621,6 +642,49 @@ impl Logger for TestLogger { } } +pub struct TestNodeSigner { + node_secret: SecretKey, +} + +impl TestNodeSigner { + pub fn new(node_secret: SecretKey) -> Self { + Self { node_secret } + } +} + +impl NodeSigner for TestNodeSigner { + fn get_inbound_payment_key_material(&self) -> crate::chain::keysinterface::KeyMaterial { + unreachable!() + } + + fn get_node_id(&self, recipient: Recipient) -> Result { + let node_secret = match recipient { + Recipient::Node => Ok(&self.node_secret), + Recipient::PhantomNode => Err(()) + }?; + Ok(PublicKey::from_secret_key(&Secp256k1::signing_only(), node_secret)) + } + + fn ecdh(&self, recipient: Recipient, other_key: &PublicKey, tweak: Option<&bitcoin::secp256k1::Scalar>) -> Result { + let mut node_secret = match recipient { + Recipient::Node => Ok(self.node_secret.clone()), + Recipient::PhantomNode => Err(()) + }?; + if let Some(tweak) = tweak { + node_secret = node_secret.mul_tweak(tweak).map_err(|_| ())?; + } + Ok(SharedSecret::new(other_key, &node_secret)) + } + + fn sign_invoice(&self, _: &[u8], _: &[bitcoin::bech32::u5], _: Recipient) -> Result { + unreachable!() + } + + fn sign_gossip_message(&self, _msg: msgs::UnsignedGossipMessage) -> Result { + unreachable!() + } +} + pub struct TestKeysInterface { pub backing: keysinterface::PhantomKeysManager, pub override_random_bytes: Mutex>, @@ -640,10 +704,6 @@ impl EntropySource for TestKeysInterface { } impl NodeSigner for TestKeysInterface { - fn get_node_secret(&self, recipient: Recipient) -> Result { - self.backing.get_node_secret(recipient) - } - fn get_node_id(&self, recipient: Recipient) -> Result { self.backing.get_node_id(recipient) } @@ -681,7 +741,7 @@ impl SignerProvider for TestKeysInterface { fn read_chan_signer(&self, buffer: &[u8]) -> Result { let mut reader = io::Cursor::new(buffer); - let inner: InMemorySigner = ReadableArgs::read(&mut reader, self.get_node_secret(Recipient::Node).unwrap())?; + let inner: InMemorySigner = Readable::read(&mut reader)?; let state = self.make_enforcement_state_cell(inner.commitment_seed); Ok(EnforcingSigner::new_with_revoked(