Skip to content

Commit ed96352

Browse files
committed
Clean up error messages and conditionals in reestablish handling
When we reestablish there are generally always 4 conditions for both local and remote commitment transactions: * we're stale and have possibly lost data * we're ahead and the peer has lost data * we're caught up * we're nearly caught up and need to retransmit one update. In especially the local commitment case we had a mess of different comparisons, which is improved here. Further, the error messages are clarified and include more information.
1 parent 589a88e commit ed96352

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

lightning/src/ln/channel.rs

+24-8
Original file line numberDiff line numberDiff line change
@@ -4180,10 +4180,12 @@ impl<SP: Deref> Channel<SP> where
41804180
// Before we change the state of the channel, we check if the peer is sending a very old
41814181
// commitment transaction number, if yes we send a warning message.
41824182
let our_commitment_transaction = INITIAL_COMMITMENT_NUMBER - self.context.cur_holder_commitment_transaction_number - 1;
4183-
if msg.next_remote_commitment_number + 1 < our_commitment_transaction {
4184-
return Err(
4185-
ChannelError::Warn(format!("Peer attempted to reestablish channel with a very old local commitment transaction: {} (received) vs {} (expected)", msg.next_remote_commitment_number, our_commitment_transaction))
4186-
);
4183+
if msg.next_remote_commitment_number + 1 < our_commitment_transaction {
4184+
return Err(ChannelError::Warn(format!(
4185+
"Peer attempted to reestablish channel with a very old local commitment transaction: {} (received) vs {} (expected)",
4186+
msg.next_remote_commitment_number,
4187+
our_commitment_transaction
4188+
)));
41874189
}
41884190

41894191
// Go ahead and unmark PeerDisconnected as various calls we may make check for it (and all
@@ -4225,19 +4227,23 @@ impl<SP: Deref> Channel<SP> where
42254227
});
42264228
}
42274229

4228-
let required_revoke = if msg.next_remote_commitment_number + 1 == INITIAL_COMMITMENT_NUMBER - self.context.cur_holder_commitment_transaction_number {
4230+
let required_revoke = if msg.next_remote_commitment_number == our_commitment_transaction {
42294231
// Remote isn't waiting on any RevokeAndACK from us!
42304232
// Note that if we need to repeat our ChannelReady we'll do that in the next if block.
42314233
None
4232-
} else if msg.next_remote_commitment_number + 1 == (INITIAL_COMMITMENT_NUMBER - 1) - self.context.cur_holder_commitment_transaction_number {
4234+
} else if msg.next_remote_commitment_number + 1 == our_commitment_transaction {
42334235
if self.context.channel_state & (ChannelState::MonitorUpdateInProgress as u32) != 0 {
42344236
self.context.monitor_pending_revoke_and_ack = true;
42354237
None
42364238
} else {
42374239
Some(self.get_last_revoke_and_ack())
42384240
}
42394241
} else {
4240-
return Err(ChannelError::Close("Peer attempted to reestablish channel with a very old local commitment transaction".to_owned()));
4242+
return Err(ChannelError::Close(format!(
4243+
"Peer attempted to reestablish channel expecting a future local commitment transaction: {} (received) vs {} (expected)",
4244+
msg.next_remote_commitment_number,
4245+
our_commitment_transaction
4246+
)));
42414247
};
42424248

42434249
// We increment cur_counterparty_commitment_transaction_number only upon receipt of
@@ -4295,8 +4301,18 @@ impl<SP: Deref> Channel<SP> where
42954301
order: self.context.resend_order.clone(),
42964302
})
42974303
}
4304+
} else if msg.next_local_commitment_number < next_counterparty_commitment_number {
4305+
Err(ChannelError::Close(format!(
4306+
"Peer attempted to reestablish channel with a very old local commitment transaction: {} (received) vs {} (expected)",
4307+
msg.next_local_commitment_number,
4308+
next_counterparty_commitment_number,
4309+
)))
42984310
} else {
4299-
Err(ChannelError::Close("Peer attempted to reestablish channel with a very old remote commitment transaction".to_owned()))
4311+
Err(ChannelError::Close(format!(
4312+
"Peer attempted to reestablish channel with a future local commitment transaction: {} (received) vs {} (expected)",
4313+
msg.next_local_commitment_number,
4314+
next_counterparty_commitment_number,
4315+
)))
43004316
}
43014317
}
43024318

0 commit comments

Comments
 (0)