@@ -1525,9 +1525,11 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1525
1525
( claimable_outpoints, Some ( ( htlc_txid, outputs) ) )
1526
1526
}
1527
1527
1528
- fn broadcast_by_holder_state ( & self , commitment_tx : & Transaction , holder_tx : & HolderSignedTx ) -> ( Vec < ClaimRequest > , Vec < ( u32 , TxOut ) > , Option < ( Script , PublicKey , PublicKey ) > ) {
1528
+ // Returns (1) `ClaimRequest`s that can be given to the OnChainTxHandler, so that the handler can
1529
+ // broadcast transactions claiming holder HTLC commitment outputs and (2) a holder revokable
1530
+ // script so we can detect whether a holder transaction has been seen on-chain.
1531
+ fn get_broadcasted_holder_claims ( & self , holder_tx : & HolderSignedTx ) -> ( Vec < ClaimRequest > , Option < ( Script , PublicKey , PublicKey ) > ) {
1529
1532
let mut claim_requests = Vec :: with_capacity ( holder_tx. htlc_outputs . len ( ) ) ;
1530
- let mut watch_outputs = Vec :: with_capacity ( holder_tx. htlc_outputs . len ( ) ) ;
1531
1533
1532
1534
let redeemscript = chan_utils:: get_revokeable_redeemscript ( & holder_tx. revocation_key , self . on_holder_tx_csv , & holder_tx. delayed_payment_key ) ;
1533
1535
let broadcasted_holder_revokable_script = Some ( ( redeemscript. to_v0_p2wsh ( ) , holder_tx. per_commitment_point . clone ( ) , holder_tx. revocation_key . clone ( ) ) ) ;
@@ -1546,11 +1548,21 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1546
1548
} else { None } ,
1547
1549
amount : htlc. amount_msat ,
1548
1550
} } ) ;
1549
- watch_outputs. push ( ( transaction_output_index, commitment_tx. output [ transaction_output_index as usize ] . clone ( ) ) ) ;
1550
1551
}
1551
1552
}
1552
1553
1553
- ( claim_requests, watch_outputs, broadcasted_holder_revokable_script)
1554
+ ( claim_requests, broadcasted_holder_revokable_script)
1555
+ }
1556
+
1557
+ // Returns holder HTLC outputs to watch and react to in case of spending.
1558
+ fn get_broadcasted_holder_watch_outputs ( & self , holder_tx : & HolderSignedTx , commitment_tx : & Transaction ) -> Vec < ( u32 , TxOut ) > {
1559
+ let mut watch_outputs = Vec :: with_capacity ( holder_tx. htlc_outputs . len ( ) ) ;
1560
+ for & ( ref htlc, _, _) in holder_tx. htlc_outputs . iter ( ) {
1561
+ if let Some ( transaction_output_index) = htlc. transaction_output_index {
1562
+ watch_outputs. push ( ( transaction_output_index, commitment_tx. output [ transaction_output_index as usize ] . clone ( ) ) ) ;
1563
+ }
1564
+ }
1565
+ watch_outputs
1554
1566
}
1555
1567
1556
1568
/// Attempts to claim any claimable HTLCs in a commitment transaction which was not (yet)
@@ -1585,10 +1597,10 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1585
1597
}
1586
1598
1587
1599
macro_rules! append_onchain_update {
1588
- ( $updates: expr) => {
1600
+ ( $updates: expr, $to_watch : expr ) => {
1589
1601
claim_requests = $updates. 0 ;
1590
- watch_outputs . append ( & mut $updates. 1 ) ;
1591
- self . broadcasted_holder_revokable_script = $updates . 2 ;
1602
+ self . broadcasted_holder_revokable_script = $updates. 1 ;
1603
+ watch_outputs . append ( & mut $to_watch ) ;
1592
1604
}
1593
1605
}
1594
1606
@@ -1598,14 +1610,16 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1598
1610
if self . current_holder_commitment_tx . txid == commitment_txid {
1599
1611
is_holder_tx = true ;
1600
1612
log_trace ! ( logger, "Got latest holder commitment tx broadcast, searching for available HTLCs to claim" ) ;
1601
- let mut res = self . broadcast_by_holder_state ( tx, & self . current_holder_commitment_tx ) ;
1602
- append_onchain_update ! ( res) ;
1613
+ let res = self . get_broadcasted_holder_claims ( & self . current_holder_commitment_tx ) ;
1614
+ let mut to_watch = self . get_broadcasted_holder_watch_outputs ( & self . current_holder_commitment_tx , tx) ;
1615
+ append_onchain_update ! ( res, to_watch) ;
1603
1616
} else if let & Some ( ref holder_tx) = & self . prev_holder_signed_commitment_tx {
1604
1617
if holder_tx. txid == commitment_txid {
1605
1618
is_holder_tx = true ;
1606
1619
log_trace ! ( logger, "Got previous holder commitment tx broadcast, searching for available HTLCs to claim" ) ;
1607
- let mut res = self . broadcast_by_holder_state ( tx, holder_tx) ;
1608
- append_onchain_update ! ( res) ;
1620
+ let res = self . get_broadcasted_holder_claims ( holder_tx) ;
1621
+ let mut to_watch = self . get_broadcasted_holder_watch_outputs ( holder_tx, tx) ;
1622
+ append_onchain_update ! ( res, to_watch) ;
1609
1623
}
1610
1624
}
1611
1625
@@ -1773,7 +1787,8 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1773
1787
self . pending_monitor_events . push ( MonitorEvent :: CommitmentTxBroadcasted ( self . funding_info . 0 ) ) ;
1774
1788
if let Some ( commitment_tx) = self . onchain_tx_handler . get_fully_signed_holder_tx ( & self . funding_redeemscript ) {
1775
1789
self . holder_tx_signed = true ;
1776
- let ( mut new_outpoints, new_outputs, _) = self . broadcast_by_holder_state ( & commitment_tx, & self . current_holder_commitment_tx ) ;
1790
+ let ( mut new_outpoints, _) = self . get_broadcasted_holder_claims ( & self . current_holder_commitment_tx ) ;
1791
+ let new_outputs = self . get_broadcasted_holder_watch_outputs ( & self . current_holder_commitment_tx , & commitment_tx) ;
1777
1792
if !new_outputs. is_empty ( ) {
1778
1793
watch_outputs. push ( ( self . current_holder_commitment_tx . txid . clone ( ) , new_outputs) ) ;
1779
1794
}
0 commit comments