From: Matt Corallo Date: Mon, 29 Oct 2018 17:38:15 +0000 (-0400) Subject: Give ManyChannelMonitor a logger and trace add_update events X-Git-Tag: v0.0.12~281^2~1 X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=64bd2eaa7ff9d9f6270af1fb631347fdc08a2885;p=rust-lightning Give ManyChannelMonitor a logger and trace add_update events --- diff --git a/fuzz/fuzz_targets/full_stack_target.rs b/fuzz/fuzz_targets/full_stack_target.rs index 7f6cf5e19..81e2cabd6 100644 --- a/fuzz/fuzz_targets/full_stack_target.rs +++ b/fuzz/fuzz_targets/full_stack_target.rs @@ -280,7 +280,7 @@ pub fn do_test(data: &[u8], logger: &Arc) { let watch = Arc::new(ChainWatchInterfaceUtil::new(Network::Bitcoin, Arc::clone(&logger))); let broadcast = Arc::new(TestBroadcaster{}); - let monitor = channelmonitor::SimpleManyChannelMonitor::new(watch.clone(), broadcast.clone()); + let monitor = channelmonitor::SimpleManyChannelMonitor::new(watch.clone(), broadcast.clone(), Arc::clone(&logger)); let keys_manager = Arc::new(KeyProvider { node_secret: our_network_key.clone() }); let channelmanager = ChannelManager::new(slice_to_be32(get_slice!(4)), get_slice!(1)[0] != 0, Network::Bitcoin, fee_est.clone(), monitor.clone(), watch.clone(), broadcast.clone(), Arc::clone(&logger), keys_manager.clone()).unwrap(); diff --git a/src/ln/channelmanager.rs b/src/ln/channelmanager.rs index f3e152b4d..5d5227dc4 100644 --- a/src/ln/channelmanager.rs +++ b/src/ln/channelmanager.rs @@ -4051,7 +4051,7 @@ mod tests { 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 chan_monitor = Arc::new(test_utils::TestChannelMonitor::new(chain_monitor.clone(), tx_broadcaster.clone())); + let chan_monitor = Arc::new(test_utils::TestChannelMonitor::new(chain_monitor.clone(), tx_broadcaster.clone(), logger.clone())); let node = ChannelManager::new(0, true, Network::Testnet, feeest.clone(), chan_monitor.clone(), chain_monitor.clone(), tx_broadcaster.clone(), Arc::clone(&logger), keys_manager.clone()).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, @@ -6881,7 +6881,7 @@ mod tests { let mut chan_0_monitor_serialized = VecWriter(Vec::new()); nodes[0].chan_monitor.simple_monitor.monitors.lock().unwrap().iter().next().unwrap().1.write_for_disk(&mut chan_0_monitor_serialized).unwrap(); - nodes[0].chan_monitor = Arc::new(test_utils::TestChannelMonitor::new(nodes[0].chain_monitor.clone(), nodes[0].tx_broadcaster.clone())); + nodes[0].chan_monitor = Arc::new(test_utils::TestChannelMonitor::new(nodes[0].chain_monitor.clone(), nodes[0].tx_broadcaster.clone(), Arc::new(test_utils::TestLogger::new()))); let mut chan_0_monitor_read = &chan_0_monitor_serialized.0[..]; let (_, chan_0_monitor) = <(Sha256dHash, ChannelMonitor)>::read(&mut chan_0_monitor_read, Arc::new(test_utils::TestLogger::new())).unwrap(); assert!(chan_0_monitor_read.is_empty()); @@ -6945,7 +6945,7 @@ mod tests { let mut chan_0_monitor_serialized = VecWriter(Vec::new()); nodes[0].chan_monitor.simple_monitor.monitors.lock().unwrap().iter().next().unwrap().1.write_for_disk(&mut chan_0_monitor_serialized).unwrap(); - nodes[0].chan_monitor = Arc::new(test_utils::TestChannelMonitor::new(nodes[0].chain_monitor.clone(), nodes[0].tx_broadcaster.clone())); + nodes[0].chan_monitor = Arc::new(test_utils::TestChannelMonitor::new(nodes[0].chain_monitor.clone(), nodes[0].tx_broadcaster.clone(), Arc::new(test_utils::TestLogger::new()))); let mut chan_0_monitor_read = &chan_0_monitor_serialized.0[..]; let (_, chan_0_monitor) = <(Sha256dHash, ChannelMonitor)>::read(&mut chan_0_monitor_read, Arc::new(test_utils::TestLogger::new())).unwrap(); assert!(chan_0_monitor_read.is_empty()); @@ -7004,7 +7004,7 @@ mod tests { node_0_monitors_serialized.push(writer.0); } - nodes[0].chan_monitor = Arc::new(test_utils::TestChannelMonitor::new(nodes[0].chain_monitor.clone(), nodes[0].tx_broadcaster.clone())); + nodes[0].chan_monitor = Arc::new(test_utils::TestChannelMonitor::new(nodes[0].chain_monitor.clone(), nodes[0].tx_broadcaster.clone(), Arc::new(test_utils::TestLogger::new()))); let mut node_0_monitors = Vec::new(); for serialized in node_0_monitors_serialized.iter() { let mut read = &serialized[..]; diff --git a/src/ln/channelmonitor.rs b/src/ln/channelmonitor.rs index 4972ae322..5d1e4e095 100644 --- a/src/ln/channelmonitor.rs +++ b/src/ln/channelmonitor.rs @@ -112,6 +112,7 @@ pub struct SimpleManyChannelMonitor { chain_monitor: Arc, broadcaster: Arc, pending_events: Mutex>, + logger: Arc, } impl ChainListener for SimpleManyChannelMonitor { @@ -144,12 +145,13 @@ impl ChainListener for SimpleManyChannelMonit impl SimpleManyChannelMonitor { /// Creates a new object which can be used to monitor several channels given the chain /// interface with which to register to receive notifications. - pub fn new(chain_monitor: Arc, broadcaster: Arc) -> Arc> { + pub fn new(chain_monitor: Arc, broadcaster: Arc, logger: Arc) -> Arc> { let res = Arc::new(SimpleManyChannelMonitor { monitors: Mutex::new(HashMap::new()), chain_monitor, broadcaster, pending_events: Mutex::new(Vec::new()), + logger, }); let weak_res = Arc::downgrade(&res); res.chain_monitor.register_listener(weak_res); @@ -160,12 +162,19 @@ impl SimpleManyChannelMonitor pub fn add_update_monitor_by_key(&self, key: Key, monitor: ChannelMonitor) -> Result<(), HandleError> { let mut monitors = self.monitors.lock().unwrap(); match monitors.get_mut(&key) { - Some(orig_monitor) => return orig_monitor.insert_combine(monitor), + Some(orig_monitor) => { + log_trace!(self, "Updating Channel Monitor for channel {}", log_funding_option!(monitor.funding_txo)); + return orig_monitor.insert_combine(monitor); + }, None => {} }; match &monitor.funding_txo { - &None => self.chain_monitor.watch_all_txn(), + &None => { + log_trace!(self, "Got new Channel Monitor for no-funding-set channel (monitoring all txn!)"); + self.chain_monitor.watch_all_txn() + }, &Some((ref outpoint, ref script)) => { + log_trace!(self, "Got new Channel Monitor for channel {}", log_bytes!(outpoint.to_channel_id()[..])); self.chain_monitor.install_watch_tx(&outpoint.txid, script); self.chain_monitor.install_watch_outpoint((outpoint.txid, outpoint.index as u32), script); }, diff --git a/src/util/macro_logger.rs b/src/util/macro_logger.rs index 47ba876dc..c10e2c204 100644 --- a/src/util/macro_logger.rs +++ b/src/util/macro_logger.rs @@ -52,6 +52,21 @@ macro_rules! log_funding_channel_id { } } +pub(crate) struct DebugFundingOption<'a, T: 'a>(pub &'a Option<(OutPoint, T)>); +impl<'a, T> std::fmt::Display for DebugFundingOption<'a, T> { + fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { + match self.0.as_ref() { + Some(&(ref funding_output, _)) => DebugBytes(&funding_output.to_channel_id()[..]).fmt(f), + None => write!(f, "without funding output set"), + } + } +} +macro_rules! log_funding_option { + ($funding_option: expr) => { + ::util::macro_logger::DebugFundingOption(&$funding_option) + } +} + pub(crate) struct DebugRoute<'a>(pub &'a Route); impl<'a> std::fmt::Display for DebugRoute<'a> { fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { diff --git a/src/util/test_utils.rs b/src/util/test_utils.rs index 0eb49702a..5da647efe 100644 --- a/src/util/test_utils.rs +++ b/src/util/test_utils.rs @@ -42,10 +42,10 @@ pub struct TestChannelMonitor { pub update_ret: Mutex>, } impl TestChannelMonitor { - pub fn new(chain_monitor: Arc, broadcaster: Arc) -> Self { + pub fn new(chain_monitor: Arc, broadcaster: Arc, logger: Arc) -> Self { Self { added_monitors: Mutex::new(Vec::new()), - simple_monitor: channelmonitor::SimpleManyChannelMonitor::new(chain_monitor, broadcaster), + simple_monitor: channelmonitor::SimpleManyChannelMonitor::new(chain_monitor, broadcaster, logger), update_ret: Mutex::new(Ok(())), } }