Skip to content

Commit 69b1095

Browse files
committed
Track HTLC resolving transaction to determine input index
1 parent 8d8e5e7 commit 69b1095

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,7 @@ struct IrrevocablyResolvedHTLC {
647647
/// was not present in the confirmed commitment transaction), HTLC-Success, or HTLC-Timeout
648648
/// transaction.
649649
resolving_txid: Option<Txid>, // Added as optional, but always filled in, in 0.0.110
650+
resolving_tx: Option<Transaction>,
650651
/// Only set if the HTLC claim was ours using a payment preimage
651652
payment_preimage: Option<PaymentPreimage>,
652653
}
@@ -662,6 +663,7 @@ impl Writeable for IrrevocablyResolvedHTLC {
662663
(0, mapped_commitment_tx_output_idx, required),
663664
(1, self.resolving_txid, option),
664665
(2, self.payment_preimage, option),
666+
(3, self.resolving_tx, option),
665667
});
666668
Ok(())
667669
}
@@ -672,15 +674,18 @@ impl Readable for IrrevocablyResolvedHTLC {
672674
let mut mapped_commitment_tx_output_idx = 0;
673675
let mut resolving_txid = None;
674676
let mut payment_preimage = None;
677+
let mut resolving_tx = None;
675678
read_tlv_fields!(reader, {
676679
(0, mapped_commitment_tx_output_idx, required),
677680
(1, resolving_txid, option),
678681
(2, payment_preimage, option),
682+
(3, resolving_tx, option),
679683
});
680684
Ok(Self {
681685
commitment_tx_output_idx: if mapped_commitment_tx_output_idx == u32::max_value() { None } else { Some(mapped_commitment_tx_output_idx) },
682686
resolving_txid,
683687
payment_preimage,
688+
resolving_tx,
684689
})
685690
}
686691
}
@@ -1517,23 +1522,26 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
15171522
if let Some(v) = htlc.transaction_output_index { v } else { return None; };
15181523

