@@ -1509,8 +1509,8 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
1509
1509
fn provide_latest_holder_commitment_tx (
1510
1510
& self , holder_commitment_tx : HolderCommitmentTransaction ,
1511
1511
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 ( ) )
1514
1514
}
1515
1515
1516
1516
/// 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> {
1737
1737
self . inner . lock ( ) . unwrap ( ) . get_cur_holder_commitment_number ( )
1738
1738
}
1739
1739
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 ( )
1744
1748
}
1745
1749
1746
1750
/// Gets the `node_id` of the counterparty for this channel.
@@ -2901,7 +2905,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
2901
2905
/// is important that any clones of this channel monitor (including remote clones) by kept
2902
2906
/// up-to-date as our holder commitment transaction is updated.
2903
2907
/// 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 > ) {
2905
2909
if htlc_outputs. iter ( ) . any ( |( _, s, _) | s. is_some ( ) ) {
2906
2910
// If we have non-dust HTLCs in htlc_outputs, ensure they match the HTLCs in the
2907
2911
// `holder_commitment_tx`. In the future, we'll no longer provide the redundant data
@@ -2978,10 +2982,6 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
2978
2982
}
2979
2983
self . counterparty_fulfilled_htlcs . insert ( * claimed_htlc_id, * claimed_preimage) ;
2980
2984
}
2981
- if self . holder_tx_signed {
2982
- return Err ( "Latest holder commitment signed has already been signed, update is rejected" ) ;
2983
- }
2984
- Ok ( ( ) )
2985
2985
}
2986
2986
2987
2987
/// Provides a payment_hash->payment_preimage mapping. Will be automatically pruned when all
@@ -3202,11 +3202,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
3202
3202
ChannelMonitorUpdateStep :: LatestHolderCommitmentTXInfo { commitment_tx, htlc_outputs, claimed_htlcs, nondust_htlc_sources } => {
3203
3203
log_trace ! ( logger, "Updating ChannelMonitor with latest holder commitment transaction info" ) ;
3204
3204
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 ( ) ) ;
3210
3206
}
3211
3207
ChannelMonitorUpdateStep :: LatestCounterpartyCommitmentTXInfo { commitment_txid, htlc_outputs, commitment_number, their_per_commitment_point, .. } => {
3212
3208
log_trace ! ( logger, "Updating ChannelMonitor with latest counterparty commitment transaction info" ) ;
@@ -3286,12 +3282,16 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
3286
3282
}
3287
3283
}
3288
3284
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 {
3290
3286
log_error ! ( logger, "Refusing Channel Monitor Update as counterparty attempted to update commitment after funding was spent" ) ;
3291
3287
Err ( ( ) )
3292
3288
} else { ret }
3293
3289
}
3294
3290
3291
+ fn no_further_updates_allowed ( & self ) -> bool {
3292
+ self . funding_spend_seen || self . lockdown_from_offchain || self . holder_tx_signed
3293
+ }
3294
+
3295
3295
fn get_latest_update_id ( & self ) -> u64 {
3296
3296
self . latest_update_id
3297
3297
}
@@ -3564,11 +3564,16 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
3564
3564
return ( claimable_outpoints, to_counterparty_output_info) ;
3565
3565
}
3566
3566
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
+ } ;
3567
3572
let justice_package = PackageTemplate :: build_package (
3568
3573
commitment_txid,
3569
3574
transaction_output_index,
3570
3575
PackageSolvingData :: RevokedHTLCOutput ( revk_htlc_outp) ,
3571
- htlc . cltv_expiry ,
3576
+ counterparty_spendable_height ,
3572
3577
) ;
3573
3578
claimable_outpoints. push ( justice_package) ;
3574
3579
}
@@ -3869,35 +3874,32 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
3869
3874
}
3870
3875
}
3871
3876
}
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 {
3876
3893
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 {
3880
3897
if let Some ( vout) = htlc. transaction_output_index {
3881
3898
outpoint. vout = vout;
3882
3899
self . onchain_tx_handler . abandon_claim ( & outpoint) ;
3883
3900
}
3884
3901
}
3885
3902
}
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.
3901
3903
}
3902
3904
}
3903
3905
@@ -4233,7 +4235,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
4233
4235
}
4234
4236
}
4235
4237
4236
- if self . lockdown_from_offchain || self . funding_spend_seen || self . holder_tx_signed {
4238
+ if self . no_further_updates_allowed ( ) {
4237
4239
// Fail back HTLCs on backwards channels if they expire within
4238
4240
// `LATENCY_GRACE_PERIOD_BLOCKS` blocks and the channel is closed (i.e. we're at a
4239
4241
// point where no further off-chain updates will be accepted). If we haven't seen the
@@ -5384,7 +5386,7 @@ mod tests {
5384
5386
let dummy_commitment_tx = HolderCommitmentTransaction :: dummy ( & mut htlcs) ;
5385
5387
5386
5388
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 ( ) ) ;
5388
5390
monitor. provide_latest_counterparty_commitment_tx ( Txid :: from_byte_array ( Sha256 :: hash ( b"1" ) . to_byte_array ( ) ) ,
5389
5391
preimages_slice_to_htlc_outputs ! ( preimages[ 5 ..15 ] ) , 281474976710655 , dummy_key, & logger) ;
5390
5392
monitor. provide_latest_counterparty_commitment_tx ( Txid :: from_byte_array ( Sha256 :: hash ( b"2" ) . to_byte_array ( ) ) ,
@@ -5422,7 +5424,7 @@ mod tests {
5422
5424
let mut htlcs = preimages_slice_to_htlcs ! ( preimages[ 0 ..5 ] ) ;
5423
5425
let dummy_commitment_tx = HolderCommitmentTransaction :: dummy ( & mut htlcs) ;
5424
5426
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 ( ) ) ;
5426
5428
secret[ 0 ..32 ] . clone_from_slice ( & <Vec < u8 > >:: from_hex ( "2273e227a5b7449b6e70f1fb4652864038b1cbf9cd7c043a7d6456b7fc275ad8" ) . unwrap ( ) ) ;
5427
5429
monitor. provide_secret ( 281474976710653 , secret. clone ( ) ) . unwrap ( ) ;
5428
5430
assert_eq ! ( monitor. inner. lock( ) . unwrap( ) . payment_preimages. len( ) , 12 ) ;
@@ -5433,7 +5435,7 @@ mod tests {
5433
5435
let mut htlcs = preimages_slice_to_htlcs ! ( preimages[ 0 ..3 ] ) ;
5434
5436
let dummy_commitment_tx = HolderCommitmentTransaction :: dummy ( & mut htlcs) ;
5435
5437
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 ( ) ) ;
5437
5439
secret[ 0 ..32 ] . clone_from_slice ( & <Vec < u8 > >:: from_hex ( "27cddaa5624534cb6cb9d7da077cf2b22ab21e9b506fd4998a51d54502e99116" ) . unwrap ( ) ) ;
5438
5440
monitor. provide_secret ( 281474976710652 , secret. clone ( ) ) . unwrap ( ) ;
5439
5441
assert_eq ! ( monitor. inner. lock( ) . unwrap( ) . payment_preimages. len( ) , 5 ) ;
0 commit comments