Skip to content

Commit 65b23d8

Browse files
authored
Merge pull request #212 from TheBlueMatt/2018-10-two-updates-disconnect
Fix reconnect message order on remote updates while waiting on RAA
2 parents 270d1bd + 13b80ce commit 65b23d8

File tree

5 files changed

+214
-33
lines changed

5 files changed

+214
-33
lines changed

src/ln/channel.rs

Lines changed: 24 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};
@@ -296,6 +296,12 @@ pub(super) struct Channel {
296296
cur_local_commitment_transaction_number: u64,
297297
cur_remote_commitment_transaction_number: u64,
298298
value_to_self_msat: u64, // Excluding all pending_htlcs, excluding fees
299+
/// Upon receipt of a channel_reestablish we have to figure out whether to send a
300+
/// revoke_and_ack first or a commitment update first. Generally, we prefer to send
301+
/// revoke_and_ack first, but if we had a pending commitment update of our own waiting on a
302+
/// remote revoke when we received the latest commitment update from the remote we have to make
303+
/// sure that commitment update gets resent first.
304+
received_commitment_while_awaiting_raa: bool,
299305
pending_inbound_htlcs: Vec<InboundHTLCOutput>,
300306
pending_outbound_htlcs: Vec<OutboundHTLCOutput>,
301307
holding_cell_htlc_updates: Vec<HTLCUpdateAwaitingACK>,
@@ -492,6 +498,8 @@ impl Channel {
492498
cur_local_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
493499
cur_remote_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
494500
value_to_self_msat: channel_value_satoshis * 1000 - push_msat,
501+
received_commitment_while_awaiting_raa: false,
502+
495503
pending_inbound_htlcs: Vec::new(),
496504
pending_outbound_htlcs: Vec::new(),
497505
holding_cell_htlc_updates: Vec::new(),
@@ -647,6 +655,8 @@ impl Channel {
647655
cur_local_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
648656
cur_remote_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
649657
value_to_self_msat: msg.push_msat,
658+
received_commitment_while_awaiting_raa: false,
659+
650660
pending_inbound_htlcs: Vec::new(),
651661
pending_outbound_htlcs: Vec::new(),
652662
holding_cell_htlc_updates: Vec::new(),
@@ -1696,6 +1706,7 @@ impl Channel {
16961706

16971707
self.cur_local_commitment_transaction_number -= 1;
16981708
self.last_local_commitment_txn = new_local_commitment_txn;
1709+
self.received_commitment_while_awaiting_raa = (self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32)) != 0;
16991710

17001711
let (our_commitment_signed, monitor_update) = if need_our_commitment && (self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32)) == 0 {
17011712
// If we're AwaitingRemoteRevoke we can't send a new commitment here, but that's ok -
@@ -1831,6 +1842,7 @@ impl Channel {
18311842
self.their_prev_commitment_point = self.their_cur_commitment_point;
18321843
self.their_cur_commitment_point = Some(msg.next_per_commitment_point);
18331844
self.cur_remote_commitment_transaction_number -= 1;
1845+
self.received_commitment_while_awaiting_raa = false;
18341846

18351847
let mut to_forward_infos = Vec::new();
18361848
let mut revoked_htlcs = Vec::new();
@@ -2072,7 +2084,7 @@ impl Channel {
20722084

20732085
/// May panic if some calls other than message-handling calls (which will all Err immediately)
20742086
/// 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> {
2087+
pub fn channel_reestablish(&mut self, msg: &msgs::ChannelReestablish) -> Result<(Option<msgs::FundingLocked>, Option<msgs::RevokeAndACK>, Option<msgs::CommitmentUpdate>, Option<ChannelMonitor>, RAACommitmentOrder), ChannelError> {
20762088
if self.channel_state & (ChannelState::PeerDisconnected as u32) == 0 {
20772089
// While BOLT 2 doesn't indicate explicitly we should error this channel here, it
20782090
// almost certainly indicates we are going to end up out-of-sync in some way, so we
@@ -2120,6 +2132,12 @@ impl Channel {
21202132
})
21212133
} else { None };
21222134

2135+
let order = if self.received_commitment_while_awaiting_raa {
2136+
RAACommitmentOrder::CommitmentFirst
2137+
} else {
2138+
RAACommitmentOrder::RevokeAndACKFirst
2139+
};
2140+
21232141
if msg.next_local_commitment_number == our_next_remote_commitment_number {
21242142
if required_revoke.is_some() {
21252143
log_debug!(self, "Reconnected channel {} with only lost outbound RAA", log_bytes!(self.channel_id()));
@@ -2142,11 +2160,11 @@ impl Channel {
21422160
panic!("Got non-channel-failing result from free_holding_cell_htlcs");
21432161
}
21442162
},
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)),
2163+
Ok(Some((commitment_update, channel_monitor))) => return Ok((resend_funding_locked, required_revoke, Some(commitment_update), Some(channel_monitor), order)),
2164+
Ok(None) => return Ok((resend_funding_locked, required_revoke, None, None, order)),
21472165
}
21482166
} else {
2149-
return Ok((resend_funding_locked, required_revoke, None, None));
2167+
return Ok((resend_funding_locked, required_revoke, None, None, order));
21502168
}
21512169
} else if msg.next_local_commitment_number == our_next_remote_commitment_number - 1 {
21522170
if required_revoke.is_some() {
@@ -2206,7 +2224,7 @@ impl Channel {
22062224
update_add_htlcs, update_fulfill_htlcs, update_fail_htlcs, update_fail_malformed_htlcs,
22072225
update_fee: None, //TODO: We need to support re-generating any update_fees in the last commitment_signed!
22082226
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));
2227+
}), None, order));
22102228
} else {
22112229
return Err(ChannelError::Close("Peer attempted to reestablish channel with a very old remote commitment transaction"));
22122230
}

