@@ -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) ) ]
@@ -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_monitor_pos = if release_cs_monitor && msg. is_some ( ) {
2001
+ // build_commitment_no_status_check may bump latest_monitor_id but we want them to be
2002
+ // strictly increasing by one, so decrement it here.
2003
+ let mut additional_update = self . build_commitment_no_status_check ( logger) ;
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_monitor_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,11 @@ 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
+ let release_monitor = self . pending_monitor_updates . iter ( ) . all ( |upd| !upd. blocked ) ;
3306
+ self . pending_monitor_updates . push ( PendingChannelMonitorUpdate {
3307
+ update : monitor_update, blocked : !release_monitor
3308
+ } ) ;
3309
+ return Ok ( if release_monitor { self . pending_monitor_updates . last ( ) . map ( |upd| & upd. update ) } else { None } ) ;
3273
3310
}
3274
3311
3275
3312
let need_commitment_signed = if need_commitment && ( self . channel_state & ( ChannelState :: AwaitingRemoteRevoke as u32 ) ) == 0 {
@@ -3286,9 +3323,12 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3286
3323
3287
3324
log_debug ! ( logger, "Received valid commitment_signed from peer in channel {}, updating HTLC state and responding with{} a revoke_and_ack." ,
3288
3325
log_bytes!( self . channel_id( ) ) , if need_commitment_signed { " our own commitment_signed and" } else { "" } ) ;
3289
- self . pending_monitor_updates . push ( monitor_update) ;
3326
+ let release_monitor = self . pending_monitor_updates . iter ( ) . all ( |upd| !upd. blocked ) ;
3327
+ self . pending_monitor_updates . push ( PendingChannelMonitorUpdate {
3328
+ update : monitor_update, blocked : !release_monitor,
3329
+ } ) ;
3290
3330
self . monitor_updating_paused ( true , need_commitment_signed, false , Vec :: new ( ) , Vec :: new ( ) , Vec :: new ( ) ) ;
3291
- return Ok ( self . pending_monitor_updates . last ( ) . unwrap ( ) ) ;
3331
+ return Ok ( if release_monitor { self . pending_monitor_updates . last ( ) . map ( |upd| & upd . update ) } else { None } ) ;
3292
3332
}
3293
3333
3294
3334
/// Public version of the below, checking relevant preconditions first.
@@ -3403,8 +3443,12 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3403
3443
update_add_htlcs. len( ) , update_fulfill_htlcs. len( ) , update_fail_htlcs. len( ) ) ;
3404
3444
3405
3445
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)
3446
+ let release_monitor = self . pending_monitor_updates . iter ( ) . all ( |upd| !upd. blocked ) ;
3447
+ self . pending_monitor_updates . push ( PendingChannelMonitorUpdate {
3448
+ update : monitor_update, blocked : !release_monitor,
3449
+ } ) ;
3450
+ ( if release_monitor { self . pending_monitor_updates . last ( ) . map ( |upd| & upd. update ) } else { None } ,
3451
+ htlcs_to_fail)
3408
3452
} else {
3409
3453
( None , Vec :: new ( ) )
3410
3454
}
@@ -3415,7 +3459,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3415
3459
/// waiting on this revoke_and_ack. The generation of this new commitment_signed may also fail,
3416
3460
/// generating an appropriate error *after* the channel state has been updated based on the
3417
3461
/// 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 >
3462
+ pub fn revoke_and_ack < L : Deref > ( & mut self , msg : & msgs:: RevokeAndACK , logger : & L ) -> Result < ( Vec < ( HTLCSource , PaymentHash ) > , Option < & ChannelMonitorUpdate > ) , ChannelError >
3419
3463
where L :: Target : Logger ,
3420
3464
{
3421
3465
if ( self . channel_state & ( ChannelState :: ChannelReady as u32 ) ) != ( ChannelState :: ChannelReady as u32 ) {
@@ -3612,21 +3656,29 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3612
3656
self . monitor_pending_failures . append ( & mut revoked_htlcs) ;
3613
3657
self . monitor_pending_finalized_fulfills . append ( & mut finalized_claimed_htlcs) ;
3614
3658
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 ( ) ) ) ;
3659
+ let release_monitor = self . pending_monitor_updates . iter ( ) . all ( |upd| !upd. blocked ) ;
3660
+ self . pending_monitor_updates . push ( PendingChannelMonitorUpdate {
3661
+ update : monitor_update, blocked : !release_monitor,
3662
+ } ) ;
3663
+ return Ok ( ( Vec :: new ( ) ,
3664
+ if release_monitor { self . pending_monitor_updates . last ( ) . map ( |upd| & upd. update ) } else { None } ) ) ;
3617
3665
}
3618
3666
3619
3667
match self . free_holding_cell_htlcs ( logger) {
3620
3668
( Some ( _) , htlcs_to_fail) => {
3621
- let mut additional_update = self . pending_monitor_updates . pop ( ) . unwrap ( ) ;
3669
+ let mut additional_update = self . pending_monitor_updates . pop ( ) . unwrap ( ) . update ;
3622
3670
// free_holding_cell_htlcs may bump latest_monitor_id multiple times but we want them to be
3623
3671
// strictly increasing by one, so decrement it here.
3624
3672
self . latest_monitor_update_id = monitor_update. update_id ;
3625
3673
monitor_update. updates . append ( & mut additional_update. updates ) ;
3626
3674
3627
3675
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 ( ) ) )
3676
+ let release_monitor = self . pending_monitor_updates . iter ( ) . all ( |upd| !upd. blocked ) ;
3677
+ self . pending_monitor_updates . push ( PendingChannelMonitorUpdate {
3678
+ update : monitor_update, blocked : !release_monitor,
3679
+ } ) ;
3680
+ Ok ( ( htlcs_to_fail,
3681
+ if release_monitor { self . pending_monitor_updates . last ( ) . map ( |upd| & upd. update ) } else { None } ) )
3630
3682
} ,
3631
3683
( None , htlcs_to_fail) => {
3632
3684
if require_commitment {
@@ -3640,13 +3692,21 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3640
3692
log_debug ! ( logger, "Received a valid revoke_and_ack for channel {}. Responding with a commitment update with {} HTLCs failed." ,
3641
3693
log_bytes!( self . channel_id( ) ) , update_fail_htlcs. len( ) + update_fail_malformed_htlcs. len( ) ) ;
3642
3694
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 ( ) ) )
3695
+ let release_monitor = self . pending_monitor_updates . iter ( ) . all ( |upd| !upd. blocked ) ;
3696
+ self . pending_monitor_updates . push ( PendingChannelMonitorUpdate {
3697
+ update : monitor_update, blocked : !release_monitor,
3698
+ } ) ;
3699
+ Ok ( ( htlcs_to_fail,
3700
+ if release_monitor { self . pending_monitor_updates . last ( ) . map ( |upd| & upd. update ) } else { None } ) )
3645
3701
} else {
3646
3702
log_debug ! ( logger, "Received a valid revoke_and_ack for channel {} with no reply necessary." , log_bytes!( self . channel_id( ) ) ) ;
3647
3703
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 ( ) ) )
3704
+ let release_monitor = self . pending_monitor_updates . iter ( ) . all ( |upd| !upd. blocked ) ;
3705
+ self . pending_monitor_updates . push ( PendingChannelMonitorUpdate {
3706
+ update : monitor_update, blocked : !release_monitor,
3707
+ } ) ;
3708
+ Ok ( ( htlcs_to_fail,
3709
+ if release_monitor { self . pending_monitor_updates . last ( ) . map ( |upd| & upd. update ) } else { None } ) )
3650
3710
}
3651
3711
}
3652
3712
}
@@ -3835,7 +3895,12 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3835
3895
{
3836
3896
assert_eq ! ( self . channel_state & ChannelState :: MonitorUpdateInProgress as u32 , ChannelState :: MonitorUpdateInProgress as u32 ) ;
3837
3897
self . channel_state &= !( ChannelState :: MonitorUpdateInProgress as u32 ) ;
3838
- self . pending_monitor_updates . clear ( ) ;
3898
+ let mut found_blocked = false ;
3899
+ self . pending_monitor_updates . retain ( |upd| {
3900
+ if found_blocked { debug_assert ! ( upd. blocked, "No mons may be unblocked after a blocked one" ) ; }
3901
+ if upd. blocked { found_blocked = true ; }
3902
+ upd. blocked
3903
+ } ) ;
3839
3904
3840
3905
// If we're past (or at) the FundingSent stage on an outbound channel, try to
3841
3906
// (re-)broadcast the funding transaction as we may have declined to broadcast it when we
@@ -4378,8 +4443,11 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
4378
4443
} ] ,
4379
4444
} ;
4380
4445
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 ( ) )
4446
+ let release_monitor = self . pending_monitor_updates . iter ( ) . all ( |upd| !upd. blocked ) ;
4447
+ self . pending_monitor_updates . push ( PendingChannelMonitorUpdate {
4448
+ update : monitor_update, blocked : !release_monitor,
4449
+ } ) ;
4450
+ if release_monitor { self . pending_monitor_updates . last ( ) . map ( |upd| & upd. update ) } else { None }
4383
4451
} else { None } ;
4384
4452
let shutdown = if send_shutdown {
4385
4453
Some ( msgs:: Shutdown {
@@ -4951,8 +5019,25 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
4951
5019
( self . channel_state & ChannelState :: MonitorUpdateInProgress as u32 ) != 0
4952
5020
}
4953
5021
4954
- pub fn get_next_monitor_update ( & self ) -> Option < & ChannelMonitorUpdate > {
4955
- self . pending_monitor_updates . first ( )
5022
+ /// Returns the next blocked monitor update, if one exists, and a bool which indicates a
5023
+ /// further blocked monitor update exists after the next.
5024
+ pub fn unblock_next_blocked_monitor_update ( & mut self ) -> Option < ( & ChannelMonitorUpdate , bool ) > {
5025
+ for i in 0 ..self . pending_monitor_updates . len ( ) {
5026
+ if self . pending_monitor_updates [ i] . blocked {
5027
+ self . pending_monitor_updates [ i] . blocked = false ;
5028
+ return Some ( ( & self . pending_monitor_updates [ i] . update ,
5029
+ self . pending_monitor_updates . len ( ) > i + 1 ) ) ;
5030
+ }
5031
+ }
5032
+ None
5033
+ }
5034
+
5035
+ pub fn no_monitor_updates_pending ( & self ) -> bool {
5036
+ self . pending_monitor_updates . is_empty ( )
5037
+ }
5038
+
5039
+ pub fn complete_one_mon_update ( & mut self , update_id : u64 ) {
5040
+ self . pending_monitor_updates . retain ( |upd| upd. update . update_id != update_id) ;
4956
5041
}
4957
5042
4958
5043
/// Returns true if funding_created was sent/received.
@@ -6000,8 +6085,12 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
6000
6085
Some ( _) => {
6001
6086
let monitor_update = self . build_commitment_no_status_check ( logger) ;
6002
6087
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 ( ) ) )
6088
+
6089
+ let release_monitor = self . pending_monitor_updates . iter ( ) . all ( |upd| !upd. blocked ) ;
6090
+ self . pending_monitor_updates . push ( PendingChannelMonitorUpdate {
6091
+ update : monitor_update, blocked : !release_monitor,
6092
+ } ) ;
6093
+ Ok ( if release_monitor { self . pending_monitor_updates . last ( ) . map ( |upd| & upd. update ) } else { None } )
6005
6094
} ,
6006
6095
None => Ok ( None )
6007
6096
}
@@ -6090,8 +6179,11 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
6090
6179
} ] ,
6091
6180
} ;
6092
6181
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 ( ) )
6182
+ let release_monitor = self . pending_monitor_updates . iter ( ) . all ( |upd| !upd. blocked ) ;
6183
+ self . pending_monitor_updates . push ( PendingChannelMonitorUpdate {
6184
+ update : monitor_update, blocked : !release_monitor,
6185
+ } ) ;
6186
+ if release_monitor { self . pending_monitor_updates . last ( ) . map ( |upd| & upd. update ) } else { None }
6095
6187
} else { None } ;
6096
6188
let shutdown = msgs:: Shutdown {
6097
6189
channel_id : self . channel_id ,
0 commit comments