Use clear helper on flags copy to mask off bits
[rust-lightning] / lightning / src / chain / chainmonitor.rs
index b5f2842d74285a30ca6688e162eac514a0f5dcf3..14544754318ad5945622c42bd0300679aef6a6de 100644 (file)
@@ -35,7 +35,7 @@ use crate::sign::ecdsa::WriteableEcdsaChannelSigner;
 use crate::events;
 use crate::events::{Event, EventHandler};
 use crate::util::atomic_counter::AtomicCounter;
-use crate::util::logger::Logger;
+use crate::util::logger::{Logger, WithContext};
 use crate::util::errors::APIError;
 use crate::util::wakers::{Future, Notifier};
 use crate::ln::channelmanager::ChannelDetails;
@@ -402,7 +402,8 @@ where C::Target: chain::Filter,
                                                outpoint: OutPoint { txid, index: idx as u16 },
                                                script_pubkey: output.script_pubkey,
                                        };
-                                       chain_source.register_output(output)
+                                       log_trace!(logger, "Adding monitoring for spends of outpoint {} to the filter", output.outpoint);
+                                       chain_source.register_output(output);
                                }
                        }
                }
@@ -620,9 +621,8 @@ where C::Target: chain::Filter,
        pub fn rebroadcast_pending_claims(&self) {
                let monitors = self.monitors.read().unwrap();
                for (_, monitor_holder) in &*monitors {
-                       let logger = WithChannelMonitor::from(&self.logger, &monitor_holder.monitor);
                        monitor_holder.monitor.rebroadcast_pending_claims(
-                               &*self.broadcaster, &*self.fee_estimator, &logger
+                               &*self.broadcaster, &*self.fee_estimator, &self.logger
                        )
                }
        }
@@ -640,9 +640,8 @@ where
        fn filtered_block_connected(&self, header: &Header, txdata: &TransactionData, height: u32) {
                log_debug!(self.logger, "New best block {} at height {} provided via block_connected", header.block_hash(), height);
                self.process_chain_data(header, Some(height), &txdata, |monitor, txdata| {
-                       let logger = WithChannelMonitor::from(&self.logger, &monitor);
                        monitor.block_connected(
-                               header, txdata, height, &*self.broadcaster, &*self.fee_estimator, &logger)
+                               header, txdata, height, &*self.broadcaster, &*self.fee_estimator, &self.logger)
                });
        }
 
@@ -650,9 +649,8 @@ where
                let monitor_states = self.monitors.read().unwrap();
                log_debug!(self.logger, "Latest block {} at height {} removed via block_disconnected", header.block_hash(), height);
                for monitor_state in monitor_states.values() {
-                       let logger = WithChannelMonitor::from(&self.logger, &monitor_state.monitor);
                        monitor_state.monitor.block_disconnected(
-                               header, height, &*self.broadcaster, &*self.fee_estimator, &logger);
+                               header, height, &*self.broadcaster, &*self.fee_estimator, &self.logger);
                }
        }
 }
@@ -669,9 +667,8 @@ where
        fn transactions_confirmed(&self, header: &Header, txdata: &TransactionData, height: u32) {
                log_debug!(self.logger, "{} provided transactions confirmed at height {} in block {}", txdata.len(), height, header.block_hash());
                self.process_chain_data(header, None, txdata, |monitor, txdata| {
-                       let logger = WithChannelMonitor::from(&self.logger, &monitor);
                        monitor.transactions_confirmed(
-                               header, txdata, height, &*self.broadcaster, &*self.fee_estimator, &logger)
+                               header, txdata, height, &*self.broadcaster, &*self.fee_estimator, &self.logger)
                });
        }
 
