Merge pull request #2966 from G8XSU/2647-distribute
[rust-lightning] / lightning / src / chain / channelmonitor.rs
index f33abee53c2178450a99ebebb10f8b0a4d5aede1..0e87f3569e605d0c25736a9d203bc1fe6170655f 100644 (file)
@@ -1812,6 +1812,12 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
                );
        }
 
+       /// Returns true if the monitor has pending claim requests that are not fully confirmed yet.
+       pub fn has_pending_claims(&self) -> bool
+       {
+               self.inner.lock().unwrap().onchain_tx_handler.has_pending_claims()
+       }
+
        /// Triggers rebroadcasts of pending claims from a force-closed channel after a transaction
        /// signature generation failure.
        pub fn signer_unblocked<B: Deref, F: Deref, L: Deref>(
@@ -1924,9 +1930,9 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
        }
 
        #[cfg(test)]
-       pub fn do_signer_call<F: FnMut(&Signer) -> ()>(&self, mut f: F) {
-               let inner = self.inner.lock().unwrap();
-               f(&inner.onchain_tx_handler.signer);
+       pub fn do_mut_signer_call<F: FnMut(&mut Signer) -> ()>(&self, mut f: F) {
+               let mut inner = self.inner.lock().unwrap();
+               f(&mut inner.onchain_tx_handler.signer);
        }
 }
 
@@ -2873,7 +2879,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
                F::Target: FeeEstimator,
                L::Target: Logger,
        {
-               let (claimable_outpoints, _) = self.generate_claimable_outpoints_and_watch_outputs(ClosureReason::HolderForceClosed);
+               let (claimable_outpoints, _) = self.generate_claimable_outpoints_and_watch_outputs(ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(true) });
                self.onchain_tx_handler.update_claims_view_from_requests(
                        claimable_outpoints, self.best_block.height, self.best_block.height, broadcaster,
                        fee_estimator, logger
@@ -3105,9 +3111,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
                                                        },
                                                        commitment_txid: htlc.commitment_txid,
                                                        per_commitment_number: htlc.per_commitment_number,
-                                                       per_commitment_point: self.onchain_tx_handler.signer.get_per_commitment_point(
-                                                               htlc.per_commitment_number, &self.onchain_tx_handler.secp_ctx,
-                                                       ),
+                                                       per_commitment_point: htlc.per_commitment_point,
                                                        feerate_per_kw: 0,
                                                        htlc: htlc.htlc,
                                                        preimage: htlc.preimage,
@@ -3233,16 +3237,14 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
        /// height > height + CLTV_SHARED_CLAIM_BUFFER. In any case, will install monitoring for
        /// HTLC-Success/HTLC-Timeout transactions.
        ///
-       /// Returns packages to claim the revoked output(s), as well as additional outputs to watch and
-       /// general information about the output that is to the counterparty in the commitment
-       /// transaction.
+       /// Returns packages to claim the revoked output(s) and general information about the output that
+       /// is to the counterparty in the commitment transaction.
        fn check_spend_counterparty_transaction<L: Deref>(&mut self, tx: &Transaction, height: u32, block_hash: &BlockHash, logger: &L)
-               -> (Vec<PackageTemplate>, TransactionOutputs, CommitmentTxCounterpartyOutputInfo)
+               -> (Vec<PackageTemplate>, CommitmentTxCounterpartyOutputInfo)
        where L::Target: Logger {
                // Most secp and related errors trying to create keys means we have no hope of constructing
                // a spend transaction...so we return no transactions to broadcast
                let mut claimable_outpoints = Vec::new();
-               let mut watch_outputs = Vec::new();
                let mut to_counterparty_output_info = None;
 
                let commitment_txid = tx.txid(); //TODO: This is gonna be a performance bottleneck for watchtowers!
@@ -3252,7 +3254,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
                        ( $thing : expr ) => {
                                match $thing {
                                        Ok(a) => a,
-                                       Err(_) => return (claimable_outpoints, (commitment_txid, watch_outputs), to_counterparty_output_info)
+                                       Err(_) => return (claimable_outpoints, to_counterparty_output_info)
                                }
                        };
                }
@@ -3286,8 +3288,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
                                                if transaction_output_index as usize >= tx.output.len() ||
                                                                tx.output[transaction_output_index as usize].value != htlc.to_bitcoin_amount() {
                                                        // per_commitment_data is corrupt or our commitment signing key leaked!
-                                                       return (claimable_outpoints, (commitment_txid, watch_outputs),
-                                                               to_counterparty_output_info);
+                                                       return (claimable_outpoints, to_counterparty_output_info);
                                                }
                                                let revk_htlc_outp = RevokedHTLCOutput::build(per_commitment_point, self.counterparty_commitment_params.counterparty_delayed_payment_base_key, self.counterparty_commitment_params.counterparty_htlc_base_key, per_commitment_key, htlc.amount_msat / 1000, htlc.clone(), &self.onchain_tx_handler.channel_transaction_parameters.channel_type_features);
                                                let justice_package = PackageTemplate::build_package(commitment_txid, transaction_output_index, PackageSolvingData::RevokedHTLCOutput(revk_htlc_outp), htlc.cltv_expiry, height);
