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