Avoid a few useless clone() calls in onchaintx.rs
[rust-lightning] / lightning / src / ln / onchaintx.rs
index b2115c0f8ac973c86d0827e0b3aaa0d8dd8291f9..102a688df0f22211b5e9a84c9db99eee754a3f77 100644 (file)
@@ -537,18 +537,15 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
                                                return None;
                                        },
                                        &InputMaterial::Funding { ref channel_value } => {
-                                               if let Some(ref mut local_commitment) = self.local_commitment {
-                                                       self.key_storage.sign_local_commitment(local_commitment, &self.funding_redeemscript, *channel_value, &self.secp_ctx);
-                                                       let signed_tx = local_commitment.with_valid_witness().clone();
-                                                       let mut amt_outputs = 0;
-                                                       for outp in signed_tx.output.iter() {
-                                                               amt_outputs += outp.value;
-                                                       }
-                                                       let feerate = (channel_value - amt_outputs) * 1000 / signed_tx.get_weight() as u64;
-                                                       // Timer set to $NEVER given we can't bump tx without anchor outputs
-                                                       log_trace!(self, "Going to broadcast Local Transaction {} claiming funding output {} from {}...", signed_tx.txid(), outp.vout, outp.txid);
-                                                       return Some((None, feerate, signed_tx));
+                                               let signed_tx = self.get_fully_signed_local_tx(*channel_value).unwrap();
+                                               let mut amt_outputs = 0;
+                                               for outp in signed_tx.output.iter() {
+                                                       amt_outputs += outp.value;
                                                }
+                                               let feerate = (channel_value - amt_outputs) * 1000 / signed_tx.get_weight() as u64;
+                                               // Timer set to $NEVER given we can't bump tx without anchor outputs
+                                               log_trace!(self, "Going to broadcast Local Transaction {} claiming funding output {} from {}...", signed_tx.txid(), outp.vout, outp.txid);
+                                               return Some((None, feerate, signed_tx));
                                        }
                                        _ => unreachable!()
                                }
@@ -588,17 +585,17 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
 
                // Generate claim transactions and track them to bump if necessary at
                // height timer expiration (i.e in how many blocks we're going to take action).
-               for claim in new_claims {
-                       let mut claim_material = ClaimTxBumpMaterial { height_timer: None, feerate_previous: 0, soonest_timelock: claim.0, per_input_material: claim.1.clone() };
+               for (soonest_timelock, claim) in new_claims.drain(..) {
+                       let mut claim_material = ClaimTxBumpMaterial { height_timer: None, feerate_previous: 0, soonest_timelock, per_input_material: claim };
                        if let Some((new_timer, new_feerate, tx)) = self.generate_claim_tx(height, &claim_material, &*fee_estimator) {
                                claim_material.height_timer = new_timer;
                                claim_material.feerate_previous = new_feerate;
                                let txid = tx.txid();
-                               self.pending_claim_requests.insert(txid, claim_material);
-                               for k in claim.1.keys() {
+                               for k in claim_material.per_input_material.keys() {
                                        log_trace!(self, "Registering claiming request for {}:{}", k.txid, k.vout);
                                        self.claimable_outpoints.insert(k.clone(), (txid, height));
                                }
+                               self.pending_claim_requests.insert(txid, claim_material);
                                log_trace!(self, "Broadcast onchain {}", log_tx!(tx));
                                broadcaster.broadcast_transaction(&tx);
                        }
@@ -716,18 +713,14 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
 
                // Build, bump and rebroadcast tx accordingly
                log_trace!(self, "Bumping {} candidates", bump_candidates.len());
-               let mut pending_claim_updates = Vec::with_capacity(bump_candidates.len());
                for (first_claim_txid, claim_material) in bump_candidates.iter() {
                        if let Some((new_timer, new_feerate, bump_tx)) = self.generate_claim_tx(height, &claim_material, &*fee_estimator) {
                                log_trace!(self, "Broadcast onchain {}", log_tx!(bump_tx));
                                broadcaster.broadcast_transaction(&bump_tx);
-                               pending_claim_updates.push((*first_claim_txid, new_timer, new_feerate));
-                       }
-               }
-               for updates in pending_claim_updates {
-                       if let Some(claim_material) = self.pending_claim_requests.get_mut(&updates.0) {
-                               claim_material.height_timer = updates.1;
-                               claim_material.feerate_previous = updates.2;
+                               if let Some(claim_material) = self.pending_claim_requests.get_mut(first_claim_txid) {
+                                       claim_material.height_timer = new_timer;
+                                       claim_material.feerate_previous = new_feerate;
+                               }
                        }
                }
        }
@@ -793,6 +786,10 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
                Ok(())
        }
 
+       //TODO: getting lastest local transactions should be infaillible and result in us "force-closing the channel", but we may
+       // have empty local commitment transaction if a ChannelMonitor is asked to force-close just after Channel::get_outbound_funding_created,
+       // before providing a initial commitment transaction. For outbound channel, init ChannelMonitor at Channel::funding_signed, there is nothing
+       // to monitor before.
        pub(super) fn get_fully_signed_local_tx(&mut self, channel_value_satoshis: u64) -> Option<Transaction> {
                if let Some(ref mut local_commitment) = self.local_commitment {
                        self.key_storage.sign_local_commitment(local_commitment, &self.funding_redeemscript, channel_value_satoshis, &self.secp_ctx);