@@ -237,19 +237,19 @@ 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. Thus, we track
246
+ /// that order here by always setting this to the opposite of the message we are generating (ie
247
+ /// when we generate a CS, we set this to RAAFirst as we should send any pending RAAs first and
248
+ /// visa-versa).
249
+ resend_order : RAACommitmentOrder ,
250
+
250
251
monitor_pending_revoke_and_ack : bool ,
251
252
monitor_pending_commitment_signed : bool ,
252
- monitor_pending_order : Option < RAACommitmentOrder > ,
253
253
monitor_pending_forwards : Vec < ( PendingForwardHTLCInfo , u64 ) > ,
254
254
monitor_pending_failures : Vec < ( HTLCSource , PaymentHash , HTLCFailReason ) > ,
255
255
@@ -457,7 +457,6 @@ impl Channel {
457
457
cur_local_commitment_transaction_number : INITIAL_COMMITMENT_NUMBER ,
458
458
cur_remote_commitment_transaction_number : INITIAL_COMMITMENT_NUMBER ,
459
459
value_to_self_msat : channel_value_satoshis * 1000 - push_msat,
460
- received_commitment_while_awaiting_raa : false ,
461
460
462
461
pending_inbound_htlcs : Vec :: new ( ) ,
463
462
pending_outbound_htlcs : Vec :: new ( ) ,
@@ -468,9 +467,10 @@ impl Channel {
468
467
next_remote_htlc_id : 0 ,
469
468
channel_update_count : 1 ,
470
469
470
+ resend_order : RAACommitmentOrder :: CommitmentFirst ,
471
+
471
472
monitor_pending_revoke_and_ack : false ,
472
473
monitor_pending_commitment_signed : false ,
473
- monitor_pending_order : None ,
474
474
monitor_pending_forwards : Vec :: new ( ) ,
475
475
monitor_pending_failures : Vec :: new ( ) ,
476
476
@@ -646,7 +646,6 @@ impl Channel {
646
646
cur_local_commitment_transaction_number : INITIAL_COMMITMENT_NUMBER ,
647
647
cur_remote_commitment_transaction_number : INITIAL_COMMITMENT_NUMBER ,
648
648
value_to_self_msat : msg. push_msat ,
649
- received_commitment_while_awaiting_raa : false ,
650
649
651
650
pending_inbound_htlcs : Vec :: new ( ) ,
652
651
pending_outbound_htlcs : Vec :: new ( ) ,
@@ -657,9 +656,10 @@ impl Channel {
657
656
next_remote_htlc_id : 0 ,
658
657
channel_update_count : 1 ,
659
658
659
+ resend_order : RAACommitmentOrder :: CommitmentFirst ,
660
+
660
661
monitor_pending_revoke_and_ack : false ,
661
662
monitor_pending_commitment_signed : false ,
662
- monitor_pending_order : None ,
663
663
monitor_pending_forwards : Vec :: new ( ) ,
664
664
monitor_pending_failures : Vec :: new ( ) ,
665
665
@@ -1810,12 +1810,6 @@ impl Channel {
1810
1810
}
1811
1811
}
1812
1812
1813
- if self . channel_state & ( ChannelState :: MonitorUpdateFailed as u32 ) == 0 {
1814
- // This is a response to our post-monitor-failed unfreeze messages, so we can clear the
1815
- // monitor_pending_order requirement as we won't re-send the monitor_pending messages.
1816
- self . monitor_pending_order = None ;
1817
- }
1818
-
1819
1813
self . channel_monitor . provide_latest_local_commitment_tx_info ( local_commitment_tx. 0 , local_keys, self . feerate_per_kw , htlcs_and_sigs) ;
1820
1814
1821
1815
for htlc in self . pending_inbound_htlcs . iter_mut ( ) {
@@ -1838,14 +1832,13 @@ impl Channel {
1838
1832
1839
1833
self . cur_local_commitment_transaction_number -= 1 ;
1840
1834
self . last_local_commitment_txn = new_local_commitment_txn;
1841
- self . received_commitment_while_awaiting_raa = ( self . channel_state & ( ChannelState :: AwaitingRemoteRevoke as u32 ) ) != 0 ;
1835
+ // Note that if we need_our_commitment & !AwaitingRemoteRevoke we'll call
1836
+ // send_commitment_no_status_check() next which will reset this to RAAFirst.
1837
+ self . resend_order = RAACommitmentOrder :: CommitmentFirst ;
1842
1838
1843
1839
if ( self . channel_state & ChannelState :: MonitorUpdateFailed as u32 ) != 0 {
1844
1840
// In case we initially failed monitor updating without requiring a response, we need
1845
1841
// to make sure the RAA gets sent first.
1846
- if !self . monitor_pending_commitment_signed {
1847
- self . monitor_pending_order = Some ( RAACommitmentOrder :: RevokeAndACKFirst ) ;
1848
- }
1849
1842
self . monitor_pending_revoke_and_ack = true ;
1850
1843
if need_our_commitment && ( self . channel_state & ( ChannelState :: AwaitingRemoteRevoke as u32 ) ) == 0 {
1851
1844
// If we were going to send a commitment_signed after the RAA, go ahead and do all
@@ -2019,12 +2012,6 @@ impl Channel {
2019
2012
self . their_prev_commitment_point = self . their_cur_commitment_point ;
2020
2013
self . their_cur_commitment_point = Some ( msg. next_per_commitment_point ) ;
2021
2014
self . cur_remote_commitment_transaction_number -= 1 ;
2022
- self . received_commitment_while_awaiting_raa = false ;
2023
- if self . channel_state & ( ChannelState :: MonitorUpdateFailed as u32 ) == 0 {
2024
- // This is a response to our post-monitor-failed unfreeze messages, so we can clear the
2025
- // monitor_pending_order requirement as we won't re-send the monitor_pending messages.
2026
- self . monitor_pending_order = None ;
2027
- }
2028
2015
2029
2016
log_trace ! ( self , "Updating HTLCs on receipt of RAA..." ) ;
2030
2017
let mut to_forward_infos = Vec :: new ( ) ;
@@ -2142,7 +2129,7 @@ impl Channel {
2142
2129
// When the monitor updating is restored we'll call get_last_commitment_update(),
2143
2130
// which does not update state, but we're definitely now awaiting a remote revoke
2144
2131
// before we can step forward any more, so set it here.
2145
- self . channel_state |= ChannelState :: AwaitingRemoteRevoke as u32 ;
2132
+ self . send_commitment_no_status_check ( ) ? ;
2146
2133
}
2147
2134
self . monitor_pending_forwards . append ( & mut to_forward_infos) ;
2148
2135
self . monitor_pending_failures . append ( & mut revoked_htlcs) ;
@@ -2290,15 +2277,13 @@ impl Channel {
2290
2277
/// Indicates that a ChannelMonitor update failed to be stored by the client and further
2291
2278
/// updates are partially paused.
2292
2279
/// This must be called immediately after the call which generated the ChannelMonitor update
2293
- /// which failed, with the order argument set to the type of call it represented (ie a
2294
- /// commitment update or a revoke_and_ack generation). The messages which were generated from
2295
- /// that original call must *not* have been sent to the remote end, and must instead have been
2296
- /// dropped. They will be regenerated when monitor_updating_restored is called.
2297
- 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 ) > ) {
2280
+ /// which failed. The messages which were generated from that call which generated the
2281
+ /// monitor update failure must *not* have been sent to the remote end, and must instead
2282
+ /// have been dropped. They will be regenerated when monitor_updating_restored is called.
2283
+ 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 ) > ) {
2298
2284
assert_eq ! ( self . channel_state & ChannelState :: MonitorUpdateFailed as u32 , 0 ) ;
2299
2285
self . monitor_pending_revoke_and_ack = resend_raa;
2300
2286
self . monitor_pending_commitment_signed = resend_commitment;
2301
- self . monitor_pending_order = Some ( order) ;
2302
2287
assert ! ( self . monitor_pending_forwards. is_empty( ) ) ;
2303
2288
mem:: swap ( & mut pending_forwards, & mut self . monitor_pending_forwards ) ;
2304
2289
assert ! ( self . monitor_pending_failures. is_empty( ) ) ;
@@ -2319,7 +2304,6 @@ impl Channel {
2319
2304
mem:: swap ( & mut failures, & mut self . monitor_pending_failures ) ;
2320
2305
2321
2306
if self . channel_state & ( ChannelState :: PeerDisconnected as u32 ) != 0 {
2322
- // Leave monitor_pending_order so we can order our channel_reestablish responses
2323
2307
self . monitor_pending_revoke_and_ack = false ;
2324
2308
self . monitor_pending_commitment_signed = false ;
2325
2309
return ( None , None , RAACommitmentOrder :: RevokeAndACKFirst , forwards, failures) ;
@@ -2334,7 +2318,7 @@ impl Channel {
2334
2318
2335
2319
self . monitor_pending_revoke_and_ack = false ;
2336
2320
self . monitor_pending_commitment_signed = false ;
2337
- let order = self . monitor_pending_order . clone ( ) . unwrap ( ) ;
2321
+ let order = self . resend_order . clone ( ) ;
2338
2322
log_trace ! ( self , "Restored monitor updating resulting in {} commitment update and {} RAA, with {} first" ,
2339
2323
if commitment_update. is_some( ) { "a" } else { "no" } ,
2340
2324
if raa. is_some( ) { "an" } else { "no" } ,
@@ -2497,33 +2481,26 @@ impl Channel {
2497
2481
} )
2498
2482
} else { None } ;
2499
2483
2500
- let order = self . monitor_pending_order . clone ( ) . unwrap_or ( if self . received_commitment_while_awaiting_raa {
2501
- RAACommitmentOrder :: CommitmentFirst
2502
- } else {
2503
- RAACommitmentOrder :: RevokeAndACKFirst
2504
- } ) ;
2505
-
2506
2484
if msg. next_local_commitment_number == our_next_remote_commitment_number {
2507
2485
if required_revoke. is_some ( ) {
2508
2486
log_debug ! ( self , "Reconnected channel {} with only lost outbound RAA" , log_bytes!( self . channel_id( ) ) ) ;
2509
2487
} else {
2510
2488
log_debug ! ( self , "Reconnected channel {} with no loss" , log_bytes!( self . channel_id( ) ) ) ;
2511
2489
}
2512
2490
2513
- if ( self . channel_state & ( ChannelState :: AwaitingRemoteRevoke as u32 | ChannelState :: MonitorUpdateFailed as u32 ) ) == 0 &&
2514
- self . monitor_pending_order . is_none ( ) { // monitor_pending_order indicates we're waiting on a response to a unfreeze
2491
+ if ( self . channel_state & ( ChannelState :: AwaitingRemoteRevoke as u32 | ChannelState :: MonitorUpdateFailed as u32 ) ) == 0 {
2515
2492
// We're up-to-date and not waiting on a remote revoke (if we are our
2516
2493
// channel_reestablish should result in them sending a revoke_and_ack), but we may
2517
2494
// have received some updates while we were disconnected. Free the holding cell
2518
2495
// now!
2519
2496
match self . free_holding_cell_htlcs ( ) {
2520
2497
Err ( ChannelError :: Close ( msg) ) => return Err ( ChannelError :: Close ( msg) ) ,
2521
2498
Err ( ChannelError :: Ignore ( _) ) => panic ! ( "Got non-channel-failing result from free_holding_cell_htlcs" ) ,
2522
- Ok ( Some ( ( commitment_update, channel_monitor) ) ) => return Ok ( ( resend_funding_locked, required_revoke, Some ( commitment_update) , Some ( channel_monitor) , order , shutdown_msg) ) ,
2523
- Ok ( None ) => return Ok ( ( resend_funding_locked, required_revoke, None , None , order , shutdown_msg) ) ,
2499
+ 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) ) ,
2500
+ Ok ( None ) => return Ok ( ( resend_funding_locked, required_revoke, None , None , self . resend_order . clone ( ) , shutdown_msg) ) ,
2524
2501
}
2525
2502
} else {
2526
- return Ok ( ( resend_funding_locked, required_revoke, None , None , order , shutdown_msg) ) ;
2503
+ return Ok ( ( resend_funding_locked, required_revoke, None , None , self . resend_order . clone ( ) , shutdown_msg) ) ;
2527
2504
}
2528
2505
} else if msg. next_local_commitment_number == our_next_remote_commitment_number - 1 {
2529
2506
if required_revoke. is_some ( ) {
@@ -2534,10 +2511,10 @@ impl Channel {
2534
2511
2535
2512
if self . channel_state & ( ChannelState :: MonitorUpdateFailed as u32 ) != 0 {
2536
2513
self . monitor_pending_commitment_signed = true ;
2537
- return Ok ( ( resend_funding_locked, None , None , None , order , shutdown_msg) ) ;
2514
+ return Ok ( ( resend_funding_locked, None , None , None , self . resend_order . clone ( ) , shutdown_msg) ) ;
2538
2515
}
2539
2516
2540
- return Ok ( ( resend_funding_locked, required_revoke, Some ( self . get_last_commitment_update ( ) ) , None , order , shutdown_msg) ) ;
2517
+ return Ok ( ( resend_funding_locked, required_revoke, Some ( self . get_last_commitment_update ( ) ) , None , self . resend_order . clone ( ) , shutdown_msg) ) ;
2541
2518
} else {
2542
2519
return Err ( ChannelError :: Close ( "Peer attempted to reestablish channel with a very old remote commitment transaction" ) ) ;
2543
2520
}
@@ -3358,6 +3335,7 @@ impl Channel {
3358
3335
htlc. state = OutboundHTLCState :: AwaitingRemovedRemoteRevoke ( fail_reason) ;
3359
3336
}
3360
3337
}
3338
+ self . resend_order = RAACommitmentOrder :: RevokeAndACKFirst ;
3361
3339
3362
3340
let ( res, remote_commitment_tx, htlcs) = match self . send_commitment_no_state_update ( ) {
3363
3341
Ok ( ( res, ( remote_commitment_tx, mut htlcs) ) ) => {
@@ -3568,8 +3546,6 @@ impl Writeable for Channel {
3568
3546
self . cur_remote_commitment_transaction_number . write ( writer) ?;
3569
3547
self . value_to_self_msat . write ( writer) ?;
3570
3548
3571
- self . received_commitment_while_awaiting_raa . write ( writer) ?;
3572
-
3573
3549
let mut dropped_inbound_htlcs = 0 ;
3574
3550
for htlc in self . pending_inbound_htlcs . iter ( ) {
3575
3551
if let InboundHTLCState :: RemoteAnnounced ( _) = htlc. state {
@@ -3669,13 +3645,13 @@ impl Writeable for Channel {
3669
3645
}
3670
3646
}
3671
3647
3648
+ match self . resend_order {
3649
+ RAACommitmentOrder :: CommitmentFirst => 0u8 . write ( writer) ?,
3650
+ RAACommitmentOrder :: RevokeAndACKFirst => 1u8 . write ( writer) ?,
3651
+ }
3652
+
3672
3653
self . monitor_pending_revoke_and_ack . write ( writer) ?;
3673
3654
self . monitor_pending_commitment_signed . write ( writer) ?;
3674
- match self . monitor_pending_order {
3675
- None => 0u8 . write ( writer) ?,
3676
- Some ( RAACommitmentOrder :: CommitmentFirst ) => 1u8 . write ( writer) ?,
3677
- Some ( RAACommitmentOrder :: RevokeAndACKFirst ) => 2u8 . write ( writer) ?,
3678
- }
3679
3655
3680
3656
( self . monitor_pending_forwards . len ( ) as u64 ) . write ( writer) ?;
3681
3657
for & ( ref pending_forward, ref htlc_id) in self . monitor_pending_forwards . iter ( ) {
@@ -3773,8 +3749,6 @@ impl<R : ::std::io::Read> ReadableArgs<R, Arc<Logger>> for Channel {
3773
3749
let cur_remote_commitment_transaction_number = Readable :: read ( reader) ?;
3774
3750
let value_to_self_msat = Readable :: read ( reader) ?;
3775
3751
3776
- let received_commitment_while_awaiting_raa = Readable :: read ( reader) ?;
3777
-
3778
3752
let pending_inbound_htlc_count: u64 = Readable :: read ( reader) ?;
3779
3753
let mut pending_inbound_htlcs = Vec :: with_capacity ( cmp:: min ( pending_inbound_htlc_count as usize , OUR_MAX_HTLCS as usize ) ) ;
3780
3754
for _ in 0 ..pending_inbound_htlc_count {
@@ -3837,16 +3811,15 @@ impl<R : ::std::io::Read> ReadableArgs<R, Arc<Logger>> for Channel {
3837
3811
} ) ;
3838
3812
}
3839
3813
3840
- let monitor_pending_revoke_and_ack = Readable :: read ( reader) ?;
3841
- let monitor_pending_commitment_signed = Readable :: read ( reader) ?;
3842
-
3843
- let monitor_pending_order = match <u8 as Readable < R > >:: read ( reader) ? {
3844
- 0 => None ,
3845
- 1 => Some ( RAACommitmentOrder :: CommitmentFirst ) ,
3846
- 2 => Some ( RAACommitmentOrder :: RevokeAndACKFirst ) ,
3814
+ let resend_order = match <u8 as Readable < R > >:: read ( reader) ? {
3815
+ 0 => RAACommitmentOrder :: CommitmentFirst ,
3816
+ 1 => RAACommitmentOrder :: RevokeAndACKFirst ,
3847
3817
_ => return Err ( DecodeError :: InvalidValue ) ,
3848
3818
} ;
3849
3819
3820
+ let monitor_pending_revoke_and_ack = Readable :: read ( reader) ?;
3821
+ let monitor_pending_commitment_signed = Readable :: read ( reader) ?;
3822
+
3850
3823
let monitor_pending_forwards_count: u64 = Readable :: read ( reader) ?;
3851
3824
let mut monitor_pending_forwards = Vec :: with_capacity ( cmp:: min ( monitor_pending_forwards_count as usize , OUR_MAX_HTLCS as usize ) ) ;
3852
3825
for _ in 0 ..monitor_pending_forwards_count {
@@ -3933,14 +3906,14 @@ impl<R : ::std::io::Read> ReadableArgs<R, Arc<Logger>> for Channel {
3933
3906
cur_remote_commitment_transaction_number,
3934
3907
value_to_self_msat,
3935
3908
3936
- received_commitment_while_awaiting_raa,
3937
3909
pending_inbound_htlcs,
3938
3910
pending_outbound_htlcs,
3939
3911
holding_cell_htlc_updates,
3940
3912
3913
+ resend_order,
3914
+
3941
3915
monitor_pending_revoke_and_ack,
3942
3916
monitor_pending_commitment_signed,
3943
- monitor_pending_order,
3944
3917
monitor_pending_forwards,
3945
3918
monitor_pending_failures,
3946
3919
0 commit comments