Merge pull request #179 from TheBlueMatt/2018-09-pre-178-cleanups
[rust-lightning] / src / ln / channel.rs
index c4b6acb7d632708c322b15915cf2bdc240cdead9..4338f9bfa232e4f9d50d7d496f9e110aa87c5368 100644 (file)
@@ -2431,8 +2431,6 @@ impl Channel {
        }
        /// Only fails in case of bad keys
        fn send_commitment_no_status_check(&mut self) -> Result<(msgs::CommitmentSigned, ChannelMonitor), HandleError> {
-               let funding_script = self.get_funding_redeemscript();
-
                // We can upgrade the status of some HTLCs that are waiting on a commitment, even if we
                // fail to generate this, we still are at least at a position where upgrading their status
                // is acceptable.
@@ -2447,6 +2445,22 @@ impl Channel {
                        }
                }
 
+               match self.send_commitment_no_state_update() {
+                       Ok((res, remote_commitment_tx)) => {
+                               // Update state now that we've passed all the can-fail calls...
+                               self.channel_monitor.provide_latest_remote_commitment_tx_info(&remote_commitment_tx.0, remote_commitment_tx.1, self.cur_remote_commitment_transaction_number);
+                               self.channel_state |= ChannelState::AwaitingRemoteRevoke as u32;
+                               Ok((res, self.channel_monitor.clone()))
+                       },
+                       Err(e) => Err(e),
+               }
+       }
+
+       /// Only fails in case of bad keys. Used for channel_reestablish commitment_signed generation
+       /// when we shouldn't change HTLC/channel state.
+       fn send_commitment_no_state_update(&self) -> Result<(msgs::CommitmentSigned, (Transaction, Vec<HTLCOutputInCommitment>)), HandleError> {
+               let funding_script = self.get_funding_redeemscript();
+
                let remote_keys = self.build_remote_transaction_keys()?;
                let remote_commitment_tx = self.build_commitment_transaction(self.cur_remote_commitment_transaction_number, &remote_keys, false, true);
                let remote_commitment_txid = remote_commitment_tx.0.txid();
@@ -2463,15 +2477,11 @@ impl Channel {
                        htlc_sigs.push(self.secp_ctx.sign(&htlc_sighash, &our_htlc_key));
                }
 
-               // Update state now that we've passed all the can-fail calls...
-               self.channel_monitor.provide_latest_remote_commitment_tx_info(&remote_commitment_tx.0, remote_commitment_tx.1, self.cur_remote_commitment_transaction_number);
-               self.channel_state |= ChannelState::AwaitingRemoteRevoke as u32;
-
                Ok((msgs::CommitmentSigned {
                        channel_id: self.channel_id,
                        signature: our_sig,
                        htlc_signatures: htlc_sigs,
-               }, self.channel_monitor.clone()))
+               }, remote_commitment_tx))
        }
 
        /// Adds a pending outbound HTLC to this channel, and creates a signed commitment transaction