Change ShutdownResult type to better capture the possibilites
[rust-lightning] / lightning / src / ln / channel.rs
index d9c7fc5ad27b049d24dbe55882c3a21a0fc983ca..16d1a0f828c66e6156d02469ca2021eed11af226 100644 (file)
@@ -1570,13 +1570,13 @@ impl<Signer: Sign> Channel<Signer> {
                let funding_redeemscript = self.get_funding_redeemscript();
                let funding_txo_script = funding_redeemscript.to_v0_p2wsh();
                let obscure_factor = get_commitment_transaction_number_obscure_factor(&self.get_holder_pubkeys().payment_point, &self.get_counterparty_pubkeys().payment_point, self.is_outbound());
-               let mut channel_monitor = ChannelMonitor::new(self.secp_ctx.clone(), self.holder_signer.clone(),
-                                                             &self.shutdown_pubkey, self.get_holder_selected_contest_delay(),
-                                                             &self.destination_script, (funding_txo, funding_txo_script.clone()),
-                                                             &self.channel_transaction_parameters,
-                                                             funding_redeemscript.clone(), self.channel_value_satoshis,
-                                                             obscure_factor,
-                                                             holder_commitment_tx);
+               let channel_monitor = ChannelMonitor::new(self.secp_ctx.clone(), self.holder_signer.clone(),
+                                                         &self.shutdown_pubkey, self.get_holder_selected_contest_delay(),
+                                                         &self.destination_script, (funding_txo, funding_txo_script.clone()),
+                                                         &self.channel_transaction_parameters,
+                                                         funding_redeemscript.clone(), self.channel_value_satoshis,
+                                                         obscure_factor,
+                                                         holder_commitment_tx);
 
                channel_monitor.provide_latest_counterparty_commitment_tx(counterparty_initial_commitment_txid, Vec::new(), self.cur_counterparty_commitment_transaction_number, self.counterparty_cur_commitment_point.unwrap(), logger);
 
@@ -1640,13 +1640,13 @@ impl<Signer: Sign> Channel<Signer> {
                let funding_txo = self.get_funding_txo().unwrap();
                let funding_txo_script = funding_redeemscript.to_v0_p2wsh();
                let obscure_factor = get_commitment_transaction_number_obscure_factor(&self.get_holder_pubkeys().payment_point, &self.get_counterparty_pubkeys().payment_point, self.is_outbound());
-               let mut channel_monitor = ChannelMonitor::new(self.secp_ctx.clone(), self.holder_signer.clone(),
-                                                             &self.shutdown_pubkey, self.get_holder_selected_contest_delay(),
-                                                             &self.destination_script, (funding_txo, funding_txo_script),
-                                                             &self.channel_transaction_parameters,
-                                                             funding_redeemscript.clone(), self.channel_value_satoshis,
-                                                             obscure_factor,
-                                                             holder_commitment_tx);
+               let channel_monitor = ChannelMonitor::new(self.secp_ctx.clone(), self.holder_signer.clone(),
+                                                         &self.shutdown_pubkey, self.get_holder_selected_contest_delay(),
+                                                         &self.destination_script, (funding_txo, funding_txo_script),
+                                                         &self.channel_transaction_parameters,
+                                                         funding_redeemscript.clone(), self.channel_value_satoshis,
+                                                         obscure_factor,
+                                                         holder_commitment_tx);
 
                channel_monitor.provide_latest_counterparty_commitment_tx(counterparty_initial_bitcoin_tx.txid, Vec::new(), self.cur_counterparty_commitment_transaction_number, self.counterparty_cur_commitment_point.unwrap(), logger);
 
@@ -3325,7 +3325,7 @@ impl<Signer: Sign> Channel<Signer> {
                        // Upper bound by capacity. We make it a bit less than full capacity to prevent attempts
                        // to use full capacity. This is an effort to reduce routing failures, because in many cases
                        // channel might have been used to route very small values (either by honest users or as DoS).
-                       self.channel_value_satoshis * 9 / 10,
+                       self.channel_value_satoshis * 1000 * 9 / 10,
 
                        Channel::<Signer>::get_holder_max_htlc_value_in_flight_msat(self.channel_value_satoshis)
                );
@@ -4179,7 +4179,7 @@ impl<Signer: Sign> Channel<Signer> {
        /// those explicitly stated to be allowed after shutdown completes, eg some simple getters).
        /// Also returns the list of payment_hashes for channels which we can safely fail backwards
        /// immediately (others we will have to allow to time out).
-       pub fn force_shutdown(&mut self, should_broadcast: bool) -> (Option<OutPoint>, ChannelMonitorUpdate, Vec<(HTLCSource, PaymentHash)>) {
+       pub fn force_shutdown(&mut self, should_broadcast: bool) -> (Option<(OutPoint, ChannelMonitorUpdate)>, Vec<(HTLCSource, PaymentHash)>) {
                assert!(self.channel_state != ChannelState::ShutdownComplete as u32);
 
                // We go ahead and "free" any holding cell HTLCs or HTLCs we haven't yet committed to and
@@ -4193,7 +4193,7 @@ impl<Signer: Sign> Channel<Signer> {
                                _ => {}
                        }
                }
-               let funding_txo = if let Some(funding_txo) = self.get_funding_txo() {
+               let monitor_update = if let Some(funding_txo) = self.get_funding_txo() {
                        // If we haven't yet exchanged funding signatures (ie channel_state < FundingSent),
                        // returning a channel monitor update here would imply a channel monitor update before
                        // we even registered the channel monitor to begin with, which is invalid.
@@ -4202,17 +4202,17 @@ impl<Signer: Sign> Channel<Signer> {
                        // monitor update to the user, even if we return one).
                        // See test_duplicate_chan_id and test_pre_lockin_no_chan_closed_update for more.
                        if self.channel_state & (ChannelState::FundingSent as u32 | ChannelState::ChannelFunded as u32 | ChannelState::ShutdownComplete as u32) != 0 {
-                               Some(funding_txo.clone())
+                               self.latest_monitor_update_id += 1;
+                               Some((funding_txo, ChannelMonitorUpdate {
+                                       update_id: self.latest_monitor_update_id,
+                                       updates: vec![ChannelMonitorUpdateStep::ChannelForceClosed { should_broadcast }],
+                               }))
                        } else { None }
                } else { None };
 
                self.channel_state = ChannelState::ShutdownComplete as u32;
                self.update_time_counter += 1;
-               self.latest_monitor_update_id += 1;
-               (funding_txo, ChannelMonitorUpdate {
-                       update_id: self.latest_monitor_update_id,
-                       updates: vec![ChannelMonitorUpdateStep::ChannelForceClosed { should_broadcast }],
-               }, dropped_outbound_htlcs)
+               (monitor_update, dropped_outbound_htlcs)
        }
 }