@@ -3300,9 +3301,6 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
                        if !claimable_outpoints.is_empty() || per_commitment_option.is_some() { // ie we're confident this is actually ours
                                // We're definitely a counterparty commitment transaction!
                                log_error!(logger, "Got broadcast of revoked counterparty commitment transaction, going to generate general spend tx with {} inputs", claimable_outpoints.len());
-                               for (idx, outp) in tx.output.iter().enumerate() {
-                                       watch_outputs.push((idx as u32, outp.clone()));
-                               }
                                self.counterparty_commitment_txn_on_chain.insert(commitment_txid, commitment_number);
 
                                if let Some(per_commitment_claimable_data) = per_commitment_option {
@@ -3328,9 +3326,6 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
                        // already processed the block, resulting in the counterparty_commitment_txn_on_chain entry
                        // not being generated by the above conditional. Thus, to be safe, we go ahead and
                        // insert it here.
-                       for (idx, outp) in tx.output.iter().enumerate() {
-                               watch_outputs.push((idx as u32, outp.clone()));
-                       }
                        self.counterparty_commitment_txn_on_chain.insert(commitment_txid, commitment_number);
 
                        log_info!(logger, "Got broadcast of non-revoked counterparty commitment transaction {}", commitment_txid);
@@ -3346,7 +3341,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
                        }
 
                }
-               (claimable_outpoints, (commitment_txid, watch_outputs), to_counterparty_output_info)
+               (claimable_outpoints, to_counterparty_output_info)
        }
 
        /// Returns the HTLC claim package templates and the counterparty output info
@@ -3778,24 +3773,25 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
                                        self.funding_spend_seen = true;
                                        let mut commitment_tx_to_counterparty_output = None;
                                        if (tx.input[0].sequence.0 >> 8*3) as u8 == 0x80 && (tx.lock_time.to_consensus_u32() >> 8*3) as u8 == 0x20 {
-                                               let (mut new_outpoints, new_outputs, counterparty_output_idx_sats) =
-                                                       self.check_spend_counterparty_transaction(&tx, height, &block_hash, &logger);
-                                               commitment_tx_to_counterparty_output = counterparty_output_idx_sats;
-                                               if !new_outputs.1.is_empty() {
-                                                       watch_outputs.push(new_outputs);
-                                               }
-                                               claimable_outpoints.append(&mut new_outpoints);
-                                               if new_outpoints.is_empty() {
-                                                       if let Some((mut new_outpoints, new_outputs)) = self.check_spend_holder_transaction(&tx, height, &block_hash, &logger) {
-                                                               #[cfg(not(fuzzing))]
-                                                               debug_assert!(commitment_tx_to_counterparty_output.is_none(),
-                                                                       "A commitment transaction matched as both a counterparty and local commitment tx?");
-                                                               if !new_outputs.1.is_empty() {
-                                                                       watch_outputs.push(new_outputs);
-                                                               }
-                                                               claimable_outpoints.append(&mut new_outpoints);
-                                                               balance_spendable_csv = Some(self.on_holder_tx_csv);
+                                               if let Some((mut new_outpoints, new_outputs)) = self.check_spend_holder_transaction(&tx, height, &block_hash, &logger) {
+                                                       if !new_outputs.1.is_empty() {
+                                                               watch_outputs.push(new_outputs);
                                                        }
+
+                                                       claimable_outpoints.append(&mut new_outpoints);
+                                                       balance_spendable_csv = Some(self.on_holder_tx_csv);
+                                               } else {
+                                                       let mut new_watch_outputs = Vec::new();
+                                                       for (idx, outp) in tx.output.iter().enumerate() {
+                                                               new_watch_outputs.push((idx as u32, outp.clone()));
+                                                       }
+                                                       watch_outputs.push((txid, new_watch_outputs));
+
+                                                       let (mut new_outpoints, counterparty_output_idx_sats) =
+                                                               self.check_spend_counterparty_transaction(&tx, height, &block_hash, &logger);
+                                                       commitment_tx_to_counterparty_output = counterparty_output_idx_sats;
+
+                                                       claimable_outpoints.append(&mut new_outpoints);
                                                }
                                        }
                                        self.onchain_events_awaiting_threshold_conf.push(OnchainEventEntry {