X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Futil%2Ftest_utils.rs;h=91af4202d28be148a7e333673fbb486089e5a061;hb=2087032e7a823f6c63a64dac951e0f4843bc4667;hp=6a652aa6b39c0016c0ac3705cfcf9206733e04a8;hpb=9098240e341cf09578819cdf1d2950303478b137;p=rust-lightning diff --git a/lightning/src/util/test_utils.rs b/lightning/src/util/test_utils.rs index 6a652aa6..91af4202 100644 --- a/lightning/src/util/test_utils.rs +++ b/lightning/src/util/test_utils.rs @@ -3,15 +3,16 @@ use chain::chaininterface::{ConfirmationTarget, ChainError, ChainWatchInterface} use chain::transaction::OutPoint; use chain::keysinterface; use ln::channelmonitor; -use ln::features::InitFeatures; +use ln::features::{ChannelFeatures, InitFeatures}; use ln::msgs; -use ln::msgs::LightningError; use ln::channelmonitor::HTLCUpdate; use util::enforcing_trait_impls::EnforcingChannelKeys; use util::events; use util::logger::{Logger, Level, Record}; -use util::ser::{Readable, ReadableArgs, Writer, Writeable}; +use util::ser::{Readable, Writer, Writeable}; +use bitcoin::BitcoinHash; +use bitcoin::blockdata::constants::genesis_block; use bitcoin::blockdata::transaction::Transaction; use bitcoin::blockdata::script::{Builder, Script}; use bitcoin::blockdata::block::Block; @@ -19,11 +20,12 @@ use bitcoin::blockdata::opcodes; use bitcoin::network::constants::Network; use bitcoin::hash_types::{Txid, BlockHash}; -use bitcoin::secp256k1::{SecretKey, PublicKey}; +use bitcoin::secp256k1::{SecretKey, PublicKey, Secp256k1, Signature}; use std::time::{SystemTime, UNIX_EPOCH}; -use std::sync::{Arc,Mutex}; -use std::{mem}; +use std::sync::Mutex; +use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; +use std::{cmp, mem}; use std::collections::HashMap; pub struct TestVecWriter(pub Vec); @@ -49,14 +51,14 @@ impl chaininterface::FeeEstimator for TestFeeEstimator { pub struct TestChannelMonitor<'a> { pub added_monitors: Mutex)>>, pub latest_monitor_update_id: Mutex>, - pub simple_monitor: channelmonitor::SimpleManyChannelMonitor, + pub simple_monitor: channelmonitor::SimpleManyChannelMonitor, pub update_ret: Mutex>, // If this is set to Some(), after the next return, we'll always return this until update_ret // is changed: pub next_update_ret: Mutex>>, } impl<'a> TestChannelMonitor<'a> { - pub fn new(chain_monitor: Arc, broadcaster: &'a chaininterface::BroadcasterInterface, logger: Arc, fee_estimator: &'a TestFeeEstimator) -> Self { + pub fn new(chain_monitor: &'a chaininterface::ChainWatchInterface, broadcaster: &'a chaininterface::BroadcasterInterface, logger: &'a TestLogger, fee_estimator: &'a TestFeeEstimator) -> Self { Self { added_monitors: Mutex::new(Vec::new()), latest_monitor_update_id: Mutex::new(HashMap::new()), @@ -66,14 +68,16 @@ impl<'a> TestChannelMonitor<'a> { } } } -impl<'a> channelmonitor::ManyChannelMonitor for TestChannelMonitor<'a> { +impl<'a> channelmonitor::ManyChannelMonitor for TestChannelMonitor<'a> { + type Keys = EnforcingChannelKeys; + fn add_monitor(&self, funding_txo: OutPoint, monitor: channelmonitor::ChannelMonitor) -> Result<(), channelmonitor::ChannelMonitorUpdateErr> { // At every point where we get a monitor update, we should be able to send a useful monitor // to a watchtower and disk... let mut w = TestVecWriter(Vec::new()); monitor.write_for_disk(&mut w).unwrap(); let new_monitor = <(BlockHash, channelmonitor::ChannelMonitor)>::read( - &mut ::std::io::Cursor::new(&w.0), Arc::new(TestLogger::new())).unwrap().1; + &mut ::std::io::Cursor::new(&w.0)).unwrap().1; assert!(new_monitor == monitor); self.latest_monitor_update_id.lock().unwrap().insert(funding_txo.to_channel_id(), (funding_txo, monitor.get_latest_update_id())); self.added_monitors.lock().unwrap().push((funding_txo, monitor)); @@ -102,7 +106,7 @@ impl<'a> channelmonitor::ManyChannelMonitor for TestChanne w.0.clear(); monitor.write_for_disk(&mut w).unwrap(); let new_monitor = <(BlockHash, channelmonitor::ChannelMonitor)>::read( - &mut ::std::io::Cursor::new(&w.0), Arc::new(TestLogger::new())).unwrap().1; + &mut ::std::io::Cursor::new(&w.0)).unwrap().1; assert!(new_monitor == *monitor); self.added_monitors.lock().unwrap().push((funding_txo, new_monitor)); @@ -170,41 +174,105 @@ impl events::MessageSendEventsProvider for TestChannelMessageHandler { } } +fn get_dummy_channel_announcement(short_chan_id: u64) -> msgs::ChannelAnnouncement { + use bitcoin::secp256k1::ffi::Signature as FFISignature; + let secp_ctx = Secp256k1::new(); + let network = Network::Testnet; + let node_1_privkey = SecretKey::from_slice(&[42; 32]).unwrap(); + let node_2_privkey = SecretKey::from_slice(&[41; 32]).unwrap(); + let node_1_btckey = SecretKey::from_slice(&[40; 32]).unwrap(); + let node_2_btckey = SecretKey::from_slice(&[39; 32]).unwrap(); + let unsigned_ann = msgs::UnsignedChannelAnnouncement { + features: ChannelFeatures::known(), + chain_hash: genesis_block(network).header.bitcoin_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), + excess_data: Vec::new(), + }; + + msgs::ChannelAnnouncement { + node_signature_1: Signature::from(FFISignature::new()), + node_signature_2: Signature::from(FFISignature::new()), + bitcoin_signature_1: Signature::from(FFISignature::new()), + bitcoin_signature_2: Signature::from(FFISignature::new()), + contents: unsigned_ann, + } +} + +fn get_dummy_channel_update(short_chan_id: u64) -> msgs::ChannelUpdate { + use bitcoin::secp256k1::ffi::Signature as FFISignature; + let network = Network::Testnet; + msgs::ChannelUpdate { + signature: Signature::from(FFISignature::new()), + contents: msgs::UnsignedChannelUpdate { + chain_hash: genesis_block(network).header.bitcoin_hash(), + short_channel_id: short_chan_id, + timestamp: 0, + flags: 0, + cltv_expiry_delta: 0, + htlc_minimum_msat: 0, + fee_base_msat: 0, + fee_proportional_millionths: 0, + excess_data: vec![], + } + } +} + pub struct TestRoutingMessageHandler { - request_full_sync: bool, + pub chan_upds_recvd: AtomicUsize, + pub chan_anns_recvd: AtomicUsize, + pub chan_anns_sent: AtomicUsize, + pub request_full_sync: AtomicBool, } impl TestRoutingMessageHandler { pub fn new() -> Self { TestRoutingMessageHandler { - request_full_sync: false, + chan_upds_recvd: AtomicUsize::new(0), + chan_anns_recvd: AtomicUsize::new(0), + chan_anns_sent: AtomicUsize::new(0), + request_full_sync: AtomicBool::new(false), } } - - pub fn set_request_full_sync(mut self) -> Self { - self.request_full_sync = true; - self - } } impl msgs::RoutingMessageHandler for TestRoutingMessageHandler { - fn handle_node_announcement(&self, _msg: &msgs::NodeAnnouncement) -> Result { - Err(LightningError { err: "", action: msgs::ErrorAction::IgnoreError }) + fn handle_node_announcement(&self, _msg: &msgs::NodeAnnouncement) -> Result { + Err(msgs::LightningError { err: "", action: msgs::ErrorAction::IgnoreError }) } - fn handle_channel_announcement(&self, _msg: &msgs::ChannelAnnouncement) -> Result { - Err(LightningError { err: "", action: msgs::ErrorAction::IgnoreError }) + fn handle_channel_announcement(&self, _msg: &msgs::ChannelAnnouncement) -> Result { + self.chan_anns_recvd.fetch_add(1, Ordering::AcqRel); + Err(msgs::LightningError { err: "", action: msgs::ErrorAction::IgnoreError }) } - fn handle_channel_update(&self, _msg: &msgs::ChannelUpdate) -> Result { - Err(LightningError { err: "", action: msgs::ErrorAction::IgnoreError }) + fn handle_channel_update(&self, _msg: &msgs::ChannelUpdate) -> Result { + self.chan_upds_recvd.fetch_add(1, Ordering::AcqRel); + Err(msgs::LightningError { err: "", action: msgs::ErrorAction::IgnoreError }) } fn handle_htlc_fail_channel_update(&self, _update: &msgs::HTLCFailChannelUpdate) {} - fn get_next_channel_announcements(&self, _starting_point: u64, _batch_amount: u8) -> Vec<(msgs::ChannelAnnouncement, Option, Option)> { - Vec::new() + fn get_next_channel_announcements(&self, starting_point: u64, batch_amount: u8) -> Vec<(msgs::ChannelAnnouncement, Option, Option)> { + let mut chan_anns = Vec::new(); + const TOTAL_UPDS: u64 = 100; + let end: u64 = cmp::min(starting_point + batch_amount as u64, TOTAL_UPDS - self.chan_anns_sent.load(Ordering::Acquire) as u64); + for i in starting_point..end { + let chan_upd_1 = get_dummy_channel_update(i); + let chan_upd_2 = get_dummy_channel_update(i); + let chan_ann = get_dummy_channel_announcement(i); + + chan_anns.push((chan_ann, Some(chan_upd_1), Some(chan_upd_2))); + } + + self.chan_anns_sent.fetch_add(chan_anns.len(), Ordering::AcqRel); + chan_anns } + fn get_next_node_announcements(&self, _starting_point: Option<&PublicKey>, _batch_amount: u8) -> Vec { Vec::new() } + fn should_request_full_sync(&self, _node_id: &PublicKey) -> bool { - self.request_full_sync + self.request_full_sync.load(Ordering::Acquire) } } @@ -275,14 +343,17 @@ impl keysinterface::KeysInterface for TestKeysInterface { } impl TestKeysInterface { - pub fn new(seed: &[u8; 32], network: Network, logger: Arc) -> Self { + pub fn new(seed: &[u8; 32], network: Network) -> Self { let now = SystemTime::now().duration_since(UNIX_EPOCH).expect("Time went backwards"); Self { - backing: keysinterface::KeysManager::new(seed, network, logger, now.as_secs(), now.subsec_nanos()), + backing: keysinterface::KeysManager::new(seed, network, now.as_secs(), now.subsec_nanos()), override_session_priv: Mutex::new(None), override_channel_id_priv: Mutex::new(None), } } + pub fn derive_channel_keys(&self, channel_value_satoshis: u64, user_id_1: u64, user_id_2: u64) -> EnforcingChannelKeys { + EnforcingChannelKeys::new(self.backing.derive_channel_keys(channel_value_satoshis, user_id_1, user_id_2)) + } } pub struct TestChainWatcher {