Skip to content

Commit 95b1188

Browse files
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 <[email protected]>
1 parent 637fb88 commit 95b1188

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

lightning/src/ln/channel.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3679,6 +3679,20 @@ impl<Signer: Sign> Channel<Signer> {
36793679
}
36803680
}
36813681

3682+
/// Check if the we are trying to reestablish a connection with a peer with an old commitment
3683+
/// transaction that it is not possible recovered.
3684+
///
3685+
/// If the peer is not out of sync we return an empty result, otherwise just the channel error to sent.
3686+
fn try_reestablish_when_peer_is_late(&self, msg: &msgs::ChannelReestablish) -> Result<(), ChannelError> {
3687+
let _our_commitment_transaction = INITIAL_COMMITMENT_NUMBER - self.cur_holder_commitment_transaction_number - 1;
3688+
if msg.next_remote_commitment_number + 1 < _our_commitment_transaction {
3689+
return Err(
3690+
ChannelError::Warn(format!("bad reestablish revocation_number: {} (received) vs {} (expected)", msg.next_remote_commitment_number, _our_commitment_transaction))
3691+
);
3692+
}
3693+
Ok(())
3694+
}
3695+
36823696
/// May panic if some calls other than message-handling calls (which will all Err immediately)
36833697
/// have been called between remove_uncommitted_htlcs_and_mark_paused and this call.
36843698
pub fn channel_reestablish<L: Deref>(&mut self, msg: &msgs::ChannelReestablish, logger: &L,
@@ -3705,6 +3719,7 @@ impl<Signer: Sign> Channel<Signer> {
37053719
if expected_point != PublicKey::from_secret_key(&self.secp_ctx, &given_secret) {
37063720
return Err(ChannelError::Close("Peer sent a garbage channel_reestablish with secret key not matching the commitment height provided".to_owned()));
37073721
}
3722+
37083723
if msg.next_remote_commitment_number > INITIAL_COMMITMENT_NUMBER - self.cur_holder_commitment_transaction_number {
37093724
return Err(ChannelError::CloseDelayBroadcast(
37103725
"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<Signer: Sign> Channel<Signer> {
37153730
}
37163731
}
37173732

3733+
// Before change the state of the channel we check if the peer are sending a very old
3734+
// commitment transaction number, if yes we send an error (warning message).
3735+
if let Err(err_msg) = self.try_reestablish_when_peer_is_late(msg) {
3736+
return Err(err_msg);
3737+
}
3738+
37183739
// Go ahead and unmark PeerDisconnected as various calls we may make check for it (and all
37193740
// remaining cases either succeed or ErrorMessage-fail).
37203741
self.channel_state &= !(ChannelState::PeerDisconnected as u32);

0 commit comments

Comments
 (0)