Skip to content

Commit

Permalink
send warning when we receive a old commitment transaction
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
vincenzopalazzo committed Apr 22, 2022
1 parent 637fb88 commit 95b1188
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lightning/src/ln/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3679,6 +3679,20 @@ impl<Signer: Sign> Channel<Signer> {
}
}

/// 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<L: Deref>(&mut self, msg: &msgs::ChannelReestablish, logger: &L,
Expand All @@ -3705,6 +3719,7 @@ impl<Signer: Sign> Channel<Signer> {
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()
Expand All @@ -3715,6 +3730,12 @@ impl<Signer: Sign> Channel<Signer> {
}
}

// 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);
Expand Down

0 comments on commit 95b1188

Please sign in to comment.