@@ -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 externl 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) ) ]
@@ -1977,28 +1987,52 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
1977
1987
}
1978
1988
1979
1989
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 {
1990
+ let release_cs_monitor = self . pending_monitor_updates . iter ( ) . all ( |upd| !upd. blocked ) ;
1980
1991
match self . get_update_fulfill_htlc ( htlc_id, payment_preimage, logger) {
1981
- UpdateFulfillFetch :: NewClaim { mut monitor_update, htlc_value_msat, msg : Some ( _) } => {
1982
- let mut additional_update = self . build_commitment_no_status_check ( logger) ;
1983
- // build_commitment_no_status_check may bump latest_monitor_id but we want them to be
1984
- // strictly increasing by one, so decrement it here.
1985
- self . latest_monitor_update_id = monitor_update. update_id ;
1986
- monitor_update. updates . append ( & mut additional_update. updates ) ;
1987
- self . monitor_updating_paused ( false , true , false , Vec :: new ( ) , Vec :: new ( ) , Vec :: new ( ) ) ;
1988
- self . pending_monitor_updates . push ( monitor_update) ;
1992
+ UpdateFulfillFetch :: NewClaim { mut monitor_update, htlc_value_msat, msg } => {
1993
+ // Even if we aren't supposed to let new monitor updates with commitment state
1994
+ // updates run, we still need to push the preimage ChannelMonitorUpdateStep no
1995
+ // matter what. Sadly, to push a new monitor update which flies before others
1996
+ // already queued, we have to insert it into the pending queue and update the
1997
+ // update_ids of all the following monitors.
1998
+ let unblocked_monitor_pos = if release_cs_monitor && msg. is_some ( ) {
1999
+ // build_commitment_no_status_check may bump latest_monitor_id but we want them to be
2000
+ // strictly increasing by one, so decrement it here.
2001
+ let mut additional_update = self . build_commitment_no_status_check ( logger) ;
2002
+ self . latest_monitor_update_id = monitor_update. update_id ;
2003
+ monitor_update. updates . append ( & mut additional_update. updates ) ;
2004
+ self . pending_monitor_updates . push ( PendingChannelMonitorUpdate {
2005
+ update : monitor_update, blocked : false ,
2006
+ } ) ;
2007
+ self . pending_monitor_updates . len ( ) - 1
2008
+ } else {
2009
+ let insert_pos = self . pending_monitor_updates . iter ( ) . position ( |upd| upd. blocked )
2010
+ . unwrap_or ( self . pending_monitor_updates . len ( ) ) ;
2011
+ let new_mon_id = self . pending_monitor_updates . get ( insert_pos)
2012
+ . map ( |upd| upd. update . update_id ) . unwrap_or ( monitor_update. update_id ) ;
2013
+ monitor_update. update_id = new_mon_id;
2014
+ self . pending_monitor_updates . insert ( insert_pos, PendingChannelMonitorUpdate {
2015
+ update : monitor_update, blocked : false ,
2016
+ } ) ;
2017
+ for held_update in self . pending_monitor_updates . iter_mut ( ) . skip ( insert_pos + 1 ) {
2018
+ held_update. update . update_id += 1 ;
2019
+ }
2020
+ if msg. is_some ( ) {
2021
+ debug_assert ! ( false , "If there is a pending blocked monitor we should have MonitorUpdateInProgress set" ) ;
2022
+ let update = self . build_commitment_no_status_check ( logger) ;
2023
+ self . pending_monitor_updates . push ( PendingChannelMonitorUpdate {
2024
+ update, blocked : true ,
2025
+ } ) ;
2026
+ }
2027
+ insert_pos
2028
+ } ;
2029
+ self . monitor_updating_paused ( false , msg. is_some ( ) , false , Vec :: new ( ) , Vec :: new ( ) , Vec :: new ( ) ) ;
1989
2030
UpdateFulfillCommitFetch :: NewClaim {
1990
- monitor_update : self . pending_monitor_updates . last ( ) . unwrap ( ) ,
2031
+ monitor_update : & self . pending_monitor_updates . get ( unblocked_monitor_pos)
2032
+ . expect ( "We just pushed the monitor update" ) . update ,
1991
2033
htlc_value_msat,
1992
2034
}
1993
2035
} ,
1994
- UpdateFulfillFetch :: NewClaim { monitor_update, htlc_value_msat, msg : None } => {
1995
- self . monitor_updating_paused ( false , false , false , Vec :: new ( ) , Vec :: new ( ) , Vec :: new ( ) ) ;
1996
- self . pending_monitor_updates . push ( monitor_update) ;
1997
- UpdateFulfillCommitFetch :: NewClaim {
1998
- monitor_update : self . pending_monitor_updates . last ( ) . unwrap ( ) ,
1999
- htlc_value_msat,
2000
- }
2001
- }
2002
2036
UpdateFulfillFetch :: DuplicateClaim { } => UpdateFulfillCommitFetch :: DuplicateClaim { } ,
2003
2037
}
2004
2038
}
@@ -3066,7 +3100,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3066
3100
Ok ( ( ) )
3067
3101
}
3068
3102
3069
- pub fn commitment_signed < L : Deref > ( & mut self , msg : & msgs:: CommitmentSigned , logger : & L ) -> Result < & ChannelMonitorUpdate , ChannelError >
3103
+ pub fn commitment_signed < L : Deref > ( & mut self , msg : & msgs:: CommitmentSigned , logger : & L ) -> Result < Option < & ChannelMonitorUpdate > , ChannelError >
3070
3104
where L :: Target : Logger
3071
3105
{
3072
3106
if ( self . channel_state & ( ChannelState :: ChannelReady as u32 ) ) != ( ChannelState :: ChannelReady as u32 ) {
@@ -3242,8 +3276,11 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3242
3276
}
3243
3277
log_debug ! ( logger, "Received valid commitment_signed from peer in channel {}, updated HTLC state but awaiting a monitor update resolution to reply." ,
3244
3278
log_bytes!( self . channel_id) ) ;
3245
- self . pending_monitor_updates . push ( monitor_update) ;
3246
- return Ok ( self . pending_monitor_updates . last ( ) . unwrap ( ) ) ;
3279
+ let release_monitor = self . pending_monitor_updates . iter ( ) . all ( |upd| !upd. blocked ) ;
3280
+ self . pending_monitor_updates . push ( PendingChannelMonitorUpdate {
3281
+ update : monitor_update, blocked : !release_monitor
3282
+ } ) ;
3283
+ return Ok ( if release_monitor { self . pending_monitor_updates . last ( ) . map ( |upd| & upd. update ) } else { None } ) ;
3247
3284
}
3248
3285
3249
3286
let need_commitment_signed = if need_commitment && ( self . channel_state & ( ChannelState :: AwaitingRemoteRevoke as u32 ) ) == 0 {
@@ -3260,9 +3297,12 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3260
3297
3261
3298
log_debug ! ( logger, "Received valid commitment_signed from peer in channel {}, updating HTLC state and responding with{} a revoke_and_ack." ,
3262
3299
log_bytes!( self . channel_id( ) ) , if need_commitment_signed { " our own commitment_signed and" } else { "" } ) ;
3263
- self . pending_monitor_updates . push ( monitor_update) ;
3300
+ let release_monitor = self . pending_monitor_updates . iter ( ) . all ( |upd| !upd. blocked ) ;
3301
+ self . pending_monitor_updates . push ( PendingChannelMonitorUpdate {
3302
+ update : monitor_update, blocked : !release_monitor,
3303
+ } ) ;
3264
3304
self . monitor_updating_paused ( true , need_commitment_signed, false , Vec :: new ( ) , Vec :: new ( ) , Vec :: new ( ) ) ;
3265
- return Ok ( self . pending_monitor_updates . last ( ) . unwrap ( ) ) ;
3305
+ return Ok ( if release_monitor { self . pending_monitor_updates . last ( ) . map ( |upd| & upd . update ) } else { None } ) ;
3266
3306
}
3267
3307
3268
3308
/// Public version of the below, checking relevant preconditions first.
@@ -3377,8 +3417,12 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3377
3417
update_add_htlcs. len( ) , update_fulfill_htlcs. len( ) , update_fail_htlcs. len( ) ) ;
3378
3418
3379
3419
self . monitor_updating_paused ( false , true , false , Vec :: new ( ) , Vec :: new ( ) , Vec :: new ( ) ) ;
3380
- self . pending_monitor_updates . push ( monitor_update) ;
3381
- ( Some ( self . pending_monitor_updates . last ( ) . unwrap ( ) ) , htlcs_to_fail)
3420
+ let release_monitor = self . pending_monitor_updates . iter ( ) . all ( |upd| !upd. blocked ) ;
3421
+ self . pending_monitor_updates . push ( PendingChannelMonitorUpdate {
3422
+ update : monitor_update, blocked : !release_monitor,
3423
+ } ) ;
3424
+ ( if release_monitor { self . pending_monitor_updates . last ( ) . map ( |upd| & upd. update ) } else { None } ,
3425
+ htlcs_to_fail)
3382
3426
} else {
3383
3427
( None , Vec :: new ( ) )
3384
3428
}
@@ -3389,7 +3433,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3389
3433
/// waiting on this revoke_and_ack. The generation of this new commitment_signed may also fail,
3390
3434
/// generating an appropriate error *after* the channel state has been updated based on the
3391
3435
/// revoke_and_ack message.
3392
- pub fn revoke_and_ack < L : Deref > ( & mut self , msg : & msgs:: RevokeAndACK , logger : & L ) -> Result < ( Vec < ( HTLCSource , PaymentHash ) > , & ChannelMonitorUpdate ) , ChannelError >
3436
+ pub fn revoke_and_ack < L : Deref > ( & mut self , msg : & msgs:: RevokeAndACK , logger : & L ) -> Result < ( Vec < ( HTLCSource , PaymentHash ) > , Option < & ChannelMonitorUpdate > ) , ChannelError >
3393
3437
where L :: Target : Logger ,
3394
3438
{
3395
3439
if ( self . channel_state & ( ChannelState :: ChannelReady as u32 ) ) != ( ChannelState :: ChannelReady as u32 ) {
@@ -3586,21 +3630,29 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3586
3630
self . monitor_pending_failures . append ( & mut revoked_htlcs) ;
3587
3631
self . monitor_pending_finalized_fulfills . append ( & mut finalized_claimed_htlcs) ;
3588
3632
log_debug ! ( logger, "Received a valid revoke_and_ack for channel {} but awaiting a monitor update resolution to reply." , log_bytes!( self . channel_id( ) ) ) ;
3589
- self . pending_monitor_updates . push ( monitor_update) ;
3590
- return Ok ( ( Vec :: new ( ) , self . pending_monitor_updates . last ( ) . unwrap ( ) ) ) ;
3633
+ let release_monitor = self . pending_monitor_updates . iter ( ) . all ( |upd| !upd. blocked ) ;
3634
+ self . pending_monitor_updates . push ( PendingChannelMonitorUpdate {
3635
+ update : monitor_update, blocked : !release_monitor,
3636
+ } ) ;
3637
+ return Ok ( ( Vec :: new ( ) ,
3638
+ if release_monitor { self . pending_monitor_updates . last ( ) . map ( |upd| & upd. update ) } else { None } ) ) ;
3591
3639
}
3592
3640
3593
3641
match self . free_holding_cell_htlcs ( logger) {
3594
3642
( Some ( _) , htlcs_to_fail) => {
3595
- let mut additional_update = self . pending_monitor_updates . pop ( ) . unwrap ( ) ;
3643
+ let mut additional_update = self . pending_monitor_updates . pop ( ) . unwrap ( ) . update ;
3596
3644
// free_holding_cell_htlcs may bump latest_monitor_id multiple times but we want them to be
3597
3645
// strictly increasing by one, so decrement it here.
3598
3646
self . latest_monitor_update_id = monitor_update. update_id ;
3599
3647
monitor_update. updates . append ( & mut additional_update. updates ) ;
3600
3648
3601
3649
self . monitor_updating_paused ( false , true , false , to_forward_infos, revoked_htlcs, finalized_claimed_htlcs) ;
3602
- self . pending_monitor_updates . push ( monitor_update) ;
3603
- Ok ( ( htlcs_to_fail, self . pending_monitor_updates . last ( ) . unwrap ( ) ) )
3650
+ let release_monitor = self . pending_monitor_updates . iter ( ) . all ( |upd| !upd. blocked ) ;
3651
+ self . pending_monitor_updates . push ( PendingChannelMonitorUpdate {
3652
+ update : monitor_update, blocked : !release_monitor,
3653
+ } ) ;
3654
+ Ok ( ( htlcs_to_fail,
3655
+ if release_monitor { self . pending_monitor_updates . last ( ) . map ( |upd| & upd. update ) } else { None } ) )
3604
3656
} ,
3605
3657
( None , htlcs_to_fail) => {
3606
3658
if require_commitment {
@@ -3614,13 +3666,21 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3614
3666
log_debug ! ( logger, "Received a valid revoke_and_ack for channel {}. Responding with a commitment update with {} HTLCs failed." ,
3615
3667
log_bytes!( self . channel_id( ) ) , update_fail_htlcs. len( ) + update_fail_malformed_htlcs. len( ) ) ;
3616
3668
self . monitor_updating_paused ( false , true , false , to_forward_infos, revoked_htlcs, finalized_claimed_htlcs) ;
3617
- self . pending_monitor_updates . push ( monitor_update) ;
3618
- Ok ( ( htlcs_to_fail, self . pending_monitor_updates . last ( ) . unwrap ( ) ) )
3669
+ let release_monitor = self . pending_monitor_updates . iter ( ) . all ( |upd| !upd. blocked ) ;
3670
+ self . pending_monitor_updates . push ( PendingChannelMonitorUpdate {
3671
+ update : monitor_update, blocked : !release_monitor,
3672
+ } ) ;
3673
+ Ok ( ( htlcs_to_fail,
3674
+ if release_monitor { self . pending_monitor_updates . last ( ) . map ( |upd| & upd. update ) } else { None } ) )
3619
3675
} else {
3620
3676
log_debug ! ( logger, "Received a valid revoke_and_ack for channel {} with no reply necessary." , log_bytes!( self . channel_id( ) ) ) ;
3621
3677
self . monitor_updating_paused ( false , false , false , to_forward_infos, revoked_htlcs, finalized_claimed_htlcs) ;
3622
- self . pending_monitor_updates . push ( monitor_update) ;
3623
- Ok ( ( htlcs_to_fail, self . pending_monitor_updates . last ( ) . unwrap ( ) ) )
3678
+ let release_monitor = self . pending_monitor_updates . iter ( ) . all ( |upd| !upd. blocked ) ;
3679
+ self . pending_monitor_updates . push ( PendingChannelMonitorUpdate {
3680
+ update : monitor_update, blocked : !release_monitor,
3681
+ } ) ;
3682
+ Ok ( ( htlcs_to_fail,
3683
+ if release_monitor { self . pending_monitor_updates . last ( ) . map ( |upd| & upd. update ) } else { None } ) )
3624
3684
}
3625
3685
}
3626
3686
}
@@ -3809,7 +3869,12 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3809
3869
{
3810
3870
assert_eq ! ( self . channel_state & ChannelState :: MonitorUpdateInProgress as u32 , ChannelState :: MonitorUpdateInProgress as u32 ) ;
3811
3871
self . channel_state &= !( ChannelState :: MonitorUpdateInProgress as u32 ) ;
3812
- self . pending_monitor_updates . clear ( ) ;
3872
+ let mut found_blocked = false ;
3873
+ self . pending_monitor_updates . retain ( |upd| {
3874
+ if found_blocked { debug_assert ! ( upd. blocked, "No mons may be unblocked after a blocked one" ) ; }
3875
+ if upd. blocked { found_blocked = true ; }
3876
+ upd. blocked
3877
+ } ) ;
3813
3878
3814
3879
// If we're past (or at) the FundingSent stage on an outbound channel, try to
3815
3880
// (re-)broadcast the funding transaction as we may have declined to broadcast it when we
@@ -4352,8 +4417,11 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
4352
4417
} ] ,
4353
4418
} ;
4354
4419
self . monitor_updating_paused ( false , false , false , Vec :: new ( ) , Vec :: new ( ) , Vec :: new ( ) ) ;
4355
- self . pending_monitor_updates . push ( monitor_update) ;
4356
- Some ( self . pending_monitor_updates . last ( ) . unwrap ( ) )
4420
+ let release_monitor = self . pending_monitor_updates . iter ( ) . all ( |upd| !upd. blocked ) ;
4421
+ self . pending_monitor_updates . push ( PendingChannelMonitorUpdate {
4422
+ update : monitor_update, blocked : !release_monitor,
4423
+ } ) ;
4424
+ if release_monitor { self . pending_monitor_updates . last ( ) . map ( |upd| & upd. update ) } else { None }
4357
4425
} else { None } ;
4358
4426
let shutdown = if send_shutdown {
4359
4427
Some ( msgs:: Shutdown {
@@ -4925,8 +4993,25 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
4925
4993
( self . channel_state & ChannelState :: MonitorUpdateInProgress as u32 ) != 0
4926
4994
}
4927
4995
4928
- pub fn get_next_monitor_update ( & self ) -> Option < & ChannelMonitorUpdate > {
4929
- self . pending_monitor_updates . first ( )
4996
+ /// Returns the next blocked monitor update, if one exists, and a bool which indicates a
4997
+ /// further blocked monitor update exists after the next.
4998
+ pub fn unblock_next_blocked_monitor_update ( & mut self ) -> Option < ( & ChannelMonitorUpdate , bool ) > {
4999
+ for i in 0 ..self . pending_monitor_updates . len ( ) {
5000
+ if self . pending_monitor_updates [ i] . blocked {
5001
+ self . pending_monitor_updates [ i] . blocked = false ;
5002
+ return Some ( ( & self . pending_monitor_updates [ i] . update ,
5003
+ self . pending_monitor_updates . len ( ) > i + 1 ) ) ;
5004
+ }
5005
+ }
5006
+ None
5007
+ }
5008
+
5009
+ pub fn no_monitor_updates_pending ( & self ) -> bool {
5010
+ self . pending_monitor_updates . is_empty ( )
5011
+ }
5012
+
5013
+ pub fn complete_one_mon_update ( & mut self , update_id : u64 ) {
5014
+ self . pending_monitor_updates . retain ( |upd| upd. update . update_id != update_id) ;
4930
5015
}
4931
5016
4932
5017
/// Returns true if funding_created was sent/received.
@@ -5974,8 +6059,12 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
5974
6059
Some ( _) => {
5975
6060
let monitor_update = self . build_commitment_no_status_check ( logger) ;
5976
6061
self . monitor_updating_paused ( false , true , false , Vec :: new ( ) , Vec :: new ( ) , Vec :: new ( ) ) ;
5977
- self . pending_monitor_updates . push ( monitor_update) ;
5978
- Ok ( Some ( self . pending_monitor_updates . last ( ) . unwrap ( ) ) )
6062
+
6063
+ let release_monitor = self . pending_monitor_updates . iter ( ) . all ( |upd| !upd. blocked ) ;
6064
+ self . pending_monitor_updates . push ( PendingChannelMonitorUpdate {
6065
+ update : monitor_update, blocked : !release_monitor,
6066
+ } ) ;
6067
+ Ok ( if release_monitor { self . pending_monitor_updates . last ( ) . map ( |upd| & upd. update ) } else { None } )
5979
6068
} ,
5980
6069
None => Ok ( None )
5981
6070
}
@@ -6064,8 +6153,11 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
6064
6153
} ] ,
6065
6154
} ;
6066
6155
self . monitor_updating_paused ( false , false , false , Vec :: new ( ) , Vec :: new ( ) , Vec :: new ( ) ) ;
6067
- self . pending_monitor_updates . push ( monitor_update) ;
6068
- Some ( self . pending_monitor_updates . last ( ) . unwrap ( ) )
6156
+ let release_monitor = self . pending_monitor_updates . iter ( ) . all ( |upd| !upd. blocked ) ;
6157
+ self . pending_monitor_updates . push ( PendingChannelMonitorUpdate {
6158
+ update : monitor_update, blocked : !release_monitor,
6159
+ } ) ;
6160
+ if release_monitor { self . pending_monitor_updates . last ( ) . map ( |upd| & upd. update ) } else { None }
6069
6161
} else { None } ;
6070
6162
let shutdown = msgs:: Shutdown {
6071
6163
channel_id : self . channel_id ,
0 commit comments