@@ -2289,7 +2289,8 @@ impl Channel {
2289
2289
return Err ( ChannelError :: Close ( "Peer sent a loose channel_reestablish not after reconnect" ) ) ;
2290
2290
}
2291
2291
2292
- if msg. next_local_commitment_number >= INITIAL_COMMITMENT_NUMBER || msg. next_remote_commitment_number >= INITIAL_COMMITMENT_NUMBER {
2292
+ if msg. next_local_commitment_number >= INITIAL_COMMITMENT_NUMBER || msg. next_remote_commitment_number >= INITIAL_COMMITMENT_NUMBER ||
2293
+ msg. next_local_commitment_number == 0 {
2293
2294
return Err ( ChannelError :: Close ( "Peer sent a garbage channel_reestablish" ) ) ;
2294
2295
}
2295
2296
@@ -2304,15 +2305,15 @@ impl Channel {
2304
2305
} )
2305
2306
} else { None } ;
2306
2307
2307
- if self . channel_state & ( ChannelState :: FundingSent as u32 | ChannelState :: OurFundingLocked as u32 ) == ChannelState :: FundingSent as u32 {
2308
- // Short circuit the whole handler as there is nothing we can resend them
2309
- return Ok ( ( None , None , None , None , RAACommitmentOrder :: CommitmentFirst , shutdown_msg) ) ;
2310
- }
2311
-
2312
- if msg. next_local_commitment_number == 0 || msg. next_remote_commitment_number == 0 {
2313
- if self . channel_state & ( ChannelState :: FundingSent as u32 ) != ChannelState :: FundingSent as u32 {
2314
- return Err ( ChannelError :: Close ( "Peer sent a pre-funding channel_reestablish after we exchanged funding_locked" ) ) ;
2308
+ if self . channel_state & ( ChannelState :: FundingSent as u32 ) == ChannelState :: FundingSent as u32 {
2309
+ if self . channel_state & ChannelState :: OurFundingLocked as u32 == 0 {
2310
+ if msg. next_remote_commitment_number != 0 {
2311
+ return Err ( ChannelError :: Close ( "Peer claimed they saw a revoke_and_ack but we haven't sent funding_locked yet" ) ) ;
2312
+ }
2313
+ // Short circuit the whole handler as there is nothing we can resend them
2314
+ return Ok ( ( None , None , None , None , RAACommitmentOrder :: CommitmentFirst , shutdown_msg) ) ;
2315
2315
}
2316
+
2316
2317
// We have OurFundingLocked set!
2317
2318
let next_per_commitment_secret = self . build_local_commitment_secret ( self . cur_local_commitment_transaction_number ) ;
2318
2319
let next_per_commitment_point = PublicKey :: from_secret_key ( & self . secp_ctx , & next_per_commitment_secret) ;
@@ -2322,11 +2323,11 @@ impl Channel {
2322
2323
} ) , None , None , None , RAACommitmentOrder :: CommitmentFirst , shutdown_msg) ) ;
2323
2324
}
2324
2325
2325
- let required_revoke = if msg. next_remote_commitment_number == INITIAL_COMMITMENT_NUMBER - self . cur_local_commitment_transaction_number {
2326
+ let required_revoke = if msg. next_remote_commitment_number + 1 == INITIAL_COMMITMENT_NUMBER - self . cur_local_commitment_transaction_number {
2326
2327
// Remote isn't waiting on any RevokeAndACK from us!
2327
2328
// Note that if we need to repeat our FundingLocked we'll do that in the next if block.
2328
2329
None
2329
- } else if msg. next_remote_commitment_number == ( INITIAL_COMMITMENT_NUMBER - 1 ) - self . cur_local_commitment_transaction_number {
2330
+ } else if msg. next_remote_commitment_number + 1 == ( INITIAL_COMMITMENT_NUMBER - 1 ) - self . cur_local_commitment_transaction_number {
2330
2331
if self . channel_state & ( ChannelState :: MonitorUpdateFailed as u32 ) != 0 {
2331
2332
self . monitor_pending_revoke_and_ack = true ;
2332
2333
None
@@ -3053,11 +3054,27 @@ impl Channel {
3053
3054
/// self.remove_uncommitted_htlcs_and_mark_paused()'d
3054
3055
pub fn get_channel_reestablish ( & self ) -> msgs:: ChannelReestablish {
3055
3056
assert_eq ! ( self . channel_state & ChannelState :: PeerDisconnected as u32 , ChannelState :: PeerDisconnected as u32 ) ;
3057
+ assert_ne ! ( self . cur_remote_commitment_transaction_number, INITIAL_COMMITMENT_NUMBER ) ;
3056
3058
msgs:: ChannelReestablish {
3057
3059
channel_id : self . channel_id ( ) ,
3060
+ // The protocol has two different commitment number concepts - the "commitment
3061
+ // transaction number", which starts from 0 and counts up, and the "revocation key
3062
+ // index" which starts at INITIAL_COMMITMENT_NUMBER and counts down. We track
3063
+ // commitment transaction numbers by the index which will be used to reveal the
3064
+ // revocation key for that commitment transaction, which means we have to convert them
3065
+ // to protocol-level commitment numbers here...
3066
+
3067
+ // next_local_commitment_number is the next commitment_signed number we expect to
3068
+ // receive (indicating if they need to resend one that we missed).
3058
3069
next_local_commitment_number : INITIAL_COMMITMENT_NUMBER - self . cur_local_commitment_transaction_number ,
3059
- next_remote_commitment_number : INITIAL_COMMITMENT_NUMBER - self . cur_remote_commitment_transaction_number -
3060
- if self . channel_state & ( ChannelState :: FundingSent as u32 | ChannelState :: OurFundingLocked as u32 ) == ( ChannelState :: FundingSent as u32 ) { 1 } else { 0 } ,
3070
+ // We have to set next_remote_commitment_number to the next revoke_and_ack we expect to
3071
+ // receive, however we track it by the next commitment number for a remote transaction
3072
+ // (which is one further, as they always revoke previous commitment transaction, not
3073
+ // the one we send) so we have to decrement by 1. Note that if
3074
+ // cur_remote_commitment_transaction_number is INITIAL_COMMITMENT_NUMBER we will have
3075
+ // dropped this channel on disconnect as it hasn't yet reached FundingSent so we can't
3076
+ // overflow here.
3077
+ next_remote_commitment_number : INITIAL_COMMITMENT_NUMBER - self . cur_remote_commitment_transaction_number - 1 ,
3061
3078
data_loss_protect : None ,
3062
3079
}
3063
3080
}
0 commit comments