@@ -1509,8 +1509,8 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
15091509 fn provide_latest_holder_commitment_tx (
15101510 & self , holder_commitment_tx : HolderCommitmentTransaction ,
15111511 htlc_outputs : Vec < ( HTLCOutputInCommitment , Option < Signature > , Option < HTLCSource > ) > ,
1512- ) -> Result < ( ) , ( ) > {
1513- self . inner . lock ( ) . unwrap ( ) . provide_latest_holder_commitment_tx ( holder_commitment_tx, htlc_outputs, & Vec :: new ( ) , Vec :: new ( ) ) . map_err ( |_| ( ) )
1512+ ) {
1513+ self . inner . lock ( ) . unwrap ( ) . provide_latest_holder_commitment_tx ( holder_commitment_tx, htlc_outputs, & Vec :: new ( ) , Vec :: new ( ) )
15141514 }
15151515
15161516 /// This is used to provide payment preimage(s) out-of-band during startup without updating the
@@ -1737,10 +1737,14 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
17371737 self . inner . lock ( ) . unwrap ( ) . get_cur_holder_commitment_number ( )
17381738 }
17391739
1740- /// Gets whether we've been notified that this channel is closed by the `ChannelManager` (i.e.
1741- /// via a [`ChannelMonitorUpdateStep::ChannelForceClosed`]).
1742- pub ( crate ) fn offchain_closed ( & self ) -> bool {
1743- self . inner . lock ( ) . unwrap ( ) . lockdown_from_offchain
1740+ /// Fetches whether this monitor has marked the channel as closed and will refuse any further
1741+ /// updates to the commitment transactions.
1742+ ///
1743+ /// It can be marked closed in a few different ways, including via a
1744+ /// [`ChannelMonitorUpdateStep::ChannelForceClosed`] or if the channel has been closed
1745+ /// on-chain.
1746+ pub ( crate ) fn no_further_updates_allowed ( & self ) -> bool {
1747+ self . inner . lock ( ) . unwrap ( ) . no_further_updates_allowed ( )
17441748 }
17451749
17461750 /// Gets the `node_id` of the counterparty for this channel.
@@ -2901,7 +2905,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
29012905 /// is important that any clones of this channel monitor (including remote clones) by kept
29022906 /// up-to-date as our holder commitment transaction is updated.
29032907 /// Panics if set_on_holder_tx_csv has never been called.
2904- fn provide_latest_holder_commitment_tx ( & mut self , holder_commitment_tx : HolderCommitmentTransaction , mut htlc_outputs : Vec < ( HTLCOutputInCommitment , Option < Signature > , Option < HTLCSource > ) > , claimed_htlcs : & [ ( SentHTLCId , PaymentPreimage ) ] , nondust_htlc_sources : Vec < HTLCSource > ) -> Result < ( ) , & ' static str > {
2908+ fn provide_latest_holder_commitment_tx ( & mut self , holder_commitment_tx : HolderCommitmentTransaction , mut htlc_outputs : Vec < ( HTLCOutputInCommitment , Option < Signature > , Option < HTLCSource > ) > , claimed_htlcs : & [ ( SentHTLCId , PaymentPreimage ) ] , nondust_htlc_sources : Vec < HTLCSource > ) {
29052909 if htlc_outputs. iter ( ) . any ( |( _, s, _) | s. is_some ( ) ) {
29062910 // If we have non-dust HTLCs in htlc_outputs, ensure they match the HTLCs in the
29072911 // `holder_commitment_tx`. In the future, we'll no longer provide the redundant data
@@ -2978,10 +2982,6 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
29782982 }
29792983 self . counterparty_fulfilled_htlcs . insert ( * claimed_htlc_id, * claimed_preimage) ;
29802984 }
2981- if self . holder_tx_signed {
2982- return Err ( "Latest holder commitment signed has already been signed, update is rejected" ) ;
2983- }
2984- Ok ( ( ) )
29852985 }
29862986
29872987 /// Provides a payment_hash->payment_preimage mapping. Will be automatically pruned when all
@@ -3202,11 +3202,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
32023202 ChannelMonitorUpdateStep :: LatestHolderCommitmentTXInfo { commitment_tx, htlc_outputs, claimed_htlcs, nondust_htlc_sources } => {
32033203 log_trace ! ( logger, "Updating ChannelMonitor with latest holder commitment transaction info" ) ;
32043204 if self . lockdown_from_offchain { panic ! ( ) ; }
3205- if let Err ( e) = self . provide_latest_holder_commitment_tx ( commitment_tx. clone ( ) , htlc_outputs. clone ( ) , & claimed_htlcs, nondust_htlc_sources. clone ( ) ) {
3206- log_error ! ( logger, "Providing latest holder commitment transaction failed/was refused:" ) ;
3207- log_error ! ( logger, " {}" , e) ;
3208- ret = Err ( ( ) ) ;
3209- }
3205+ self . provide_latest_holder_commitment_tx ( commitment_tx. clone ( ) , htlc_outputs. clone ( ) , & claimed_htlcs, nondust_htlc_sources. clone ( ) ) ;
32103206 }
32113207 ChannelMonitorUpdateStep :: LatestCounterpartyCommitmentTXInfo { commitment_txid, htlc_outputs, commitment_number, their_per_commitment_point, .. } => {
32123208 log_trace ! ( logger, "Updating ChannelMonitor with latest counterparty commitment transaction info" ) ;
@@ -3286,12 +3282,16 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
32863282 }
32873283 }
32883284
3289- if ret. is_ok ( ) && ( self . funding_spend_seen || self . lockdown_from_offchain || self . holder_tx_signed ) && is_pre_close_update {
3285+ if ret. is_ok ( ) && self . no_further_updates_allowed ( ) && is_pre_close_update {
32903286 log_error ! ( logger, "Refusing Channel Monitor Update as counterparty attempted to update commitment after funding was spent" ) ;
32913287 Err ( ( ) )
32923288 } else { ret }
32933289 }
32943290
3291+ fn no_further_updates_allowed ( & self ) -> bool {
3292+ self . funding_spend_seen || self . lockdown_from_offchain || self . holder_tx_signed
3293+ }
3294+
32953295 fn get_latest_update_id ( & self ) -> u64 {
32963296 self . latest_update_id
32973297 }
@@ -3564,11 +3564,16 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
35643564 return ( claimable_outpoints, to_counterparty_output_info) ;
35653565 }
35663566 let revk_htlc_outp = RevokedHTLCOutput :: build ( per_commitment_point, self . counterparty_commitment_params . counterparty_delayed_payment_base_key , self . counterparty_commitment_params . counterparty_htlc_base_key , per_commitment_key, htlc. amount_msat / 1000 , htlc. clone ( ) , & self . onchain_tx_handler . channel_transaction_parameters . channel_type_features ) ;
3567+ let counterparty_spendable_height = if htlc. offered {
3568+ htlc. cltv_expiry
3569+ } else {
3570+ height
3571+ } ;
35673572 let justice_package = PackageTemplate :: build_package (
35683573 commitment_txid,
35693574 transaction_output_index,
35703575 PackageSolvingData :: RevokedHTLCOutput ( revk_htlc_outp) ,
3571- htlc . cltv_expiry ,
3576+ counterparty_spendable_height ,
35723577 ) ;
35733578 claimable_outpoints. push ( justice_package) ;
35743579 }
@@ -3869,35 +3874,32 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
38693874 }
38703875 }
38713876 }
3872- if self . holder_tx_signed {
3873- // If we've signed, we may have broadcast either commitment (prev or current), and
3874- // attempted to claim from it immediately without waiting for a confirmation.
3875- if self . current_holder_commitment_tx . txid != * confirmed_commitment_txid {
3877+ // Cancel any pending claims for any holder commitments in case they had previously
3878+ // confirmed or been signed (in which case we will start attempting to claim without
3879+ // waiting for confirmation).
3880+ if self . current_holder_commitment_tx . txid != * confirmed_commitment_txid {
3881+ log_trace ! ( logger, "Canceling claims for previously broadcast holder commitment {}" ,
3882+ self . current_holder_commitment_tx. txid) ;
3883+ let mut outpoint = BitcoinOutPoint { txid : self . current_holder_commitment_tx . txid , vout : 0 } ;
3884+ for ( htlc, _, _) in & self . current_holder_commitment_tx . htlc_outputs {
3885+ if let Some ( vout) = htlc. transaction_output_index {
3886+ outpoint. vout = vout;
3887+ self . onchain_tx_handler . abandon_claim ( & outpoint) ;
3888+ }
3889+ }
3890+ }
3891+ if let Some ( prev_holder_commitment_tx) = & self . prev_holder_signed_commitment_tx {
3892+ if prev_holder_commitment_tx. txid != * confirmed_commitment_txid {
38763893 log_trace ! ( logger, "Canceling claims for previously broadcast holder commitment {}" ,
3877- self . current_holder_commitment_tx . txid) ;
3878- let mut outpoint = BitcoinOutPoint { txid : self . current_holder_commitment_tx . txid , vout : 0 } ;
3879- for ( htlc, _, _) in & self . current_holder_commitment_tx . htlc_outputs {
3894+ prev_holder_commitment_tx . txid) ;
3895+ let mut outpoint = BitcoinOutPoint { txid : prev_holder_commitment_tx . txid , vout : 0 } ;
3896+ for ( htlc, _, _) in & prev_holder_commitment_tx . htlc_outputs {
38803897 if let Some ( vout) = htlc. transaction_output_index {
38813898 outpoint. vout = vout;
38823899 self . onchain_tx_handler . abandon_claim ( & outpoint) ;
38833900 }
38843901 }
38853902 }
3886- if let Some ( prev_holder_commitment_tx) = & self . prev_holder_signed_commitment_tx {
3887- if prev_holder_commitment_tx. txid != * confirmed_commitment_txid {
3888- log_trace ! ( logger, "Canceling claims for previously broadcast holder commitment {}" ,
3889- prev_holder_commitment_tx. txid) ;
3890- let mut outpoint = BitcoinOutPoint { txid : prev_holder_commitment_tx. txid , vout : 0 } ;
3891- for ( htlc, _, _) in & prev_holder_commitment_tx. htlc_outputs {
3892- if let Some ( vout) = htlc. transaction_output_index {
3893- outpoint. vout = vout;
3894- self . onchain_tx_handler . abandon_claim ( & outpoint) ;
3895- }
3896- }
3897- }
3898- }
3899- } else {
3900- // No previous claim.
39013903 }
39023904 }
39033905
@@ -4233,7 +4235,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
42334235 }
42344236 }
42354237
4236- if self . lockdown_from_offchain || self . funding_spend_seen || self . holder_tx_signed {
4238+ if self . no_further_updates_allowed ( ) {
42374239 // Fail back HTLCs on backwards channels if they expire within
42384240 // `LATENCY_GRACE_PERIOD_BLOCKS` blocks and the channel is closed (i.e. we're at a
42394241 // point where no further off-chain updates will be accepted). If we haven't seen the
@@ -5384,7 +5386,7 @@ mod tests {
53845386 let dummy_commitment_tx = HolderCommitmentTransaction :: dummy ( & mut htlcs) ;
53855387
53865388 monitor. provide_latest_holder_commitment_tx ( dummy_commitment_tx. clone ( ) ,
5387- htlcs. into_iter ( ) . map ( |( htlc, _) | ( htlc, Some ( dummy_sig) , None ) ) . collect ( ) ) . unwrap ( ) ;
5389+ htlcs. into_iter ( ) . map ( |( htlc, _) | ( htlc, Some ( dummy_sig) , None ) ) . collect ( ) ) ;
53885390 monitor. provide_latest_counterparty_commitment_tx ( Txid :: from_byte_array ( Sha256 :: hash ( b"1" ) . to_byte_array ( ) ) ,
53895391 preimages_slice_to_htlc_outputs ! ( preimages[ 5 ..15 ] ) , 281474976710655 , dummy_key, & logger) ;
53905392 monitor. provide_latest_counterparty_commitment_tx ( Txid :: from_byte_array ( Sha256 :: hash ( b"2" ) . to_byte_array ( ) ) ,
@@ -5422,7 +5424,7 @@ mod tests {
54225424 let mut htlcs = preimages_slice_to_htlcs ! ( preimages[ 0 ..5 ] ) ;
54235425 let dummy_commitment_tx = HolderCommitmentTransaction :: dummy ( & mut htlcs) ;
54245426 monitor. provide_latest_holder_commitment_tx ( dummy_commitment_tx. clone ( ) ,
5425- htlcs. into_iter ( ) . map ( |( htlc, _) | ( htlc, Some ( dummy_sig) , None ) ) . collect ( ) ) . unwrap ( ) ;
5427+ htlcs. into_iter ( ) . map ( |( htlc, _) | ( htlc, Some ( dummy_sig) , None ) ) . collect ( ) ) ;
54265428 secret[ 0 ..32 ] . clone_from_slice ( & <Vec < u8 > >:: from_hex ( "2273e227a5b7449b6e70f1fb4652864038b1cbf9cd7c043a7d6456b7fc275ad8" ) . unwrap ( ) ) ;
54275429 monitor. provide_secret ( 281474976710653 , secret. clone ( ) ) . unwrap ( ) ;
54285430 assert_eq ! ( monitor. inner. lock( ) . unwrap( ) . payment_preimages. len( ) , 12 ) ;
@@ -5433,7 +5435,7 @@ mod tests {
54335435 let mut htlcs = preimages_slice_to_htlcs ! ( preimages[ 0 ..3 ] ) ;
54345436 let dummy_commitment_tx = HolderCommitmentTransaction :: dummy ( & mut htlcs) ;
54355437 monitor. provide_latest_holder_commitment_tx ( dummy_commitment_tx,
5436- htlcs. into_iter ( ) . map ( |( htlc, _) | ( htlc, Some ( dummy_sig) , None ) ) . collect ( ) ) . unwrap ( ) ;
5438+ htlcs. into_iter ( ) . map ( |( htlc, _) | ( htlc, Some ( dummy_sig) , None ) ) . collect ( ) ) ;
54375439 secret[ 0 ..32 ] . clone_from_slice ( & <Vec < u8 > >:: from_hex ( "27cddaa5624534cb6cb9d7da077cf2b22ab21e9b506fd4998a51d54502e99116" ) . unwrap ( ) ) ;
54385440 monitor. provide_secret ( 281474976710652 , secret. clone ( ) ) . unwrap ( ) ;
54395441 assert_eq ! ( monitor. inner. lock( ) . unwrap( ) . payment_preimages. len( ) , 5 ) ;
0 commit comments