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