Introduce ChannelMonitor::is_paying_spendable_output
[rust-lightning] / lightning / src / ln / channelmonitor.rs
index c2dd9c352a32a4885d8204680a70f3819125b028..d714f1e34a052b8ca19c4c72ccd6636a1a55adc0 100644 (file)
@@ -759,6 +759,7 @@ pub struct ChannelMonitor<ChanSigner: ChannelKeys> {
        latest_update_id: u64,
        commitment_transaction_number_obscure_factor: u64,
 
+       destination_script: Script,
        key_storage: Storage<ChanSigner>,
        their_htlc_base_key: Option<PublicKey>,
        their_delayed_payment_base_key: Option<PublicKey>,
@@ -838,6 +839,7 @@ impl<ChanSigner: ChannelKeys> PartialEq for ChannelMonitor<ChanSigner> {
        fn eq(&self, other: &Self) -> bool {
                if self.latest_update_id != other.latest_update_id ||
                        self.commitment_transaction_number_obscure_factor != other.commitment_transaction_number_obscure_factor ||
+                       self.destination_script != other.destination_script ||
                        self.key_storage != other.key_storage ||
                        self.their_htlc_base_key != other.their_htlc_base_key ||
                        self.their_delayed_payment_base_key != other.their_delayed_payment_base_key ||
@@ -880,6 +882,7 @@ impl<ChanSigner: ChannelKeys + Writeable> ChannelMonitor<ChanSigner> {
                // Set in initial Channel-object creation, so should always be set by now:
                U48(self.commitment_transaction_number_obscure_factor).write(writer)?;
 
+               self.destination_script.write(writer)?;
                match self.key_storage {
                        Storage::Local { ref keys, ref funding_key, ref revocation_base_key, ref htlc_base_key, ref delayed_payment_base_key, ref payment_base_key, ref shutdown_pubkey, ref funding_info, ref current_remote_commitment_txid, ref prev_remote_commitment_txid } => {
                                writer.write_all(&[0; 1])?;
@@ -1111,6 +1114,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
                        latest_update_id: 0,
                        commitment_transaction_number_obscure_factor,
 
+                       destination_script: destination_script.clone(),
                        key_storage: Storage::Local {
                                keys,
                                funding_key,
@@ -2076,6 +2080,10 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
                        // can also be resolved in a few other ways which can have more than one output. Thus,
                        // we call is_resolving_htlc_output here outside of the tx.input.len() == 1 check.
                        self.is_resolving_htlc_output(&tx, height);
+
+                       if let Some(spendable_output) = self.is_paying_spendable_output(&tx) {
+                               spendable_outputs.push(spendable_output);
+                       }
                }
                let should_broadcast = if let Some(_) = self.current_local_signed_commitment_tx {
                        self.would_broadcast_at_height(height)
@@ -2124,8 +2132,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
                                }
                        }
                }
-               let mut spendable_output = self.onchain_tx_handler.block_connected(txn_matched, claimable_outpoints, height, &*broadcaster, &*fee_estimator);
-               spendable_outputs.append(&mut spendable_output);
+               self.onchain_tx_handler.block_connected(txn_matched, claimable_outpoints, height, &*broadcaster, &*fee_estimator);
 
                self.last_block_hash = block_hash.clone();
                for &(ref txid, ref output_scripts) in watch_outputs.iter() {
@@ -2367,6 +2374,19 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
                        }
                }
        }
+
+       /// Check if any transaction broadcasted is paying fund back to some address we can assume to own
+       fn is_paying_spendable_output(&self, tx: &Transaction) -> Option<SpendableOutputDescriptor> {
+               for (i, outp) in tx.output.iter().enumerate() { // There is max one spendable output for any channel tx, including ones generated by us
+                       if outp.script_pubkey == self.destination_script {
+                               return Some(SpendableOutputDescriptor::StaticOutput {
+                                       outpoint: BitcoinOutPoint { txid: tx.txid(), vout: i as u32 },
+                                       output: outp.clone(),
+                               });
+                       }
+               }
+               None
+       }
 }
 
 const MAX_ALLOC_SIZE: usize = 64*1024;
@@ -2391,6 +2411,8 @@ impl<ChanSigner: ChannelKeys + Readable> ReadableArgs<Arc<Logger>> for (Sha256dH
                let latest_update_id: u64 = Readable::read(reader)?;
                let commitment_transaction_number_obscure_factor = <U48 as Readable>::read(reader)?.0;
 
+               let destination_script = Readable::read(reader)?;
+
                let key_storage = match <u8 as Readable>::read(reader)? {
                        0 => {
                                let keys = Readable::read(reader)?;
@@ -2631,6 +2653,8 @@ impl<ChanSigner: ChannelKeys + Readable> ReadableArgs<Arc<Logger>> for (Sha256dH
                        latest_update_id,
                        commitment_transaction_number_obscure_factor,
 
+                       destination_script,
+
                        key_storage,
                        their_htlc_base_key,
                        their_delayed_payment_base_key,