2121//! ChannelMonitors to get out of the HSM and onto monitoring devices.
2222
2323use bitcoin:: blockdata:: block:: BlockHeader ;
24- use bitcoin:: blockdata:: transaction:: { TxOut , Transaction } ;
25- use bitcoin:: blockdata:: transaction:: OutPoint as BitcoinOutPoint ;
24+ use bitcoin:: blockdata:: transaction:: { OutPoint as BitcoinOutPoint , TxOut , Transaction } ;
2625use bitcoin:: blockdata:: script:: { Script , Builder } ;
2726use bitcoin:: blockdata:: opcodes;
2827
@@ -44,13 +43,17 @@ use chain::{BestBlock, WatchedOutput};
4443use chain:: chaininterface:: { BroadcasterInterface , FeeEstimator , LowerBoundedFeeEstimator } ;
4544use chain:: transaction:: { OutPoint , TransactionData } ;
4645use chain:: keysinterface:: { SpendableOutputDescriptor , StaticPaymentOutputDescriptor , DelayedPaymentOutputDescriptor , Sign , KeysInterface } ;
46+ #[ cfg( feature = "anchors" ) ]
47+ use chain:: onchaintx:: ClaimEvent ;
4748use chain:: onchaintx:: OnchainTxHandler ;
4849use chain:: package:: { CounterpartyOfferedHTLCOutput , CounterpartyReceivedHTLCOutput , HolderFundingOutput , HolderHTLCOutput , PackageSolvingData , PackageTemplate , RevokedOutput , RevokedHTLCOutput } ;
4950use chain:: Filter ;
5051use util:: logger:: Logger ;
5152use util:: ser:: { Readable , ReadableArgs , MaybeReadable , Writer , Writeable , U48 , OptionDeserWrapper } ;
5253use util:: byte_utils;
5354use util:: events:: Event ;
55+ #[ cfg( feature = "anchors" ) ]
56+ use util:: events:: { AnchorDescriptor , BumpTransactionEvent } ;
5457
5558use prelude:: * ;
5659use core:: { cmp, mem} ;
@@ -263,6 +266,20 @@ impl_writeable_tlv_based!(HolderSignedTx, {
263266 ( 14 , htlc_outputs, vec_type)
264267} ) ;
265268
269+ #[ cfg( feature = "anchors" ) ]
270+ impl HolderSignedTx {
271+ fn non_dust_htlcs ( & self ) -> Vec < HTLCOutputInCommitment > {
272+ self . htlc_outputs . iter ( ) . filter_map ( |( htlc, _, _) | {
273+ if let Some ( _) = htlc. transaction_output_index {
274+ Some ( htlc. clone ( ) )
275+ } else {
276+ None
277+ }
278+ } )
279+ . collect ( )
280+ }
281+ }
282+
266283/// We use this to track static counterparty commitment transaction data and to generate any
267284/// justice or 2nd-stage preimage/timeout transactions.
268285#[ derive( PartialEq ) ]
@@ -1221,7 +1238,7 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
12211238 B :: Target : BroadcasterInterface ,
12221239 L :: Target : Logger ,
12231240 {
1224- self . inner . lock ( ) . unwrap ( ) . broadcast_latest_holder_commitment_txn ( broadcaster, logger)
1241+ self . inner . lock ( ) . unwrap ( ) . broadcast_latest_holder_commitment_txn ( broadcaster, logger) ;
12251242 }
12261243
12271244 /// Updates a ChannelMonitor on the basis of some new information provided by the Channel
@@ -2222,6 +2239,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
22222239 panic ! ( "Attempted to apply ChannelMonitorUpdates out of order, check the update_id before passing an update to update_monitor!" ) ;
22232240 }
22242241 let mut ret = Ok ( ( ) ) ;
2242+ let bounded_fee_estimator = LowerBoundedFeeEstimator :: new ( & * fee_estimator) ;
22252243 for update in updates. updates . iter ( ) {
22262244 match update {
22272245 ChannelMonitorUpdateStep :: LatestHolderCommitmentTXInfo { commitment_tx, htlc_outputs } => {
@@ -2239,7 +2257,6 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
22392257 } ,
22402258 ChannelMonitorUpdateStep :: PaymentPreimage { payment_preimage } => {
22412259 log_trace ! ( logger, "Updating ChannelMonitor with payment preimage" ) ;
2242- let bounded_fee_estimator = LowerBoundedFeeEstimator :: new ( & * fee_estimator) ;
22432260 self . provide_payment_preimage ( & PaymentHash ( Sha256 :: hash ( & payment_preimage. 0 [ ..] ) . into_inner ( ) ) , & payment_preimage, broadcaster, & bounded_fee_estimator, logger)
22442261 } ,
22452262 ChannelMonitorUpdateStep :: CommitmentSecret { idx, secret } => {
@@ -2255,6 +2272,25 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
22552272 self . lockdown_from_offchain = true ;
22562273 if * should_broadcast {
22572274 self . broadcast_latest_holder_commitment_txn ( broadcaster, logger) ;
2275+ // If the channel supports anchor outputs, we'll need to emit an external
2276+ // event to be consumed such that a child transaction is broadcast with a
2277+ // high enough feerate for the parent commitment transaction to confirm.
2278+ if self . onchain_tx_handler . opt_anchors ( ) {
2279+ let funding_output = HolderFundingOutput :: build (
2280+ self . funding_redeemscript . clone ( ) , self . channel_value_satoshis ,
2281+ self . onchain_tx_handler . opt_anchors ( ) ,
2282+ ) ;
2283+ let best_block_height = self . best_block . height ( ) ;
2284+ let commitment_package = PackageTemplate :: build_package (
2285+ self . funding_info . 0 . txid . clone ( ) , self . funding_info . 0 . index as u32 ,
2286+ PackageSolvingData :: HolderFundingOutput ( funding_output) ,
2287+ best_block_height, false , best_block_height,
2288+ ) ;
2289+ self . onchain_tx_handler . update_claims_view (
2290+ & [ ] , vec ! [ commitment_package] , best_block_height, best_block_height,
2291+ broadcaster, & bounded_fee_estimator, logger,
2292+ ) ;
2293+ }
22582294 } else if !self . holder_tx_signed {
22592295 log_error ! ( logger, "WARNING: You have a potentially-unsafe holder commitment transaction available to broadcast" ) ;
22602296 log_error ! ( logger, " in channel monitor for channel {}!" , log_bytes!( self . funding_info. 0 . to_channel_id( ) ) ) ;
@@ -2309,6 +2345,31 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
23092345 pub fn get_and_clear_pending_events ( & mut self ) -> Vec < Event > {
23102346 let mut ret = Vec :: new ( ) ;
23112347 mem:: swap ( & mut ret, & mut self . pending_events ) ;
2348+ #[ cfg( feature = "anchors" ) ]
2349+ for claim_event in self . onchain_tx_handler . get_and_clear_pending_claim_events ( ) . drain ( ..) {
2350+ match claim_event {
2351+ ClaimEvent :: BumpCommitment {
2352+ package_target_feerate_sat_per_1000_weight, commitment_tx, anchor_output_idx,
2353+ } => {
2354+ let commitment_txid = commitment_tx. txid ( ) ;
2355+ debug_assert_eq ! ( self . current_holder_commitment_tx. txid, commitment_txid) ;
2356+ let pending_htlcs = self . current_holder_commitment_tx . non_dust_htlcs ( ) ;
2357+ ret. push ( Event :: BumpTransaction ( BumpTransactionEvent :: ChannelClose {
2358+ package_target_feerate_sat_per_1000_weight,
2359+ commitment_tx,
2360+ anchor_descriptor : AnchorDescriptor {
2361+ channel_keys_id : self . channel_keys_id ,
2362+ channel_value_satoshis : self . channel_value_satoshis ,
2363+ outpoint : BitcoinOutPoint {
2364+ txid : commitment_txid,
2365+ vout : anchor_output_idx,
2366+ } ,
2367+ } ,
2368+ pending_htlcs,
2369+ } ) ) ;
2370+ } ,
2371+ }
2372+ }
23122373 ret
23132374 }
23142375
@@ -2890,15 +2951,20 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
28902951 self . pending_monitor_events . push ( MonitorEvent :: CommitmentTxConfirmed ( self . funding_info . 0 ) ) ;
28912952 let commitment_tx = self . onchain_tx_handler . get_fully_signed_holder_tx ( & self . funding_redeemscript ) ;
28922953 self . holder_tx_signed = true ;
2893- // Because we're broadcasting a commitment transaction, we should construct the package
2894- // assuming it gets confirmed in the next block. Sadly, we have code which considers
2895- // "not yet confirmed" things as discardable, so we cannot do that here.
2896- let ( mut new_outpoints, _) = self . get_broadcasted_holder_claims ( & self . current_holder_commitment_tx , self . best_block . height ( ) ) ;
2897- let new_outputs = self . get_broadcasted_holder_watch_outputs ( & self . current_holder_commitment_tx , & commitment_tx) ;
2898- if !new_outputs. is_empty ( ) {
2899- watch_outputs. push ( ( self . current_holder_commitment_tx . txid . clone ( ) , new_outputs) ) ;
2954+ // We can't broadcast our HTLC transactions while the commitment transaction is
2955+ // unconfirmed. We'll delay doing so until we detect the confirmed commitment in
2956+ // `transactions_confirmed`.
2957+ if !self . onchain_tx_handler . opt_anchors ( ) {
2958+ // Because we're broadcasting a commitment transaction, we should construct the package
2959+ // assuming it gets confirmed in the next block. Sadly, we have code which considers
2960+ // "not yet confirmed" things as discardable, so we cannot do that here.
2961+ let ( mut new_outpoints, _) = self . get_broadcasted_holder_claims ( & self . current_holder_commitment_tx , self . best_block . height ( ) ) ;
2962+ let new_outputs = self . get_broadcasted_holder_watch_outputs ( & self . current_holder_commitment_tx , & commitment_tx) ;
2963+ if !new_outputs. is_empty ( ) {
2964+ watch_outputs. push ( ( self . current_holder_commitment_tx . txid . clone ( ) , new_outputs) ) ;
2965+ }
2966+ claimable_outpoints. append ( & mut new_outpoints) ;
29002967 }
2901- claimable_outpoints. append ( & mut new_outpoints) ;
29022968 }
29032969
29042970 // Find which on-chain events have reached their confirmation threshold.
0 commit comments