Give ManyChannelMonitor a logger and trace add_update events
[rust-lightning] / src / ln / channelmonitor.rs
index 594045f464ace5373a7db284ae4cf1eef7ed99f9..5d1e4e09516b73200ed3834ae1190975ba28e2de 100644 (file)
@@ -112,6 +112,7 @@ pub struct SimpleManyChannelMonitor<Key> {
        chain_monitor: Arc<ChainWatchInterface>,
        broadcaster: Arc<BroadcasterInterface>,
        pending_events: Mutex<Vec<events::Event>>,
+       logger: Arc<Logger>,
 }
 
 impl<Key : Send + cmp::Eq + hash::Hash> ChainListener for SimpleManyChannelMonitor<Key> {
@@ -144,12 +145,13 @@ impl<Key : Send + cmp::Eq + hash::Hash> ChainListener for SimpleManyChannelMonit
 impl<Key : Send + cmp::Eq + hash::Hash + 'static> SimpleManyChannelMonitor<Key> {
        /// 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<ChainWatchInterface>, broadcaster: Arc<BroadcasterInterface>) -> Arc<SimpleManyChannelMonitor<Key>> {
+       pub fn new(chain_monitor: Arc<ChainWatchInterface>, broadcaster: Arc<BroadcasterInterface>, logger: Arc<Logger>) -> Arc<SimpleManyChannelMonitor<Key>> {
                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<Key : Send + cmp::Eq + hash::Hash + 'static> SimpleManyChannelMonitor<Key>
        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);
                        },
@@ -692,7 +701,7 @@ impl ChannelMonitor {
                }
 
                writer.write_all(&byte_utils::be64_to_array(self.remote_claimable_outpoints.len() as u64))?;
-               for (txid, htlc_outputs) in self.remote_claimable_outpoints.iter() {
+               for (ref txid, ref htlc_outputs) in self.remote_claimable_outpoints.iter() {
                        writer.write_all(&txid[..])?;
                        writer.write_all(&byte_utils::be64_to_array(htlc_outputs.len() as u64))?;
                        for htlc_output in htlc_outputs.iter() {
@@ -701,9 +710,9 @@ impl ChannelMonitor {
                }
 
                writer.write_all(&byte_utils::be64_to_array(self.remote_commitment_txn_on_chain.len() as u64))?;
-               for (txid, (commitment_number, txouts)) in self.remote_commitment_txn_on_chain.iter() {
+               for (ref txid, &(commitment_number, ref txouts)) in self.remote_commitment_txn_on_chain.iter() {
                        writer.write_all(&txid[..])?;
-                       writer.write_all(&byte_utils::be48_to_array(*commitment_number))?;
+                       writer.write_all(&byte_utils::be48_to_array(commitment_number))?;
                        (txouts.len() as u64).write(writer)?;
                        for script in txouts.iter() {
                                script.write(writer)?;
@@ -712,8 +721,8 @@ impl ChannelMonitor {
 
                if for_local_storage {
                        writer.write_all(&byte_utils::be64_to_array(self.remote_hash_commitment_number.len() as u64))?;
-                       for (payment_hash, commitment_number) in self.remote_hash_commitment_number.iter() {
-                               writer.write_all(payment_hash)?;
+                       for (ref payment_hash, commitment_number) in self.remote_hash_commitment_number.iter() {
+                               writer.write_all(*payment_hash)?;
                                writer.write_all(&byte_utils::be48_to_array(*commitment_number))?;
                        }
                } else {