@@ -647,6 +647,7 @@ struct IrrevocablyResolvedHTLC {
647
647
/// was not present in the confirmed commitment transaction), HTLC-Success, or HTLC-Timeout
648
648
/// transaction.
649
649
resolving_txid : Option < Txid > , // Added as optional, but always filled in, in 0.0.110
650
+ resolving_tx : Option < Transaction > ,
650
651
/// Only set if the HTLC claim was ours using a payment preimage
651
652
payment_preimage : Option < PaymentPreimage > ,
652
653
}
@@ -662,6 +663,7 @@ impl Writeable for IrrevocablyResolvedHTLC {
662
663
( 0 , mapped_commitment_tx_output_idx, required) ,
663
664
( 1 , self . resolving_txid, option) ,
664
665
( 2 , self . payment_preimage, option) ,
666
+ ( 3 , self . resolving_tx, option) ,
665
667
} ) ;
666
668
Ok ( ( ) )
667
669
}
@@ -672,15 +674,18 @@ impl Readable for IrrevocablyResolvedHTLC {
672
674
let mut mapped_commitment_tx_output_idx = 0 ;
673
675
let mut resolving_txid = None ;
674
676
let mut payment_preimage = None ;
677
+ let mut resolving_tx = None ;
675
678
read_tlv_fields ! ( reader, {
676
679
( 0 , mapped_commitment_tx_output_idx, required) ,
677
680
( 1 , resolving_txid, option) ,
678
681
( 2 , payment_preimage, option) ,
682
+ ( 3 , resolving_tx, option) ,
679
683
} ) ;
680
684
Ok ( Self {
681
685
commitment_tx_output_idx : if mapped_commitment_tx_output_idx == u32:: max_value ( ) { None } else { Some ( mapped_commitment_tx_output_idx) } ,
682
686
resolving_txid,
683
687
payment_preimage,
688
+ resolving_tx,
684
689
} )
685
690
}
686
691
}
@@ -1526,6 +1531,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
1526
1531
if let Some ( v) = htlc. transaction_output_index { v } else { return None ; } ;
1527
1532
1528
1533
let mut htlc_spend_txid_opt = None ;
1534
+ let mut htlc_spend_tx_opt = None ;
1529
1535
let mut holder_timeout_spend_pending = None ;
1530
1536
let mut htlc_spend_pending = None ;
1531
1537
let mut holder_delayed_output_pending = None ;
@@ -1534,15 +1540,19 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
1534
1540
OnchainEvent :: HTLCUpdate { commitment_tx_output_idx, htlc_value_satoshis, .. }
1535
1541
if commitment_tx_output_idx == Some ( htlc_commitment_tx_output_idx) => {
1536
1542
debug_assert ! ( htlc_spend_txid_opt. is_none( ) ) ;
1537
- htlc_spend_txid_opt = event. transaction . as_ref ( ) . map ( |tx| tx. txid ( ) ) ;
1543
+ htlc_spend_txid_opt = Some ( & event. txid ) ;
1544
+ debug_assert ! ( htlc_spend_tx_opt. is_none( ) ) ;
1545
+ htlc_spend_tx_opt = event. transaction . as_ref ( ) ;
1538
1546
debug_assert ! ( holder_timeout_spend_pending. is_none( ) ) ;
1539
1547
debug_assert_eq ! ( htlc_value_satoshis. unwrap( ) , htlc. amount_msat / 1000 ) ;
1540
1548
holder_timeout_spend_pending = Some ( event. confirmation_threshold ( ) ) ;
1541
1549
} ,
1542
1550
OnchainEvent :: HTLCSpendConfirmation { commitment_tx_output_idx, preimage, .. }
1543
1551
if commitment_tx_output_idx == htlc_commitment_tx_output_idx => {
1544
1552
debug_assert ! ( htlc_spend_txid_opt. is_none( ) ) ;
1545
- htlc_spend_txid_opt = event. transaction . as_ref ( ) . map ( |tx| tx. txid ( ) ) ;
1553
+ htlc_spend_txid_opt = Some ( & event. txid ) ;
1554
+ debug_assert ! ( htlc_spend_tx_opt. is_none( ) ) ;
1555
+ htlc_spend_tx_opt = event. transaction . as_ref ( ) ;
1546
1556
debug_assert ! ( htlc_spend_pending. is_none( ) ) ;
1547
1557
htlc_spend_pending = Some ( ( event. confirmation_threshold ( ) , preimage. is_some ( ) ) ) ;
1548
1558
} ,
@@ -1558,19 +1568,32 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
1558
1568
let htlc_resolved = self . htlcs_resolved_on_chain . iter ( )
1559
1569
. find ( |v| if v. commitment_tx_output_idx == Some ( htlc_commitment_tx_output_idx) {
1560
1570
debug_assert ! ( htlc_spend_txid_opt. is_none( ) ) ;
1561
- htlc_spend_txid_opt = v. resolving_txid ;
1571
+ htlc_spend_txid_opt = v. resolving_txid . as_ref ( ) ;
1572
+ debug_assert ! ( htlc_spend_tx_opt. is_none( ) ) ;
1573
+ htlc_spend_tx_opt = v. resolving_tx . as_ref ( ) ;
1562
1574
true
1563
1575
} else { false } ) ;
1564
1576
debug_assert ! ( holder_timeout_spend_pending. is_some( ) as u8 + htlc_spend_pending. is_some( ) as u8 + htlc_resolved. is_some( ) as u8 <= 1 ) ;
1565
1577
1578
+ let htlc_commitment_outpoint = BitcoinOutPoint :: new ( confirmed_txid. unwrap ( ) , htlc_commitment_tx_output_idx) ;
1566
1579
let htlc_output_to_spend =
1567
1580
if let Some ( txid) = htlc_spend_txid_opt {
1568
- debug_assert ! (
1569
- self . onchain_tx_handler. channel_transaction_parameters. opt_anchors. is_none( ) ,
1570
- "This code needs updating for anchors" ) ;
1571
- BitcoinOutPoint :: new ( txid, 0 )
1581
+ // Because HTLC transactions either only have 1 input and 1 output (pre-anchors) or
1582
+ // are signed with SIGHASH_SINGLE|ANYONECANPAY under BIP-0143 (post-anchors), we can
1583
+ // locate the correct output by ensuring its adjacent input spends the HTLC output
1584
+ // in the commitment.
1585
+ if let Some ( ref tx) = htlc_spend_tx_opt {
1586
+ let htlc_input_idx_opt = tx. input . iter ( ) . enumerate ( )
1587
+ . find ( |( _, input) | input. previous_output == htlc_commitment_outpoint)
1588
+ . map ( |( idx, _) | idx as u32 ) ;
1589
+ debug_assert ! ( htlc_input_idx_opt. is_some( ) ) ;
1590
+ BitcoinOutPoint :: new ( * txid, htlc_input_idx_opt. unwrap_or ( 0 ) )
1591
+ } else {
1592
+ debug_assert ! ( !self . onchain_tx_handler. opt_anchors( ) ) ;
1593
+ BitcoinOutPoint :: new ( * txid, 0 )
1594
+ }
1572
1595
} else {
1573
- BitcoinOutPoint :: new ( confirmed_txid . unwrap ( ) , htlc_commitment_tx_output_idx )
1596
+ htlc_commitment_outpoint
1574
1597
} ;
1575
1598
let htlc_output_spend_pending = self . onchain_tx_handler . is_output_spend_pending ( & htlc_output_to_spend) ;
1576
1599
@@ -1594,8 +1617,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
1594
1617
} = & event. event {
1595
1618
if event. transaction . as_ref ( ) . map ( |tx| tx. input . iter ( ) . any ( |inp| {
1596
1619
if let Some ( htlc_spend_txid) = htlc_spend_txid_opt {
1597
- Some ( tx. txid ( ) ) == htlc_spend_txid_opt ||
1598
- inp. previous_output . txid == htlc_spend_txid
1620
+ tx. txid ( ) == * htlc_spend_txid || inp. previous_output . txid == * htlc_spend_txid
1599
1621
} else {
1600
1622
Some ( inp. previous_output . txid ) == confirmed_txid &&
1601
1623
inp. previous_output . vout == htlc_commitment_tx_output_idx
@@ -3074,7 +3096,9 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
3074
3096
htlc_value_satoshis,
3075
3097
} ) ) ;
3076
3098
self . htlcs_resolved_on_chain . push ( IrrevocablyResolvedHTLC {
3077
- commitment_tx_output_idx, resolving_txid : Some ( entry. txid ) ,
3099
+ commitment_tx_output_idx,
3100
+ resolving_txid : Some ( entry. txid ) ,
3101
+ resolving_tx : entry. transaction ,
3078
3102
payment_preimage : None ,
3079
3103
} ) ;
3080
3104
} ,
@@ -3087,7 +3111,9 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
3087
3111
} ,
3088
3112
OnchainEvent :: HTLCSpendConfirmation { commitment_tx_output_idx, preimage, .. } => {
3089
3113
self . htlcs_resolved_on_chain . push ( IrrevocablyResolvedHTLC {
3090
- commitment_tx_output_idx : Some ( commitment_tx_output_idx) , resolving_txid : Some ( entry. txid ) ,
3114
+ commitment_tx_output_idx : Some ( commitment_tx_output_idx) ,
3115
+ resolving_txid : Some ( entry. txid ) ,
3116
+ resolving_tx : entry. transaction ,
3091
3117
payment_preimage : preimage,
3092
3118
} ) ;
3093
3119
} ,
0 commit comments