Skip to content

Commit

Permalink
bolt2: give the possibility to the sender to be outdate
Browse files Browse the repository at this point in the history
This commit give the possibility to the remote peer to be outdate when is trying to reconnect to us.

Fixes lightningdevkit#1207

Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
  • Loading branch information
vincenzopalazzo committed Apr 16, 2022
1 parent 83595db commit 9fd0292
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
30 changes: 29 additions & 1 deletion lightning/src/ln/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ pub(super) struct ReestablishResponses {
pub holding_cell_failed_htlcs: Vec<(HTLCSource, PaymentHash)>,
pub announcement_sigs: Option<msgs::AnnouncementSignatures>,
pub shutdown_msg: Option<msgs::Shutdown>,
pub channel_reestablish_msg: Option<msgs::ChannelReestablish>,
}

/// If the majority of the channels funds are to the fundee and the initiator holds only just
Expand Down Expand Up @@ -3743,6 +3744,7 @@ impl<Signer: Sign> Channel<Signer> {
order: RAACommitmentOrder::CommitmentFirst,
holding_cell_failed_htlcs: Vec::new(),
shutdown_msg, announcement_sigs,
channel_reestablish_msg: None,
});
}

Expand All @@ -3758,6 +3760,7 @@ impl<Signer: Sign> Channel<Signer> {
order: RAACommitmentOrder::CommitmentFirst,
holding_cell_failed_htlcs: Vec::new(),
shutdown_msg, announcement_sigs,
channel_reestablish_msg: None,
});
}

Expand Down Expand Up @@ -3816,6 +3819,7 @@ impl<Signer: Sign> Channel<Signer> {
order: self.resend_order.clone(),
mon_update: Some(monitor_update),
holding_cell_failed_htlcs,
channel_reestablish_msg: None,
})
},
Ok((None, holding_cell_failed_htlcs)) => {
Expand All @@ -3826,6 +3830,7 @@ impl<Signer: Sign> Channel<Signer> {
order: self.resend_order.clone(),
mon_update: None,
holding_cell_failed_htlcs,
channel_reestablish_msg: None,
})
},
}
Expand All @@ -3837,6 +3842,7 @@ impl<Signer: Sign> Channel<Signer> {
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 {
Expand All @@ -3853,6 +3859,7 @@ impl<Signer: Sign> Channel<Signer> {
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 {
Expand All @@ -3862,10 +3869,31 @@ impl<Signer: Sign> Channel<Signer> {
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)
})
}
}

Expand Down
11 changes: 11 additions & 0 deletions lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4761,6 +4761,17 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> 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.
Expand Down

0 comments on commit 9fd0292

Please sign in to comment.