X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Futil%2Ftest_utils.rs;h=a9a10451b2c1dd0825a2f985e13075c45b759961;hb=69ee4860848d5992b238eca3343141004d9d1572;hp=43bb1ef930391d7966af82308d74aef1ad3e6073;hpb=df829a85263e0bc675ca696c0f6ff0a49d89e623;p=rust-lightning diff --git a/lightning/src/util/test_utils.rs b/lightning/src/util/test_utils.rs index 43bb1ef9..a9a10451 100644 --- a/lightning/src/util/test_utils.rs +++ b/lightning/src/util/test_utils.rs @@ -28,6 +28,7 @@ use bitcoin::blockdata::constants::genesis_block; use bitcoin::blockdata::transaction::{Transaction, TxOut}; use bitcoin::blockdata::script::{Builder, Script}; use bitcoin::blockdata::opcodes; +use bitcoin::blockdata::block::BlockHeader; use bitcoin::network::constants::Network; use bitcoin::hash_types::{BlockHash, Txid}; @@ -36,17 +37,17 @@ use bitcoin::secp256k1::recovery::RecoverableSignature; use regex; +use io; use prelude::*; use core::time::Duration; -use std::sync::{Mutex, Arc}; +use sync::{Mutex, Arc}; use core::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; use core::{cmp, mem}; -use std::collections::{HashMap, HashSet, VecDeque}; use chain::keysinterface::InMemorySigner; pub struct TestVecWriter(pub Vec); impl Writer for TestVecWriter { - fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> { + fn write_all(&mut self, buf: &[u8]) -> Result<(), io::Error> { self.0.extend_from_slice(buf); Ok(()) } @@ -56,11 +57,11 @@ impl Writer for TestVecWriter { } pub struct TestFeeEstimator { - pub sat_per_kw: u32, + pub sat_per_kw: Mutex, } impl chaininterface::FeeEstimator for TestFeeEstimator { fn get_est_sat_per_1000_weight(&self, _confirmation_target: ConfirmationTarget) -> u32 { - self.sat_per_kw + *self.sat_per_kw.lock().unwrap() } } @@ -75,7 +76,7 @@ impl keysinterface::KeysInterface for OnlyReadsKeysInterface { fn get_secure_random_bytes(&self) -> [u8; 32] { [0; 32] } fn read_chan_signer(&self, reader: &[u8]) -> Result { - EnforcingSigner::read(&mut std::io::Cursor::new(reader)) + EnforcingSigner::read(&mut io::Cursor::new(reader)) } fn sign_invoice(&self, _invoice_preimage: Vec) -> Result { unreachable!(); } } @@ -114,7 +115,7 @@ impl<'a> chain::Watch for TestChainMonitor<'a> { let mut w = TestVecWriter(Vec::new()); monitor.write(&mut w).unwrap(); let new_monitor = <(BlockHash, channelmonitor::ChannelMonitor)>::read( - &mut ::std::io::Cursor::new(&w.0), self.keys_manager).unwrap().1; + &mut io::Cursor::new(&w.0), self.keys_manager).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)); @@ -136,7 +137,7 @@ impl<'a> chain::Watch for TestChainMonitor<'a> { let mut w = TestVecWriter(Vec::new()); update.write(&mut w).unwrap(); assert!(channelmonitor::ChannelMonitorUpdate::read( - &mut ::std::io::Cursor::new(&w.0)).unwrap() == update); + &mut io::Cursor::new(&w.0)).unwrap() == update); if let Some(exp) = self.expect_channel_force_closed.lock().unwrap().take() { assert_eq!(funding_txo.to_channel_id(), exp.0); @@ -155,7 +156,7 @@ impl<'a> chain::Watch for TestChainMonitor<'a> { w.0.clear(); monitor.write(&mut w).unwrap(); let new_monitor = <(BlockHash, channelmonitor::ChannelMonitor)>::read( - &mut ::std::io::Cursor::new(&w.0), self.keys_manager).unwrap().1; + &mut io::Cursor::new(&w.0), self.keys_manager).unwrap().1; assert!(new_monitor == *monitor); self.added_monitors.lock().unwrap().push((funding_txo, new_monitor)); @@ -201,9 +202,18 @@ impl channelmonitor::Persist for TestPersis pub struct TestBroadcaster { pub txn_broadcasted: Mutex>, + pub blocks: Arc>>, } impl chaininterface::BroadcasterInterface for TestBroadcaster { fn broadcast_transaction(&self, tx: &Transaction) { + assert!(tx.lock_time < 1_500_000_000); + if tx.lock_time > self.blocks.lock().unwrap().len() as u32 + 1 && tx.lock_time < 500_000_000 { + for inp in tx.input.iter() { + if inp.sequence != 0xffffffff { + panic!("We should never broadcast a transaction before its locktime ({})!", tx.lock_time); + } + } + } self.txn_broadcasted.lock().unwrap().push(tx.clone()); } } @@ -430,7 +440,7 @@ impl TestLogger { 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 self.level >= record.level { + if record.level >= self.level { println!("{:<5} {} [{} : {}, {}] {}", record.level.to_string(), self.id, record.module_path, record.file, record.line, record.args); } } @@ -472,7 +482,7 @@ impl keysinterface::KeysInterface for TestKeysInterface { } fn read_chan_signer(&self, buffer: &[u8]) -> Result { - let mut reader = std::io::Cursor::new(buffer); + let mut reader = io::Cursor::new(buffer); let inner: InMemorySigner = Readable::read(&mut reader)?; let revoked_commitment = self.make_revoked_commitment_cell(inner.commitment_seed);