15191524
let mut htlc_spend_txid_opt = None;
1525+
let mut htlc_spend_tx_opt = None;
15201526
let mut holder_timeout_spend_pending = None;
15211527
let mut htlc_spend_pending = None;
15221528
let mut holder_delayed_output_pending = None;
15231529
for event in self.onchain_events_awaiting_threshold_conf.iter() {
15241530
match event.event {
15251531
OnchainEvent::HTLCUpdate { commitment_tx_output_idx, htlc_value_satoshis, .. }
15261532
if commitment_tx_output_idx == Some(htlc_commitment_tx_output_idx) => {
1527-
debug_assert!(htlc_spend_txid_opt.is_none());
1528-
htlc_spend_txid_opt = event.transaction.as_ref().map(|tx| tx.txid());
1533+
htlc_spend_txid_opt = Some(&event.txid);
1534+
debug_assert!(htlc_spend_tx_opt.is_none());
1535+
htlc_spend_tx_opt = event.transaction.as_ref();
15291536
debug_assert!(holder_timeout_spend_pending.is_none());
15301537
debug_assert_eq!(htlc_value_satoshis.unwrap(), htlc.amount_msat / 1000);
15311538
holder_timeout_spend_pending = Some(event.confirmation_threshold());
15321539
},
15331540
OnchainEvent::HTLCSpendConfirmation { commitment_tx_output_idx, preimage, .. }
15341541
if commitment_tx_output_idx == htlc_commitment_tx_output_idx => {
1535-
debug_assert!(htlc_spend_txid_opt.is_none());
1536-
htlc_spend_txid_opt = event.transaction.as_ref().map(|tx| tx.txid());
1542+
htlc_spend_txid_opt = Some(&event.txid);
1543+
debug_assert!(htlc_spend_tx_opt.is_none());
1544+
htlc_spend_tx_opt = event.transaction.as_ref();
15371545
debug_assert!(htlc_spend_pending.is_none());
15381546
htlc_spend_pending = Some((event.confirmation_threshold(), preimage.is_some()));
15391547
},
@@ -1548,20 +1556,26 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
15481556
}
15491557
let htlc_resolved = self.htlcs_resolved_on_chain.iter()
15501558
.find(|v| if v.commitment_tx_output_idx == Some(htlc_commitment_tx_output_idx) {
1551-
debug_assert!(htlc_spend_txid_opt.is_none());
1552-
htlc_spend_txid_opt = v.resolving_txid;
1559+
htlc_spend_txid_opt = v.resolving_txid.as_ref();
1560+
debug_assert!(htlc_spend_tx_opt.is_none());
1561+
htlc_spend_tx_opt = v.resolving_tx.as_ref();
15531562
true
15541563
} else { false });
15551564
debug_assert!(holder_timeout_spend_pending.is_some() as u8 + htlc_spend_pending.is_some() as u8 + htlc_resolved.is_some() as u8 <= 1);
15561565

1566+
let htlc_commitment_outpoint = BitcoinOutPoint::new(confirmed_txid.unwrap(), htlc_commitment_tx_output_idx);
15571567
let htlc_output_to_spend =
1558-
if let Some(txid) = htlc_spend_txid_opt {
1559-
debug_assert!(
1560-
self.onchain_tx_handler.channel_transaction_parameters.opt_anchors.is_none(),
1561-
"This code needs updating for anchors");
1562-
BitcoinOutPoint::new(txid, 0)
1568+
if let Some(ref tx) = htlc_spend_tx_opt {
1569+
// Because HTLCs are signed with SIGHASH_SINGLE|ANYONECANPAY under BIP-0143, we can
1570+
// locate the correct output by ensuring its adjacent input spends the HTLC output in
1571+
// the commitment.
1572+
let htlc_input_idx_opt = tx.input.iter().enumerate()
1573+
.find(|(_, input)| input.previous_output == htlc_commitment_outpoint)
1574+
.map(|(idx, _)| idx as u32);
1575+
debug_assert!(htlc_input_idx_opt.is_some());
1576+
BitcoinOutPoint::new(*htlc_spend_txid_opt.unwrap(), htlc_input_idx_opt.unwrap_or(0))
15631577
} else {
1564-
BitcoinOutPoint::new(confirmed_txid.unwrap(), htlc_commitment_tx_output_idx)
1578+
htlc_commitment_outpoint
15651579
};
15661580
let htlc_output_spend_pending = self.onchain_tx_handler.is_output_spend_pending(&htlc_output_to_spend);
15671581

@@ -1585,8 +1599,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
15851599
} = &event.event {
15861600
if event.transaction.as_ref().map(|tx| tx.input.iter().any(|inp| {
15871601
if let Some(htlc_spend_txid) = htlc_spend_txid_opt {
1588-
Some(tx.txid()) == htlc_spend_txid_opt ||
1589-
inp.previous_output.txid == htlc_spend_txid
1602+
tx.txid() == *htlc_spend_txid || inp.previous_output.txid == *htlc_spend_txid
15901603
} else {
15911604
Some(inp.previous_output.txid) == confirmed_txid &&
15921605
inp.previous_output.vout == htlc_commitment_tx_output_idx
@@ -3065,7 +3078,9 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
30653078
htlc_value_satoshis,
30663079
}));
30673080
self.htlcs_resolved_on_chain.push(IrrevocablyResolvedHTLC {
3068-
commitment_tx_output_idx, resolving_txid: Some(entry.txid),
3081+
commitment_tx_output_idx,
3082+
resolving_txid: Some(entry.txid),
3083+
resolving_tx: entry.transaction,
30693084
payment_preimage: None,
30703085
});
30713086
},
@@ -3077,7 +3092,9 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
30773092
},
30783093
OnchainEvent::HTLCSpendConfirmation { commitment_tx_output_idx, preimage, .. } => {
30793094
self.htlcs_resolved_on_chain.push(IrrevocablyResolvedHTLC {
3080-
commitment_tx_output_idx: Some(commitment_tx_output_idx), resolving_txid: Some(entry.txid),
3095+
commitment_tx_output_idx: Some(commitment_tx_output_idx),
3096+
resolving_txid: Some(entry.txid),
3097+
resolving_tx: entry.transaction,
30813098
payment_preimage: preimage,
30823099
});
30833100
},

lightning/src/util/events.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ pub enum BumpTransactionEvent {
273273
/// with additional inputs to meet the target feerate. Failure to meet the target feerate
274274
/// decreases the confirmation odds of the transaction package (which includes the commitment
275275
/// and child anchor transactions), possibly resulting in a loss of funds. Once the transaction
276-
/// is constructed, it must be fully signed for and broadcasted by the consumer of the event
276+
/// is constructed, it must be fully signed for and broadcast by the consumer of the event
277277
/// along with the `commitment_tx` enclosed. Note that the `commitment_tx` must always be
278278
/// broadcast first, as the child anchor transaction depends on it.
279279
///

0 commit comments

Comments
 (0)