Skip to content

Commit 8844e64

Browse files
committed
Fix reconnect message order on remote updates while waiting on RAA
1 parent 17577f7 commit 8844e64

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/ln/channel.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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+
/// Unon recipt of a channel_reestablish we have to figure out whether to send a revoke_and_ack
300+
/// first or a commitment update first. Generally, we prefer to send revoke_and_ack first, but
301+
/// if we had a pending commitment update of our own waiting on a remote revoke when we
302+
/// received the latest commitment update from the remote we have to make sure that gets resent
303+
/// 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();
@@ -2120,7 +2132,11 @@ impl Channel {
21202132
})
21212133
} else { None };
21222134

2123-
let order = RAACommitmentOrder::RevokeAndACKFirst;
2135+
let order = if self.received_commitment_while_awaiting_raa {
2136+
RAACommitmentOrder::CommitmentFirst
2137+
} else {
2138+
RAACommitmentOrder::RevokeAndACKFirst
2139+
};
21242140

21252141
if msg.next_local_commitment_number == our_next_remote_commitment_number {
21262142
if required_revoke.is_some() {

0 commit comments

Comments
 (0)