@@ -237,19 +237,21 @@ pub(super) struct Channel {
237
237
cur_local_commitment_transaction_number : u64 ,
238
238
cur_remote_commitment_transaction_number : u64 ,
239
239
value_to_self_msat : u64 , // Excluding all pending_htlcs, excluding fees
240
- /// Upon receipt of a channel_reestablish we have to figure out whether to send a
241
- /// revoke_and_ack first or a commitment update first. Generally, we prefer to send
242
- /// revoke_and_ack first, but if we had a pending commitment update of our own waiting on a
243
- /// remote revoke when we received the latest commitment update from the remote we have to make
244
- /// sure that commitment update gets resent first.
245
- received_commitment_while_awaiting_raa : bool ,
246
240
pending_inbound_htlcs : Vec < InboundHTLCOutput > ,
247
241
pending_outbound_htlcs : Vec < OutboundHTLCOutput > ,
248
242
holding_cell_htlc_updates : Vec < HTLCUpdateAwaitingACK > ,
249
243
244
+ /// When resending CS/RAA messages on channel monitor restoration or on reconnect, we always
245
+ /// need to ensure we resend them in the order we originally generated them. Note that because
246
+ /// there can only ever be one in-flight CS and/or one in-flight RAA at any time, it is
247
+ /// sufficient to simply set this to the opposite of any message we are generating as we
248
+ /// generate it. ie when we generate a CS, we set this to RAAFirst as, if there is a pending
249
+ /// in-flight RAA to resend, it will have been the first thing we generated, and thus we should
250
+ /// send it first.
251
+ resend_order : RAACommitmentOrder ,
252
+
250
253
monitor_pending_revoke_and_ack : bool ,
251
254
monitor_pending_commitment_signed : bool ,
252
- monitor_pending_order : Option < RAACommitmentOrder > ,
253
255
monitor_pending_forwards : Vec < ( PendingForwardHTLCInfo , u64 ) > ,
254
256
monitor_pending_failures : Vec < ( HTLCSource , PaymentHash , HTLCFailReason ) > ,
255
257
@@ -450,7 +452,6 @@ impl Channel {
450
452
cur_local_commitment_transaction_number : INITIAL_COMMITMENT_NUMBER ,
451
453
cur_remote_commitment_transaction_number : INITIAL_COMMITMENT_NUMBER ,
452
454
value_to_self_msat : channel_value_satoshis * 1000 - push_msat,
453
- received_commitment_while_awaiting_raa : false ,
454
455
455
456
pending_inbound_htlcs : Vec :: new ( ) ,
456
457
pending_outbound_htlcs : Vec :: new ( ) ,
@@ -461,9 +462,10 @@ impl Channel {
461
462
next_remote_htlc_id : 0 ,
462
463
channel_update_count : 1 ,
463
464
465
+ resend_order : RAACommitmentOrder :: CommitmentFirst ,
466
+
464
467
monitor_pending_revoke_and_ack : false ,
465
468
monitor_pending_commitment_signed : false ,
466
- monitor_pending_order : None ,
467
469
monitor_pending_forwards : Vec :: new ( ) ,
468
470
monitor_pending_failures : Vec :: new ( ) ,
469
471
@@ -639,7 +641,6 @@ impl Channel {
639
641
cur_local_commitment_transaction_number : INITIAL_COMMITMENT_NUMBER ,
640
642
cur_remote_commitment_transaction_number : INITIAL_COMMITMENT_NUMBER ,
641
643
value_to_self_msat : msg. push_msat ,
642
- received_commitment_while_awaiting_raa : false ,
643
644
644
645
pending_inbound_htlcs : Vec :: new ( ) ,
645
646
pending_outbound_htlcs : Vec :: new ( ) ,
@@ -650,9 +651,10 @@ impl Channel {
650
651
next_remote_htlc_id : 0 ,
651
652
channel_update_count : 1 ,
652
653
654
+ resend_order : RAACommitmentOrder :: CommitmentFirst ,
655
+
653
656
monitor_pending_revoke_and_ack : false ,
654
657
monitor_pending_commitment_signed : false ,
655
- monitor_pending_order : None ,
656
658
monitor_pending_forwards : Vec :: new ( ) ,
657
659
monitor_pending_failures : Vec :: new ( ) ,
658
660
@@ -1805,12 +1807,6 @@ impl Channel {
1805
1807
}
1806
1808
}
1807
1809
1808
- if self . channel_state & ( ChannelState :: MonitorUpdateFailed as u32 ) == 0 {
1809
- // This is a response to our post-monitor-failed unfreeze messages, so we can clear the
1810
- // monitor_pending_order requirement as we won't re-send the monitor_pending messages.
1811
- self . monitor_pending_order = None ;
1812
- }
1813
-
1814
1810
self . channel_monitor . provide_latest_local_commitment_tx_info ( local_commitment_tx. 0 , local_keys, self . feerate_per_kw , htlcs_and_sigs) ;
1815
1811
1816
1812
for htlc in self . pending_inbound_htlcs . iter_mut ( ) {
@@ -1833,14 +1829,13 @@ impl Channel {
1833
1829
1834
1830
self . cur_local_commitment_transaction_number -= 1 ;
1835
1831
self . last_local_commitment_txn = new_local_commitment_txn;
1836
- self . received_commitment_while_awaiting_raa = ( self . channel_state & ( ChannelState :: AwaitingRemoteRevoke as u32 ) ) != 0 ;
1832
+ // Note that if we need_our_commitment & !AwaitingRemoteRevoke we'll call
1833
+ // send_commitment_no_status_check() next which will reset this to RAAFirst.
1834
+ self . resend_order = RAACommitmentOrder :: CommitmentFirst ;
1837
1835
1838
1836
if ( self . channel_state & ChannelState :: MonitorUpdateFailed as u32 ) != 0 {
1839
1837
// In case we initially failed monitor updating without requiring a response, we need
1840
1838
// to make sure the RAA gets sent first.
1841
- if !self . monitor_pending_commitment_signed {
1842
- self . monitor_pending_order = Some ( RAACommitmentOrder :: RevokeAndACKFirst ) ;
1843
- }
1844
1839
self . monitor_pending_revoke_and_ack = true ;
1845
1840
if need_our_commitment && ( self . channel_state & ( ChannelState :: AwaitingRemoteRevoke as u32 ) ) == 0 {
1846
1841
// If we were going to send a commitment_signed after the RAA, go ahead and do all
@@ -2014,12 +2009,6 @@ impl Channel {
2014
2009
self . their_prev_commitment_point = self . their_cur_commitment_point ;
2015
2010
self . their_cur_commitment_point = Some ( msg. next_per_commitment_point ) ;
2016
2011
self . cur_remote_commitment_transaction_number -= 1 ;
2017
- self . received_commitment_while_awaiting_raa = false ;
2018
- if self . channel_state & ( ChannelState :: MonitorUpdateFailed as u32 ) == 0 {
2019
- // This is a response to our post-monitor-failed unfreeze messages, so we can clear the
2020
- // monitor_pending_order requirement as we won't re-send the monitor_pending messages.
2021
- self . monitor_pending_order = None ;
2022
- }
2023
2012
2024
2013
log_trace ! ( self , "Updating HTLCs on receipt of RAA..." ) ;
2025
2014
let mut to_forward_infos = Vec :: new ( ) ;
@@ -2137,7 +2126,7 @@ impl Channel {
2137
2126
// When the monitor updating is restored we'll call get_last_commitment_update(),
2138
2127
// which does not update state, but we're definitely now awaiting a remote revoke
2139
2128
// before we can step forward any more, so set it here.
2140
- self . channel_state |= ChannelState :: AwaitingRemoteRevoke as u32 ;
2129
+ self . send_commitment_no_status_check ( ) ? ;
2141
2130
}
2142
2131
self . monitor_pending_forwards . append ( & mut to_forward_infos) ;
2143
2132
self . monitor_pending_failures . append ( & mut revoked_htlcs) ;
@@ -2285,15 +2274,13 @@ impl Channel {
2285
2274
/// Indicates that a ChannelMonitor update failed to be stored by the client and further
2286
2275
/// updates are partially paused.
2287
2276
/// This must be called immediately after the call which generated the ChannelMonitor update
2288
- /// which failed, with the order argument set to the type of call it represented (ie a
2289
- /// commitment update or a revoke_and_ack generation). The messages which were generated from
2290
- /// that original call must *not* have been sent to the remote end, and must instead have been
2291
- /// dropped. They will be regenerated when monitor_updating_restored is called.
2292
- pub fn monitor_update_failed ( & mut self , order : RAACommitmentOrder , resend_raa : bool , resend_commitment : bool , mut pending_forwards : Vec < ( PendingForwardHTLCInfo , u64 ) > , mut pending_fails : Vec < ( HTLCSource , PaymentHash , HTLCFailReason ) > ) {
2277
+ /// which failed. The messages which were generated from that call which generated the
2278
+ /// monitor update failure must *not* have been sent to the remote end, and must instead
2279
+ /// have been dropped. They will be regenerated when monitor_updating_restored is called.
2280
+ pub fn monitor_update_failed ( & mut self , resend_raa : bool , resend_commitment : bool , mut pending_forwards : Vec < ( PendingForwardHTLCInfo , u64 ) > , mut pending_fails : Vec < ( HTLCSource , PaymentHash , HTLCFailReason ) > ) {
2293
2281
assert_eq ! ( self . channel_state & ChannelState :: MonitorUpdateFailed as u32 , 0 ) ;
2294
2282
self . monitor_pending_revoke_and_ack = resend_raa;
2295
2283
self . monitor_pending_commitment_signed = resend_commitment;
2296
- self . monitor_pending_order = Some ( order) ;
2297
2284
assert ! ( self . monitor_pending_forwards. is_empty( ) ) ;
2298
2285
mem:: swap ( & mut pending_forwards, & mut self . monitor_pending_forwards ) ;
2299
2286
assert ! ( self . monitor_pending_failures. is_empty( ) ) ;
@@ -2314,7 +2301,6 @@ impl Channel {
2314
2301
mem:: swap ( & mut failures, & mut self . monitor_pending_failures ) ;
2315
2302
2316
2303
if self . channel_state & ( ChannelState :: PeerDisconnected as u32 ) != 0 {
2317
- // Leave monitor_pending_order so we can order our channel_reestablish responses
2318
2304
self . monitor_pending_revoke_and_ack = false ;
2319
2305
self . monitor_pending_commitment_signed = false ;
2320
2306
return ( None , None , RAACommitmentOrder :: RevokeAndACKFirst , forwards, failures) ;
@@ -2329,7 +2315,12 @@ impl Channel {
2329
2315
2330
2316
self . monitor_pending_revoke_and_ack = false ;
2331
2317
self . monitor_pending_commitment_signed = false ;
2332
- ( raa, commitment_update, self . monitor_pending_order . clone ( ) . unwrap ( ) , forwards, failures)
2318
+ let order = self . resend_order . clone ( ) ;
2319
+ log_trace ! ( self , "Restored monitor updating resulting in {} commitment update and {} RAA, with {} first" ,
2320
+ if commitment_update. is_some( ) { "a" } else { "no" } ,
2321
+ if raa. is_some( ) { "an" } else { "no" } ,
2322
+ match order { RAACommitmentOrder :: CommitmentFirst => "commitment" , RAACommitmentOrder :: RevokeAndACKFirst => "RAA" } ) ;
2323
+ ( raa, commitment_update, order, forwards, failures)
2333
2324
}
2334
2325
2335
2326
pub fn update_fee ( & mut self , fee_estimator : & FeeEstimator , msg : & msgs:: UpdateFee ) -> Result < ( ) , ChannelError > {
@@ -2487,33 +2478,26 @@ impl Channel {
2487
2478
} )
2488
2479
} else { None } ;
2489
2480
2490
- let order = self . monitor_pending_order . clone ( ) . unwrap_or ( if self . received_commitment_while_awaiting_raa {
2491
- RAACommitmentOrder :: CommitmentFirst
2492
- } else {
2493
- RAACommitmentOrder :: RevokeAndACKFirst
2494
- } ) ;
2495
-
2496
2481
if msg. next_local_commitment_number == our_next_remote_commitment_number {
2497
2482
if required_revoke. is_some ( ) {
2498
2483
log_debug ! ( self , "Reconnected channel {} with only lost outbound RAA" , log_bytes!( self . channel_id( ) ) ) ;
2499
2484
} else {
2500
2485
log_debug ! ( self , "Reconnected channel {} with no loss" , log_bytes!( self . channel_id( ) ) ) ;
2501
2486
}
2502
2487
2503
- if ( self . channel_state & ( ChannelState :: AwaitingRemoteRevoke as u32 | ChannelState :: MonitorUpdateFailed as u32 ) ) == 0 &&
2504
- self . monitor_pending_order . is_none ( ) { // monitor_pending_order indicates we're waiting on a response to a unfreeze
2488
+ if ( self . channel_state & ( ChannelState :: AwaitingRemoteRevoke as u32 | ChannelState :: MonitorUpdateFailed as u32 ) ) == 0 {
2505
2489
// We're up-to-date and not waiting on a remote revoke (if we are our
2506
2490
// channel_reestablish should result in them sending a revoke_and_ack), but we may
2507
2491
// have received some updates while we were disconnected. Free the holding cell
2508
2492
// now!
2509
2493
match self . free_holding_cell_htlcs ( ) {
2510
2494
Err ( ChannelError :: Close ( msg) ) => return Err ( ChannelError :: Close ( msg) ) ,
2511
2495
Err ( ChannelError :: Ignore ( _) ) => panic ! ( "Got non-channel-failing result from free_holding_cell_htlcs" ) ,
2512
- Ok ( Some ( ( commitment_update, channel_monitor) ) ) => return Ok ( ( resend_funding_locked, required_revoke, Some ( commitment_update) , Some ( channel_monitor) , order , shutdown_msg) ) ,
2513
- Ok ( None ) => return Ok ( ( resend_funding_locked, required_revoke, None , None , order , shutdown_msg) ) ,
2496
+ Ok ( Some ( ( commitment_update, channel_monitor) ) ) => return Ok ( ( resend_funding_locked, required_revoke, Some ( commitment_update) , Some ( channel_monitor) , self . resend_order . clone ( ) , shutdown_msg) ) ,
2497
+ Ok ( None ) => return Ok ( ( resend_funding_locked, required_revoke, None , None , self . resend_order . clone ( ) , shutdown_msg) ) ,
2514
2498
}
2515
2499
} else {
2516
- return Ok ( ( resend_funding_locked, required_revoke, None , None , order , shutdown_msg) ) ;
2500
+ return Ok ( ( resend_funding_locked, required_revoke, None , None , self . resend_order . clone ( ) , shutdown_msg) ) ;
2517
2501
}
2518
2502
} else if msg. next_local_commitment_number == our_next_remote_commitment_number - 1 {
2519
2503
if required_revoke. is_some ( ) {
@@ -2524,10 +2508,10 @@ impl Channel {
2524
2508
2525
2509
if self . channel_state & ( ChannelState :: MonitorUpdateFailed as u32 ) != 0 {
2526
2510
self . monitor_pending_commitment_signed = true ;
2527
- return Ok ( ( resend_funding_locked, None , None , None , order , shutdown_msg) ) ;
2511
+ return Ok ( ( resend_funding_locked, None , None , None , self . resend_order . clone ( ) , shutdown_msg) ) ;
2528
2512
}
2529
2513
2530
- return Ok ( ( resend_funding_locked, required_revoke, Some ( self . get_last_commitment_update ( ) ) , None , order , shutdown_msg) ) ;
2514
+ return Ok ( ( resend_funding_locked, required_revoke, Some ( self . get_last_commitment_update ( ) ) , None , self . resend_order . clone ( ) , shutdown_msg) ) ;
2531
2515
} else {
2532
2516
return Err ( ChannelError :: Close ( "Peer attempted to reestablish channel with a very old remote commitment transaction" ) ) ;
2533
2517
}
@@ -3348,6 +3332,7 @@ impl Channel {
3348
3332
htlc. state = OutboundHTLCState :: AwaitingRemovedRemoteRevoke ( fail_reason) ;
3349
3333
}
3350
3334
}
3335
+ self . resend_order = RAACommitmentOrder :: RevokeAndACKFirst ;
3351
3336
3352
3337
let ( res, remote_commitment_tx, htlcs) = match self . send_commitment_no_state_update ( ) {
3353
3338
Ok ( ( res, ( remote_commitment_tx, mut htlcs) ) ) => {
@@ -3558,8 +3543,6 @@ impl Writeable for Channel {
3558
3543
self . cur_remote_commitment_transaction_number . write ( writer) ?;
3559
3544
self . value_to_self_msat . write ( writer) ?;
3560
3545
3561
- self . received_commitment_while_awaiting_raa . write ( writer) ?;
3562
-
3563
3546
let mut dropped_inbound_htlcs = 0 ;
3564
3547
for htlc in self . pending_inbound_htlcs . iter ( ) {
3565
3548
if let InboundHTLCState :: RemoteAnnounced ( _) = htlc. state {
@@ -3659,13 +3642,13 @@ impl Writeable for Channel {
3659
3642
}
3660
3643
}
3661
3644
3645
+ match self . resend_order {
3646
+ RAACommitmentOrder :: CommitmentFirst => 0u8 . write ( writer) ?,
3647
+ RAACommitmentOrder :: RevokeAndACKFirst => 1u8 . write ( writer) ?,
3648
+ }
3649
+
3662
3650
self . monitor_pending_revoke_and_ack . write ( writer) ?;
3663
3651
self . monitor_pending_commitment_signed . write ( writer) ?;
3664
- match self . monitor_pending_order {
3665
- None => 0u8 . write ( writer) ?,
3666
- Some ( RAACommitmentOrder :: CommitmentFirst ) => 1u8 . write ( writer) ?,
3667
- Some ( RAACommitmentOrder :: RevokeAndACKFirst ) => 2u8 . write ( writer) ?,
3668
- }
3669
3652
3670
3653
( self . monitor_pending_forwards . len ( ) as u64 ) . write ( writer) ?;
3671
3654
for & ( ref pending_forward, ref htlc_id) in self . monitor_pending_forwards . iter ( ) {
@@ -3763,8 +3746,6 @@ impl<R : ::std::io::Read> ReadableArgs<R, Arc<Logger>> for Channel {
3763
3746
let cur_remote_commitment_transaction_number = Readable :: read ( reader) ?;
3764
3747
let value_to_self_msat = Readable :: read ( reader) ?;
3765
3748
3766
- let received_commitment_while_awaiting_raa = Readable :: read ( reader) ?;
3767
-
3768
3749
let pending_inbound_htlc_count: u64 = Readable :: read ( reader) ?;
3769
3750
let mut pending_inbound_htlcs = Vec :: with_capacity ( cmp:: min ( pending_inbound_htlc_count as usize , OUR_MAX_HTLCS as usize ) ) ;
3770
3751
for _ in 0 ..pending_inbound_htlc_count {
@@ -3827,16 +3808,15 @@ impl<R : ::std::io::Read> ReadableArgs<R, Arc<Logger>> for Channel {
3827
3808
} ) ;
3828
3809
}
3829
3810
3830
- let monitor_pending_revoke_and_ack = Readable :: read ( reader) ?;
3831
- let monitor_pending_commitment_signed = Readable :: read ( reader) ?;
3832
-
3833
- let monitor_pending_order = match <u8 as Readable < R > >:: read ( reader) ? {
3834
- 0 => None ,
3835
- 1 => Some ( RAACommitmentOrder :: CommitmentFirst ) ,
3836
- 2 => Some ( RAACommitmentOrder :: RevokeAndACKFirst ) ,
3811
+ let resend_order = match <u8 as Readable < R > >:: read ( reader) ? {
3812
+ 0 => RAACommitmentOrder :: CommitmentFirst ,
3813
+ 1 => RAACommitmentOrder :: RevokeAndACKFirst ,
3837
3814
_ => return Err ( DecodeError :: InvalidValue ) ,
3838
3815
} ;
3839
3816
3817
+ let monitor_pending_revoke_and_ack = Readable :: read ( reader) ?;
3818
+ let monitor_pending_commitment_signed = Readable :: read ( reader) ?;
3819
+
3840
3820
let monitor_pending_forwards_count: u64 = Readable :: read ( reader) ?;
3841
3821
let mut monitor_pending_forwards = Vec :: with_capacity ( cmp:: min ( monitor_pending_forwards_count as usize , OUR_MAX_HTLCS as usize ) ) ;
3842
3822
for _ in 0 ..monitor_pending_forwards_count {
@@ -3923,14 +3903,14 @@ impl<R : ::std::io::Read> ReadableArgs<R, Arc<Logger>> for Channel {
3923
3903
cur_remote_commitment_transaction_number,
3924
3904
value_to_self_msat,
3925
3905
3926
- received_commitment_while_awaiting_raa,
3927
3906
pending_inbound_htlcs,
3928
3907
pending_outbound_htlcs,
3929
3908
holding_cell_htlc_updates,
3930
3909
3910
+ resend_order,
3911
+
3931
3912
monitor_pending_revoke_and_ack,
3932
3913
monitor_pending_commitment_signed,
3933
- monitor_pending_order,
3934
3914
monitor_pending_forwards,
3935
3915
monitor_pending_failures,
3936
3916
0 commit comments