Swap order of checks in `get_htlc_balance`
authorMatt Corallo <git@bluematt.me>
Sun, 24 Sep 2023 01:09:43 +0000 (01:09 +0000)
committerMatt Corallo <git@bluematt.me>
Sun, 24 Sep 2023 01:09:43 +0000 (01:09 +0000)
This will allow us to use the HTLC resolution tracking to detect
the HTLC spend via `OnchainEvent::MaturingOutput` in the next
commit.

lightning/src/chain/channelmonitor.rs

index 3f9c83bb54393290c40b9a6784bcce7dd48c53da..d98d9f8607df333ab492752e3132a403e6bc36bd 100644 (file)
@@ -1685,6 +1685,38 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
                let mut holder_timeout_spend_pending = None;
                let mut htlc_spend_pending = None;
                let mut holder_delayed_output_pending = None;
+
+               let htlc_resolved = self.htlcs_resolved_on_chain.iter()
+                       .find(|v| if v.commitment_tx_output_idx == Some(htlc_commitment_tx_output_idx) {
+                               debug_assert!(htlc_spend_txid_opt.is_none());
+                               htlc_spend_txid_opt = v.resolving_txid.as_ref();
+                               debug_assert!(htlc_spend_tx_opt.is_none());
+                               htlc_spend_tx_opt = v.resolving_tx.as_ref();
+                               true
+                       } else { false });
+
+               let htlc_commitment_outpoint = BitcoinOutPoint::new(confirmed_txid.unwrap(), htlc_commitment_tx_output_idx);
+               let htlc_output_to_spend =
+                       if let Some(txid) = htlc_spend_txid_opt {
+                               // Because HTLC transactions either only have 1 input and 1 output (pre-anchors) or
+                               // are signed with SIGHASH_SINGLE|ANYONECANPAY under BIP-0143 (post-anchors), we can
+                               // locate the correct output by ensuring its adjacent input spends the HTLC output
+                               // in the commitment.
+                               if let Some(ref tx) = htlc_spend_tx_opt {
+                                       let htlc_input_idx_opt = tx.input.iter().enumerate()
+                                               .find(|(_, input)| input.previous_output == htlc_commitment_outpoint)
+                                               .map(|(idx, _)| idx as u32);
+                                       debug_assert!(htlc_input_idx_opt.is_some());
+                                       BitcoinOutPoint::new(*txid, htlc_input_idx_opt.unwrap_or(0))
+                               } else {
+                                       debug_assert!(!self.onchain_tx_handler.channel_type_features().supports_anchors_zero_fee_htlc_tx());
+                                       BitcoinOutPoint::new(*txid, 0)
+                               }
+                       } else {
+                               htlc_commitment_outpoint
+                       };
+               let htlc_output_spend_pending = self.onchain_tx_handler.is_output_spend_pending(&htlc_output_to_spend);
+
                for event in self.onchain_events_awaiting_threshold_conf.iter() {
                        match event.event {
                                OnchainEvent::HTLCUpdate { commitment_tx_output_idx, htlc_value_satoshis, .. }
@@ -1715,37 +1747,8 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
                                _ => {},
                        }
                }
-               let htlc_resolved = self.htlcs_resolved_on_chain.iter()
-                       .find(|v| if v.commitment_tx_output_idx == Some(htlc_commitment_tx_output_idx) {
-                               debug_assert!(htlc_spend_txid_opt.is_none());
-                               htlc_spend_txid_opt = v.resolving_txid.as_ref();
-                               debug_assert!(htlc_spend_tx_opt.is_none());
-                               htlc_spend_tx_opt = v.resolving_tx.as_ref();
-                               true
-                       } else { false });
-               debug_assert!(holder_timeout_spend_pending.is_some() as u8 + htlc_spend_pending.is_some() as u8 + htlc_resolved.is_some() as u8 <= 1);
 
-               let htlc_commitment_outpoint = BitcoinOutPoint::new(confirmed_txid.unwrap(), htlc_commitment_tx_output_idx);
-               let htlc_output_to_spend =
-                       if let Some(txid) = htlc_spend_txid_opt {
-                               // Because HTLC transactions either only have 1 input and 1 output (pre-anchors) or
-                               // are signed with SIGHASH_SINGLE|ANYONECANPAY under BIP-0143 (post-anchors), we can
-                               // locate the correct output by ensuring its adjacent input spends the HTLC output
-                               // in the commitment.
-                               if let Some(ref tx) = htlc_spend_tx_opt {
-                                       let htlc_input_idx_opt = tx.input.iter().enumerate()
-                                               .find(|(_, input)| input.previous_output == htlc_commitment_outpoint)
-                                               .map(|(idx, _)| idx as u32);
-                                       debug_assert!(htlc_input_idx_opt.is_some());
-                                       BitcoinOutPoint::new(*txid, htlc_input_idx_opt.unwrap_or(0))
-                               } else {
-                                       debug_assert!(!self.onchain_tx_handler.channel_type_features().supports_anchors_zero_fee_htlc_tx());
-                                       BitcoinOutPoint::new(*txid, 0)
-                               }
-                       } else {
-                               htlc_commitment_outpoint
-                       };
-               let htlc_output_spend_pending = self.onchain_tx_handler.is_output_spend_pending(&htlc_output_to_spend);
+               debug_assert!(holder_timeout_spend_pending.is_some() as u8 + htlc_spend_pending.is_some() as u8 + htlc_resolved.is_some() as u8 <= 1);
 
                if let Some(conf_thresh) = holder_delayed_output_pending {
                        debug_assert!(holder_commitment);