src/ln/channelmanager.rs

Lines changed: 142 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 {
@@ -5316,6 +5318,141 @@ mod tests {
53165318
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage);
53175319
}
53185320

5321+
#[test]
5322+
fn test_drop_messages_peer_disconnect_dual_htlc() {
5323+
// Test that we can handle reconnecting when both sides of a channel have pending
5324+
// commitment_updates when we disconnect.
5325+
let mut nodes = create_network(2);
5326+
create_announced_chan_between_nodes(&nodes, 0, 1);
5327+
5328+
let (payment_preimage_1, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
5329+
5330+
// Now try to send a second payment which will fail to send
5331+
let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
5332+
let (payment_preimage_2, payment_hash_2) = get_payment_preimage_hash!(nodes[0]);
5333+
5334+
nodes[0].node.send_payment(route.clone(), payment_hash_2).unwrap();
5335+
check_added_monitors!(nodes[0], 1);
5336+
5337+
let events_1 = nodes[0].node.get_and_clear_pending_events();
5338+
assert_eq!(events_1.len(), 1);
5339+
match events_1[0] {
5340+
Event::UpdateHTLCs { .. } => {},
5341+
_ => panic!("Unexpected event"),
5342+
}
5343+
5344+
assert!(nodes[1].node.claim_funds(payment_preimage_1));
5345+
check_added_monitors!(nodes[1], 1);
5346+
5347+
let events_2 = nodes[1].node.get_and_clear_pending_events();
5348+
assert_eq!(events_2.len(), 1);
5349+
match events_2[0] {
5350+
Event::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref update_fee, ref commitment_signed } } => {
5351+
assert_eq!(*node_id, nodes[0].node.get_our_node_id());
5352+
assert!(update_add_htlcs.is_empty());
5353+
assert_eq!(update_fulfill_htlcs.len(), 1);
5354+
assert!(update_fail_htlcs.is_empty());
5355+
assert!(update_fail_malformed_htlcs.is_empty());
5356+
assert!(update_fee.is_none());
5357+
5358+
nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &update_fulfill_htlcs[0]).unwrap();
5359+
let events_3 = nodes[0].node.get_and_clear_pending_events();
5360+
assert_eq!(events_3.len(), 1);
5361+
match events_3[0] {
5362+
Event::PaymentSent { ref payment_preimage } => {
5363+
assert_eq!(*payment_preimage, payment_preimage_1);
5364+
},
5365+
_ => panic!("Unexpected event"),
5366+
}
5367+
5368+
let (_, commitment_update) = nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), commitment_signed).unwrap();
5369+
assert!(commitment_update.is_none());
5370+
check_added_monitors!(nodes[0], 1);
5371+
},
5372+
_ => panic!("Unexpected event"),
5373+
}
5374+
5375+
nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
5376+
nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
5377+
5378+
let reestablish_1 = nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id());
5379+
assert_eq!(reestablish_1.len(), 1);
5380+
let reestablish_2 = nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id());
5381+
assert_eq!(reestablish_2.len(), 1);
5382+
5383+
let as_resp = nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_2[0]).unwrap();
5384+
let bs_resp = nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[0]).unwrap();
5385+
5386+
assert!(as_resp.0.is_none());
5387+
assert!(bs_resp.0.is_none());
5388+
5389+
assert!(bs_resp.1.is_none());
5390+
assert!(bs_resp.2.is_none());
5391+
5392+
assert!(as_resp.3 == msgs::RAACommitmentOrder::CommitmentFirst);
5393+
5394+
assert_eq!(as_resp.2.as_ref().unwrap().update_add_htlcs.len(), 1);
5395+
assert!(as_resp.2.as_ref().unwrap().update_fulfill_htlcs.is_empty());
5396+
assert!(as_resp.2.as_ref().unwrap().update_fail_htlcs.is_empty());
5397+
assert!(as_resp.2.as_ref().unwrap().update_fail_malformed_htlcs.is_empty());
5398+
assert!(as_resp.2.as_ref().unwrap().update_fee.is_none());
5399+
nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &as_resp.2.as_ref().unwrap().update_add_htlcs[0]).unwrap();
5400+
let (bs_revoke_and_ack, bs_commitment_signed) = nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_resp.2.as_ref().unwrap().commitment_signed).unwrap();
5401+
assert!(bs_commitment_signed.is_none());
5402+
check_added_monitors!(nodes[1], 1);
5403+
5404+
let bs_second_commitment_signed = nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), as_resp.1.as_ref().unwrap()).unwrap().unwrap();
5405+
assert!(bs_second_commitment_signed.update_add_htlcs.is_empty());
5406+
assert!(bs_second_commitment_signed.update_fulfill_htlcs.is_empty());
5407+
assert!(bs_second_commitment_signed.update_fail_htlcs.is_empty());
5408+
assert!(bs_second_commitment_signed.update_fail_malformed_htlcs.is_empty());
5409+
assert!(bs_second_commitment_signed.update_fee.is_none());
5410+
check_added_monitors!(nodes[1], 1);
5411+
5412+
let as_commitment_signed = nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_revoke_and_ack).unwrap().unwrap();
5413+
assert!(as_commitment_signed.update_add_htlcs.is_empty());
5414+
assert!(as_commitment_signed.update_fulfill_htlcs.is_empty());
5415+
assert!(as_commitment_signed.update_fail_htlcs.is_empty());
5416+
assert!(as_commitment_signed.update_fail_malformed_htlcs.is_empty());
5417+
assert!(as_commitment_signed.update_fee.is_none());
5418+
check_added_monitors!(nodes[0], 1);
5419+
5420+
let (as_revoke_and_ack, as_second_commitment_signed) = nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_second_commitment_signed.commitment_signed).unwrap();
5421+
assert!(as_second_commitment_signed.is_none());
5422+
check_added_monitors!(nodes[0], 1);
5423+
5424+
let (bs_second_revoke_and_ack, bs_third_commitment_signed) = nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_commitment_signed.commitment_signed).unwrap();
5425+
assert!(bs_third_commitment_signed.is_none());
5426+
check_added_monitors!(nodes[1], 1);
5427+
5428+
assert!(nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_revoke_and_ack).unwrap().is_none());
5429+
check_added_monitors!(nodes[1], 1);
5430+
5431+
let events_4 = nodes[1].node.get_and_clear_pending_events();
5432+
assert_eq!(events_4.len(), 1);
5433+
match events_4[0] {
5434+
Event::PendingHTLCsForwardable { .. } => { },
5435+
_ => panic!("Unexpected event"),
5436+
};
5437+
5438+
nodes[1].node.channel_state.lock().unwrap().next_forward = Instant::now();
5439+
nodes[1].node.process_pending_htlc_forwards();
5440+
5441+
let events_5 = nodes[1].node.get_and_clear_pending_events();
5442+
assert_eq!(events_5.len(), 1);
5443+
match events_5[0] {
5444+
Event::PaymentReceived { ref payment_hash, amt: _ } => {
5445+
assert_eq!(payment_hash_2, *payment_hash);
5446+
},
5447+
_ => panic!("Unexpected event"),
5448+
}
5449+
5450+
assert!(nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_second_revoke_and_ack).unwrap().is_none());
5451+
check_added_monitors!(nodes[0], 1);
5452+
5453+
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2);
5454+
}
5455+
53195456
#[test]
53205457
fn test_invalid_channel_announcement() {
53215458
//Test BOLT 7 channel_announcement msg requirement for final node, gather data to build customed channel_announcement msgs

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.

0 commit comments

Comments
 (0)