@@ -679,20 +676,19 @@ where
                log_debug!(self.logger, "Transaction {} reorganized out of chain", txid);
                let monitor_states = self.monitors.read().unwrap();
                for monitor_state in monitor_states.values() {
-                       let logger = WithChannelMonitor::from(&self.logger, &monitor_state.monitor);
-                       monitor_state.monitor.transaction_unconfirmed(txid, &*self.broadcaster, &*self.fee_estimator, &logger);
+                       monitor_state.monitor.transaction_unconfirmed(txid, &*self.broadcaster, &*self.fee_estimator, &self.logger);
                }
        }
 
        fn best_block_updated(&self, header: &Header, height: u32) {
                log_debug!(self.logger, "New best block {} at height {} provided via best_block_updated", header.block_hash(), height);
                self.process_chain_data(header, Some(height), &[], |monitor, txdata| {
-                       let logger = WithChannelMonitor::from(&self.logger, &monitor);
                        // While in practice there shouldn't be any recursive calls when given empty txdata,
                        // it's still possible if a chain::Filter implementation returns a transaction.
                        debug_assert!(txdata.is_empty());
                        monitor.best_block_updated(
-                               header, height, &*self.broadcaster, &*self.fee_estimator, &logger)
+                               header, height, &*self.broadcaster, &*self.fee_estimator, &self.logger
+                       )
                });
        }
 
@@ -746,7 +742,7 @@ where C::Target: chain::Filter,
                        },
                }
                if let Some(ref chain_source) = self.chain_source {
-                       monitor.load_outputs_to_watch(chain_source);
+                       monitor.load_outputs_to_watch(chain_source , &self.logger);
                }
                entry.insert(MonitorHolder {
                        monitor,
@@ -759,9 +755,10 @@ where C::Target: chain::Filter,
        fn update_channel(&self, funding_txo: OutPoint, update: &ChannelMonitorUpdate) -> ChannelMonitorUpdateStatus {
                // Update the monitor that watches the channel referred to by the given outpoint.
                let monitors = self.monitors.read().unwrap();
-               let ret = match monitors.get(&funding_txo) {
+               match monitors.get(&funding_txo) {
                        None => {
-                               log_error!(self.logger, "Failed to update channel monitor: no such monitor registered");
+                               let logger = WithContext::from(&self.logger, update.counterparty_node_id, Some(funding_txo.to_channel_id()));
+                               log_error!(logger, "Failed to update channel monitor: no such monitor registered");
 
                                // We should never ever trigger this from within ChannelManager. Technically a
                                // user could use this object with some proxying in between which makes this
@@ -798,7 +795,16 @@ where C::Target: chain::Filter,
                                        ChannelMonitorUpdateStatus::Completed => {
                                                log_debug!(logger, "Persistence of ChannelMonitorUpdate for channel {} completed", log_funding_info!(monitor));
                                        },
-                                       ChannelMonitorUpdateStatus::UnrecoverableError => { /* we'll panic in a moment */ },
+                                       ChannelMonitorUpdateStatus::UnrecoverableError => {
+                                               // Take the monitors lock for writing so that we poison it and any future
+                                               // operations going forward fail immediately.
+                                               core::mem::drop(pending_monitor_updates);
+                                               core::mem::drop(monitors);
+                                               let _poison = self.monitors.write().unwrap();
+                                               let err_str = "ChannelMonitor[Update] persistence failed unrecoverably. This indicates we cannot continue normal operation and must shut down.";
+                                               log_error!(logger, "{}", err_str);
+                                               panic!("{}", err_str);
+                                       },
                                }
                                if update_res.is_err() {
                                        ChannelMonitorUpdateStatus::InProgress
@@ -806,21 +812,7 @@ where C::Target: chain::Filter,
                                        persist_res
                                }
                        }
-               };
-               if let ChannelMonitorUpdateStatus::UnrecoverableError = ret {
-                       let logger = WithChannelMonitor::from(
-                               &self.logger, &monitors.get(&funding_txo).unwrap().monitor
-                       );
-
-                       // Take the monitors lock for writing so that we poison it and any future
-                       // operations going forward fail immediately.
-                       core::mem::drop(monitors);
-                       let _poison = self.monitors.write().unwrap();
-                       let err_str = "ChannelMonitor[Update] persistence failed unrecoverably. This indicates we cannot continue normal operation and must shut down.";
-                       log_error!(logger, "{}", err_str);
-                       panic!("{}", err_str);
                }
-               ret
        }
 
        fn release_pending_monitor_events(&self) -> Vec<(OutPoint, Vec<MonitorEvent>, Option<PublicKey>)> {