Skip to content

Commit 224fb05

Browse files
committed
Rebroadcast shutdown on channel_reestablish (and reprocess them)
1 parent 527d036 commit 224fb05

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

src/ln/channel.rs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2071,6 +2071,9 @@ impl Channel {
20712071
self.channel_state = ChannelState::ShutdownComplete as u32;
20722072
return outbound_drops;
20732073
}
2074+
// Upon reconnect we have to start the closing_signed dance over, but shutdown messages
2075+
// will be retransmitted.
2076+
self.last_sent_closing_fee = None;
20742077

20752078
let mut inbound_drop_count = 0;
20762079
self.pending_inbound_htlcs.retain(|htlc| {
@@ -2258,7 +2261,7 @@ impl Channel {
22582261

22592262
/// May panic if some calls other than message-handling calls (which will all Err immediately)
22602263
/// have been called between remove_uncommitted_htlcs_and_mark_paused and this call.
2261-
pub fn channel_reestablish(&mut self, msg: &msgs::ChannelReestablish) -> Result<(Option<msgs::FundingLocked>, Option<msgs::RevokeAndACK>, Option<msgs::CommitmentUpdate>, Option<ChannelMonitor>, RAACommitmentOrder), ChannelError> {
2264+
pub fn channel_reestablish(&mut self, msg: &msgs::ChannelReestablish) -> Result<(Option<msgs::FundingLocked>, Option<msgs::RevokeAndACK>, Option<msgs::CommitmentUpdate>, Option<ChannelMonitor>, RAACommitmentOrder, Option<msgs::Shutdown>), ChannelError> {
22622265
if self.channel_state & (ChannelState::PeerDisconnected as u32) == 0 {
22632266
// While BOLT 2 doesn't indicate explicitly we should error this channel here, it
22642267
// almost certainly indicates we are going to end up out-of-sync in some way, so we
@@ -2274,9 +2277,16 @@ impl Channel {
22742277
// remaining cases either succeed or ErrorMessage-fail).
22752278
self.channel_state &= !(ChannelState::PeerDisconnected as u32);
22762279

2280+
let shutdown_msg = if self.channel_state & (ChannelState::LocalShutdownSent as u32) != 0 {
2281+
Some(msgs::Shutdown {
2282+
channel_id: self.channel_id,
2283+
scriptpubkey: self.get_closing_scriptpubkey(),
2284+
})
2285+
} else { None };
2286+
22772287
if self.channel_state & (ChannelState::FundingSent as u32 | ChannelState::OurFundingLocked as u32) == ChannelState::FundingSent as u32 {
22782288
// Short circuit the whole handler as there is nothing we can resend them
2279-
return Ok((None, None, None, None, RAACommitmentOrder::CommitmentFirst));
2289+
return Ok((None, None, None, None, RAACommitmentOrder::CommitmentFirst, shutdown_msg));
22802290
}
22812291

22822292
if msg.next_local_commitment_number == 0 || msg.next_remote_commitment_number == 0 {
@@ -2289,7 +2299,7 @@ impl Channel {
22892299
return Ok((Some(msgs::FundingLocked {
22902300
channel_id: self.channel_id(),
22912301
next_per_commitment_point: next_per_commitment_point,
2292-
}), None, None, None, RAACommitmentOrder::CommitmentFirst));
2302+
}), None, None, None, RAACommitmentOrder::CommitmentFirst, shutdown_msg));
22932303
}
22942304

22952305
let required_revoke = if msg.next_remote_commitment_number == INITIAL_COMMITMENT_NUMBER - self.cur_local_commitment_transaction_number {
@@ -2352,11 +2362,11 @@ impl Channel {
23522362
panic!("Got non-channel-failing result from free_holding_cell_htlcs");
23532363
}
23542364
},
2355-
Ok(Some((commitment_update, channel_monitor))) => return Ok((resend_funding_locked, required_revoke, Some(commitment_update), Some(channel_monitor), order)),
2356-
Ok(None) => return Ok((resend_funding_locked, required_revoke, None, None, order)),
2365+
Ok(Some((commitment_update, channel_monitor))) => return Ok((resend_funding_locked, required_revoke, Some(commitment_update), Some(channel_monitor), order, shutdown_msg)),
2366+
Ok(None) => return Ok((resend_funding_locked, required_revoke, None, None, order, shutdown_msg)),
23572367
}
23582368
} else {
2359-
return Ok((resend_funding_locked, required_revoke, None, None, order));
2369+
return Ok((resend_funding_locked, required_revoke, None, None, order, shutdown_msg));
23602370
}
23612371
} else if msg.next_local_commitment_number == our_next_remote_commitment_number - 1 {
23622372
if required_revoke.is_some() {
@@ -2370,10 +2380,10 @@ impl Channel {
23702380

23712381
if self.channel_state & (ChannelState::MonitorUpdateFailed as u32) != 0 {
23722382
self.monitor_pending_commitment_signed = true;
2373-
return Ok((resend_funding_locked, None, None, None, order));
2383+
return Ok((resend_funding_locked, None, None, None, order, shutdown_msg));
23742384
}
23752385

2376-
return Ok((resend_funding_locked, required_revoke, Some(self.get_last_commitment_update()), None, order));
2386+
return Ok((resend_funding_locked, required_revoke, Some(self.get_last_commitment_update()), None, order, shutdown_msg));
23772387
} else {
23782388
return Err(ChannelError::Close("Peer attempted to reestablish channel with a very old remote commitment transaction"));
23792389
}
@@ -2421,9 +2431,6 @@ impl Channel {
24212431
return Err(ChannelError::Close("Got shutdown with remote pending HTLCs"));
24222432
}
24232433
}
2424-
if (self.channel_state & ChannelState::RemoteShutdownSent as u32) == ChannelState::RemoteShutdownSent as u32 {
2425-
return Err(ChannelError::Ignore("Remote peer sent duplicate shutdown message"));
2426-
}
24272434
assert_eq!(self.channel_state & ChannelState::ShutdownComplete as u32, 0);
24282435

24292436
// BOLT 2 says we must only send a scriptpubkey of certain standard forms, which are up to

src/ln/channelmanager.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2383,7 +2383,7 @@ impl ChannelManager {
23832383
if chan.get_their_node_id() != *their_node_id {
23842384
return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!", msg.channel_id));
23852385
}
2386-
let (funding_locked, revoke_and_ack, commitment_update, channel_monitor, order) = chan.channel_reestablish(msg)
2386+
let (funding_locked, revoke_and_ack, commitment_update, channel_monitor, order, shutdown) = chan.channel_reestablish(msg)
23872387
.map_err(|e| MsgHandleErrInternal::from_chan_maybe_close(e, msg.channel_id))?;
23882388
if let Some(monitor) = channel_monitor {
23892389
if let Err(_e) = self.monitor.add_update_monitor(monitor.get_funding_txo().unwrap(), monitor) {
@@ -2422,6 +2422,12 @@ impl ChannelManager {
24222422
send_raa!();
24232423
},
24242424
}
2425+
if let Some(msg) = shutdown {
2426+
channel_state.pending_msg_events.push(events::MessageSendEvent::SendShutdown {
2427+
node_id: their_node_id.clone(),
2428+
msg,
2429+
});
2430+
}
24252431
Ok(())
24262432
},
24272433
None => Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel", msg.channel_id))

0 commit comments

Comments
 (0)