Skip to content

Commit f794aa4

Browse files
committed
Add message ordering return value to handling channel_reestablish
1 parent 920d145 commit f794aa4

File tree

5 files changed

+63
-33
lines changed

5 files changed

+63
-33
lines changed

src/ln/channel.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crypto::digest::Digest;
1414
use crypto::hkdf::{hkdf_extract,hkdf_expand};
1515

1616
use ln::msgs;
17-
use ln::msgs::{ErrorAction, HandleError};
17+
use ln::msgs::{ErrorAction, HandleError, RAACommitmentOrder};
1818
use ln::channelmonitor::ChannelMonitor;
1919
use ln::channelmanager::{PendingHTLCStatus, HTLCSource, PendingForwardHTLCInfo, HTLCFailReason, HTLCFailureMsg};
2020
use ln::chan_utils::{TxCreationKeys,HTLCOutputInCommitment,HTLC_SUCCESS_TX_WEIGHT,HTLC_TIMEOUT_TX_WEIGHT};
@@ -2072,7 +2072,7 @@ impl Channel {
20722072

20732073
/// May panic if some calls other than message-handling calls (which will all Err immediately)
20742074
/// have been called between remove_uncommitted_htlcs_and_mark_paused and this call.
2075-
pub fn channel_reestablish(&mut self, msg: &msgs::ChannelReestablish) -> Result<(Option<msgs::FundingLocked>, Option<msgs::RevokeAndACK>, Option<msgs::CommitmentUpdate>, Option<ChannelMonitor>), ChannelError> {
2075+
pub fn channel_reestablish(&mut self, msg: &msgs::ChannelReestablish) -> Result<(Option<msgs::FundingLocked>, Option<msgs::RevokeAndACK>, Option<msgs::CommitmentUpdate>, Option<ChannelMonitor>, RAACommitmentOrder), ChannelError> {
20762076
if self.channel_state & (ChannelState::PeerDisconnected as u32) == 0 {
20772077
// While BOLT 2 doesn't indicate explicitly we should error this channel here, it
20782078
// almost certainly indicates we are going to end up out-of-sync in some way, so we
@@ -2120,6 +2120,8 @@ impl Channel {
21202120
})
21212121
} else { None };
21222122

2123+
let order = RAACommitmentOrder::RevokeAndACKFirst;
2124+
21232125
if msg.next_local_commitment_number == our_next_remote_commitment_number {
21242126
if required_revoke.is_some() {
21252127
log_debug!(self, "Reconnected channel {} with only lost outbound RAA", log_bytes!(self.channel_id()));
@@ -2142,11 +2144,11 @@ impl Channel {
21422144
panic!("Got non-channel-failing result from free_holding_cell_htlcs");
21432145
}
21442146
},
2145-
Ok(Some((commitment_update, channel_monitor))) => return Ok((resend_funding_locked, required_revoke, Some(commitment_update), Some(channel_monitor))),
2146-
Ok(None) => return Ok((resend_funding_locked, required_revoke, None, None)),
2147+
Ok(Some((commitment_update, channel_monitor))) => return Ok((resend_funding_locked, required_revoke, Some(commitment_update), Some(channel_monitor), order)),
2148+
Ok(None) => return Ok((resend_funding_locked, required_revoke, None, None, order)),
21472149
}
21482150
} else {
2149-
return Ok((resend_funding_locked, required_revoke, None, None));
2151+
return Ok((resend_funding_locked, required_revoke, None, None, order));
21502152
}
21512153
} else if msg.next_local_commitment_number == our_next_remote_commitment_number - 1 {
21522154
if required_revoke.is_some() {
@@ -2206,7 +2208,7 @@ impl Channel {
22062208
update_add_htlcs, update_fulfill_htlcs, update_fail_htlcs, update_fail_malformed_htlcs,
22072209
update_fee: None, //TODO: We need to support re-generating any update_fees in the last commitment_signed!
22082210
commitment_signed: self.send_commitment_no_state_update().expect("It looks like we failed to re-generate a commitment_signed we had previously sent?").0,
2209-
}), None));
2211+
}), None, order));
22102212
} else {
22112213
return Err(ChannelError::Close("Peer attempted to reestablish channel with a very old remote commitment transaction"));
22122214
}

src/ln/channelmanager.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use ln::channel::{Channel, ChannelError, ChannelKeys};
2626
use ln::channelmonitor::{ManyChannelMonitor, CLTV_CLAIM_BUFFER, HTLC_FAIL_TIMEOUT_BLOCKS};
2727
use ln::router::{Route,RouteHop};
2828
use ln::msgs;
29-
use ln::msgs::{HandleError,ChannelMessageHandler};
29+
use ln::msgs::{ChannelMessageHandler, HandleError, RAACommitmentOrder};
3030
use util::{byte_utils, events, internal_traits, rng};
3131
use util::sha2::Sha256;
3232
use util::ser::{Readable, Writeable};
@@ -2168,17 +2168,17 @@ impl ChannelManager {
21682168
Ok(())
21692169
}
21702170

