Give OnchainEvent::HTLCUpdate individual fields instead of a pair
[rust-lightning] / lightning / src / chain / channelmonitor.rs
index 7fc512a5a1859429b265d2b7ffc967b92fec3f4b..7a504d9e421fa1ac5f0b9dc42009183f636b2ddf 100644 (file)
@@ -351,7 +351,8 @@ enum OnchainEvent {
        /// inbound HTLC in backward channel. Note, in case of preimage, we pass info to upstream without delay as we can
        /// only win from it, so it's never an OnchainEvent
        HTLCUpdate {
-               htlc_update: (HTLCSource, PaymentHash),
+               source: HTLCSource,
+               payment_hash: PaymentHash,
        },
        MaturingOutput {
                descriptor: SpendableOutputDescriptor,
@@ -818,10 +819,10 @@ impl<Signer: Sign> Writeable for ChannelMonitorImpl<Signer> {
                        entry.txid.write(writer)?;
                        writer.write_all(&byte_utils::be32_to_array(entry.height))?;
                        match entry.event {
-                               OnchainEvent::HTLCUpdate { ref htlc_update } => {
+                               OnchainEvent::HTLCUpdate { ref source, ref payment_hash } => {
                                        0u8.write(writer)?;
-                                       htlc_update.0.write(writer)?;
-                                       htlc_update.1.write(writer)?;
+                                       source.write(writer)?;
+                                       payment_hash.write(writer)?;
                                },
                                OnchainEvent::MaturingOutput { ref descriptor } => {
                                        1u8.write(writer)?;
@@ -1387,7 +1388,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
                macro_rules! claim_htlcs {
                        ($commitment_number: expr, $txid: expr) => {
                                let htlc_claim_reqs = self.get_counterparty_htlc_output_claim_reqs($commitment_number, $txid, None);
-                               self.onchain_tx_handler.update_claims_view(&Vec::new(), htlc_claim_reqs, None, broadcaster, fee_estimator, logger);
+                               self.onchain_tx_handler.update_claims_view(&Vec::new(), htlc_claim_reqs, self.best_block.height(), broadcaster, fee_estimator, logger);
                        }
                }
                if let Some(txid) = self.current_counterparty_commitment_txid {
@@ -1410,10 +1411,10 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
                // holder commitment transactions.
                if self.broadcasted_holder_revokable_script.is_some() {
                        let (claim_reqs, _) = self.get_broadcasted_holder_claims(&self.current_holder_commitment_tx, 0);
-                       self.onchain_tx_handler.update_claims_view(&Vec::new(), claim_reqs, None, broadcaster, fee_estimator, logger);
+                       self.onchain_tx_handler.update_claims_view(&Vec::new(), claim_reqs, self.best_block.height(), broadcaster, fee_estimator, logger);
                        if let Some(ref tx) = self.prev_holder_signed_commitment_tx {
                                let (claim_reqs, _) = self.get_broadcasted_holder_claims(&tx, 0);
-                               self.onchain_tx_handler.update_claims_view(&Vec::new(), claim_reqs, None, broadcaster, fee_estimator, logger);
+                               self.onchain_tx_handler.update_claims_view(&Vec::new(), claim_reqs, self.best_block.height(), broadcaster, fee_estimator, logger);
                        }
                }
        }
@@ -1609,17 +1610,18 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
                                                                        self.onchain_events_awaiting_threshold_conf.retain(|ref entry| {
                                                                                if entry.height != height { return true; }
                                                                                match entry.event {
-                                                                                        OnchainEvent::HTLCUpdate { ref htlc_update } => {
-                                                                                                htlc_update.0 != **source
-                                                                                        },
-                                                                                        _ => true,
+                                                                                       OnchainEvent::HTLCUpdate { source: ref update_source, .. } => {
+                                                                                               *update_source != **source
+                                                                                       },
+                                                                                       _ => true,
                                                                                }
                                                                        });
                                                                        let entry = OnchainEventEntry {
                                                                                txid: *$txid,
                                                                                height,
                                                                                event: OnchainEvent::HTLCUpdate {
-                                                                                       htlc_update: ((**source).clone(), htlc.payment_hash.clone())
+                                                                                       source: (**source).clone(),
+                                                                                       payment_hash: htlc.payment_hash.clone(),
                                                                                },
                                                                        };
                                                                        log_info!(logger, "Failing HTLC with payment_hash {} from {} counterparty commitment tx due to broadcast of revoked counterparty commitment transaction, waiting for confirmation (at height {})", log_bytes!(htlc.payment_hash.0), $commitment_tx, entry.confirmation_threshold());
@@ -1675,17 +1677,18 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
                                                                self.onchain_events_awaiting_threshold_conf.retain(|ref entry| {
                                                                        if entry.height != height { return true; }
                                                                        match entry.event {
-                                                                                OnchainEvent::HTLCUpdate { ref htlc_update } => {
-                                                                                        htlc_update.0 != **source
-                                                                                },
-                                                                                _ => true,
+                                                                               OnchainEvent::HTLCUpdate { source: ref update_source, .. } => {
+                                                                                       *update_source != **source
+                                                                               },
+                                                                               _ => true,
                                                                        }
                                                                });
                                                                self.onchain_events_awaiting_threshold_conf.push(OnchainEventEntry {
                                                                        txid: *$txid,
                                                                        height,
                                                                        event: OnchainEvent::HTLCUpdate {
-                                                                               htlc_update: ((**source).clone(), htlc.payment_hash.clone())
+                                                                               source: (**source).clone(),
+                                                                               payment_hash: htlc.payment_hash.clone(),
                                                                        },
                                                                });
                                                        }
@@ -1829,16 +1832,16 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
                                self.onchain_events_awaiting_threshold_conf.retain(|ref entry| {
                                        if entry.height != height { return true; }
                                        match entry.event {
-                                                OnchainEvent::HTLCUpdate { ref htlc_update } => {
-                                                        htlc_update.0 != $source
-                                                },
-                                                _ => true,
+                                               OnchainEvent::HTLCUpdate { source: ref update_source, .. } => {
+                                                       *update_source != $source
+                                               },
+                                               _ => true,
                                        }
                                });
                                let entry = OnchainEventEntry {
                                        txid: commitment_txid,
                                        height,
-                                       event: OnchainEvent::HTLCUpdate { htlc_update: ($source, $payment_hash) },
+                                       event: OnchainEvent::HTLCUpdate { source: $source, payment_hash: $payment_hash },
                                };
                                log_trace!(logger, "Failing HTLC with payment_hash {} from {} holder commitment tx due to broadcast of transaction, waiting confirmation (at height{})", log_bytes!($payment_hash.0), $commitment_tx, entry.confirmation_threshold());
                                self.onchain_events_awaiting_threshold_conf.push(entry);
@@ -1899,7 +1902,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
                self.holder_tx_signed = true;
                let commitment_tx = self.onchain_tx_handler.get_fully_signed_holder_tx(&self.funding_redeemscript);
                let txid = commitment_tx.txid();
-               let mut res = vec![commitment_tx];
+               let mut holder_transactions = vec![commitment_tx];
                for htlc in self.current_holder_commitment_tx.htlc_outputs.iter() {
                        if let Some(vout) = htlc.0.transaction_output_index {
                                let preimage = if !htlc.0.offered {
@@ -1907,24 +1910,32 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
                                                // We can't build an HTLC-Success transaction without the preimage
                                                continue;
                                        }
+                               } else if htlc.0.cltv_expiry > self.best_block.height() + 1 {
+                                       // Don't broadcast HTLC-Timeout transactions immediately as they don't meet the
+                                       // current locktime requirements on-chain. We will broadcast them in
+                                       // `block_confirmed` when `would_broadcast_at_height` returns true.
+                                       // Note that we add + 1 as transactions are broadcastable when they can be
+                                       // confirmed in the next block.
+                                       continue;
                                } else { None };
                                if let Some(htlc_tx) = self.onchain_tx_handler.get_fully_signed_htlc_tx(
                                        &::bitcoin::OutPoint { txid, vout }, &preimage) {
-                                       res.push(htlc_tx);
+                                       holder_transactions.push(htlc_tx);
                                }
                        }
                }
                // We throw away the generated waiting_first_conf data as we aren't (yet) confirmed and we don't actually know what the caller wants to do.
                // The data will be re-generated and tracked in check_spend_holder_transaction if we get a confirmation.
-               return res;
+               holder_transactions
        }
 
        #[cfg(any(test,feature = "unsafe_revoked_tx_signing"))]
+       /// Note that this includes possibly-locktimed-in-the-future transactions!
        fn unsafe_get_latest_holder_commitment_txn<L: Deref>(&mut self, logger: &L) -> Vec<Transaction> where L::Target: Logger {
                log_trace!(logger, "Getting signed copy of latest holder commitment transaction!");
                let commitment_tx = self.onchain_tx_handler.get_fully_signed_copy_holder_tx(&self.funding_redeemscript);
                let txid = commitment_tx.txid();
-               let mut res = vec![commitment_tx];
+               let mut holder_transactions = vec![commitment_tx];
                for htlc in self.current_holder_commitment_tx.htlc_outputs.iter() {
                        if let Some(vout) = htlc.0.transaction_output_index {
                                let preimage = if !htlc.0.offered {
@@ -1935,11 +1946,11 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
                                } else { None };
                                if let Some(htlc_tx) = self.onchain_tx_handler.unsafe_get_fully_signed_htlc_tx(
                                        &::bitcoin::OutPoint { txid, vout }, &preimage) {
-                                       res.push(htlc_tx);
+                                       holder_transactions.push(htlc_tx);
                                }
                        }
                }
-               return res
+               holder_transactions
        }
 
        pub fn block_connected<B: Deref, F: Deref, L: Deref>(&mut self, header: &BlockHeader, txdata: &TransactionData, height: u32, broadcaster: B, fee_estimator: F, logger: L) -> Vec<TransactionOutputs>
@@ -2101,7 +2112,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
                let unmatured_htlcs: Vec<_> = self.onchain_events_awaiting_threshold_conf
                        .iter()
                        .filter_map(|entry| match &entry.event {
-                               OnchainEvent::HTLCUpdate { htlc_update } => Some(htlc_update.0.clone()),
+                               OnchainEvent::HTLCUpdate { source, .. } => Some(source),
                                OnchainEvent::MaturingOutput { .. } => None,
                        })
                        .collect();
@@ -2111,28 +2122,28 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
                // Produce actionable events from on-chain events having reached their threshold.
                for entry in onchain_events_reaching_threshold_conf.drain(..) {
                        match entry.event {
-                               OnchainEvent::HTLCUpdate { htlc_update } => {
+                               OnchainEvent::HTLCUpdate { ref source, payment_hash } => {
                                        // Check for duplicate HTLC resolutions.
                                        #[cfg(debug_assertions)]
                                        {
                                                debug_assert!(
-                                                       unmatured_htlcs.iter().find(|&htlc| htlc == &htlc_update.0).is_none(),
+                                                       unmatured_htlcs.iter().find(|&htlc| htlc == &source).is_none(),
                                                        "An unmature HTLC transaction conflicts with a maturing one; failed to \
                                                         call either transaction_unconfirmed for the conflicting transaction \
                                                         or block_disconnected for a block containing it.");
                                                debug_assert!(
-                                                       matured_htlcs.iter().find(|&htlc| htlc == &htlc_update.0).is_none(),
+                                                       matured_htlcs.iter().find(|&htlc| htlc == source).is_none(),
                                                        "A matured HTLC transaction conflicts with a maturing one; failed to \
                                                         call either transaction_unconfirmed for the conflicting transaction \
                                                         or block_disconnected for a block containing it.");
-                                               matured_htlcs.push(htlc_update.0.clone());
+                                               matured_htlcs.push(source.clone());
                                        }
 
-                                       log_trace!(logger, "HTLC {} failure update has got enough confirmations to be passed upstream", log_bytes!((htlc_update.1).0));
+                                       log_trace!(logger, "HTLC {} failure update has got enough confirmations to be passed upstream", log_bytes!(payment_hash.0));
                                        self.pending_monitor_events.push(MonitorEvent::HTLCEvent(HTLCUpdate {
-                                               payment_hash: htlc_update.1,
+                                               payment_hash: payment_hash,
                                                payment_preimage: None,
-                                               source: htlc_update.0,
+                                               source: source.clone(),
                                        }));
                                },
                                OnchainEvent::MaturingOutput { descriptor } => {
@@ -2144,7 +2155,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
                        }
                }
 
-               self.onchain_tx_handler.update_claims_view(&txn_matched, claimable_outpoints, Some(height), &&*broadcaster, &&*fee_estimator, &&*logger);
+               self.onchain_tx_handler.update_claims_view(&txn_matched, claimable_outpoints, height, &&*broadcaster, &&*fee_estimator, &&*logger);
 
                // Determine new outputs to watch by comparing against previously known outputs to watch,
                // updating the latter in the process.
@@ -2429,16 +2440,16 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
                                        self.onchain_events_awaiting_threshold_conf.retain(|ref entry| {
                                                if entry.height != height { return true; }
                                                match entry.event {
-                                                        OnchainEvent::HTLCUpdate { ref htlc_update } => {
-                                                                htlc_update.0 != source
-                                                        },
-                                                        _ => true,
+                                                       OnchainEvent::HTLCUpdate { source: ref htlc_source, .. } => {
+                                                               *htlc_source != source
+                                                       },
+                                                       _ => true,
                                                }
                                        });
                                        let entry = OnchainEventEntry {
                                                txid: tx.txid(),
                                                height,
-                                               event: OnchainEvent::HTLCUpdate { htlc_update: (source, payment_hash) },
+                                               event: OnchainEvent::HTLCUpdate { source: source, payment_hash: payment_hash },
                                        };
                                        log_info!(logger, "Failing HTLC with payment_hash {} timeout by a spend tx, waiting for confirmation (at height{})", log_bytes!(payment_hash.0), entry.confirmation_threshold());
                                        self.onchain_events_awaiting_threshold_conf.push(entry);
@@ -2800,7 +2811,8 @@ impl<'a, Signer: Sign, K: KeysInterface<Signer = Signer>> ReadableArgs<&'a K>
                                        let htlc_source = Readable::read(reader)?;
                                        let hash = Readable::read(reader)?;
                                        OnchainEvent::HTLCUpdate {
-                                               htlc_update: (htlc_source, hash)
+                                               source: htlc_source,
+                                               payment_hash: hash,
                                        }
                                },
                                1 => {
@@ -2921,7 +2933,7 @@ mod tests {
        fn test_prune_preimages() {
                let secp_ctx = Secp256k1::new();
                let logger = Arc::new(TestLogger::new());
-               let broadcaster = Arc::new(TestBroadcaster{txn_broadcasted: Mutex::new(Vec::new())});
+               let broadcaster = Arc::new(TestBroadcaster{txn_broadcasted: Mutex::new(Vec::new()), blocks: Arc::new(Mutex::new(Vec::new()))});
                let fee_estimator = Arc::new(TestFeeEstimator { sat_per_kw: 253 });
 
                let dummy_key = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());