]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Add test_util for overriding session privs for onion crypt
authorMatt Corallo <git@bluematt.me>
Mon, 17 Dec 2018 19:20:27 +0000 (14:20 -0500)
committerMatt Corallo <git@bluematt.me>
Tue, 18 Dec 2018 03:55:45 +0000 (22:55 -0500)
src/ln/channelmanager.rs
src/util/test_utils.rs

index 217a57bc48b90ea2e4a7df067d325dea8c8cfc14..5216882d4a90126be786776f60781443eb2b31f2 100644 (file)
@@ -3570,6 +3570,7 @@ mod tests {
                chain_monitor: Arc<chaininterface::ChainWatchInterfaceUtil>,
                tx_broadcaster: Arc<test_utils::TestBroadcaster>,
                chan_monitor: Arc<test_utils::TestChannelMonitor>,
+               keys_manager: Arc<test_utils::TestKeysInterface>,
                node: Arc<ChannelManager>,
                router: Router,
                node_seed: [u8; 32],
@@ -4303,14 +4304,14 @@ mod tests {
                        let tx_broadcaster = Arc::new(test_utils::TestBroadcaster{txn_broadcasted: Mutex::new(Vec::new())});
                        let mut seed = [0; 32];
                        rng.fill_bytes(&mut seed);
-                       let keys_manager = Arc::new(keysinterface::KeysManager::new(&seed, Network::Testnet, Arc::clone(&logger)));
+                       let keys_manager = Arc::new(test_utils::TestKeysInterface::new(&seed, Network::Testnet, Arc::clone(&logger)));
                        let chan_monitor = Arc::new(test_utils::TestChannelMonitor::new(chain_monitor.clone(), tx_broadcaster.clone(), logger.clone()));
                        let mut config = UserConfig::new();
                        config.channel_options.announced_channel = true;
                        config.channel_limits.force_announced_channel_preference = false;
                        let node = ChannelManager::new(Network::Testnet, feeest.clone(), chan_monitor.clone(), chain_monitor.clone(), tx_broadcaster.clone(), Arc::clone(&logger), keys_manager.clone(), config).unwrap();
                        let router = Router::new(PublicKey::from_secret_key(&secp_ctx, &keys_manager.get_node_secret()), chain_monitor.clone(), Arc::clone(&logger));
-                       nodes.push(Node { chain_monitor, tx_broadcaster, chan_monitor, node, router, node_seed: seed,
+                       nodes.push(Node { chain_monitor, tx_broadcaster, chan_monitor, node, router, keys_manager, node_seed: seed,
                                network_payment_count: payment_count.clone(),
                                network_chan_count: chan_count.clone(),
                        });
index 3acb0d02e78e2bdec6214dd82b5ea117e341d16d..b04f728b0f71edde16afa738fa2caa248c242bdb 100644 (file)
@@ -1,6 +1,7 @@
 use chain::chaininterface;
 use chain::chaininterface::ConfirmationTarget;
 use chain::transaction::OutPoint;
+use chain::keysinterface;
 use ln::channelmonitor;
 use ln::msgs;
 use ln::msgs::{HandleError};
@@ -10,9 +11,11 @@ use util::logger::{Logger, Level, Record};
 use util::ser::{ReadableArgs, Writer};
 
 use bitcoin::blockdata::transaction::Transaction;
+use bitcoin::blockdata::script::Script;
 use bitcoin::util::hash::Sha256dHash;
+use bitcoin::network::constants::Network;
 
-use secp256k1::PublicKey;
+use secp256k1::{SecretKey, PublicKey};
 
 use std::sync::{Arc,Mutex};
 use std::{mem};
@@ -208,3 +211,31 @@ impl Logger for TestLogger {
                }
        }
 }
+
+pub struct TestKeysInterface {
+       backing: keysinterface::KeysManager,
+       pub override_session_priv: Mutex<Option<SecretKey>>,
+}
+
+impl keysinterface::KeysInterface for TestKeysInterface {
+       fn get_node_secret(&self) -> SecretKey { self.backing.get_node_secret() }
+       fn get_destination_script(&self) -> Script { self.backing.get_destination_script() }
+       fn get_shutdown_pubkey(&self) -> PublicKey { self.backing.get_shutdown_pubkey() }
+       fn get_channel_keys(&self, inbound: bool) -> keysinterface::ChannelKeys { self.backing.get_channel_keys(inbound) }
+
+       fn get_session_key(&self) -> SecretKey {
+               match *self.override_session_priv.lock().unwrap() {
+                       Some(key) => key.clone(),
+                       None => self.backing.get_session_key()
+               }
+       }
+}
+
+impl TestKeysInterface {
+       pub fn new(seed: &[u8; 32], network: Network, logger: Arc<Logger>) -> Self {
+               Self {
+                       backing: keysinterface::KeysManager::new(seed, network, logger),
+                       override_session_priv: Mutex::new(None),
+               }
+       }
+}