From 9fd0292112fca9993e1eb6059226a65faaaf5dab Mon Sep 17 00:00:00 2001 From: Vincenzo Palazzo Date: Sat, 16 Apr 2022 23:17:45 +0200 Subject: [PATCH] bolt2: give the possibility to the sender to be outdate This commit give the possibility to the remote peer to be outdate when is trying to reconnect to us. Fixes https://github.com/lightningdevkit/rust-lightning/issues/1207 Signed-off-by: Vincenzo Palazzo --- lightning/src/ln/channel.rs | 30 +++++++++++++++++++++++++++++- lightning/src/ln/channelmanager.rs | 11 +++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/lightning/src/ln/channel.rs b/lightning/src/ln/channel.rs index 9ccdf816ad..50ccb5c76f 100644 --- a/lightning/src/ln/channel.rs +++ b/lightning/src/ln/channel.rs @@ -432,6 +432,7 @@ pub(super) struct ReestablishResponses { pub holding_cell_failed_htlcs: Vec<(HTLCSource, PaymentHash)>, pub announcement_sigs: Option, pub shutdown_msg: Option, + pub channel_reestablish_msg: Option, } /// If the majority of the channels funds are to the fundee and the initiator holds only just @@ -3743,6 +3744,7 @@ impl Channel { order: RAACommitmentOrder::CommitmentFirst, holding_cell_failed_htlcs: Vec::new(), shutdown_msg, announcement_sigs, + channel_reestablish_msg: None, }); } @@ -3758,6 +3760,7 @@ impl Channel { order: RAACommitmentOrder::CommitmentFirst, holding_cell_failed_htlcs: Vec::new(), shutdown_msg, announcement_sigs, + channel_reestablish_msg: None, }); } @@ -3816,6 +3819,7 @@ impl Channel { order: self.resend_order.clone(), mon_update: Some(monitor_update), holding_cell_failed_htlcs, + channel_reestablish_msg: None, }) }, Ok((None, holding_cell_failed_htlcs)) => { @@ -3826,6 +3830,7 @@ impl Channel { order: self.resend_order.clone(), mon_update: None, holding_cell_failed_htlcs, + channel_reestablish_msg: None, }) }, } @@ -3837,6 +3842,7 @@ impl Channel { order: self.resend_order.clone(), mon_update: None, holding_cell_failed_htlcs: Vec::new(), + channel_reestablish_msg: None, }) } } else if msg.next_local_commitment_number == next_counterparty_commitment_number - 1 { @@ -3853,6 +3859,7 @@ impl Channel { commitment_update: None, raa: None, mon_update: None, order: self.resend_order.clone(), holding_cell_failed_htlcs: Vec::new(), + channel_reestablish_msg: None, }) } else { Ok(ReestablishResponses { @@ -3862,10 +3869,31 @@ impl Channel { order: self.resend_order.clone(), mon_update: None, holding_cell_failed_htlcs: Vec::new(), + channel_reestablish_msg: None, }) } } else { - Err(ChannelError::Close("Peer attempted to reestablish channel with a very old remote commitment transaction".to_owned())) + // BOLT 2 allow the sender to be outdate, and to avoid + // to close channel due the very old remote commitment + // a channel_reestablish message form us is sent, to + // give the possibility to try to resolver from some problem. + let channel_state_updated = msgs::ChannelReestablish{ + channel_id: self.channel_id, + next_local_commitment_number: self.cur_holder_commitment_transaction_number, + next_remote_commitment_number: self.cur_holder_commitment_transaction_number, + data_loss_protect: OptionalField::Absent, + }; + Ok(ReestablishResponses{ + funding_locked, + raa: None, + commitment_update: None, + order: self.resend_order.clone(), + mon_update: None, + holding_cell_failed_htlcs: Vec::new(), + announcement_sigs, + shutdown_msg, + channel_reestablish_msg: Some(channel_state_updated) + }) } } diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index 7323930f33..c96528bb90 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -4761,6 +4761,17 @@ impl ChannelMana msg, }); } else if chan.get().is_usable() { + // the channel should be in a usable condition, but the counterparty + // is stuck in the past, so we send the updated data with a + // `channel_reestablish` msg. + if let Some(reestablish_msg) = responses.channel_reestablish_msg { + channel_state.pending_msg_events.push(events::MessageSendEvent::SendChannelReestablish { + node_id: counterparty_node_id.clone(), + msg: reestablish_msg, + }); + return Ok(()); + } + // If the channel is in a usable state (ie the channel is not being shut // down), send a unicast channel_update to our counterparty to make sure // they have the latest channel parameters.