Skip to content

Commit

Permalink
send warning when we have 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 have a old commitment transaction.

Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
  • Loading branch information
vincenzopalazzo committed Apr 22, 2022
1 parent 637fb88 commit 7ae6fdc
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 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(&mut 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 < _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 Down Expand Up @@ -3715,6 +3729,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 7ae6fdc

Please sign in to comment.