Cache local_commitment_tx in OnchainTxHandler
[rust-lightning] / lightning / src / ln / channelmonitor.rs
index 2ac113b9845eef43391f5758ebad8335f82af453..70c92143af8b50d58eb06f9c10878fc6fe190e2d 100644 (file)
@@ -1098,7 +1098,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
                        onchain_detection: onchain_detection,
                        their_htlc_base_key: Some(their_htlc_base_key.clone()),
                        their_delayed_payment_base_key: Some(their_delayed_payment_base_key.clone()),
-                       funding_redeemscript: Some(funding_redeemscript),
+                       funding_redeemscript: Some(funding_redeemscript.clone()),
                        channel_value_satoshis: Some(channel_value_satoshis),
                        their_cur_revocation_points: None,
 
@@ -1121,7 +1121,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
                        onchain_events_waiting_threshold_conf: HashMap::new(),
                        outputs_to_watch: HashMap::new(),
 
-                       onchain_tx_handler: OnchainTxHandler::new(destination_script.clone(), keys, logger.clone()),
+                       onchain_tx_handler: OnchainTxHandler::new(destination_script.clone(), keys, funding_redeemscript, logger.clone()),
 
                        last_block_hash: Default::default(),
                        secp_ctx: Secp256k1::new(),
@@ -1244,7 +1244,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
                self.prev_local_signed_commitment_tx = self.current_local_signed_commitment_tx.take();
                self.current_local_signed_commitment_tx = Some(LocalSignedTx {
                        txid: commitment_tx.txid(),
-                       tx: commitment_tx,
+                       tx: commitment_tx.clone(),
                        revocation_key: local_keys.revocation_key,
                        a_htlc_key: local_keys.a_htlc_key,
                        b_htlc_key: local_keys.b_htlc_key,
@@ -1253,6 +1253,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
                        feerate_per_kw,
                        htlc_outputs,
                });
+               self.onchain_tx_handler.provide_latest_local_tx(commitment_tx);
                Ok(())
        }
 
@@ -1806,7 +1807,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
                // tracking state and panic!()ing if we get an update after force-closure/local-tx signing.
                log_trace!(self, "Getting signed latest local commitment transaction!");
                if let &mut Some(ref mut local_tx) = &mut self.current_local_signed_commitment_tx {
-                       local_tx.tx.add_local_sig(&self.onchain_detection.keys.funding_key(), self.funding_redeemscript.as_ref().unwrap(), self.channel_value_satoshis.unwrap(), &self.secp_ctx);
+                       self.onchain_detection.keys.sign_local_commitment(&mut local_tx.tx, self.funding_redeemscript.as_ref().unwrap(), self.channel_value_satoshis.unwrap(), &self.secp_ctx);
                }
                if let &Some(ref local_tx) = &self.current_local_signed_commitment_tx {
                        let mut res = vec![local_tx.tx.with_valid_witness().clone()];
@@ -1888,7 +1889,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
                } else { false };
                if let Some(ref mut cur_local_tx) = self.current_local_signed_commitment_tx {
                        if should_broadcast {
-                               cur_local_tx.tx.add_local_sig(&self.onchain_detection.keys.funding_key(), self.funding_redeemscript.as_ref().unwrap(), self.channel_value_satoshis.unwrap(), &self.secp_ctx);
+                               self.onchain_detection.keys.sign_local_commitment(&mut cur_local_tx.tx, self.funding_redeemscript.as_ref().unwrap(), self.channel_value_satoshis.unwrap(), &mut self.secp_ctx);
                        }
                }
                if let Some(ref cur_local_tx) = self.current_local_signed_commitment_tx {