Merge pull request #1301 from TheBlueMatt/2022-02-router-no-test
[rust-lightning] / lightning / src / util / test_utils.rs
index 6442c9cfa27bd6b7b8a28efaf45008ab0d8cdd52..0e1c92d009c112cbc74c6f811a5fc9d158f56dd3 100644 (file)
@@ -21,8 +21,7 @@ use ln::features::{ChannelFeatures, InitFeatures};
 use ln::msgs;
 use ln::msgs::OptionalField;
 use ln::script::ShutdownScript;
-use routing::scoring::ScorerUsingTime;
-use routing::scoring::time::Eternity;
+use routing::scoring::FixedPenaltyScorer;
 use util::enforcing_trait_impls::{EnforcingSigner, EnforcementState};
 use util::events;
 use util::logger::{Logger, Level, Record};
@@ -47,7 +46,8 @@ use core::time::Duration;
 use sync::{Mutex, Arc};
 use core::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
 use core::{cmp, mem};
-use chain::keysinterface::InMemorySigner;
+use bitcoin::bech32::u5;
+use chain::keysinterface::{InMemorySigner, KeyMaterial};
 
 pub struct TestVecWriter(pub Vec<u8>);
 impl Writer for TestVecWriter {
@@ -71,13 +71,15 @@ impl keysinterface::KeysInterface for OnlyReadsKeysInterface {
        type Signer = EnforcingSigner;
 
        fn get_node_secret(&self) -> SecretKey { 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 read_chan_signer(&self, mut reader: &[u8]) -> Result<Self::Signer, msgs::DecodeError> {
-               let inner: InMemorySigner = Readable::read(&mut reader)?;
+               let dummy_sk = SecretKey::from_slice(&[42; 32]).unwrap();
+               let inner: InMemorySigner = ReadableArgs::read(&mut reader, dummy_sk)?;
                let state = Arc::new(Mutex::new(EnforcementState::new()));
 
                Ok(EnforcingSigner::new_with_revoked(
@@ -86,7 +88,7 @@ impl keysinterface::KeysInterface for OnlyReadsKeysInterface {
                        false
                ))
        }
-       fn sign_invoice(&self, _invoice_preimage: Vec<u8>) -> Result<RecoverableSignature, ()> { unreachable!(); }
+       fn sign_invoice(&self, _hrp_bytes: &[u8], _invoice_data: &[u5]) -> Result<RecoverableSignature, ()> { unreachable!(); }
 }
 
 pub struct TestChainMonitor<'a> {
@@ -461,6 +463,7 @@ impl Logger for TestLogger {
        fn log(&self, record: &Record) {
                *self.lines.lock().unwrap().entry((record.module_path.to_string(), format!("{}", record.args))).or_insert(0) += 1;
                if record.level >= self.level {
+                       #[cfg(feature = "std")]
                        println!("{:<5} {} [{} : {}, {}] {}", record.level.to_string(), self.id, record.module_path, record.file, record.line, record.args);
                }
        }
@@ -479,6 +482,7 @@ impl keysinterface::KeysInterface for TestKeysInterface {
        type Signer = EnforcingSigner;
 
        fn get_node_secret(&self) -> SecretKey { self.backing.get_node_secret() }
+       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 {
@@ -515,7 +519,7 @@ impl keysinterface::KeysInterface for TestKeysInterface {
        fn read_chan_signer(&self, buffer: &[u8]) -> Result<Self::Signer, msgs::DecodeError> {
                let mut reader = io::Cursor::new(buffer);
 
-               let inner: InMemorySigner = Readable::read(&mut reader)?;
+               let inner: InMemorySigner = ReadableArgs::read(&mut reader, self.get_node_secret())?;
                let state = self.make_enforcement_state_cell(inner.commitment_seed);
 
                Ok(EnforcingSigner::new_with_revoked(
@@ -525,8 +529,8 @@ impl keysinterface::KeysInterface for TestKeysInterface {
                ))
        }
 
-       fn sign_invoice(&self, invoice_preimage: Vec<u8>) -> Result<RecoverableSignature, ()> {
-               self.backing.sign_invoice(invoice_preimage)
+       fn sign_invoice(&self, hrp_bytes: &[u8], invoice_data: &[u5]) -> Result<RecoverableSignature, ()> {
+               self.backing.sign_invoice(hrp_bytes, invoice_data)
        }
 }
 
@@ -569,9 +573,17 @@ impl TestKeysInterface {
        }
 }
 
+pub(crate) fn panicking() -> bool {
+       #[cfg(feature = "std")]
+       let panicking = ::std::thread::panicking();
+       #[cfg(not(feature = "std"))]
+       let panicking = false;
+       return panicking;
+}
+
 impl Drop for TestKeysInterface {
        fn drop(&mut self) {
-               if std::thread::panicking() {
+               if panicking() {
                        return;
                }
 
@@ -663,7 +675,7 @@ impl chain::Filter for TestChainSource {
 
 impl Drop for TestChainSource {
        fn drop(&mut self) {
-               if std::thread::panicking() {
+               if panicking() {
                        return;
                }
 
@@ -711,4 +723,4 @@ impl core::fmt::Debug for OnRegisterOutput {
 }
 
 /// A scorer useful in testing, when the passage of time isn't a concern.
-pub type TestScorer = ScorerUsingTime<Eternity>;
+pub type TestScorer = FixedPenaltyScorer;