2171-
fn internal_channel_reestablish(&self, their_node_id: &PublicKey, msg: &msgs::ChannelReestablish) -> Result<(Option<msgs::FundingLocked>, Option<msgs::RevokeAndACK>, Option<msgs::CommitmentUpdate>), MsgHandleErrInternal> {
2171+
fn internal_channel_reestablish(&self, their_node_id: &PublicKey, msg: &msgs::ChannelReestablish) -> Result<(Option<msgs::FundingLocked>, Option<msgs::RevokeAndACK>, Option<msgs::CommitmentUpdate>, RAACommitmentOrder), MsgHandleErrInternal> {
21722172
let (res, chan_monitor) = {
21732173
let mut channel_state = self.channel_state.lock().unwrap();
21742174
match channel_state.by_id.get_mut(&msg.channel_id) {
21752175
Some(chan) => {
21762176
if chan.get_their_node_id() != *their_node_id {
21772177
return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!", msg.channel_id));
21782178
}
2179-
let (funding_locked, revoke_and_ack, commitment_update, channel_monitor) = chan.channel_reestablish(msg)
2179+
let (funding_locked, revoke_and_ack, commitment_update, channel_monitor, order) = chan.channel_reestablish(msg)
21802180
.map_err(|e| MsgHandleErrInternal::from_chan_maybe_close(e, msg.channel_id))?;
2181-
(Ok((funding_locked, revoke_and_ack, commitment_update)), channel_monitor)
2181+
(Ok((funding_locked, revoke_and_ack, commitment_update, order)), channel_monitor)
21822182
},
21832183
None => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel", msg.channel_id))
21842184
}
@@ -2448,7 +2448,7 @@ impl ChannelMessageHandler for ChannelManager {
24482448
handle_error!(self, self.internal_announcement_signatures(their_node_id, msg), their_node_id)
24492449
}
24502450

2451-
fn handle_channel_reestablish(&self, their_node_id: &PublicKey, msg: &msgs::ChannelReestablish) -> Result<(Option<msgs::FundingLocked>, Option<msgs::RevokeAndACK>, Option<msgs::CommitmentUpdate>), HandleError> {
2451+
fn handle_channel_reestablish(&self, their_node_id: &PublicKey, msg: &msgs::ChannelReestablish) -> Result<(Option<msgs::FundingLocked>, Option<msgs::RevokeAndACK>, Option<msgs::CommitmentUpdate>, RAACommitmentOrder), HandleError> {
24522452
handle_error!(self, self.internal_channel_reestablish(their_node_id, msg), their_node_id)
24532453
}
24542454

@@ -4938,6 +4938,7 @@ mod tests {
49384938
assert!(chan_msgs.0.is_none());
49394939
}
49404940
if pending_raa.0 {
4941+
assert!(chan_msgs.3 == msgs::RAACommitmentOrder::RevokeAndACKFirst);
49414942
assert!(node_a.node.handle_revoke_and_ack(&node_b.node.get_our_node_id(), &chan_msgs.1.unwrap()).unwrap().is_none());
49424943
check_added_monitors!(node_a, 1);
49434944
} else {
@@ -4985,6 +4986,7 @@ mod tests {
49854986
assert!(chan_msgs.0.is_none());
49864987
}
49874988
if pending_raa.1 {
4989+
assert!(chan_msgs.3 == msgs::RAACommitmentOrder::RevokeAndACKFirst);
49884990
assert!(node_b.node.handle_revoke_and_ack(&node_a.node.get_our_node_id(), &chan_msgs.1.unwrap()).unwrap().is_none());
49894991
check_added_monitors!(node_b, 1);
49904992
} else {

src/ln/msgs.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,18 @@ pub enum HTLCFailChannelUpdate {
500500
}
501501
}
502502

503+
/// For events which result in both a RevokeAndACK and a CommitmentUpdate, by default they should
504+
/// be sent in the order they appear in the return value, however sometimes the order needs to be
505+
/// variable at runtime (eg handle_channel_reestablish needs to re-send messages in the order they
506+
/// were originally sent). In those cases, this enum is also returned.
507+
#[derive(Clone, PartialEq)]
508+
pub enum RAACommitmentOrder {
509+
/// Send the CommitmentUpdate messages first
510+
CommitmentFirst,
511+
/// Send the RevokeAndACK message first
512+
RevokeAndACKFirst,
513+
}
514+
503515
/// A trait to describe an object which can receive channel messages.
504516
///
505517
/// Messages MAY be called in parallel when they originate from different their_node_ids, however
@@ -554,7 +566,7 @@ pub trait ChannelMessageHandler : events::EventsProvider + Send + Sync {
554566
/// Handle a peer reconnecting, possibly generating channel_reestablish message(s).
555567
fn peer_connected(&self, their_node_id: &PublicKey) -> Vec<ChannelReestablish>;
556568
/// Handle an incoming channel_reestablish message from the given peer.
557-
fn handle_channel_reestablish(&self, their_node_id: &PublicKey, msg: &ChannelReestablish) -> Result<(Option<FundingLocked>, Option<RevokeAndACK>, Option<CommitmentUpdate>), HandleError>;
569+
fn handle_channel_reestablish(&self, their_node_id: &PublicKey, msg: &ChannelReestablish) -> Result<(Option<FundingLocked>, Option<RevokeAndACK>, Option<CommitmentUpdate>, RAACommitmentOrder), HandleError>;
558570

559571
// Error:
560572
/// Handle an incoming error message from the given peer.

src/ln/peer_handler.rs

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -658,30 +658,44 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
658658
},
659659
136 => {
660660
let msg = try_potential_decodeerror!(msgs::ChannelReestablish::read(&mut reader));
661-
let (funding_locked, revoke_and_ack, commitment_update) = try_potential_handleerror!(self.message_handler.chan_handler.handle_channel_reestablish(&peer.their_node_id.unwrap(), &msg));
661+
let (funding_locked, revoke_and_ack, commitment_update, order) = try_potential_handleerror!(self.message_handler.chan_handler.handle_channel_reestablish(&peer.their_node_id.unwrap(), &msg));
662662
if let Some(lock_msg) = funding_locked {
663663
encode_and_send_msg!(lock_msg, 36);
664664
}
665-
if let Some(revoke_msg) = revoke_and_ack {
666-
encode_and_send_msg!(revoke_msg, 133);
667-
}
668-
match commitment_update {
669-
Some(resps) => {
670-
for resp in resps.update_add_htlcs {
671-
encode_and_send_msg!(resp, 128);
672-
}
673-
for resp in resps.update_fulfill_htlcs {
674-
encode_and_send_msg!(resp, 130);
675-
}
676-
for resp in resps.update_fail_htlcs {
677-
encode_and_send_msg!(resp, 131);
678-
}
679-
if let Some(resp) = resps.update_fee {
680-
encode_and_send_msg!(resp, 134);
681-
}
682-
encode_and_send_msg!(resps.commitment_signed, 132);
665+
macro_rules! handle_raa { () => {
666+
if let Some(revoke_msg) = revoke_and_ack {
667+
encode_and_send_msg!(revoke_msg, 133);
668+
}
669+
} }
670+
macro_rules! handle_cu { () => {
671+
match commitment_update {
672+
Some(resps) => {
673+
for resp in resps.update_add_htlcs {
674+
encode_and_send_msg!(resp, 128);
675+
}
676+
for resp in resps.update_fulfill_htlcs {
677+
encode_and_send_msg!(resp, 130);
678+
}
679+
for resp in resps.update_fail_htlcs {
680+
encode_and_send_msg!(resp, 131);
681+
}
682+
if let Some(resp) = resps.update_fee {
683+
encode_and_send_msg!(resp, 134);
684+
}
685+
encode_and_send_msg!(resps.commitment_signed, 132);
686+
},
687+
None => {},
688+
}
689+
} }
690+
match order {
691+
msgs::RAACommitmentOrder::RevokeAndACKFirst => {
692+
handle_raa!();
693+
handle_cu!();
694+
},
695+
msgs::RAACommitmentOrder::CommitmentFirst => {
696+
handle_cu!();
697+
handle_raa!();
683698
},
684-
None => {},
685699
}
686700
},
687701

src/util/test_utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ impl msgs::ChannelMessageHandler for TestChannelMessageHandler {
128128
fn handle_announcement_signatures(&self, _their_node_id: &PublicKey, _msg: &msgs::AnnouncementSignatures) -> Result<(), HandleError> {
129129
Err(HandleError { err: "", action: None })
130130
}
131-
fn handle_channel_reestablish(&self, _their_node_id: &PublicKey, _msg: &msgs::ChannelReestablish) -> Result<(Option<msgs::FundingLocked>, Option<msgs::RevokeAndACK>, Option<msgs::CommitmentUpdate>), HandleError> {
131+
fn handle_channel_reestablish(&self, _their_node_id: &PublicKey, _msg: &msgs::ChannelReestablish) -> Result<(Option<msgs::FundingLocked>, Option<msgs::RevokeAndACK>, Option<msgs::CommitmentUpdate>, msgs::RAACommitmentOrder), HandleError> {
132132
Err(HandleError { err: "", action: None })
133133
}
134134
fn peer_disconnected(&self, _their_node_id: &PublicKey, _no_connection_possible: bool) {}

0 commit comments

Comments
 (0)