@@ -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
@@ -457,7 +459,6 @@ impl Channel {
457
459
cur_local_commitment_transaction_number : INITIAL_COMMITMENT_NUMBER ,
458
460
cur_remote_commitment_transaction_number : INITIAL_COMMITMENT_NUMBER ,
459
461
value_to_self_msat : channel_value_satoshis * 1000 - push_msat,
460
- received_commitment_while_awaiting_raa : false ,
461
462
462
463
pending_inbound_htlcs : Vec :: new ( ) ,
463
464
pending_outbound_htlcs : Vec :: new ( ) ,
@@ -468,9 +469,10 @@ impl Channel {
468
469
next_remote_htlc_id : 0 ,
469
470
channel_update_count : 1 ,
470
471
472
+ resend_order : RAACommitmentOrder :: CommitmentFirst ,
473
+
471
474
monitor_pending_revoke_and_ack : false ,
472
475
monitor_pending_commitment_signed : false ,
473
- monitor_pending_order : None ,
474
476
monitor_pending_forwards : Vec :: new ( ) ,
475
477
monitor_pending_failures : Vec :: new ( ) ,
476
478
@@ -646,7 +648,6 @@ impl Channel {
646
648
cur_local_commitment_transaction_number : INITIAL_COMMITMENT_NUMBER ,
647
649
cur_remote_commitment_transaction_number : INITIAL_COMMITMENT_NUMBER ,
648
650
value_to_self_msat : msg. push_msat ,
649
- received_commitment_while_awaiting_raa : false ,
650
651
651
652
pending_inbound_htlcs : Vec :: new ( ) ,
652
653
pending_outbound_htlcs : Vec :: new ( ) ,
@@ -657,9 +658,10 @@ impl Channel {
657
658
next_remote_htlc_id : 0 ,
658
659
channel_update_count : 1 ,
659
660
661
+ resend_order : RAACommitmentOrder :: CommitmentFirst ,
662
+
660
663
monitor_pending_revoke_and_ack : false ,
661
664
monitor_pending_commitment_signed : false ,
662
- monitor_pending_order : None ,
663
665
monitor_pending_forwards : Vec :: new ( ) ,
664
666
monitor_pending_failures : Vec :: new ( ) ,
665
667
@@ -1812,12 +1814,6 @@ impl Channel {
1812
1814
}
1813
1815
}
1814
1816
1815
- if self . channel_state & ( ChannelState :: MonitorUpdateFailed as u32 ) == 0 {
1816
- // This is a response to our post-monitor-failed unfreeze messages, so we can clear the
1817
- // monitor_pending_order requirement as we won't re-send the monitor_pending messages.
1818
- self . monitor_pending_order = None ;
1819
- }
1820
-
1821
1817
self . channel_monitor . provide_latest_local_commitment_tx_info ( local_commitment_tx. 0 , local_keys, self . feerate_per_kw , htlcs_and_sigs) ;
1822
1818
1823
1819
for htlc in self . pending_inbound_htlcs . iter_mut ( ) {
@@ -1840,14 +1836,13 @@ impl Channel {
1840
1836
1841
1837
self . cur_local_commitment_transaction_number -= 1 ;
1842
1838
self . last_local_commitment_txn = new_local_commitment_txn;
1843
- self . received_commitment_while_awaiting_raa = ( self . channel_state & ( ChannelState :: AwaitingRemoteRevoke as u32 ) ) != 0 ;
1839
+ // Note that if we need_our_commitment & !AwaitingRemoteRevoke we'll call
1840
+ // send_commitment_no_status_check() next which will reset this to RAAFirst.
1841
+ self . resend_order = RAACommitmentOrder :: CommitmentFirst ;
1844
1842
1845
1843
if ( self . channel_state & ChannelState :: MonitorUpdateFailed as u32 ) != 0 {
1846
1844
// In case we initially failed monitor updating without requiring a response, we need
1847
1845
// to make sure the RAA gets sent first.
1848
- if !self . monitor_pending_commitment_signed {
1849
- self . monitor_pending_order = Some ( RAACommitmentOrder :: RevokeAndACKFirst ) ;
1850
- }
1851
1846
self . monitor_pending_revoke_and_ack = true ;
1852
1847
if need_our_commitment && ( self . channel_state & ( ChannelState :: AwaitingRemoteRevoke as u32 ) ) == 0 {
1853
1848
// If we were going to send a commitment_signed after the RAA, go ahead and do all
@@ -2021,12 +2016,6 @@ impl Channel {
2021
2016
self . their_prev_commitment_point = self . their_cur_commitment_point ;
2022
2017
self . their_cur_commitment_point = Some ( msg. next_per_commitment_point ) ;
2023
2018
self . cur_remote_commitment_transaction_number -= 1 ;
2024
- self . received_commitment_while_awaiting_raa = false ;
2025
- if self . channel_state & ( ChannelState :: MonitorUpdateFailed as u32 ) == 0 {
2026
- // This is a response to our post-monitor-failed unfreeze messages, so we can clear the
2027
- // monitor_pending_order requirement as we won't re-send the monitor_pending messages.
2028
- self . monitor_pending_order = None ;
2029
- }
2030
2019
2031
2020
log_trace ! ( self , "Updating HTLCs on receipt of RAA..." ) ;
2032
2021
let mut to_forward_infos = Vec :: new ( ) ;
@@ -2144,7 +2133,7 @@ impl Channel {
2144
2133
// When the monitor updating is restored we'll call get_last_commitment_update(),
2145
2134
// which does not update state, but we're definitely now awaiting a remote revoke
2146
2135
// before we can step forward any more, so set it here.
2147
- self . channel_state |= ChannelState :: AwaitingRemoteRevoke as u32 ;
2136
+ self . send_commitment_no_status_check ( ) ? ;
2148
2137
}
2149
2138
self . monitor_pending_forwards . append ( & mut to_forward_infos) ;
2150
2139
self . monitor_pending_failures . append ( & mut revoked_htlcs) ;
@@ -2292,15 +2281,13 @@ impl Channel {
2292
2281
/// Indicates that a ChannelMonitor update failed to be stored by the client and further
2293
2282
/// updates are partially paused.
2294
2283
/// This must be called immediately after the call which generated the ChannelMonitor update
2295
- /// which failed, with the order argument set to the type of call it represented (ie a
2296
- /// commitment update or a revoke_and_ack generation). The messages which were generated from
2297
- /// that original call must *not* have been sent to the remote end, and must instead have been
2298
- /// dropped. They will be regenerated when monitor_updating_restored is called.
2299
- 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 ) > ) {
2284
+ /// which failed. The messages which were generated from that call which generated the
2285
+ /// monitor update failure must *not* have been sent to the remote end, and must instead
2286
+ /// have been dropped. They will be regenerated when monitor_updating_restored is called.
2287
+ 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 ) > ) {
2300
2288
assert_eq ! ( self . channel_state & ChannelState :: MonitorUpdateFailed as u32 , 0 ) ;
2301
2289
self . monitor_pending_revoke_and_ack = resend_raa;
2302
2290
self . monitor_pending_commitment_signed = resend_commitment;
2303
- self . monitor_pending_order = Some ( order) ;
2304
2291
assert ! ( self . monitor_pending_forwards. is_empty( ) ) ;
2305
2292
mem:: swap ( & mut pending_forwards, & mut self . monitor_pending_forwards ) ;
2306
2293
assert ! ( self . monitor_pending_failures. is_empty( ) ) ;
@@ -2321,7 +2308,6 @@ impl Channel {
2321
2308
mem:: swap ( & mut failures, & mut self . monitor_pending_failures ) ;
2322
2309
2323
2310
if self . channel_state & ( ChannelState :: PeerDisconnected as u32 ) != 0 {
2324
- // Leave monitor_pending_order so we can order our channel_reestablish responses
2325
2311
self . monitor_pending_revoke_and_ack = false ;
2326
2312
self . monitor_pending_commitment_signed = false ;
2327
2313
return ( None , None , RAACommitmentOrder :: RevokeAndACKFirst , forwards, failures) ;
@@ -2336,7 +2322,7 @@ impl Channel {
2336
2322
2337
2323
self . monitor_pending_revoke_and_ack = false ;
2338
2324
self . monitor_pending_commitment_signed = false ;
2339
- let order = self . monitor_pending_order . clone ( ) . unwrap ( ) ;
2325
+ let order = self . resend_order . clone ( ) ;
2340
2326
log_trace ! ( self , "Restored monitor updating resulting in {} commitment update and {} RAA, with {} first" ,
2341
2327
if commitment_update. is_some( ) { "a" } else { "no" } ,
2342
2328
if raa. is_some( ) { "an" } else { "no" } ,
@@ -2499,33 +2485,26 @@ impl Channel {
2499
2485
} )
2500
2486
} else { None } ;
2501
2487
2502
- let order = self . monitor_pending_order . clone ( ) . unwrap_or ( if self . received_commitment_while_awaiting_raa {
2503
- RAACommitmentOrder :: CommitmentFirst
2504
- } else {
2505
- RAACommitmentOrder :: RevokeAndACKFirst
2506
- } ) ;
2507
-
2508
2488
if msg. next_local_commitment_number == our_next_remote_commitment_number {
2509
2489
if required_revoke. is_some ( ) {
2510
2490
log_debug ! ( self , "Reconnected channel {} with only lost outbound RAA" , log_bytes!( self . channel_id( ) ) ) ;
2511
2491
} else {
2512
2492
log_debug ! ( self , "Reconnected channel {} with no loss" , log_bytes!( self . channel_id( ) ) ) ;
2513
2493
}
2514
2494
2515
- if ( self . channel_state & ( ChannelState :: AwaitingRemoteRevoke as u32 | ChannelState :: MonitorUpdateFailed as u32 ) ) == 0 &&
2516
- self . monitor_pending_order . is_none ( ) { // monitor_pending_order indicates we're waiting on a response to a unfreeze
2495
+ if ( self . channel_state & ( ChannelState :: AwaitingRemoteRevoke as u32 | ChannelState :: MonitorUpdateFailed as u32 ) ) == 0 {
2517
2496
// We're up-to-date and not waiting on a remote revoke (if we are our
2518
2497
// channel_reestablish should result in them sending a revoke_and_ack), but we may
2519
2498
// have received some updates while we were disconnected. Free the holding cell
2520
2499
// now!
2521
2500
match self . free_holding_cell_htlcs ( ) {
2522
2501
Err ( ChannelError :: Close ( msg) ) => return Err ( ChannelError :: Close ( msg) ) ,
2523
2502
Err ( ChannelError :: Ignore ( _) ) => panic ! ( "Got non-channel-failing result from free_holding_cell_htlcs" ) ,
2524
- Ok ( Some ( ( commitment_update, channel_monitor) ) ) => return Ok ( ( resend_funding_locked, required_revoke, Some ( commitment_update) , Some ( channel_monitor) , order , shutdown_msg) ) ,
2525
- Ok ( None ) => return Ok ( ( resend_funding_locked, required_revoke, None , None , order , shutdown_msg) ) ,
2503
+ 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) ) ,
2504
+ Ok ( None ) => return Ok ( ( resend_funding_locked, required_revoke, None , None , self . resend_order . clone ( ) , shutdown_msg) ) ,
2526
2505
}
2527
2506
} else {
2528
- return Ok ( ( resend_funding_locked, required_revoke, None , None , order , shutdown_msg) ) ;
2507
+ return Ok ( ( resend_funding_locked, required_revoke, None , None , self . resend_order . clone ( ) , shutdown_msg) ) ;
2529
2508
}
2530
2509
} else if msg. next_local_commitment_number == our_next_remote_commitment_number - 1 {
2531
2510
if required_revoke. is_some ( ) {
@@ -2536,10 +2515,10 @@ impl Channel {
2536
2515
2537
2516
if self . channel_state & ( ChannelState :: MonitorUpdateFailed as u32 ) != 0 {
2538
2517
self . monitor_pending_commitment_signed = true ;
2539
- return Ok ( ( resend_funding_locked, None , None , None , order , shutdown_msg) ) ;
2518
+ return Ok ( ( resend_funding_locked, None , None , None , self . resend_order . clone ( ) , shutdown_msg) ) ;
2540
2519
}
2541
2520
2542
- return Ok ( ( resend_funding_locked, required_revoke, Some ( self . get_last_commitment_update ( ) ) , None , order , shutdown_msg) ) ;
2521
+ return Ok ( ( resend_funding_locked, required_revoke, Some ( self . get_last_commitment_update ( ) ) , None , self . resend_order . clone ( ) , shutdown_msg) ) ;
2543
2522
} else {
2544
2523
return Err ( ChannelError :: Close ( "Peer attempted to reestablish channel with a very old remote commitment transaction" ) ) ;
2545
2524
}
@@ -3360,6 +3339,7 @@ impl Channel {
3360
3339
htlc. state = OutboundHTLCState :: AwaitingRemovedRemoteRevoke ( fail_reason) ;
3361
3340
}
3362
3341
}
3342
+ self . resend_order = RAACommitmentOrder :: RevokeAndACKFirst ;
3363
3343
3364
3344
let ( res, remote_commitment_tx, htlcs) = match self . send_commitment_no_state_update ( ) {
3365
3345
Ok ( ( res, ( remote_commitment_tx, mut htlcs) ) ) => {
@@ -3570,8 +3550,6 @@ impl Writeable for Channel {
3570
3550
self . cur_remote_commitment_transaction_number . write ( writer) ?;
3571
3551
self . value_to_self_msat . write ( writer) ?;
3572
3552
3573
- self . received_commitment_while_awaiting_raa . write ( writer) ?;
3574
-
3575
3553
let mut dropped_inbound_htlcs = 0 ;
3576
3554
for htlc in self . pending_inbound_htlcs . iter ( ) {
3577
3555
if let InboundHTLCState :: RemoteAnnounced ( _) = htlc. state {
@@ -3671,13 +3649,13 @@ impl Writeable for Channel {
3671
3649
}
3672
3650
}
3673
3651
3652
+ match self . resend_order {
3653
+ RAACommitmentOrder :: CommitmentFirst => 0u8 . write ( writer) ?,
3654
+ RAACommitmentOrder :: RevokeAndACKFirst => 1u8 . write ( writer) ?,
3655
+ }
3656
+
3674
3657
self . monitor_pending_revoke_and_ack . write ( writer) ?;
3675
3658
self . monitor_pending_commitment_signed . write ( writer) ?;
3676
- match self . monitor_pending_order {
3677
- None => 0u8 . write ( writer) ?,
3678
- Some ( RAACommitmentOrder :: CommitmentFirst ) => 1u8 . write ( writer) ?,
3679
- Some ( RAACommitmentOrder :: RevokeAndACKFirst ) => 2u8 . write ( writer) ?,
3680
- }
3681
3659
3682
3660
( self . monitor_pending_forwards . len ( ) as u64 ) . write ( writer) ?;
3683
3661
for & ( ref pending_forward, ref htlc_id) in self . monitor_pending_forwards . iter ( ) {
@@ -3775,8 +3753,6 @@ impl<R : ::std::io::Read> ReadableArgs<R, Arc<Logger>> for Channel {
3775
3753
let cur_remote_commitment_transaction_number = Readable :: read ( reader) ?;
3776
3754
let value_to_self_msat = Readable :: read ( reader) ?;
3777
3755
3778
- let received_commitment_while_awaiting_raa = Readable :: read ( reader) ?;
3779
-
3780
3756
let pending_inbound_htlc_count: u64 = Readable :: read ( reader) ?;
3781
3757
let mut pending_inbound_htlcs = Vec :: with_capacity ( cmp:: min ( pending_inbound_htlc_count as usize , OUR_MAX_HTLCS as usize ) ) ;
3782
3758
for _ in 0 ..pending_inbound_htlc_count {
@@ -3839,16 +3815,15 @@ impl<R : ::std::io::Read> ReadableArgs<R, Arc<Logger>> for Channel {
3839
3815
} ) ;
3840
3816
}
3841
3817
3842
- let monitor_pending_revoke_and_ack = Readable :: read ( reader) ?;
3843
- let monitor_pending_commitment_signed = Readable :: read ( reader) ?;
3844
-
3845
- let monitor_pending_order = match <u8 as Readable < R > >:: read ( reader) ? {
3846
- 0 => None ,
3847
- 1 => Some ( RAACommitmentOrder :: CommitmentFirst ) ,
3848
- 2 => Some ( RAACommitmentOrder :: RevokeAndACKFirst ) ,
3818
+ let resend_order = match <u8 as Readable < R > >:: read ( reader) ? {
3819
+ 0 => RAACommitmentOrder :: CommitmentFirst ,
3820
+ 1 => RAACommitmentOrder :: RevokeAndACKFirst ,
3849
3821
_ => return Err ( DecodeError :: InvalidValue ) ,
3850
3822
} ;
3851
3823
3824
+ let monitor_pending_revoke_and_ack = Readable :: read ( reader) ?;
3825
+ let monitor_pending_commitment_signed = Readable :: read ( reader) ?;
3826
+
3852
3827
let monitor_pending_forwards_count: u64 = Readable :: read ( reader) ?;
3853
3828
let mut monitor_pending_forwards = Vec :: with_capacity ( cmp:: min ( monitor_pending_forwards_count as usize , OUR_MAX_HTLCS as usize ) ) ;
3854
3829
for _ in 0 ..monitor_pending_forwards_count {
@@ -3935,14 +3910,14 @@ impl<R : ::std::io::Read> ReadableArgs<R, Arc<Logger>> for Channel {
3935
3910
cur_remote_commitment_transaction_number,
3936
3911
value_to_self_msat,
3937
3912
3938
- received_commitment_while_awaiting_raa,
3939
3913
pending_inbound_htlcs,
3940
3914
pending_outbound_htlcs,
3941
3915
holding_cell_htlc_updates,
3942
3916
3917
+ resend_order,
3918
+
3943
3919
monitor_pending_revoke_and_ack,
3944
3920
monitor_pending_commitment_signed,
3945
- monitor_pending_order,
3946
3921
monitor_pending_forwards,
3947
3922
monitor_pending_failures,
3948
3923
0 commit comments