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,13 @@ use chain::{BestBlock, WatchedOutput};
4443use chain:: chaininterface:: { BroadcasterInterface , FeeEstimator , LowerBoundedFeeEstimator } ;
4544use chain:: transaction:: { OutPoint , TransactionData } ;
4645use chain:: keysinterface:: { SpendableOutputDescriptor , StaticPaymentOutputDescriptor , DelayedPaymentOutputDescriptor , Sign , KeysInterface } ;
47- use chain:: onchaintx:: OnchainTxHandler ;
46+ use chain:: onchaintx:: { ClaimEvent , OnchainTxHandler } ;
4847use chain:: package:: { CounterpartyOfferedHTLCOutput , CounterpartyReceivedHTLCOutput , HolderFundingOutput , HolderHTLCOutput , PackageSolvingData , PackageTemplate , RevokedOutput , RevokedHTLCOutput } ;
4948use chain:: Filter ;
5049use util:: logger:: Logger ;
5150use util:: ser:: { Readable , ReadableArgs , MaybeReadable , Writer , Writeable , U48 , OptionDeserWrapper } ;
5251use util:: byte_utils;
53- use util:: events:: Event ;
52+ use util:: events:: { AnchorDescriptor , CommitmentPendingHTLC , BumpTransaction , Event } ;
5453
5554use prelude:: * ;
5655use core:: { cmp, mem} ;
@@ -1219,7 +1218,7 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
12191218 B :: Target : BroadcasterInterface ,
12201219 L :: Target : Logger ,
12211220 {
1222- self . inner . lock ( ) . unwrap ( ) . broadcast_latest_holder_commitment_txn ( broadcaster, logger)
1221+ self . inner . lock ( ) . unwrap ( ) . broadcast_latest_holder_commitment_txn ( broadcaster, logger) ;
12231222 }
12241223
12251224 /// Updates a ChannelMonitor on the basis of some new information provided by the Channel
@@ -2190,6 +2189,41 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
21902189 self . pending_monitor_events . push ( MonitorEvent :: CommitmentTxConfirmed ( self . funding_info . 0 ) ) ;
21912190 }
21922191
2192+ fn construct_bump_channel_close_event (
2193+ & self , target_feerate_sat_per_1000_weight : u32 , commitment_tx : Transaction , anchor_output_idx : u32 ,
2194+ ) -> Event {
2195+ let commitment_txid = commitment_tx. txid ( ) ;
2196+ debug_assert_eq ! ( self . current_holder_commitment_tx. txid, commitment_txid) ;
2197+ let pending_htlcs = self . current_holder_commitment_tx . htlc_outputs . iter ( )
2198+ . filter_map ( |( htlc, _, _) | {
2199+ if let Some ( _) = htlc. transaction_output_index {
2200+ Some ( htlc)
2201+ } else {
2202+ None
2203+ }
2204+ } )
2205+ . map ( |htlc| CommitmentPendingHTLC {
2206+ outbound : htlc. offered ,
2207+ amount_sat : htlc. amount_msat / 1000 ,
2208+ expiry : htlc. cltv_expiry ,
2209+ output_index : htlc. transaction_output_index . unwrap ( ) ,
2210+ } )
2211+ . collect ( ) ;
2212+ Event :: BumpTransaction ( BumpTransaction :: ChannelClose {
2213+ target_feerate_sat_per_1000_weight,
2214+ commitment_tx,
2215+ anchor_descriptor : AnchorDescriptor {
2216+ channel_keys_id : self . channel_keys_id ,
2217+ channel_value_satoshis : self . channel_value_satoshis ,
2218+ outpoint : BitcoinOutPoint {
2219+ txid : commitment_txid,
2220+ vout : anchor_output_idx,
2221+ } ,
2222+ } ,
2223+ pending_htlcs,
2224+ } )
2225+ }
2226+
21932227 pub fn update_monitor < B : Deref , F : Deref , L : Deref > ( & mut self , updates : & ChannelMonitorUpdate , broadcaster : & B , fee_estimator : F , logger : & L ) -> Result < ( ) , ( ) >
21942228 where B :: Target : BroadcasterInterface ,
21952229 F :: Target : FeeEstimator ,
@@ -2214,6 +2248,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
22142248 panic ! ( "Attempted to apply ChannelMonitorUpdates out of order, check the update_id before passing an update to update_monitor!" ) ;
22152249 }
22162250 let mut ret = Ok ( ( ) ) ;
2251+ let bounded_fee_estimator = LowerBoundedFeeEstimator :: new ( & * fee_estimator) ;
22172252 for update in updates. updates . iter ( ) {
22182253 match update {
22192254 ChannelMonitorUpdateStep :: LatestHolderCommitmentTXInfo { commitment_tx, htlc_outputs } => {
@@ -2231,7 +2266,6 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
22312266 } ,
22322267 ChannelMonitorUpdateStep :: PaymentPreimage { payment_preimage } => {
22332268 log_trace ! ( logger, "Updating ChannelMonitor with payment preimage" ) ;
2234- let bounded_fee_estimator = LowerBoundedFeeEstimator :: new ( & * fee_estimator) ;
22352269 self . provide_payment_preimage ( & PaymentHash ( Sha256 :: hash ( & payment_preimage. 0 [ ..] ) . into_inner ( ) ) , & payment_preimage, broadcaster, & bounded_fee_estimator, logger)
22362270 } ,
22372271 ChannelMonitorUpdateStep :: CommitmentSecret { idx, secret } => {
@@ -2247,6 +2281,25 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
22472281 self . lockdown_from_offchain = true ;
22482282 if * should_broadcast {
22492283 self . broadcast_latest_holder_commitment_txn ( broadcaster, logger) ;
2284+ // If the channel supports anchor outputs, we'll need to emit an external
2285+ // event to be consumed such that a child transaction is broadcast with a
2286+ // high enough feerate for the parent commitment transaction to confirm.
2287+ if self . onchain_tx_handler . opt_anchors ( ) {
2288+ let funding_output = HolderFundingOutput :: build (
2289+ self . funding_redeemscript . clone ( ) , self . channel_value_satoshis ,
2290+ self . onchain_tx_handler . opt_anchors ( ) ,
2291+ ) ;
2292+ let best_block_height = self . best_block . height ( ) ;
2293+ let commitment_package = PackageTemplate :: build_package (
2294+ self . funding_info . 0 . txid . clone ( ) , self . funding_info . 0 . index as u32 ,
2295+ PackageSolvingData :: HolderFundingOutput ( funding_output) ,
2296+ best_block_height, false , best_block_height,
2297+ ) ;
2298+ self . onchain_tx_handler . update_claims_view (
2299+ & [ ] , vec ! [ commitment_package] , best_block_height, best_block_height,
2300+ broadcaster, & bounded_fee_estimator, logger,
2301+ ) ;
2302+ }
22502303 } else if !self . holder_tx_signed {
22512304 log_error ! ( logger, "You have a toxic holder commitment transaction avaible in channel monitor, read comment in ChannelMonitor::get_latest_holder_commitment_txn to be informed of manual action to take" ) ;
22522305 } else {
@@ -2299,6 +2352,17 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
22992352 pub fn get_and_clear_pending_events ( & mut self ) -> Vec < Event > {
23002353 let mut ret = Vec :: new ( ) ;
23012354 mem:: swap ( & mut ret, & mut self . pending_events ) ;
2355+ for claim_event in self . onchain_tx_handler . get_and_clear_pending_claim_events ( ) . drain ( ..) {
2356+ match claim_event {
2357+ ClaimEvent :: BumpCommitment {
2358+ target_feerate_sat_per_1000_weight, commitment_tx, anchor_output_idx,
2359+ } => {
2360+ ret. push ( self . construct_bump_channel_close_event (
2361+ target_feerate_sat_per_1000_weight, commitment_tx, anchor_output_idx,
2362+ ) ) ;
2363+ } ,
2364+ }
2365+ }
23022366 ret
23032367 }
23042368
@@ -2880,15 +2944,20 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
28802944 self . pending_monitor_events . push ( MonitorEvent :: CommitmentTxConfirmed ( self . funding_info . 0 ) ) ;
28812945 let commitment_tx = self . onchain_tx_handler . get_fully_signed_holder_tx ( & self . funding_redeemscript ) ;
28822946 self . holder_tx_signed = true ;
2883- // Because we're broadcasting a commitment transaction, we should construct the package
2884- // assuming it gets confirmed in the next block. Sadly, we have code which considers
2885- // "not yet confirmed" things as discardable, so we cannot do that here.
2886- let ( mut new_outpoints, _) = self . get_broadcasted_holder_claims ( & self . current_holder_commitment_tx , self . best_block . height ( ) ) ;
2887- let new_outputs = self . get_broadcasted_holder_watch_outputs ( & self . current_holder_commitment_tx , & commitment_tx) ;
2888- if !new_outputs. is_empty ( ) {
2889- watch_outputs. push ( ( self . current_holder_commitment_tx . txid . clone ( ) , new_outputs) ) ;
2947+ // We can't broadcast our HTLC transactions while the commitment transaction is
2948+ // unconfirmed. We'll delay doing so until we detect the confirmed commitment in
2949+ // `transactions_confirmed`.
2950+ if !self . onchain_tx_handler . opt_anchors ( ) {
2951+ // Because we're broadcasting a commitment transaction, we should construct the package
2952+ // assuming it gets confirmed in the next block. Sadly, we have code which considers
2953+ // "not yet confirmed" things as discardable, so we cannot do that here.
2954+ let ( mut new_outpoints, _) = self . get_broadcasted_holder_claims ( & self . current_holder_commitment_tx , self . best_block . height ( ) ) ;
2955+ let new_outputs = self . get_broadcasted_holder_watch_outputs ( & self . current_holder_commitment_tx , & commitment_tx) ;
2956+ if !new_outputs. is_empty ( ) {
2957+ watch_outputs. push ( ( self . current_holder_commitment_tx . txid . clone ( ) , new_outputs) ) ;
2958+ }
2959+ claimable_outpoints. append ( & mut new_outpoints) ;
28902960 }
2891- claimable_outpoints. append ( & mut new_outpoints) ;
28922961 }
28932962
28942963 // Find which on-chain events have reached their confirmation threshold.
0 commit comments