From 95b1188b265be1c979e78a95d16c93db0fe1d26d Mon Sep 17 00:00:00 2001 From: Vincenzo Palazzo Date: Fri, 22 Apr 2022 12:13:54 +0200 Subject: [PATCH] send warning when we receive a old commitment transaction During a `channel_restablish` now we send a warning message when we we receive a old commitment transaction from the peer Signed-off-by: Vincenzo Palazzo --- lightning/src/ln/channel.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/lightning/src/ln/channel.rs b/lightning/src/ln/channel.rs index b0551bf8323..08ace6fded2 100644 --- a/lightning/src/ln/channel.rs +++ b/lightning/src/ln/channel.rs @@ -3679,6 +3679,20 @@ impl Channel { } } + /// Check if the we are trying to reestablish a connection with a peer with an old commitment + /// transaction that it is not possible recovered. + /// + /// If the peer is not out of sync we return an empty result, otherwise just the channel error to sent. + fn try_reestablish_when_peer_is_late(&self, msg: &msgs::ChannelReestablish) -> Result<(), ChannelError> { + let _our_commitment_transaction = INITIAL_COMMITMENT_NUMBER - self.cur_holder_commitment_transaction_number - 1; + if msg.next_remote_commitment_number + 1 < _our_commitment_transaction { + return Err( + ChannelError::Warn(format!("bad reestablish revocation_number: {} (received) vs {} (expected)", msg.next_remote_commitment_number, _our_commitment_transaction)) + ); + } + Ok(()) + } + /// May panic if some calls other than message-handling calls (which will all Err immediately) /// have been called between remove_uncommitted_htlcs_and_mark_paused and this call. pub fn channel_reestablish(&mut self, msg: &msgs::ChannelReestablish, logger: &L, @@ -3705,6 +3719,7 @@ impl Channel { if expected_point != PublicKey::from_secret_key(&self.secp_ctx, &given_secret) { return Err(ChannelError::Close("Peer sent a garbage channel_reestablish with secret key not matching the commitment height provided".to_owned())); } + if msg.next_remote_commitment_number > INITIAL_COMMITMENT_NUMBER - self.cur_holder_commitment_transaction_number { return Err(ChannelError::CloseDelayBroadcast( "We have fallen behind - we have received proof that if we broadcast remote is going to claim our funds - we can't do any automated broadcasting".to_owned() @@ -3715,6 +3730,12 @@ impl Channel { } } + // Before change the state of the channel we check if the peer are sending a very old + // commitment transaction number, if yes we send an error (warning message). + if let Err(err_msg) = self.try_reestablish_when_peer_is_late(msg) { + return Err(err_msg); + } + // Go ahead and unmark PeerDisconnected as various calls we may make check for it (and all // remaining cases either succeed or ErrorMessage-fail). self.channel_state &= !(ChannelState::PeerDisconnected as u32);