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