@@ -69,34 +69,36 @@ use crate::sync::{Mutex, LockTestExt};
69
69
/// much smaller than a full [`ChannelMonitor`]. However, for large single commitment transaction
70
70
/// updates (e.g. ones during which there are hundreds of HTLCs pending on the commitment
71
71
/// transaction), a single update may reach upwards of 1 MiB in serialized size.
72
- #[ cfg_attr( any( test, fuzzing, feature = "_test_utils" ) , derive( PartialEq , Eq ) ) ]
73
- #[ derive( Clone ) ]
72
+ #[ derive( Clone , PartialEq , Eq ) ]
74
73
#[ must_use]
75
74
pub struct ChannelMonitorUpdate {
76
75
pub ( crate ) updates : Vec < ChannelMonitorUpdateStep > ,
77
76
/// The sequence number of this update. Updates *must* be replayed in-order according to this
78
77
/// sequence number (and updates may panic if they are not). The update_id values are strictly
79
- /// increasing and increase by one for each new update, with one exception specified below.
78
+ /// increasing and increase by one for each new update, with two exceptions specified below.
80
79
///
81
80
/// This sequence number is also used to track up to which points updates which returned
82
81
/// [`ChannelMonitorUpdateStatus::InProgress`] have been applied to all copies of a given
83
82
/// ChannelMonitor when ChannelManager::channel_monitor_updated is called.
84
83
///
85
- /// The only instance where update_id values are not strictly increasing is the case where we
86
- /// allow post-force-close updates with a special update ID of [`CLOSED_CHANNEL_UPDATE_ID`]. See
87
- /// its docs for more details.
84
+ /// The only instances we allow where update_id values are not strictly increasing have a
85
+ /// special update ID of [`CLOSED_CHANNEL_UPDATE_ID`]. This update ID is used for updates that
86
+ /// will force close the channel by broadcasting the latest commitment transaction or
87
+ /// special post-force-close updates, like providing preimages necessary to claim outputs on the
88
+ /// broadcast commitment transaction. See its docs for more details.
88
89
///
89
90
/// [`ChannelMonitorUpdateStatus::InProgress`]: super::ChannelMonitorUpdateStatus::InProgress
90
91
pub update_id : u64 ,
91
92
}
92
93
93
- /// If:
94
- /// (1) a channel has been force closed and
95
- /// (2) we receive a preimage from a forward link that allows us to spend an HTLC output on
96
- /// this channel's (the backward link's) broadcasted commitment transaction
97
- /// then we allow the `ChannelManager` to send a `ChannelMonitorUpdate` with this update ID,
98
- /// with the update providing said payment preimage. No other update types are allowed after
99
- /// force-close.
94
+ /// The update ID used for a [`ChannelMonitorUpdate`] that is either:
95
+ ///
96
+ /// (1) attempting to force close the channel by broadcasting our latest commitment transaction or
97
+ /// (2) providing a preimage (after the channel has been force closed) from a forward link that
98
+ /// allows us to spend an HTLC output on this channel's (the backward link's) broadcasted
99
+ /// commitment transaction.
100
+ ///
101
+ /// No other [`ChannelMonitorUpdate`]s are allowed after force-close.
100
102
pub const CLOSED_CHANNEL_UPDATE_ID : u64 = core:: u64:: MAX ;
101
103
102
104
impl Writeable for ChannelMonitorUpdate {
@@ -488,8 +490,7 @@ impl_writeable_tlv_based_enum_upgradable!(OnchainEvent,
488
490
489
491
) ;
490
492
491
- #[ cfg_attr( any( test, fuzzing, feature = "_test_utils" ) , derive( PartialEq , Eq ) ) ]
492
- #[ derive( Clone ) ]
493
+ #[ derive( Clone , PartialEq , Eq ) ]
493
494
pub ( crate ) enum ChannelMonitorUpdateStep {
494
495
LatestHolderCommitmentTXInfo {
495
496
commitment_tx : HolderCommitmentTransaction ,
@@ -1201,17 +1202,6 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
1201
1202
payment_hash, payment_preimage, broadcaster, fee_estimator, logger)
1202
1203
}
1203
1204
1204
- pub ( crate ) fn broadcast_latest_holder_commitment_txn < B : Deref , L : Deref > (
1205
- & self ,
1206
- broadcaster : & B ,
1207
- logger : & L ,
1208
- ) where
1209
- B :: Target : BroadcasterInterface ,
1210
- L :: Target : Logger ,
1211
- {
1212
- self . inner . lock ( ) . unwrap ( ) . broadcast_latest_holder_commitment_txn ( broadcaster, logger) ;
1213
- }
1214
-
1215
1205
/// Updates a ChannelMonitor on the basis of some new information provided by the Channel
1216
1206
/// itself.
1217
1207
///
@@ -2265,14 +2255,22 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
2265
2255
{
2266
2256
log_info ! ( logger, "Applying update to monitor {}, bringing update_id from {} to {} with {} changes." ,
2267
2257
log_funding_info!( self ) , self . latest_update_id, updates. update_id, updates. updates. len( ) ) ;
2268
- // ChannelMonitor updates may be applied after force close if we receive a
2269
- // preimage for a broadcasted commitment transaction HTLC output that we'd
2270
- // like to claim on-chain. If this is the case, we no longer have guaranteed
2271
- // access to the monitor's update ID, so we use a sentinel value instead.
2258
+ // ChannelMonitor updates may be applied after force close if we receive a preimage for a
2259
+ // broadcasted commitment transaction HTLC output that we'd like to claim on-chain. If this
2260
+ // is the case, we no longer have guaranteed access to the monitor's update ID, so we use a
2261
+ // sentinel value instead.
2262
+ //
2263
+ // The `ChannelManager` may also queue redundant `ChannelForceClosed` updates if it still
2264
+ // thinks the channel needs to have its commitment transaction broadcast, so we'll allow
2265
+ // them as well.
2272
2266
if updates. update_id == CLOSED_CHANNEL_UPDATE_ID {
2273
2267
assert_eq ! ( updates. updates. len( ) , 1 ) ;
2274
2268
match updates. updates [ 0 ] {
2275
- ChannelMonitorUpdateStep :: PaymentPreimage { .. } => { } ,
2269
+ ChannelMonitorUpdateStep :: ChannelForceClosed { .. } => { } ,
2270
+ // We should have already seen a `ChannelForceClosed` update if we're trying to
2271
+ // provide a preimage at this point.
2272
+ ChannelMonitorUpdateStep :: PaymentPreimage { .. } =>
2273
+ debug_assert_eq ! ( self . latest_update_id, CLOSED_CHANNEL_UPDATE_ID ) ,
2276
2274
_ => {
2277
2275
log_error ! ( logger, "Attempted to apply post-force-close ChannelMonitorUpdate of type {}" , updates. updates[ 0 ] . variant_name( ) ) ;
2278
2276
panic ! ( "Attempted to apply post-force-close ChannelMonitorUpdate that wasn't providing a payment preimage" ) ;
@@ -2364,6 +2362,13 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
2364
2362
} ,
2365
2363
}
2366
2364
}
2365
+
2366
+ // If the updates succeeded and we were in an already closed channel state, then there's no
2367
+ // need to refuse any updates we expect to receive afer seeing a confirmed commitment.
2368
+ if ret. is_ok ( ) && updates. update_id == CLOSED_CHANNEL_UPDATE_ID && self . latest_update_id == updates. update_id {
2369
+ return Ok ( ( ) ) ;
2370
+ }
2371
+
2367
2372
self . latest_update_id = updates. update_id ;
2368
2373
2369
2374
if ret. is_ok ( ) && self . funding_spend_seen {
0 commit comments