From: Matt Corallo Date: Thu, 18 Oct 2018 19:00:12 +0000 (-0400) Subject: Give ChannelMonitor a logger via new ReadableArgs trait X-Git-Tag: v0.0.12~283^2~9 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=47fe673c57ba09fc1aa5122f438d3228fbf24290;p=rust-lightning Give ChannelMonitor a logger via new ReadableArgs trait --- diff --git a/fuzz/fuzz_targets/chanmon_deser_target.rs b/fuzz/fuzz_targets/chanmon_deser_target.rs index ba525081..466a15c4 100644 --- a/fuzz/fuzz_targets/chanmon_deser_target.rs +++ b/fuzz/fuzz_targets/chanmon_deser_target.rs @@ -5,9 +5,13 @@ extern crate lightning; use lightning::ln::channelmonitor; use lightning::util::reset_rng_state; -use lightning::util::ser::{Readable, Writer}; +use lightning::util::ser::{ReadableArgs, Writer}; + +mod utils; +use utils::test_logger; use std::io::Cursor; +use std::sync::Arc; struct VecWriter(Vec); impl Writer for VecWriter { @@ -23,10 +27,11 @@ impl Writer for VecWriter { #[inline] pub fn do_test(data: &[u8]) { reset_rng_state(); - if let Ok(monitor) = channelmonitor::ChannelMonitor::read(&mut Cursor::new(data)) { + let logger = Arc::new(test_logger::TestLogger{}); + if let Ok(monitor) = channelmonitor::ChannelMonitor::read(&mut Cursor::new(data), logger.clone()) { let mut w = VecWriter(Vec::new()); monitor.write_for_disk(&mut w).unwrap(); - assert!(channelmonitor::ChannelMonitor::read(&mut Cursor::new(&w.0)).unwrap() == monitor); + assert!(channelmonitor::ChannelMonitor::read(&mut Cursor::new(&w.0), logger.clone()).unwrap() == monitor); w.0.clear(); monitor.write_for_watchtower(&mut w).unwrap(); } diff --git a/src/ln/channel.rs b/src/ln/channel.rs index e8ec23d9..9935f88c 100644 --- a/src/ln/channel.rs +++ b/src/ln/channel.rs @@ -438,7 +438,7 @@ impl Channel { let secp_ctx = Secp256k1::new(); let channel_monitor = ChannelMonitor::new(&chan_keys.revocation_base_key, &chan_keys.delayed_payment_base_key, &chan_keys.htlc_base_key, BREAKDOWN_TIMEOUT, - keys_provider.get_destination_script()); + keys_provider.get_destination_script(), logger.clone()); Ok(Channel { user_id: user_id, @@ -600,7 +600,7 @@ impl Channel { let secp_ctx = Secp256k1::new(); let mut channel_monitor = ChannelMonitor::new(&chan_keys.revocation_base_key, &chan_keys.delayed_payment_base_key, &chan_keys.htlc_base_key, BREAKDOWN_TIMEOUT, - keys_provider.get_destination_script()); + keys_provider.get_destination_script(), logger.clone()); channel_monitor.set_their_base_keys(&msg.htlc_basepoint, &msg.delayed_payment_basepoint); channel_monitor.set_their_to_self_delay(msg.to_self_delay); diff --git a/src/ln/channelmonitor.rs b/src/ln/channelmonitor.rs index 5adfe926..fb965434 100644 --- a/src/ln/channelmonitor.rs +++ b/src/ln/channelmonitor.rs @@ -31,7 +31,8 @@ use ln::chan_utils::HTLCOutputInCommitment; use chain::chaininterface::{ChainListener, ChainWatchInterface, BroadcasterInterface}; use chain::transaction::OutPoint; use chain::keysinterface::SpendableOutputDescriptor; -use util::ser::{Readable, Writer}; +use util::logger::Logger; +use util::ser::{ReadableArgs, Writer}; use util::sha2::Sha256; use util::{byte_utils, events}; @@ -276,7 +277,9 @@ pub struct ChannelMonitor { payment_preimages: HashMap<[u8; 32], [u8; 32]>, destination_script: Script, + secp_ctx: Secp256k1, //TODO: dedup this a bit... + logger: Arc, } impl Clone for ChannelMonitor { fn clone(&self) -> Self { @@ -304,6 +307,7 @@ impl Clone for ChannelMonitor { destination_script: self.destination_script.clone(), secp_ctx: self.secp_ctx.clone(), + logger: self.logger.clone(), } } } @@ -343,7 +347,7 @@ impl PartialEq for ChannelMonitor { } impl ChannelMonitor { - pub(super) fn new(revocation_base_key: &SecretKey, delayed_payment_base_key: &SecretKey, htlc_base_key: &SecretKey, our_to_self_delay: u16, destination_script: Script) -> ChannelMonitor { + pub(super) fn new(revocation_base_key: &SecretKey, delayed_payment_base_key: &SecretKey, htlc_base_key: &SecretKey, our_to_self_delay: u16, destination_script: Script, logger: Arc) -> ChannelMonitor { ChannelMonitor { funding_txo: None, commitment_transaction_number_obscure_factor: 0, @@ -371,9 +375,10 @@ impl ChannelMonitor { current_local_signed_commitment_tx: None, payment_preimages: HashMap::new(), - destination_script: destination_script, + secp_ctx: Secp256k1::new(), + logger, } } @@ -1373,8 +1378,8 @@ impl ChannelMonitor { } } -impl Readable for ChannelMonitor { - fn read(reader: &mut R) -> Result { +impl ReadableArgs> for ChannelMonitor { + fn read(reader: &mut R, logger: Arc) -> Result { // TODO: read_to_end and then deserializing from that vector is really dumb, we should // actually use the fancy serialization framework we have instead of hacking around it. let mut datavec = Vec::new(); @@ -1631,6 +1636,7 @@ impl Readable for ChannelMonitor { destination_script, secp_ctx, + logger, }) } @@ -1645,9 +1651,11 @@ mod tests { use ln::channelmonitor::ChannelMonitor; use ln::chan_utils::{HTLCOutputInCommitment, TxCreationKeys}; use util::sha2::Sha256; + use util::test_utils::TestLogger; use secp256k1::key::{SecretKey,PublicKey}; use secp256k1::{Secp256k1, Signature}; use rand::{thread_rng,Rng}; + use std::sync::Arc; #[test] fn test_per_commitment_storage() { @@ -1655,6 +1663,7 @@ mod tests { let mut secrets: Vec<[u8; 32]> = Vec::new(); let mut monitor: ChannelMonitor; let secp_ctx = Secp256k1::new(); + let logger = Arc::new(TestLogger::new()); macro_rules! test_secrets { () => { @@ -1670,7 +1679,7 @@ mod tests { { // insert_secret correct sequence - monitor = ChannelMonitor::new(&SecretKey::from_slice(&secp_ctx, &[42; 32]).unwrap(), &SecretKey::from_slice(&secp_ctx, &[43; 32]).unwrap(), &SecretKey::from_slice(&secp_ctx, &[44; 32]).unwrap(), 0, Script::new()); + monitor = ChannelMonitor::new(&SecretKey::from_slice(&secp_ctx, &[42; 32]).unwrap(), &SecretKey::from_slice(&secp_ctx, &[43; 32]).unwrap(), &SecretKey::from_slice(&secp_ctx, &[44; 32]).unwrap(), 0, Script::new(), logger.clone()); secrets.clear(); secrets.push([0; 32]); @@ -1716,7 +1725,7 @@ mod tests { { // insert_secret #1 incorrect - monitor = ChannelMonitor::new(&SecretKey::from_slice(&secp_ctx, &[42; 32]).unwrap(), &SecretKey::from_slice(&secp_ctx, &[43; 32]).unwrap(), &SecretKey::from_slice(&secp_ctx, &[44; 32]).unwrap(), 0, Script::new()); + monitor = ChannelMonitor::new(&SecretKey::from_slice(&secp_ctx, &[42; 32]).unwrap(), &SecretKey::from_slice(&secp_ctx, &[43; 32]).unwrap(), &SecretKey::from_slice(&secp_ctx, &[44; 32]).unwrap(), 0, Script::new(), logger.clone()); secrets.clear(); secrets.push([0; 32]); @@ -1732,7 +1741,7 @@ mod tests { { // insert_secret #2 incorrect (#1 derived from incorrect) - monitor = ChannelMonitor::new(&SecretKey::from_slice(&secp_ctx, &[42; 32]).unwrap(), &SecretKey::from_slice(&secp_ctx, &[43; 32]).unwrap(), &SecretKey::from_slice(&secp_ctx, &[44; 32]).unwrap(), 0, Script::new()); + monitor = ChannelMonitor::new(&SecretKey::from_slice(&secp_ctx, &[42; 32]).unwrap(), &SecretKey::from_slice(&secp_ctx, &[43; 32]).unwrap(), &SecretKey::from_slice(&secp_ctx, &[44; 32]).unwrap(), 0, Script::new(), logger.clone()); secrets.clear(); secrets.push([0; 32]); @@ -1758,7 +1767,7 @@ mod tests { { // insert_secret #3 incorrect - monitor = ChannelMonitor::new(&SecretKey::from_slice(&secp_ctx, &[42; 32]).unwrap(), &SecretKey::from_slice(&secp_ctx, &[43; 32]).unwrap(), &SecretKey::from_slice(&secp_ctx, &[44; 32]).unwrap(), 0, Script::new()); + monitor = ChannelMonitor::new(&SecretKey::from_slice(&secp_ctx, &[42; 32]).unwrap(), &SecretKey::from_slice(&secp_ctx, &[43; 32]).unwrap(), &SecretKey::from_slice(&secp_ctx, &[44; 32]).unwrap(), 0, Script::new(), logger.clone()); secrets.clear(); secrets.push([0; 32]); @@ -1784,7 +1793,7 @@ mod tests { { // insert_secret #4 incorrect (1,2,3 derived from incorrect) - monitor = ChannelMonitor::new(&SecretKey::from_slice(&secp_ctx, &[42; 32]).unwrap(), &SecretKey::from_slice(&secp_ctx, &[43; 32]).unwrap(), &SecretKey::from_slice(&secp_ctx, &[44; 32]).unwrap(), 0, Script::new()); + monitor = ChannelMonitor::new(&SecretKey::from_slice(&secp_ctx, &[42; 32]).unwrap(), &SecretKey::from_slice(&secp_ctx, &[43; 32]).unwrap(), &SecretKey::from_slice(&secp_ctx, &[44; 32]).unwrap(), 0, Script::new(), logger.clone()); secrets.clear(); secrets.push([0; 32]); @@ -1830,7 +1839,7 @@ mod tests { { // insert_secret #5 incorrect - monitor = ChannelMonitor::new(&SecretKey::from_slice(&secp_ctx, &[42; 32]).unwrap(), &SecretKey::from_slice(&secp_ctx, &[43; 32]).unwrap(), &SecretKey::from_slice(&secp_ctx, &[44; 32]).unwrap(), 0, Script::new()); + monitor = ChannelMonitor::new(&SecretKey::from_slice(&secp_ctx, &[42; 32]).unwrap(), &SecretKey::from_slice(&secp_ctx, &[43; 32]).unwrap(), &SecretKey::from_slice(&secp_ctx, &[44; 32]).unwrap(), 0, Script::new(), logger.clone()); secrets.clear(); secrets.push([0; 32]); @@ -1866,7 +1875,7 @@ mod tests { { // insert_secret #6 incorrect (5 derived from incorrect) - monitor = ChannelMonitor::new(&SecretKey::from_slice(&secp_ctx, &[42; 32]).unwrap(), &SecretKey::from_slice(&secp_ctx, &[43; 32]).unwrap(), &SecretKey::from_slice(&secp_ctx, &[44; 32]).unwrap(), 0, Script::new()); + monitor = ChannelMonitor::new(&SecretKey::from_slice(&secp_ctx, &[42; 32]).unwrap(), &SecretKey::from_slice(&secp_ctx, &[43; 32]).unwrap(), &SecretKey::from_slice(&secp_ctx, &[44; 32]).unwrap(), 0, Script::new(), logger.clone()); secrets.clear(); secrets.push([0; 32]); @@ -1912,7 +1921,7 @@ mod tests { { // insert_secret #7 incorrect - monitor = ChannelMonitor::new(&SecretKey::from_slice(&secp_ctx, &[42; 32]).unwrap(), &SecretKey::from_slice(&secp_ctx, &[43; 32]).unwrap(), &SecretKey::from_slice(&secp_ctx, &[44; 32]).unwrap(), 0, Script::new()); + monitor = ChannelMonitor::new(&SecretKey::from_slice(&secp_ctx, &[42; 32]).unwrap(), &SecretKey::from_slice(&secp_ctx, &[43; 32]).unwrap(), &SecretKey::from_slice(&secp_ctx, &[44; 32]).unwrap(), 0, Script::new(), logger.clone()); secrets.clear(); secrets.push([0; 32]); @@ -1958,7 +1967,7 @@ mod tests { { // insert_secret #8 incorrect - monitor = ChannelMonitor::new(&SecretKey::from_slice(&secp_ctx, &[42; 32]).unwrap(), &SecretKey::from_slice(&secp_ctx, &[43; 32]).unwrap(), &SecretKey::from_slice(&secp_ctx, &[44; 32]).unwrap(), 0, Script::new()); + monitor = ChannelMonitor::new(&SecretKey::from_slice(&secp_ctx, &[42; 32]).unwrap(), &SecretKey::from_slice(&secp_ctx, &[43; 32]).unwrap(), &SecretKey::from_slice(&secp_ctx, &[44; 32]).unwrap(), 0, Script::new(), logger.clone()); secrets.clear(); secrets.push([0; 32]); @@ -2006,6 +2015,7 @@ mod tests { #[test] fn test_prune_preimages() { let secp_ctx = Secp256k1::new(); + let logger = Arc::new(TestLogger::new()); let dummy_sig = Signature::from_der(&secp_ctx, &hex::decode("3045022100fa86fa9a36a8cd6a7bb8f06a541787d51371d067951a9461d5404de6b928782e02201c8b7c334c10aed8976a3a465be9a28abff4cb23acbf00022295b378ce1fa3cd").unwrap()[..]).unwrap(); macro_rules! dummy_keys { @@ -2076,7 +2086,7 @@ mod tests { // Prune with one old state and a local commitment tx holding a few overlaps with the // old state. - let mut monitor = ChannelMonitor::new(&SecretKey::from_slice(&secp_ctx, &[42; 32]).unwrap(), &SecretKey::from_slice(&secp_ctx, &[43; 32]).unwrap(), &SecretKey::from_slice(&secp_ctx, &[44; 32]).unwrap(), 0, Script::new()); + let mut monitor = ChannelMonitor::new(&SecretKey::from_slice(&secp_ctx, &[42; 32]).unwrap(), &SecretKey::from_slice(&secp_ctx, &[43; 32]).unwrap(), &SecretKey::from_slice(&secp_ctx, &[44; 32]).unwrap(), 0, Script::new(), logger.clone()); monitor.set_their_to_self_delay(10); monitor.provide_latest_local_commitment_tx_info(dummy_tx.clone(), dummy_keys!(), 0, preimages_to_local_htlcs!(preimages[0..10])); diff --git a/src/util/test_utils.rs b/src/util/test_utils.rs index 4fa29fd8..3981750c 100644 --- a/src/util/test_utils.rs +++ b/src/util/test_utils.rs @@ -6,7 +6,7 @@ use ln::msgs; use ln::msgs::{HandleError}; use util::events; use util::logger::{Logger, Level, Record}; -use util::ser::{Readable, Writer}; +use util::ser::{ReadableArgs, Writer}; use bitcoin::blockdata::transaction::Transaction; @@ -55,7 +55,7 @@ impl channelmonitor::ManyChannelMonitor for TestChannelMonitor { // to a watchtower and disk... let mut w = VecWriter(Vec::new()); monitor.write_for_disk(&mut w).unwrap(); - assert!(channelmonitor::ChannelMonitor::read(&mut ::std::io::Cursor::new(&w.0)).unwrap() == monitor); + assert!(channelmonitor::ChannelMonitor::read(&mut ::std::io::Cursor::new(&w.0), Arc::new(TestLogger::new())).unwrap() == monitor); w.0.clear(); monitor.write_for_watchtower(&mut w).unwrap(); // This at least shouldn't crash... self.added_monitors.lock().unwrap().push((funding_txo, monitor.clone()));