Skip to content

Commit 17577f7

Browse files
committed
Add message ordering return value to handling channel_reestablish
1 parent da70db2 commit 17577f7

File tree

5 files changed

+62
-33
lines changed

5 files changed

+62
-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;
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};
@@ -1967,17 +1967,17 @@ impl ChannelManager {
19671967
Ok(())
19681968
}
19691969

1970-
fn internal_channel_reestablish(&self, their_node_id: &PublicKey, msg: &msgs::ChannelReestablish) -> Result<(Option<msgs::FundingLocked>, Option<msgs::RevokeAndACK>, Option<msgs::CommitmentUpdate>), MsgHandleErrInternal> {
1970+
fn internal_channel_reestablish(&self, their_node_id: &PublicKey, msg: &msgs::ChannelReestablish) -> Result<(Option<msgs::FundingLocked>, Option<msgs::RevokeAndACK>, Option<msgs::CommitmentUpdate>, RAACommitmentOrder), MsgHandleErrInternal> {
19711971
let (res, chan_monitor) = {
19721972
let mut channel_state = self.channel_state.lock().unwrap();
19731973
match channel_state.by_id.get_mut(&msg.channel_id) {
19741974
Some(chan) => {
19751975
if chan.get_their_node_id() != *their_node_id {
19761976
return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!", msg.channel_id));
19771977
}
1978-
let (funding_locked, revoke_and_ack, commitment_update, channel_monitor) = chan.channel_reestablish(msg)
1978+
let (funding_locked, revoke_and_ack, commitment_update, channel_monitor, order) = chan.channel_reestablish(msg)
19791979
.map_err(|e| MsgHandleErrInternal::from_chan_maybe_close(e, msg.channel_id))?;
1980-
(Ok((funding_locked, revoke_and_ack, commitment_update)), channel_monitor)
1980+
(Ok((funding_locked, revoke_and_ack, commitment_update, order)), channel_monitor)
19811981
},
19821982
None => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel", msg.channel_id))
19831983
}
@@ -2247,7 +2247,7 @@ impl ChannelMessageHandler for ChannelManager {
22472247
handle_error!(self, self.internal_announcement_signatures(their_node_id, msg), their_node_id)
22482248
}
22492249

2250-
fn handle_channel_reestablish(&self, their_node_id: &PublicKey, msg: &msgs::ChannelReestablish) -> Result<(Option<msgs::FundingLocked>, Option<msgs::RevokeAndACK>, Option<msgs::CommitmentUpdate>), HandleError> {
2250+
fn handle_channel_reestablish(&self, their_node_id: &PublicKey, msg: &msgs::ChannelReestablish) -> Result<(Option<msgs::FundingLocked>, Option<msgs::RevokeAndACK>, Option<msgs::CommitmentUpdate>, RAACommitmentOrder), HandleError> {
22512251
handle_error!(self, self.internal_channel_reestablish(their_node_id, msg), their_node_id)
22522252
}
22532253

@@ -4725,6 +4725,7 @@ mod tests {
47254725
assert!(chan_msgs.0.is_none());
47264726
}
47274727
if pending_raa.0 {
4728+
assert!(chan_msgs.3 == msgs::RAACommitmentOrder::RevokeAndACKFirst);
47284729
assert!(node_a.node.handle_revoke_and_ack(&node_b.node.get_our_node_id(), &chan_msgs.1.unwrap()).unwrap().is_none());
47294730
check_added_monitors!(node_a, 1);
47304731
} else {
@@ -4772,6 +4773,7 @@ mod tests {
47724773
assert!(chan_msgs.0.is_none());
47734774
}
47744775
if pending_raa.1 {
4776+
assert!(chan_msgs.3 == msgs::RAACommitmentOrder::RevokeAndACKFirst);
47754777
assert!(node_b.node.handle_revoke_and_ack(&node_a.node.get_our_node_id(), &chan_msgs.1.unwrap()).unwrap().is_none());
47764778
check_added_monitors!(node_b, 1);
47774779
} else {

src/ln/msgs.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,17 @@ pub enum HTLCFailChannelUpdate {
488488
},
489489
}
490490

491+
/// For events which result in both a RevokeAndACK and a CommitmentUpdate, by default they should
492+
/// be sent in the order they appear in the return value, however sometimes the order needs to be
493+
/// variable at rumtime. In those cases, this enum is also returned.
494+
#[derive(Clone, PartialEq)]
495+
pub enum RAACommitmentOrder {
496+
/// Send the CommitmentUpdate messages first
497+
CommitmentFirst,
498+
/// Send the RevokeAndACK message first
499+
RevokeAndACKFirst,
500+
}
501+
491502
/// A trait to describe an object which can receive channel messages.
492503
///
493504
/// Messages MAY be called in parallel when they originate from different their_node_ids, however
@@ -542,7 +553,7 @@ pub trait ChannelMessageHandler : events::EventsProvider + Send + Sync {
542553
/// Handle a peer reconnecting, possibly generating channel_reestablish message(s).
543554
fn peer_connected(&self, their_node_id: &PublicKey) -> Vec<ChannelReestablish>;
544555
/// Handle an incoming channel_reestablish message from the given peer.
545-
fn handle_channel_reestablish(&self, their_node_id: &PublicKey, msg: &ChannelReestablish) -> Result<(Option<FundingLocked>, Option<RevokeAndACK>, Option<CommitmentUpdate>), HandleError>;
556+
fn handle_channel_reestablish(&self, their_node_id: &PublicKey, msg: &ChannelReestablish) -> Result<(Option<FundingLocked>, Option<RevokeAndACK>, Option<CommitmentUpdate>, RAACommitmentOrder), HandleError>;
546557

547558
// Error:
548559
/// 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
@@ -661,30 +661,44 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
661661
},
662662
136 => {
663663
let msg = try_potential_decodeerror!(msgs::ChannelReestablish::read(&mut reader));
664-
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));
664+
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));
665665
if let Some(lock_msg) = funding_locked {
666666
encode_and_send_msg!(lock_msg, 36);
667667
}
668-
if let Some(revoke_msg) = revoke_and_ack {
669-
encode_and_send_msg!(revoke_msg, 133);
670-
}
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);
668+
macro_rules! handle_raa { () => {
669+
if let Some(revoke_msg) = revoke_and_ack {
670+
encode_and_send_msg!(revoke_msg, 133);
671+
}
672+
} }
673+
macro_rules! handle_cu { () => {
674+
match commitment_update {
675+
Some(resps) => {
676+
for resp in resps.update_add_htlcs {
677+
encode_and_send_msg!(resp, 128);
678+
}
679+
for resp in resps.update_fulfill_htlcs {
680+
encode_and_send_msg!(resp, 130);
681+
}
682+
for resp in resps.update_fail_htlcs {
683+
encode_and_send_msg!(resp, 131);
684+
}
685+
if let Some(resp) = resps.update_fee {
686+
encode_and_send_msg!(resp, 134);
687+
}
688+
encode_and_send_msg!(resps.commitment_signed, 132);
689+
},
690+
None => {},
691+
}
692+
} }
693+
match order {
694+
msgs::RAACommitmentOrder::RevokeAndACKFirst => {
695+
handle_raa!();
696+
handle_cu!();
697+
},
698+
msgs::RAACommitmentOrder::CommitmentFirst => {
699+
handle_cu!();
700+
handle_raa!();
686701
},
687-
None => {},
688702
}
689703
},
690704

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)