@@ -479,6 +479,16 @@ pub(crate) const MIN_AFFORDABLE_HTLC_COUNT: usize = 4;
479
479
/// * `EXPIRE_PREV_CONFIG_TICKS` = convergence_delay / tick_interval
480
480
pub ( crate ) const EXPIRE_PREV_CONFIG_TICKS : usize = 5 ;
481
481
482
+ struct PendingChannelMonitorUpdate {
483
+ update : ChannelMonitorUpdate ,
484
+ /// In some cases we need to delay letting the [`ChannelMonitorUpdate`] go until after an
485
+ /// `Event` is processed by the user. This bool indicates the [`ChannelMonitorUpdate`] is
486
+ /// blocked on some external event and the [`ChannelManager`] will update us when we're ready.
487
+ ///
488
+ /// [`ChannelManager`]: super::channelmanager::ChannelManager
489
+ blocked : bool ,
490
+ }
491
+
482
492
// TODO: We should refactor this to be an Inbound/OutboundChannel until initial setup handshaking
483
493
// has been completed, and then turn into a Channel to get compiler-time enforcement of things like
484
494
// calling channel_id() before we're set up or things like get_outbound_funding_signed on an
@@ -744,7 +754,7 @@ pub(super) struct Channel<Signer: ChannelSigner> {
744
754
/// If we then persist the [`channelmanager::ChannelManager`] and crash before the persistence
745
755
/// completes we still need to be able to complete the persistence. Thus, we have to keep a
746
756
/// copy of the [`ChannelMonitorUpdate`] here until it is complete.
747
- pending_monitor_updates : Vec < ChannelMonitorUpdate > ,
757
+ pending_monitor_updates : Vec < PendingChannelMonitorUpdate > ,
748
758
}
749
759
750
760
#[ cfg( any( test, fuzzing) ) ]
@@ -1979,28 +1989,52 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
1979
1989
}
1980
1990
1981
1991
pub fn get_update_fulfill_htlc_and_commit < L : Deref > ( & mut self , htlc_id : u64 , payment_preimage : PaymentPreimage , logger : & L ) -> UpdateFulfillCommitFetch where L :: Target : Logger {
1992
+ let release_cs_monitor = self . pending_monitor_updates . iter ( ) . all ( |upd| !upd. blocked ) ;
1982
1993
match self . get_update_fulfill_htlc ( htlc_id, payment_preimage, logger) {
1983
- UpdateFulfillFetch :: NewClaim { mut monitor_update, htlc_value_msat, msg : Some ( _) } => {
1984
- let mut additional_update = self . build_commitment_no_status_check ( logger) ;
1985
- // build_commitment_no_status_check may bump latest_monitor_id but we want them to be
1986
- // strictly increasing by one, so decrement it here.
1987
- self . latest_monitor_update_id = monitor_update. update_id ;
1988
- monitor_update. updates . append ( & mut additional_update. updates ) ;
1989
- self . monitor_updating_paused ( false , true , false , Vec :: new ( ) , Vec :: new ( ) , Vec :: new ( ) ) ;
1990
- self . pending_monitor_updates . push ( monitor_update) ;
1994
+ UpdateFulfillFetch :: NewClaim { mut monitor_update, htlc_value_msat, msg } => {
1995
+ // Even if we aren't supposed to let new monitor updates with commitment state
1996
+ // updates run, we still need to push the preimage ChannelMonitorUpdateStep no
1997
+ // matter what. Sadly, to push a new monitor update which flies before others
1998
+ // already queued, we have to insert it into the pending queue and update the
1999
+ // update_ids of all the following monitors.
2000
+ let unblocked_update_pos = if release_cs_monitor && msg. is_some ( ) {
2001
+ let mut additional_update = self . build_commitment_no_status_check ( logger) ;
2002
+ // build_commitment_no_status_check may bump latest_monitor_id but we want them
2003
+ // to be strictly increasing by one, so decrement it here.
2004
+ self . latest_monitor_update_id = monitor_update. update_id ;
2005
+ monitor_update. updates . append ( & mut additional_update. updates ) ;
2006
+ self . pending_monitor_updates . push ( PendingChannelMonitorUpdate {
2007
+ update : monitor_update, blocked : false ,
2008
+ } ) ;
2009
+ self . pending_monitor_updates . len ( ) - 1
2010
+ } else {
2011
+ let insert_pos = self . pending_monitor_updates . iter ( ) . position ( |upd| upd. blocked )
2012
+ . unwrap_or ( self . pending_monitor_updates . len ( ) ) ;
2013
+ let new_mon_id = self . pending_monitor_updates . get ( insert_pos)
2014
+ . map ( |upd| upd. update . update_id ) . unwrap_or ( monitor_update. update_id ) ;
2015
+ monitor_update. update_id = new_mon_id;
2016
+ self . pending_monitor_updates . insert ( insert_pos, PendingChannelMonitorUpdate {
2017
+ update : monitor_update, blocked : false ,
2018
+ } ) ;
2019
+ for held_update in self . pending_monitor_updates . iter_mut ( ) . skip ( insert_pos + 1 ) {
2020
+ held_update. update . update_id += 1 ;
2021
+ }
2022
+ if msg. is_some ( ) {
2023
+ debug_assert ! ( false , "If there is a pending blocked monitor we should have MonitorUpdateInProgress set" ) ;
2024
+ let update = self . build_commitment_no_status_check ( logger) ;
2025
+ self . pending_monitor_updates . push ( PendingChannelMonitorUpdate {
2026
+ update, blocked : true ,
2027
+ } ) ;
2028
+ }
2029
+ insert_pos
2030
+ } ;
2031
+ self . monitor_updating_paused ( false , msg. is_some ( ) , false , Vec :: new ( ) , Vec :: new ( ) , Vec :: new ( ) ) ;
1991
2032
UpdateFulfillCommitFetch :: NewClaim {
1992
- monitor_update : self . pending_monitor_updates . last ( ) . unwrap ( ) ,
2033
+ monitor_update : & self . pending_monitor_updates . get ( unblocked_update_pos)
2034
+ . expect ( "We just pushed the monitor update" ) . update ,
1993
2035
htlc_value_msat,
1994
2036
}
1995
2037
} ,
1996
- UpdateFulfillFetch :: NewClaim { monitor_update, htlc_value_msat, msg : None } => {
1997
- self . monitor_updating_paused ( false , false , false , Vec :: new ( ) , Vec :: new ( ) , Vec :: new ( ) ) ;
1998
- self . pending_monitor_updates . push ( monitor_update) ;
1999
- UpdateFulfillCommitFetch :: NewClaim {
2000
- monitor_update : self . pending_monitor_updates . last ( ) . unwrap ( ) ,
2001
- htlc_value_msat,
2002
- }
2003
- }
2004
2038
UpdateFulfillFetch :: DuplicateClaim { } => UpdateFulfillCommitFetch :: DuplicateClaim { } ,
2005
2039
}
2006
2040
}
@@ -3068,7 +3102,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3068
3102
Ok ( ( ) )
3069
3103
}
3070
3104
3071
- pub fn commitment_signed < L : Deref > ( & mut self , msg : & msgs:: CommitmentSigned , logger : & L ) -> Result < & ChannelMonitorUpdate , ChannelError >
3105
+ pub fn commitment_signed < L : Deref > ( & mut self , msg : & msgs:: CommitmentSigned , logger : & L ) -> Result < Option < & ChannelMonitorUpdate > , ChannelError >
3072
3106
where L :: Target : Logger
3073
3107
{
3074
3108
if ( self . channel_state & ( ChannelState :: ChannelReady as u32 ) ) != ( ChannelState :: ChannelReady as u32 ) {
@@ -3268,8 +3302,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3268
3302
}
3269
3303
log_debug ! ( logger, "Received valid commitment_signed from peer in channel {}, updated HTLC state but awaiting a monitor update resolution to reply." ,
3270
3304
log_bytes!( self . channel_id) ) ;
3271
- self . pending_monitor_updates . push ( monitor_update) ;
3272
- return Ok ( self . pending_monitor_updates . last ( ) . unwrap ( ) ) ;
3305
+ return Ok ( self . push_ret_blockable_mon_update ( monitor_update) ) ;
3273
3306
}
3274
3307
3275
3308
let need_commitment_signed = if need_commitment && ( self . channel_state & ( ChannelState :: AwaitingRemoteRevoke as u32 ) ) == 0 {
@@ -3286,9 +3319,8 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3286
3319
3287
3320
log_debug ! ( logger, "Received valid commitment_signed from peer in channel {}, updating HTLC state and responding with{} a revoke_and_ack." ,
3288
3321
log_bytes!( self . channel_id( ) ) , if need_commitment_signed { " our own commitment_signed and" } else { "" } ) ;
3289
- self . pending_monitor_updates . push ( monitor_update) ;
3290
3322
self . monitor_updating_paused ( true , need_commitment_signed, false , Vec :: new ( ) , Vec :: new ( ) , Vec :: new ( ) ) ;
3291
- return Ok ( self . pending_monitor_updates . last ( ) . unwrap ( ) ) ;
3323
+ return Ok ( self . push_ret_blockable_mon_update ( monitor_update ) ) ;
3292
3324
}
3293
3325
3294
3326
/// Public version of the below, checking relevant preconditions first.
@@ -3403,8 +3435,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3403
3435
update_add_htlcs. len( ) , update_fulfill_htlcs. len( ) , update_fail_htlcs. len( ) ) ;
3404
3436
3405
3437
self . monitor_updating_paused ( false , true , false , Vec :: new ( ) , Vec :: new ( ) , Vec :: new ( ) ) ;
3406
- self . pending_monitor_updates . push ( monitor_update) ;
3407
- ( Some ( self . pending_monitor_updates . last ( ) . unwrap ( ) ) , htlcs_to_fail)
3438
+ ( self . push_ret_blockable_mon_update ( monitor_update) , htlcs_to_fail)
3408
3439
} else {
3409
3440
( None , Vec :: new ( ) )
3410
3441
}
@@ -3415,7 +3446,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3415
3446
/// waiting on this revoke_and_ack. The generation of this new commitment_signed may also fail,
3416
3447
/// generating an appropriate error *after* the channel state has been updated based on the
3417
3448
/// revoke_and_ack message.
3418
- pub fn revoke_and_ack < L : Deref > ( & mut self , msg : & msgs:: RevokeAndACK , logger : & L ) -> Result < ( Vec < ( HTLCSource , PaymentHash ) > , & ChannelMonitorUpdate ) , ChannelError >
3449
+ pub fn revoke_and_ack < L : Deref > ( & mut self , msg : & msgs:: RevokeAndACK , logger : & L ) -> Result < ( Vec < ( HTLCSource , PaymentHash ) > , Option < & ChannelMonitorUpdate > ) , ChannelError >
3419
3450
where L :: Target : Logger ,
3420
3451
{
3421
3452
if ( self . channel_state & ( ChannelState :: ChannelReady as u32 ) ) != ( ChannelState :: ChannelReady as u32 ) {
@@ -3612,21 +3643,19 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3612
3643
self . monitor_pending_failures . append ( & mut revoked_htlcs) ;
3613
3644
self . monitor_pending_finalized_fulfills . append ( & mut finalized_claimed_htlcs) ;
3614
3645
log_debug ! ( logger, "Received a valid revoke_and_ack for channel {} but awaiting a monitor update resolution to reply." , log_bytes!( self . channel_id( ) ) ) ;
3615
- self . pending_monitor_updates . push ( monitor_update) ;
3616
- return Ok ( ( Vec :: new ( ) , self . pending_monitor_updates . last ( ) . unwrap ( ) ) ) ;
3646
+ return Ok ( ( Vec :: new ( ) , self . push_ret_blockable_mon_update ( monitor_update) ) ) ;
3617
3647
}
3618
3648
3619
3649
match self . free_holding_cell_htlcs ( logger) {
3620
3650
( Some ( _) , htlcs_to_fail) => {
3621
- let mut additional_update = self . pending_monitor_updates . pop ( ) . unwrap ( ) ;
3651
+ let mut additional_update = self . pending_monitor_updates . pop ( ) . unwrap ( ) . update ;
3622
3652
// free_holding_cell_htlcs may bump latest_monitor_id multiple times but we want them to be
3623
3653
// strictly increasing by one, so decrement it here.
3624
3654
self . latest_monitor_update_id = monitor_update. update_id ;
3625
3655
monitor_update. updates . append ( & mut additional_update. updates ) ;
3626
3656
3627
3657
self . monitor_updating_paused ( false , true , false , to_forward_infos, revoked_htlcs, finalized_claimed_htlcs) ;
3628
- self . pending_monitor_updates . push ( monitor_update) ;
3629
- Ok ( ( htlcs_to_fail, self . pending_monitor_updates . last ( ) . unwrap ( ) ) )
3658
+ Ok ( ( htlcs_to_fail, self . push_ret_blockable_mon_update ( monitor_update) ) )
3630
3659
} ,
3631
3660
( None , htlcs_to_fail) => {
3632
3661
if require_commitment {
@@ -3640,13 +3669,11 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3640
3669
log_debug ! ( logger, "Received a valid revoke_and_ack for channel {}. Responding with a commitment update with {} HTLCs failed." ,
3641
3670
log_bytes!( self . channel_id( ) ) , update_fail_htlcs. len( ) + update_fail_malformed_htlcs. len( ) ) ;
3642
3671
self . monitor_updating_paused ( false , true , false , to_forward_infos, revoked_htlcs, finalized_claimed_htlcs) ;
3643
- self . pending_monitor_updates . push ( monitor_update) ;
3644
- Ok ( ( htlcs_to_fail, self . pending_monitor_updates . last ( ) . unwrap ( ) ) )
3672
+ Ok ( ( htlcs_to_fail, self . push_ret_blockable_mon_update ( monitor_update) ) )
3645
3673
} else {
3646
3674
log_debug ! ( logger, "Received a valid revoke_and_ack for channel {} with no reply necessary." , log_bytes!( self . channel_id( ) ) ) ;
3647
3675
self . monitor_updating_paused ( false , false , false , to_forward_infos, revoked_htlcs, finalized_claimed_htlcs) ;
3648
- self . pending_monitor_updates . push ( monitor_update) ;
3649
- Ok ( ( htlcs_to_fail, self . pending_monitor_updates . last ( ) . unwrap ( ) ) )
3676
+ Ok ( ( htlcs_to_fail, self . push_ret_blockable_mon_update ( monitor_update) ) )
3650
3677
}
3651
3678
}
3652
3679
}
@@ -3835,7 +3862,12 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3835
3862
{
3836
3863
assert_eq ! ( self . channel_state & ChannelState :: MonitorUpdateInProgress as u32 , ChannelState :: MonitorUpdateInProgress as u32 ) ;
3837
3864
self . channel_state &= !( ChannelState :: MonitorUpdateInProgress as u32 ) ;
3838
- self . pending_monitor_updates . clear ( ) ;
3865
+ let mut found_blocked = false ;
3866
+ self . pending_monitor_updates . retain ( |upd| {
3867
+ if found_blocked { debug_assert ! ( upd. blocked, "No mons may be unblocked after a blocked one" ) ; }
3868
+ if upd. blocked { found_blocked = true ; }
3869
+ upd. blocked
3870
+ } ) ;
3839
3871
3840
3872
// If we're past (or at) the FundingSent stage on an outbound channel, try to
3841
3873
// (re-)broadcast the funding transaction as we may have declined to broadcast it when we
@@ -4378,8 +4410,9 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
4378
4410
} ] ,
4379
4411
} ;
4380
4412
self . monitor_updating_paused ( false , false , false , Vec :: new ( ) , Vec :: new ( ) , Vec :: new ( ) ) ;
4381
- self . pending_monitor_updates . push ( monitor_update) ;
4382
- Some ( self . pending_monitor_updates . last ( ) . unwrap ( ) )
4413
+ if self . push_blockable_mon_update ( monitor_update) {
4414
+ self . pending_monitor_updates . last ( ) . map ( |upd| & upd. update )
4415
+ } else { None }
4383
4416
} else { None } ;
4384
4417
let shutdown = if send_shutdown {
4385
4418
Some ( msgs:: Shutdown {
@@ -4951,8 +4984,44 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
4951
4984
( self . channel_state & ChannelState :: MonitorUpdateInProgress as u32 ) != 0
4952
4985
}
4953
4986
4954
- pub fn get_next_monitor_update ( & self ) -> Option < & ChannelMonitorUpdate > {
4955
- self . pending_monitor_updates . first ( )
4987
+ /// Returns the next blocked monitor update, if one exists, and a bool which indicates a
4988
+ /// further blocked monitor update exists after the next.
4989
+ pub fn unblock_next_blocked_monitor_update ( & mut self ) -> Option < ( & ChannelMonitorUpdate , bool ) > {
4990
+ for i in 0 ..self . pending_monitor_updates . len ( ) {
4991
+ if self . pending_monitor_updates [ i] . blocked {
4992
+ self . pending_monitor_updates [ i] . blocked = false ;
4993
+ return Some ( ( & self . pending_monitor_updates [ i] . update ,
4994
+ self . pending_monitor_updates . len ( ) > i + 1 ) ) ;
4995
+ }
4996
+ }
4997
+ None
4998
+ }
4999
+
5000
+ /// Pushes a new monitor update into our monitor update queue, returning whether it should be
5001
+ /// immediately given to the user for persisting or if it should be held as blocked.
5002
+ fn push_blockable_mon_update ( & mut self , update : ChannelMonitorUpdate ) -> bool {
5003
+ let release_monitor = self . pending_monitor_updates . iter ( ) . all ( |upd| !upd. blocked ) ;
5004
+ self . pending_monitor_updates . push ( PendingChannelMonitorUpdate {
5005
+ update, blocked : !release_monitor
5006
+ } ) ;
5007
+ release_monitor
5008
+ }
5009
+
5010
+ /// Pushes a new monitor update into our monitor update queue, returning a reference to it if
5011
+ /// it should be immediately given to the user for persisting or `None` if it should be held as
5012
+ /// blocked.
5013
+ fn push_ret_blockable_mon_update ( & mut self , update : ChannelMonitorUpdate )
5014
+ -> Option < & ChannelMonitorUpdate > {
5015
+ let release_monitor = self . push_blockable_mon_update ( update) ;
5016
+ if release_monitor { self . pending_monitor_updates . last ( ) . map ( |upd| & upd. update ) } else { None }
5017
+ }
5018
+
5019
+ pub fn no_monitor_updates_pending ( & self ) -> bool {
5020
+ self . pending_monitor_updates . is_empty ( )
5021
+ }
5022
+
5023
+ pub fn complete_one_mon_update ( & mut self , update_id : u64 ) {
5024
+ self . pending_monitor_updates . retain ( |upd| upd. update . update_id != update_id) ;
4956
5025
}
4957
5026
4958
5027
/// Returns true if funding_created was sent/received.
@@ -6000,8 +6069,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
6000
6069
Some ( _) => {
6001
6070
let monitor_update = self . build_commitment_no_status_check ( logger) ;
6002
6071
self . monitor_updating_paused ( false , true , false , Vec :: new ( ) , Vec :: new ( ) , Vec :: new ( ) ) ;
6003
- self . pending_monitor_updates . push ( monitor_update) ;
6004
- Ok ( Some ( self . pending_monitor_updates . last ( ) . unwrap ( ) ) )
6072
+ Ok ( self . push_ret_blockable_mon_update ( monitor_update) )
6005
6073
} ,
6006
6074
None => Ok ( None )
6007
6075
}
@@ -6090,8 +6158,9 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
6090
6158
} ] ,
6091
6159
} ;
6092
6160
self . monitor_updating_paused ( false , false , false , Vec :: new ( ) , Vec :: new ( ) , Vec :: new ( ) ) ;
6093
- self . pending_monitor_updates . push ( monitor_update) ;
6094
- Some ( self . pending_monitor_updates . last ( ) . unwrap ( ) )
6161
+ if self . push_blockable_mon_update ( monitor_update) {
6162
+ self . pending_monitor_updates . last ( ) . map ( |upd| & upd. update )
6163
+ } else { None }
6095
6164
} else { None } ;
6096
6165
let shutdown = msgs:: Shutdown {
6097
6166
channel_id : self . channel_id ,
0 commit comments