Skip to content

Commit 119668a

Browse files
committed
Swap order of checks in get_htlc_balance
This will allow us to use the HTLC resolution tracking to detect the HTLC spend via `OnchainEvent::MaturingOutput` in the next commit.
1 parent a37a16a commit 119668a

File tree

1 file changed

+33
-30
lines changed

1 file changed

+33
-30
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1685,6 +1685,38 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
16851685
let mut holder_timeout_spend_pending = None;
16861686
let mut htlc_spend_pending = None;
16871687
let mut holder_delayed_output_pending = None;
1688+
1689+
let htlc_resolved = self.htlcs_resolved_on_chain.iter()
1690+
.find(|v| if v.commitment_tx_output_idx == Some(htlc_commitment_tx_output_idx) {
1691+
debug_assert!(htlc_spend_txid_opt.is_none());
1692+
htlc_spend_txid_opt = v.resolving_txid.as_ref();
1693+
debug_assert!(htlc_spend_tx_opt.is_none());
1694+
htlc_spend_tx_opt = v.resolving_tx.as_ref();
1695+
true
1696+
} else { false });
1697+
1698+
let htlc_commitment_outpoint = BitcoinOutPoint::new(confirmed_txid.unwrap(), htlc_commitment_tx_output_idx);
1699+
let htlc_output_to_spend =
1700+
if let Some(txid) = htlc_spend_txid_opt {
1701+
// Because HTLC transactions either only have 1 input and 1 output (pre-anchors) or
1702+
// are signed with SIGHASH_SINGLE|ANYONECANPAY under BIP-0143 (post-anchors), we can
1703+
// locate the correct output by ensuring its adjacent input spends the HTLC output
1704+
// in the commitment.
1705+
if let Some(ref tx) = htlc_spend_tx_opt {
1706+
let htlc_input_idx_opt = tx.input.iter().enumerate()
1707+
.find(|(_, input)| input.previous_output == htlc_commitment_outpoint)
1708+
.map(|(idx, _)| idx as u32);
1709+
debug_assert!(htlc_input_idx_opt.is_some());
1710+
BitcoinOutPoint::new(*txid, htlc_input_idx_opt.unwrap_or(0))
1711+
} else {
1712+
debug_assert!(!self.onchain_tx_handler.channel_type_features().supports_anchors_zero_fee_htlc_tx());
1713+
BitcoinOutPoint::new(*txid, 0)
1714+
}
1715+
} else {
1716+
htlc_commitment_outpoint
1717+
};
1718+
let htlc_output_spend_pending = self.onchain_tx_handler.is_output_spend_pending(&htlc_output_to_spend);
1719+
16881720
for event in self.onchain_events_awaiting_threshold_conf.iter() {
16891721
match event.event {
16901722
OnchainEvent::HTLCUpdate { commitment_tx_output_idx, htlc_value_satoshis, .. }
@@ -1715,37 +1747,8 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
17151747
_ => {},
17161748
}
17171749
}
1718-
let htlc_resolved = self.htlcs_resolved_on_chain.iter()
1719-
.find(|v| if v.commitment_tx_output_idx == Some(htlc_commitment_tx_output_idx) {
1720-
debug_assert!(htlc_spend_txid_opt.is_none());
1721-
htlc_spend_txid_opt = v.resolving_txid.as_ref();
1722-
debug_assert!(htlc_spend_tx_opt.is_none());
1723-
htlc_spend_tx_opt = v.resolving_tx.as_ref();
1724-
true
1725-
} else { false });
1726-
debug_assert!(holder_timeout_spend_pending.is_some() as u8 + htlc_spend_pending.is_some() as u8 + htlc_resolved.is_some() as u8 <= 1);
17271750

1728-
let htlc_commitment_outpoint = BitcoinOutPoint::new(confirmed_txid.unwrap(), htlc_commitment_tx_output_idx);
1729-
let htlc_output_to_spend =
1730-
if let Some(txid) = htlc_spend_txid_opt {
1731-
// Because HTLC transactions either only have 1 input and 1 output (pre-anchors) or
1732-
// are signed with SIGHASH_SINGLE|ANYONECANPAY under BIP-0143 (post-anchors), we can
1733-
// locate the correct output by ensuring its adjacent input spends the HTLC output
1734-
// in the commitment.
1735-
if let Some(ref tx) = htlc_spend_tx_opt {
1736-
let htlc_input_idx_opt = tx.input.iter().enumerate()
1737-
.find(|(_, input)| input.previous_output == htlc_commitment_outpoint)
1738-
.map(|(idx, _)| idx as u32);
1739-
debug_assert!(htlc_input_idx_opt.is_some());
1740-
BitcoinOutPoint::new(*txid, htlc_input_idx_opt.unwrap_or(0))
1741-
} else {
1742-
debug_assert!(!self.onchain_tx_handler.channel_type_features().supports_anchors_zero_fee_htlc_tx());
1743-
BitcoinOutPoint::new(*txid, 0)
1744-
}
1745-
} else {
1746-
htlc_commitment_outpoint
1747-
};
1748-
let htlc_output_spend_pending = self.onchain_tx_handler.is_output_spend_pending(&htlc_output_to_spend);
1751+
debug_assert!(holder_timeout_spend_pending.is_some() as u8 + htlc_spend_pending.is_some() as u8 + htlc_resolved.is_some() as u8 <= 1);
17491752

17501753
if let Some(conf_thresh) = holder_delayed_output_pending {
17511754
debug_assert!(holder_commitment);

0 commit comments

Comments
 (0)