@@ -1536,8 +1536,8 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
1536
1536
fn provide_latest_holder_commitment_tx (
1537
1537
& self , holder_commitment_tx : HolderCommitmentTransaction ,
1538
1538
htlc_outputs : Vec < ( HTLCOutputInCommitment , Option < Signature > , Option < HTLCSource > ) > ,
1539
- ) -> Result < ( ) , ( ) > {
1540
- self . inner . lock ( ) . unwrap ( ) . provide_latest_holder_commitment_tx ( holder_commitment_tx, htlc_outputs, & Vec :: new ( ) , Vec :: new ( ) ) . map_err ( |_| ( ) )
1539
+ ) {
1540
+ self . inner . lock ( ) . unwrap ( ) . provide_latest_holder_commitment_tx ( holder_commitment_tx, htlc_outputs, & Vec :: new ( ) , Vec :: new ( ) )
1541
1541
}
1542
1542
1543
1543
/// This is used to provide payment preimage(s) out-of-band during startup without updating the
@@ -1774,10 +1774,14 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
1774
1774
self . inner . lock ( ) . unwrap ( ) . get_cur_holder_commitment_number ( )
1775
1775
}
1776
1776
1777
- /// Gets whether we've been notified that this channel is closed by the `ChannelManager` (i.e.
1778
- /// via a [`ChannelMonitorUpdateStep::ChannelForceClosed`]).
1779
- pub ( crate ) fn offchain_closed ( & self ) -> bool {
1780
- self . inner . lock ( ) . unwrap ( ) . lockdown_from_offchain
1777
+ /// Fetches whether this monitor has marked the channel as closed and will refuse any further
1778
+ /// updates to the commitment transactions.
1779
+ ///
1780
+ /// It can be marked closed in a few different ways, including via a
1781
+ /// [`ChannelMonitorUpdateStep::ChannelForceClosed`] or if the channel has been closed
1782
+ /// on-chain.
1783
+ pub ( crate ) fn no_further_updates_allowed ( & self ) -> bool {
1784
+ self . inner . lock ( ) . unwrap ( ) . no_further_updates_allowed ( )
1781
1785
}
1782
1786
1783
1787
/// Gets the `node_id` of the counterparty for this channel.
@@ -2938,7 +2942,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
2938
2942
/// is important that any clones of this channel monitor (including remote clones) by kept
2939
2943
/// up-to-date as our holder commitment transaction is updated.
2940
2944
/// Panics if set_on_holder_tx_csv has never been called.
2941
- 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 > {
2945
+ 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 > ) {
2942
2946
if htlc_outputs. iter ( ) . any ( |( _, s, _) | s. is_some ( ) ) {
2943
2947
// If we have non-dust HTLCs in htlc_outputs, ensure they match the HTLCs in the
2944
2948
// `holder_commitment_tx`. In the future, we'll no longer provide the redundant data
@@ -3015,10 +3019,6 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
3015
3019
}
3016
3020
self . counterparty_fulfilled_htlcs . insert ( * claimed_htlc_id, * claimed_preimage) ;
3017
3021
}
3018
- if self . holder_tx_signed {
3019
- return Err ( "Latest holder commitment signed has already been signed, update is rejected" ) ;
3020
- }
3021
- Ok ( ( ) )
3022
3022
}
3023
3023
3024
3024
/// Provides a payment_hash->payment_preimage mapping. Will be automatically pruned when all
@@ -3239,11 +3239,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
3239
3239
ChannelMonitorUpdateStep :: LatestHolderCommitmentTXInfo { commitment_tx, htlc_outputs, claimed_htlcs, nondust_htlc_sources } => {
3240
3240
log_trace ! ( logger, "Updating ChannelMonitor with latest holder commitment transaction info" ) ;
3241
3241
if self . lockdown_from_offchain { panic ! ( ) ; }
3242
- if let Err ( e) = self . provide_latest_holder_commitment_tx ( commitment_tx. clone ( ) , htlc_outputs. clone ( ) , & claimed_htlcs, nondust_htlc_sources. clone ( ) ) {
3243
- log_error ! ( logger, "Providing latest holder commitment transaction failed/was refused:" ) ;
3244
- log_error ! ( logger, " {}" , e) ;
3245
- ret = Err ( ( ) ) ;
3246
- }
3242
+ self . provide_latest_holder_commitment_tx ( commitment_tx. clone ( ) , htlc_outputs. clone ( ) , & claimed_htlcs, nondust_htlc_sources. clone ( ) ) ;
3247
3243
}
3248
3244
ChannelMonitorUpdateStep :: LatestCounterpartyCommitmentTXInfo { commitment_txid, htlc_outputs, commitment_number, their_per_commitment_point, .. } => {
3249
3245
log_trace ! ( logger, "Updating ChannelMonitor with latest counterparty commitment transaction info" ) ;
@@ -3323,12 +3319,16 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
3323
3319
}
3324
3320
}
3325
3321
3326
- if ret. is_ok ( ) && ( self . funding_spend_seen || self . lockdown_from_offchain || self . holder_tx_signed ) && is_pre_close_update {
3322
+ if ret. is_ok ( ) && self . no_further_updates_allowed ( ) && is_pre_close_update {
3327
3323
log_error ! ( logger, "Refusing Channel Monitor Update as counterparty attempted to update commitment after funding was spent" ) ;
3328
3324
Err ( ( ) )
3329
3325
} else { ret }
3330
3326
}
3331
3327
3328
+ fn no_further_updates_allowed ( & self ) -> bool {
3329
+ self . funding_spend_seen || self . lockdown_from_offchain || self . holder_tx_signed
3330
+ }
3331
+
3332
3332
fn get_latest_update_id ( & self ) -> u64 {
3333
3333
self . latest_update_id
3334
3334
}
@@ -3918,35 +3918,32 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
3918
3918
}
3919
3919
}
3920
3920
}
3921
- if self . holder_tx_signed {
3922
- // If we've signed, we may have broadcast either commitment (prev or current), and
3923
- // attempted to claim from it immediately without waiting for a confirmation.
3924
- if self . current_holder_commitment_tx . txid != * confirmed_commitment_txid {
3921
+ // Cancel any pending claims for any holder commitments in case they had previously
3922
+ // confirmed or been signed (in which case we will start attempting to claim without
3923
+ // waiting for confirmation).
3924
+ if self . current_holder_commitment_tx . txid != * confirmed_commitment_txid {
3925
+ log_trace ! ( logger, "Canceling claims for previously broadcast holder commitment {}" ,
3926
+ self . current_holder_commitment_tx. txid) ;
3927
+ let mut outpoint = BitcoinOutPoint { txid : self . current_holder_commitment_tx . txid , vout : 0 } ;
3928
+ for ( htlc, _, _) in & self . current_holder_commitment_tx . htlc_outputs {
3929
+ if let Some ( vout) = htlc. transaction_output_index {
3930
+ outpoint. vout = vout;
3931
+ self . onchain_tx_handler . abandon_claim ( & outpoint) ;
3932
+ }
3933
+ }
3934
+ }
3935
+ if let Some ( prev_holder_commitment_tx) = & self . prev_holder_signed_commitment_tx {
3936
+ if prev_holder_commitment_tx. txid != * confirmed_commitment_txid {
3925
3937
log_trace ! ( logger, "Canceling claims for previously broadcast holder commitment {}" ,
3926
- self . current_holder_commitment_tx . txid) ;
3927
- let mut outpoint = BitcoinOutPoint { txid : self . current_holder_commitment_tx . txid , vout : 0 } ;
3928
- for ( htlc, _, _) in & self . current_holder_commitment_tx . htlc_outputs {
3938
+ prev_holder_commitment_tx . txid) ;
3939
+ let mut outpoint = BitcoinOutPoint { txid : prev_holder_commitment_tx . txid , vout : 0 } ;
3940
+ for ( htlc, _, _) in & prev_holder_commitment_tx . htlc_outputs {
3929
3941
if let Some ( vout) = htlc. transaction_output_index {
3930
3942
outpoint. vout = vout;
3931
3943
self . onchain_tx_handler . abandon_claim ( & outpoint) ;
3932
3944
}
3933
3945
}
3934
3946
}
3935
- if let Some ( prev_holder_commitment_tx) = & self . prev_holder_signed_commitment_tx {
3936
- if prev_holder_commitment_tx. txid != * confirmed_commitment_txid {
3937
- log_trace ! ( logger, "Canceling claims for previously broadcast holder commitment {}" ,
3938
- prev_holder_commitment_tx. txid) ;
3939
- let mut outpoint = BitcoinOutPoint { txid : prev_holder_commitment_tx. txid , vout : 0 } ;
3940
- for ( htlc, _, _) in & prev_holder_commitment_tx. htlc_outputs {
3941
- if let Some ( vout) = htlc. transaction_output_index {
3942
- outpoint. vout = vout;
3943
- self . onchain_tx_handler . abandon_claim ( & outpoint) ;
3944
- }
3945
- }
3946
- }
3947
- }
3948
- } else {
3949
- // No previous claim.
3950
3947
}
3951
3948
}
3952
3949
@@ -4282,7 +4279,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
4282
4279
}
4283
4280
}
4284
4281
4285
- if self . lockdown_from_offchain || self . funding_spend_seen || self . holder_tx_signed {
4282
+ if self . no_further_updates_allowed ( ) {
4286
4283
// Fail back HTLCs on backwards channels if they expire within
4287
4284
// `LATENCY_GRACE_PERIOD_BLOCKS` blocks and the channel is closed (i.e. we're at a
4288
4285
// point where no further off-chain updates will be accepted). If we haven't seen the
@@ -5440,7 +5437,7 @@ mod tests {
5440
5437
let dummy_commitment_tx = HolderCommitmentTransaction :: dummy ( 0 , & mut htlcs) ;
5441
5438
5442
5439
monitor. provide_latest_holder_commitment_tx ( dummy_commitment_tx. clone ( ) ,
5443
- htlcs. into_iter ( ) . map ( |( htlc, _) | ( htlc, Some ( dummy_sig) , None ) ) . collect ( ) ) . unwrap ( ) ;
5440
+ htlcs. into_iter ( ) . map ( |( htlc, _) | ( htlc, Some ( dummy_sig) , None ) ) . collect ( ) ) ;
5444
5441
monitor. provide_latest_counterparty_commitment_tx ( Txid :: from_byte_array ( Sha256 :: hash ( b"1" ) . to_byte_array ( ) ) ,
5445
5442
preimages_slice_to_htlc_outputs ! ( preimages[ 5 ..15 ] ) , 281474976710655 , dummy_key, & logger) ;
5446
5443
monitor. provide_latest_counterparty_commitment_tx ( Txid :: from_byte_array ( Sha256 :: hash ( b"2" ) . to_byte_array ( ) ) ,
@@ -5478,7 +5475,7 @@ mod tests {
5478
5475
let mut htlcs = preimages_slice_to_htlcs ! ( preimages[ 0 ..5 ] ) ;
5479
5476
let dummy_commitment_tx = HolderCommitmentTransaction :: dummy ( 0 , & mut htlcs) ;
5480
5477
monitor. provide_latest_holder_commitment_tx ( dummy_commitment_tx. clone ( ) ,
5481
- htlcs. into_iter ( ) . map ( |( htlc, _) | ( htlc, Some ( dummy_sig) , None ) ) . collect ( ) ) . unwrap ( ) ;
5478
+ htlcs. into_iter ( ) . map ( |( htlc, _) | ( htlc, Some ( dummy_sig) , None ) ) . collect ( ) ) ;
5482
5479
secret[ 0 ..32 ] . clone_from_slice ( & <Vec < u8 > >:: from_hex ( "2273e227a5b7449b6e70f1fb4652864038b1cbf9cd7c043a7d6456b7fc275ad8" ) . unwrap ( ) ) ;
5483
5480
monitor. provide_secret ( 281474976710653 , secret. clone ( ) ) . unwrap ( ) ;
5484
5481
assert_eq ! ( monitor. inner. lock( ) . unwrap( ) . payment_preimages. len( ) , 12 ) ;
@@ -5489,7 +5486,7 @@ mod tests {
5489
5486
let mut htlcs = preimages_slice_to_htlcs ! ( preimages[ 0 ..3 ] ) ;
5490
5487
let dummy_commitment_tx = HolderCommitmentTransaction :: dummy ( 0 , & mut htlcs) ;
5491
5488
monitor. provide_latest_holder_commitment_tx ( dummy_commitment_tx,
5492
- htlcs. into_iter ( ) . map ( |( htlc, _) | ( htlc, Some ( dummy_sig) , None ) ) . collect ( ) ) . unwrap ( ) ;
5489
+ htlcs. into_iter ( ) . map ( |( htlc, _) | ( htlc, Some ( dummy_sig) , None ) ) . collect ( ) ) ;
5493
5490
secret[ 0 ..32 ] . clone_from_slice ( & <Vec < u8 > >:: from_hex ( "27cddaa5624534cb6cb9d7da077cf2b22ab21e9b506fd4998a51d54502e99116" ) . unwrap ( ) ) ;
5494
5491
monitor. provide_secret ( 281474976710652 , secret. clone ( ) ) . unwrap ( ) ;
5495
5492
assert_eq ! ( monitor. inner. lock( ) . unwrap( ) . payment_preimages. len( ) , 5 ) ;
0 commit comments