@@ -1622,42 +1622,77 @@ impl ChannelMonitor {
1622
1622
/// Attempts to claim any claimable HTLCs in a commitment transaction which was not (yet)
1623
1623
/// revoked using data in local_claimable_outpoints.
1624
1624
/// Should not be used if check_spend_revoked_transaction succeeds.
1625
- fn check_spend_local_transaction ( & self , tx : & Transaction , _height : u32 ) -> ( Vec < Transaction > , Vec < SpendableOutputDescriptor > , ( Sha256dHash , Vec < TxOut > ) ) {
1625
+ fn check_spend_local_transaction ( & mut self , tx : & Transaction , height : u32 ) -> ( Vec < Transaction > , Vec < SpendableOutputDescriptor > , ( Sha256dHash , Vec < TxOut > ) ) {
1626
1626
let commitment_txid = tx. txid ( ) ;
1627
- // TODO: If we find a match here we need to fail back HTLCs that weren't included in the
1628
- // broadcast commitment transaction, either because they didn't meet dust or because they
1629
- // weren't yet included in our commitment transaction(s).
1627
+ let mut local_txn = Vec :: new ( ) ;
1628
+ let mut spendable_outputs = Vec :: new ( ) ;
1629
+ let mut watch_outputs = Vec :: new ( ) ;
1630
+
1631
+ macro_rules! wait_threshold_conf {
1632
+ ( $height: expr, $source: expr, $update: expr, $commitment_tx: expr, $payment_hash: expr) => {
1633
+ log_trace!( self , "Failing HTLC with payment_hash {} from {} local commitment tx due to broadcast of transaction, waiting confirmation until {} height" , log_bytes!( $payment_hash. 0 ) , $commitment_tx, height + HTLC_FAIL_ANTI_REORG_DELAY - 1 ) ;
1634
+ match self . htlc_updated_waiting_threshold_conf. entry( $height + HTLC_FAIL_ANTI_REORG_DELAY - 1 ) {
1635
+ hash_map:: Entry :: Occupied ( mut entry) => {
1636
+ let e = entry. get_mut( ) ;
1637
+ e. retain( |ref update| update. 0 != $source) ;
1638
+ e. push( ( $source, $update, $payment_hash) ) ;
1639
+ }
1640
+ hash_map:: Entry :: Vacant ( entry) => {
1641
+ entry. insert( vec![ ( $source, $update, $payment_hash) ] ) ;
1642
+ }
1643
+ }
1644
+ }
1645
+ }
1646
+
1647
+ macro_rules! append_onchain_update {
1648
+ ( $updates: expr) => {
1649
+ local_txn. append( & mut $updates. 0 ) ;
1650
+ spendable_outputs. append( & mut $updates. 1 ) ;
1651
+ watch_outputs. append( & mut $updates. 2 ) ;
1652
+ }
1653
+ }
1654
+
1630
1655
if let & Some ( ref local_tx) = & self . current_local_signed_commitment_tx {
1656
+ for & ( ref htlc, _, ref source) in & local_tx. htlc_outputs {
1657
+ if htlc. transaction_output_index . is_none ( ) {
1658
+ if let & Some ( ref source) = source {
1659
+ wait_threshold_conf ! ( height, source. clone( ) , None , "lastest" , htlc. payment_hash. clone( ) ) ;
1660
+ }
1661
+ }
1662
+ }
1631
1663
if local_tx. txid == commitment_txid {
1632
1664
log_trace ! ( self , "Got latest local commitment tx broadcast, searching for available HTLCs to claim" ) ;
1633
1665
match self . key_storage {
1634
1666
Storage :: Local { ref delayed_payment_base_key, ref latest_per_commitment_point, .. } => {
1635
- let ( local_txn, spendable_outputs, watch_outputs) = self . broadcast_by_local_state ( local_tx, latest_per_commitment_point, & Some ( * delayed_payment_base_key) ) ;
1636
- return ( local_txn, spendable_outputs, ( commitment_txid, watch_outputs) ) ;
1667
+ append_onchain_update ! ( self . broadcast_by_local_state( local_tx, latest_per_commitment_point, & Some ( * delayed_payment_base_key) ) ) ;
1637
1668
} ,
1638
1669
Storage :: Watchtower { .. } => {
1639
- let ( local_txn, spendable_outputs, watch_outputs) = self . broadcast_by_local_state ( local_tx, & None , & None ) ;
1640
- return ( local_txn, spendable_outputs, ( commitment_txid, watch_outputs) ) ;
1670
+ append_onchain_update ! ( self . broadcast_by_local_state( local_tx, & None , & None ) ) ;
1641
1671
}
1642
1672
}
1643
1673
}
1644
1674
}
1645
1675
if let & Some ( ref local_tx) = & self . prev_local_signed_commitment_tx {
1676
+ for & ( ref htlc, _, ref source) in & local_tx. htlc_outputs {
1677
+ if htlc. transaction_output_index . is_none ( ) {
1678
+ if let & Some ( ref source) = source {
1679
+ wait_threshold_conf ! ( height, source. clone( ) , None , "previous" , htlc. payment_hash. clone( ) ) ;
1680
+ }
1681
+ }
1682
+ }
1646
1683
if local_tx. txid == commitment_txid {
1647
1684
log_trace ! ( self , "Got previous local commitment tx broadcast, searching for available HTLCs to claim" ) ;
1648
1685
match self . key_storage {
1649
1686
Storage :: Local { ref delayed_payment_base_key, ref prev_latest_per_commitment_point, .. } => {
1650
- let ( local_txn, spendable_outputs, watch_outputs) = self . broadcast_by_local_state ( local_tx, prev_latest_per_commitment_point, & Some ( * delayed_payment_base_key) ) ;
1651
- return ( local_txn, spendable_outputs, ( commitment_txid, watch_outputs) ) ;
1687
+ append_onchain_update ! ( self . broadcast_by_local_state( local_tx, prev_latest_per_commitment_point, & Some ( * delayed_payment_base_key) ) ) ;
1652
1688
} ,
1653
1689
Storage :: Watchtower { .. } => {
1654
- let ( local_txn, spendable_outputs, watch_outputs) = self . broadcast_by_local_state ( local_tx, & None , & None ) ;
1655
- return ( local_txn, spendable_outputs, ( commitment_txid, watch_outputs) ) ;
1690
+ append_onchain_update ! ( self . broadcast_by_local_state( local_tx, & None , & None ) ) ;
1656
1691
}
1657
1692
}
1658
1693
}
1659
1694
}
1660
- ( Vec :: new ( ) , Vec :: new ( ) , ( commitment_txid, Vec :: new ( ) ) )
1695
+ ( local_txn , spendable_outputs , ( commitment_txid, watch_outputs ) )
1661
1696
}
1662
1697
1663
1698
/// Generate a spendable output event when closing_transaction get registered onchain.
0 commit comments