@@ -400,7 +400,7 @@ pub enum UpdateFulfillCommitFetch<'a> {
400
400
/// previously placed in the holding cell (and has since been removed).
401
401
NewClaim {
402
402
/// The ChannelMonitorUpdate which places the new payment preimage in the channel monitor
403
- monitor_update : & ' a ChannelMonitorUpdate ,
403
+ monitor_update_opt : Option < & ' a ChannelMonitorUpdate > ,
404
404
/// The value of the HTLC which was claimed, in msat.
405
405
htlc_value_msat : u64 ,
406
406
} ,
@@ -479,6 +479,17 @@ 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`] fly until after an
485
+ /// `Event` is processed by the user. This bool indicates the [`ChannelMonitorUpdate`] has
486
+ /// flown and we're waiting to hear back, otherwise the update is waiting on some external
487
+ /// event and the [`ChannelManager`] will update us when we're ready.
488
+ ///
489
+ /// [`ChannelManager`]: super::channelmanager::ChannelManager
490
+ flown : bool ,
491
+ }
492
+
482
493
// TODO: We should refactor this to be an Inbound/OutboundChannel until initial setup handshaking
483
494
// has been completed, and then turn into a Channel to get compiler-time enforcement of things like
484
495
// calling channel_id() before we're set up or things like get_outbound_funding_signed on an
@@ -740,7 +751,7 @@ pub(super) struct Channel<Signer: ChannelSigner> {
740
751
/// If we then persist the [`channelmanager::ChannelManager`] and crash before the persistence
741
752
/// completes we still need to be able to complete the persistence. Thus, we have to keep a
742
753
/// copy of the [`ChannelMonitorUpdate`] here until it is complete.
743
- pending_monitor_updates : Vec < ChannelMonitorUpdate > ,
754
+ pending_monitor_updates : Vec < PendingChannelMonitorUpdate > ,
744
755
}
745
756
746
757
#[ cfg( any( test, fuzzing) ) ]
@@ -1967,6 +1978,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
1967
1978
}
1968
1979
1969
1980
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 {
1981
+ let fly_monitor = self . pending_monitor_updates . iter ( ) . all ( |upd| upd. flown ) ;
1970
1982
match self . get_update_fulfill_htlc ( htlc_id, payment_preimage, logger) {
1971
1983
UpdateFulfillFetch :: NewClaim { mut monitor_update, htlc_value_msat, msg : Some ( _) } => {
1972
1984
let mut additional_update = self . build_commitment_no_status_check ( logger) ;
@@ -1975,17 +1987,21 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
1975
1987
self . latest_monitor_update_id = monitor_update. update_id ;
1976
1988
monitor_update. updates . append ( & mut additional_update. updates ) ;
1977
1989
self . monitor_updating_paused ( false , true , false , Vec :: new ( ) , Vec :: new ( ) , Vec :: new ( ) ) ;
1978
- self . pending_monitor_updates . push ( monitor_update) ;
1990
+ self . pending_monitor_updates . push ( PendingChannelMonitorUpdate {
1991
+ update : monitor_update, flown : fly_monitor,
1992
+ } ) ;
1979
1993
UpdateFulfillCommitFetch :: NewClaim {
1980
- monitor_update : self . pending_monitor_updates . last ( ) . unwrap ( ) ,
1994
+ monitor_update_opt : if fly_monitor { self . pending_monitor_updates . last ( ) . map ( |upd| & upd . update ) } else { None } ,
1981
1995
htlc_value_msat,
1982
1996
}
1983
1997
} ,
1984
1998
UpdateFulfillFetch :: NewClaim { monitor_update, htlc_value_msat, msg : None } => {
1985
1999
self . monitor_updating_paused ( false , false , false , Vec :: new ( ) , Vec :: new ( ) , Vec :: new ( ) ) ;
1986
- self . pending_monitor_updates . push ( monitor_update) ;
2000
+ self . pending_monitor_updates . push ( PendingChannelMonitorUpdate {
2001
+ update : monitor_update, flown : fly_monitor
2002
+ } ) ;
1987
2003
UpdateFulfillCommitFetch :: NewClaim {
1988
- monitor_update : self . pending_monitor_updates . last ( ) . unwrap ( ) ,
2004
+ monitor_update_opt : if fly_monitor { self . pending_monitor_updates . last ( ) . map ( |upd| & upd . update ) } else { None } ,
1989
2005
htlc_value_msat,
1990
2006
}
1991
2007
}
@@ -3054,7 +3070,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3054
3070
Ok ( ( ) )
3055
3071
}
3056
3072
3057
- pub fn commitment_signed < L : Deref > ( & mut self , msg : & msgs:: CommitmentSigned , logger : & L ) -> Result < & ChannelMonitorUpdate , ChannelError >
3073
+ pub fn commitment_signed < L : Deref > ( & mut self , msg : & msgs:: CommitmentSigned , logger : & L ) -> Result < Option < & ChannelMonitorUpdate > , ChannelError >
3058
3074
where L :: Target : Logger
3059
3075
{
3060
3076
if ( self . channel_state & ( ChannelState :: ChannelReady as u32 ) ) != ( ChannelState :: ChannelReady as u32 ) {
@@ -3230,8 +3246,11 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3230
3246
}
3231
3247
log_debug ! ( logger, "Received valid commitment_signed from peer in channel {}, updated HTLC state but awaiting a monitor update resolution to reply." ,
3232
3248
log_bytes!( self . channel_id) ) ;
3233
- self . pending_monitor_updates . push ( monitor_update) ;
3234
- return Ok ( self . pending_monitor_updates . last ( ) . unwrap ( ) ) ;
3249
+ let fly_monitor = self . pending_monitor_updates . iter ( ) . all ( |upd| upd. flown ) ;
3250
+ self . pending_monitor_updates . push ( PendingChannelMonitorUpdate {
3251
+ update : monitor_update, flown : fly_monitor
3252
+ } ) ;
3253
+ return Ok ( if fly_monitor { self . pending_monitor_updates . last ( ) . map ( |upd| & upd. update ) } else { None } ) ;
3235
3254
}
3236
3255
3237
3256
let need_commitment_signed = if need_commitment && ( self . channel_state & ( ChannelState :: AwaitingRemoteRevoke as u32 ) ) == 0 {
@@ -3248,9 +3267,12 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3248
3267
3249
3268
log_debug ! ( logger, "Received valid commitment_signed from peer in channel {}, updating HTLC state and responding with{} a revoke_and_ack." ,
3250
3269
log_bytes!( self . channel_id( ) ) , if need_commitment_signed { " our own commitment_signed and" } else { "" } ) ;
3251
- self . pending_monitor_updates . push ( monitor_update) ;
3270
+ let fly_monitor = self . pending_monitor_updates . iter ( ) . all ( |upd| upd. flown ) ;
3271
+ self . pending_monitor_updates . push ( PendingChannelMonitorUpdate {
3272
+ update : monitor_update, flown : fly_monitor,
3273
+ } ) ;
3252
3274
self . monitor_updating_paused ( true , need_commitment_signed, false , Vec :: new ( ) , Vec :: new ( ) , Vec :: new ( ) ) ;
3253
- return Ok ( self . pending_monitor_updates . last ( ) . unwrap ( ) ) ;
3275
+ return Ok ( if fly_monitor { self . pending_monitor_updates . last ( ) . map ( |upd| & upd . update ) } else { None } ) ;
3254
3276
}
3255
3277
3256
3278
/// Public version of the below, checking relevant preconditions first.
@@ -3365,8 +3387,12 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3365
3387
update_add_htlcs. len( ) , update_fulfill_htlcs. len( ) , update_fail_htlcs. len( ) ) ;
3366
3388
3367
3389
self . monitor_updating_paused ( false , true , false , Vec :: new ( ) , Vec :: new ( ) , Vec :: new ( ) ) ;
3368
- self . pending_monitor_updates . push ( monitor_update) ;
3369
- ( Some ( self . pending_monitor_updates . last ( ) . unwrap ( ) ) , htlcs_to_fail)
3390
+ let fly_monitor = self . pending_monitor_updates . iter ( ) . all ( |upd| upd. flown ) ;
3391
+ self . pending_monitor_updates . push ( PendingChannelMonitorUpdate {
3392
+ update : monitor_update, flown : fly_monitor,
3393
+ } ) ;
3394
+ ( if fly_monitor { self . pending_monitor_updates . last ( ) . map ( |upd| & upd. update ) } else { None } ,
3395
+ htlcs_to_fail)
3370
3396
} else {
3371
3397
( None , Vec :: new ( ) )
3372
3398
}
@@ -3377,7 +3403,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3377
3403
/// waiting on this revoke_and_ack. The generation of this new commitment_signed may also fail,
3378
3404
/// generating an appropriate error *after* the channel state has been updated based on the
3379
3405
/// revoke_and_ack message.
3380
- pub fn revoke_and_ack < L : Deref > ( & mut self , msg : & msgs:: RevokeAndACK , logger : & L ) -> Result < ( Vec < ( HTLCSource , PaymentHash ) > , & ChannelMonitorUpdate ) , ChannelError >
3406
+ pub fn revoke_and_ack < L : Deref > ( & mut self , msg : & msgs:: RevokeAndACK , logger : & L ) -> Result < ( Vec < ( HTLCSource , PaymentHash ) > , Option < & ChannelMonitorUpdate > ) , ChannelError >
3381
3407
where L :: Target : Logger ,
3382
3408
{
3383
3409
if ( self . channel_state & ( ChannelState :: ChannelReady as u32 ) ) != ( ChannelState :: ChannelReady as u32 ) {
@@ -3574,21 +3600,29 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3574
3600
self . monitor_pending_failures . append ( & mut revoked_htlcs) ;
3575
3601
self . monitor_pending_finalized_fulfills . append ( & mut finalized_claimed_htlcs) ;
3576
3602
log_debug ! ( logger, "Received a valid revoke_and_ack for channel {} but awaiting a monitor update resolution to reply." , log_bytes!( self . channel_id( ) ) ) ;
3577
- self . pending_monitor_updates . push ( monitor_update) ;
3578
- return Ok ( ( Vec :: new ( ) , self . pending_monitor_updates . last ( ) . unwrap ( ) ) ) ;
3603
+ let fly_monitor = self . pending_monitor_updates . iter ( ) . all ( |upd| upd. flown ) ;
3604
+ self . pending_monitor_updates . push ( PendingChannelMonitorUpdate {
3605
+ update : monitor_update, flown : fly_monitor,
3606
+ } ) ;
3607
+ return Ok ( ( Vec :: new ( ) ,
3608
+ if fly_monitor { self . pending_monitor_updates . last ( ) . map ( |upd| & upd. update ) } else { None } ) ) ;
3579
3609
}
3580
3610
3581
3611
match self . free_holding_cell_htlcs ( logger) {
3582
3612
( Some ( _) , htlcs_to_fail) => {
3583
- let mut additional_update = self . pending_monitor_updates . pop ( ) . unwrap ( ) ;
3613
+ let mut additional_update = self . pending_monitor_updates . pop ( ) . unwrap ( ) . update ;
3584
3614
// free_holding_cell_htlcs may bump latest_monitor_id multiple times but we want them to be
3585
3615
// strictly increasing by one, so decrement it here.
3586
3616
self . latest_monitor_update_id = monitor_update. update_id ;
3587
3617
monitor_update. updates . append ( & mut additional_update. updates ) ;
3588
3618
3589
3619
self . monitor_updating_paused ( false , true , false , to_forward_infos, revoked_htlcs, finalized_claimed_htlcs) ;
3590
- self . pending_monitor_updates . push ( monitor_update) ;
3591
- Ok ( ( htlcs_to_fail, self . pending_monitor_updates . last ( ) . unwrap ( ) ) )
3620
+ let fly_monitor = self . pending_monitor_updates . iter ( ) . all ( |upd| upd. flown ) ;
3621
+ self . pending_monitor_updates . push ( PendingChannelMonitorUpdate {
3622
+ update : monitor_update, flown : fly_monitor,
3623
+ } ) ;
3624
+ Ok ( ( htlcs_to_fail,
3625
+ if fly_monitor { self . pending_monitor_updates . last ( ) . map ( |upd| & upd. update ) } else { None } ) )
3592
3626
} ,
3593
3627
( None , htlcs_to_fail) => {
3594
3628
if require_commitment {
@@ -3602,13 +3636,21 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3602
3636
log_debug ! ( logger, "Received a valid revoke_and_ack for channel {}. Responding with a commitment update with {} HTLCs failed." ,
3603
3637
log_bytes!( self . channel_id( ) ) , update_fail_htlcs. len( ) + update_fail_malformed_htlcs. len( ) ) ;
3604
3638
self . monitor_updating_paused ( false , true , false , to_forward_infos, revoked_htlcs, finalized_claimed_htlcs) ;
3605
- self . pending_monitor_updates . push ( monitor_update) ;
3606
- Ok ( ( htlcs_to_fail, self . pending_monitor_updates . last ( ) . unwrap ( ) ) )
3639
+ let fly_monitor = self . pending_monitor_updates . iter ( ) . all ( |upd| upd. flown ) ;
3640
+ self . pending_monitor_updates . push ( PendingChannelMonitorUpdate {
3641
+ update : monitor_update, flown : fly_monitor,
3642
+ } ) ;
3643
+ Ok ( ( htlcs_to_fail,
3644
+ if fly_monitor { self . pending_monitor_updates . last ( ) . map ( |upd| & upd. update ) } else { None } ) )
3607
3645
} else {
3608
3646
log_debug ! ( logger, "Received a valid revoke_and_ack for channel {} with no reply necessary." , log_bytes!( self . channel_id( ) ) ) ;
3609
3647
self . monitor_updating_paused ( false , false , false , to_forward_infos, revoked_htlcs, finalized_claimed_htlcs) ;
3610
- self . pending_monitor_updates . push ( monitor_update) ;
3611
- Ok ( ( htlcs_to_fail, self . pending_monitor_updates . last ( ) . unwrap ( ) ) )
3648
+ let fly_monitor = self . pending_monitor_updates . iter ( ) . all ( |upd| upd. flown ) ;
3649
+ self . pending_monitor_updates . push ( PendingChannelMonitorUpdate {
3650
+ update : monitor_update, flown : fly_monitor,
3651
+ } ) ;
3652
+ Ok ( ( htlcs_to_fail,
3653
+ if fly_monitor { self . pending_monitor_updates . last ( ) . map ( |upd| & upd. update ) } else { None } ) )
3612
3654
}
3613
3655
}
3614
3656
}
@@ -3797,7 +3839,12 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3797
3839
{
3798
3840
assert_eq ! ( self . channel_state & ChannelState :: MonitorUpdateInProgress as u32 , ChannelState :: MonitorUpdateInProgress as u32 ) ;
3799
3841
self . channel_state &= !( ChannelState :: MonitorUpdateInProgress as u32 ) ;
3800
- self . pending_monitor_updates . clear ( ) ;
3842
+ let mut found_unflown = false ;
3843
+ self . pending_monitor_updates . retain ( |upd| {
3844
+ if found_unflown { debug_assert ! ( !upd. flown, "No mons may fly after one is paused" ) ; }
3845
+ if !upd. flown { found_unflown = true ; }
3846
+ !upd. flown
3847
+ } ) ;
3801
3848
3802
3849
// If we're past (or at) the FundingSent stage on an outbound channel, try to
3803
3850
// (re-)broadcast the funding transaction as we may have declined to broadcast it when we
@@ -4338,8 +4385,11 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
4338
4385
} ] ,
4339
4386
} ;
4340
4387
self . monitor_updating_paused ( false , false , false , Vec :: new ( ) , Vec :: new ( ) , Vec :: new ( ) ) ;
4341
- self . pending_monitor_updates . push ( monitor_update) ;
4342
- Some ( self . pending_monitor_updates . last ( ) . unwrap ( ) )
4388
+ let fly_monitor = self . pending_monitor_updates . iter ( ) . all ( |upd| upd. flown ) ;
4389
+ self . pending_monitor_updates . push ( PendingChannelMonitorUpdate {
4390
+ update : monitor_update, flown : fly_monitor,
4391
+ } ) ;
4392
+ if fly_monitor { self . pending_monitor_updates . last ( ) . map ( |upd| & upd. update ) } else { None }
4343
4393
} else { None } ;
4344
4394
let shutdown = if send_shutdown {
4345
4395
Some ( msgs:: Shutdown {
@@ -4889,8 +4939,25 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
4889
4939
( self . channel_state & ChannelState :: MonitorUpdateInProgress as u32 ) != 0
4890
4940
}
4891
4941
4892
- pub fn get_next_monitor_update ( & self ) -> Option < & ChannelMonitorUpdate > {
4893
- self . pending_monitor_updates . first ( )
4942
+ /// Returns the next unflown monitor update, if one exists, and a bool which indicates a
4943
+ /// further unflown monitor update exists after the next.
4944
+ pub fn fly_next_unflown_monitor_update ( & mut self ) -> Option < ( & ChannelMonitorUpdate , bool ) > {
4945
+ for i in 0 ..self . pending_monitor_updates . len ( ) {
4946
+ if !self . pending_monitor_updates [ i] . flown {
4947
+ self . pending_monitor_updates [ i] . flown = true ;
4948
+ return Some ( ( & self . pending_monitor_updates [ i] . update ,
4949
+ self . pending_monitor_updates . len ( ) > i + 1 ) ) ;
4950
+ }
4951
+ }
4952
+ None
4953
+ }
4954
+
4955
+ pub fn no_monitor_updates_pending ( & self ) -> bool {
4956
+ self . pending_monitor_updates . is_empty ( )
4957
+ }
4958
+
4959
+ pub fn complete_one_mon_update ( & mut self , update_id : u64 ) {
4960
+ self . pending_monitor_updates . retain ( |upd| upd. update . update_id != update_id) ;
4894
4961
}
4895
4962
4896
4963
/// Returns true if funding_created was sent/received.
@@ -5930,8 +5997,12 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
5930
5997
Some ( _) => {
5931
5998
let monitor_update = self . build_commitment_no_status_check ( logger) ;
5932
5999
self . monitor_updating_paused ( false , true , false , Vec :: new ( ) , Vec :: new ( ) , Vec :: new ( ) ) ;
5933
- self . pending_monitor_updates . push ( monitor_update) ;
5934
- Ok ( Some ( self . pending_monitor_updates . last ( ) . unwrap ( ) ) )
6000
+
6001
+ let fly_monitor = self . pending_monitor_updates . iter ( ) . all ( |upd| upd. flown ) ;
6002
+ self . pending_monitor_updates . push ( PendingChannelMonitorUpdate {
6003
+ update : monitor_update, flown : fly_monitor,
6004
+ } ) ;
6005
+ Ok ( if fly_monitor { self . pending_monitor_updates . last ( ) . map ( |upd| & upd. update ) } else { None } )
5935
6006
} ,
5936
6007
None => Ok ( None )
5937
6008
}
@@ -6020,8 +6091,11 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
6020
6091
} ] ,
6021
6092
} ;
6022
6093
self . monitor_updating_paused ( false , false , false , Vec :: new ( ) , Vec :: new ( ) , Vec :: new ( ) ) ;
6023
- self . pending_monitor_updates . push ( monitor_update) ;
6024
- Some ( self . pending_monitor_updates . last ( ) . unwrap ( ) )
6094
+ let fly_monitor = self . pending_monitor_updates . iter ( ) . all ( |upd| upd. flown ) ;
6095
+ self . pending_monitor_updates . push ( PendingChannelMonitorUpdate {
6096
+ update : monitor_update, flown : fly_monitor,
6097
+ } ) ;
6098
+ if fly_monitor { self . pending_monitor_updates . last ( ) . map ( |upd| & upd. update ) } else { None }
6025
6099
} else { None } ;
6026
6100
let shutdown = msgs:: Shutdown {
6027
6101
channel_id : self . channel_id ,
0 commit comments