X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Futil%2Ftest_utils.rs;h=2cac5a874a62f0b87770e642905527879ba058ff;hb=2e06efe2ffda522ba8d34213106244af2b608ccf;hp=89bf27de2805ef8c152279cc02855d114d7a4e9b;hpb=593d8c4610f082441563d4906d64175a354b1cfc;p=rust-lightning diff --git a/lightning/src/util/test_utils.rs b/lightning/src/util/test_utils.rs index 89bf27de..2cac5a87 100644 --- a/lightning/src/util/test_utils.rs +++ b/lightning/src/util/test_utils.rs @@ -21,6 +21,7 @@ use crate::ln::channelmanager; use crate::ln::features::{ChannelFeatures, InitFeatures, NodeFeatures}; use crate::ln::{msgs, wire}; use crate::ln::script::ShutdownScript; +use crate::routing::router::{InFlightHtlcs, Route, RouteHop, RouteParameters, Router}; use crate::routing::scoring::FixedPenaltyScorer; use crate::util::enforcing_trait_impls::{EnforcingSigner, EnforcementState}; use crate::util::events; @@ -48,7 +49,7 @@ use crate::sync::{Mutex, Arc}; use core::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; use core::mem; use bitcoin::bech32::u5; -use crate::chain::keysinterface::{InMemorySigner, Recipient, KeyMaterial}; +use crate::chain::keysinterface::{InMemorySigner, Recipient, KeyMaterial, EntropySource, NodeSigner, SignerProvider}; #[cfg(feature = "std")] use std::time::{SystemTime, UNIX_EPOCH}; @@ -71,17 +72,46 @@ impl chaininterface::FeeEstimator for TestFeeEstimator { } } +pub struct TestRouter {} + +impl Router for TestRouter { + fn find_route( + &self, _payer: &PublicKey, _params: &RouteParameters, _first_hops: Option<&[&channelmanager::ChannelDetails]>, + _inflight_htlcs: InFlightHtlcs + ) -> Result { + Err(msgs::LightningError { + err: String::from("Not implemented"), + action: msgs::ErrorAction::IgnoreError + }) + } + fn notify_payment_path_failed(&self, _path: &[&RouteHop], _short_channel_id: u64) {} + fn notify_payment_path_successful(&self, _path: &[&RouteHop]) {} + fn notify_payment_probe_successful(&self, _path: &[&RouteHop]) {} + fn notify_payment_probe_failed(&self, _path: &[&RouteHop], _short_channel_id: u64) {} +} + pub struct OnlyReadsKeysInterface {} -impl keysinterface::KeysInterface for OnlyReadsKeysInterface { - type Signer = EnforcingSigner; +impl EntropySource for OnlyReadsKeysInterface { + fn get_secure_random_bytes(&self) -> [u8; 32] { [0; 32] }} + +impl NodeSigner for OnlyReadsKeysInterface { fn get_node_secret(&self, _recipient: Recipient) -> Result { unreachable!(); } + fn get_node_id(&self, recipient: Recipient) -> Result { + let secp_ctx = Secp256k1::signing_only(); + Ok(PublicKey::from_secret_key(&secp_ctx, &self.get_node_secret(recipient)?)) + } fn ecdh(&self, _recipient: Recipient, _other_key: &PublicKey, _tweak: Option<&Scalar>) -> Result { unreachable!(); } fn get_inbound_payment_key_material(&self) -> KeyMaterial { unreachable!(); } - fn get_destination_script(&self) -> Script { unreachable!(); } - fn get_shutdown_scriptpubkey(&self) -> ShutdownScript { unreachable!(); } - fn get_channel_signer(&self, _inbound: bool, _channel_value_satoshis: u64) -> EnforcingSigner { unreachable!(); } - fn get_secure_random_bytes(&self) -> [u8; 32] { [0; 32] } + fn sign_invoice(&self, _hrp_bytes: &[u8], _invoice_data: &[u5], _recipient: Recipient) -> Result { unreachable!(); } +} + +impl SignerProvider for OnlyReadsKeysInterface { + type Signer = EnforcingSigner; + + fn generate_channel_keys_id(&self, _inbound: bool, _channel_value_satoshis: u64, _user_channel_id: u128) -> [u8; 32] { unreachable!(); } + + 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(); @@ -94,7 +124,12 @@ impl keysinterface::KeysInterface for OnlyReadsKeysInterface { false )) } - fn sign_invoice(&self, _hrp_bytes: &[u8], _invoice_data: &[u5], _recipient: Recipient) -> Result { unreachable!(); } + + fn get_destination_script(&self) -> Script { unreachable!(); } + fn get_shutdown_scriptpubkey(&self) -> ShutdownScript { unreachable!(); } +} + +impl keysinterface::KeysInterface for OnlyReadsKeysInterface { } pub struct TestChainMonitor<'a> { @@ -602,45 +637,49 @@ pub struct TestKeysInterface { expectations: Mutex>>, } -impl keysinterface::KeysInterface for TestKeysInterface { - type Signer = EnforcingSigner; +impl EntropySource for TestKeysInterface { + fn get_secure_random_bytes(&self) -> [u8; 32] { + let override_random_bytes = self.override_random_bytes.lock().unwrap(); + if let Some(bytes) = &*override_random_bytes { + return *bytes; + } + self.backing.get_secure_random_bytes() + } +} +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) } + fn ecdh(&self, recipient: Recipient, other_key: &PublicKey, tweak: Option<&Scalar>) -> Result { self.backing.ecdh(recipient, other_key, tweak) } + fn get_inbound_payment_key_material(&self) -> keysinterface::KeyMaterial { self.backing.get_inbound_payment_key_material() } - fn get_destination_script(&self) -> Script { self.backing.get_destination_script() } - fn get_shutdown_scriptpubkey(&self) -> ShutdownScript { - match &mut *self.expectations.lock().unwrap() { - None => self.backing.get_shutdown_scriptpubkey(), - Some(expectations) => match expectations.pop_front() { - None => panic!("Unexpected get_shutdown_scriptpubkey"), - Some(expectation) => expectation.returns, - }, - } + fn sign_invoice(&self, hrp_bytes: &[u8], invoice_data: &[u5], recipient: Recipient) -> Result { + self.backing.sign_invoice(hrp_bytes, invoice_data, recipient) } +} - fn get_channel_signer(&self, inbound: bool, channel_value_satoshis: u64) -> EnforcingSigner { - let keys = self.backing.get_channel_signer(inbound, channel_value_satoshis); - let state = self.make_enforcement_state_cell(keys.commitment_seed); - EnforcingSigner::new_with_revoked(keys, state, self.disable_revocation_policy_check) +impl SignerProvider for TestKeysInterface { + type Signer = EnforcingSigner; + + fn generate_channel_keys_id(&self, inbound: bool, channel_value_satoshis: u64, user_channel_id: u128) -> [u8; 32] { + self.backing.generate_channel_keys_id(inbound, channel_value_satoshis, user_channel_id) } - fn get_secure_random_bytes(&self) -> [u8; 32] { - let override_random_bytes = self.override_random_bytes.lock().unwrap(); - if let Some(bytes) = &*override_random_bytes { - return *bytes; - } - self.backing.get_secure_random_bytes() + fn derive_channel_signer(&self, channel_value_satoshis: u64, channel_keys_id: [u8; 32]) -> EnforcingSigner { + let keys = self.backing.derive_channel_signer(channel_value_satoshis, channel_keys_id); + let state = self.make_enforcement_state_cell(keys.commitment_seed); + EnforcingSigner::new_with_revoked(keys, state, self.disable_revocation_policy_check) } fn read_chan_signer(&self, buffer: &[u8]) -> Result { @@ -656,11 +695,21 @@ impl keysinterface::KeysInterface for TestKeysInterface { )) } - fn sign_invoice(&self, hrp_bytes: &[u8], invoice_data: &[u5], recipient: Recipient) -> Result { - self.backing.sign_invoice(hrp_bytes, invoice_data, recipient) + fn get_destination_script(&self) -> Script { self.backing.get_destination_script() } + + fn get_shutdown_scriptpubkey(&self) -> ShutdownScript { + match &mut *self.expectations.lock().unwrap() { + None => self.backing.get_shutdown_scriptpubkey(), + Some(expectations) => match expectations.pop_front() { + None => panic!("Unexpected get_shutdown_scriptpubkey"), + Some(expectation) => expectation.returns, + }, + } } } +impl keysinterface::KeysInterface for TestKeysInterface {} + impl TestKeysInterface { pub fn new(seed: &[u8; 32], network: Network) -> Self { let now = Duration::from_secs(genesis_block(network).header.time as u64); @@ -673,7 +722,7 @@ impl TestKeysInterface { } } - /// Sets an expectation that [`keysinterface::KeysInterface::get_shutdown_scriptpubkey`] is + /// Sets an expectation that [`keysinterface::SignerProvider::get_shutdown_scriptpubkey`] is /// called. pub fn expect(&self, expectation: OnGetShutdownScriptpubkey) -> &Self { self.expectations.lock().unwrap() @@ -721,7 +770,7 @@ impl Drop for TestKeysInterface { } } -/// An expectation that [`keysinterface::KeysInterface::get_shutdown_scriptpubkey`] was called and +/// An expectation that [`keysinterface::SignerProvider::get_shutdown_scriptpubkey`] was called and /// returns a [`ShutdownScript`]. pub struct OnGetShutdownScriptpubkey { /// A shutdown script used to close a channel.