Skip to content

Commit de15e68

Browse files
committed
Rebroadcast shutdown on channel_reestablish (and reprocess them)
1 parent a2f9d35 commit de15e68

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| {
@@ -2261,7 +2264,7 @@ impl Channel {
22612264

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

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

22852295
if msg.next_local_commitment_number == 0 || msg.next_remote_commitment_number == 0 {
@@ -2292,7 +2302,7 @@ impl Channel {
22922302
return Ok((Some(msgs::FundingLocked {
22932303
channel_id: self.channel_id(),
22942304
next_per_commitment_point: next_per_commitment_point,
2295-
}), None, None, None, RAACommitmentOrder::CommitmentFirst));
2305+
}), None, None, None, RAACommitmentOrder::CommitmentFirst, shutdown_msg));
22962306
}
22972307

22982308
let required_revoke = if msg.next_remote_commitment_number == INITIAL_COMMITMENT_NUMBER - self.cur_local_commitment_transaction_number {
@@ -2355,11 +2365,11 @@ impl Channel {
23552365
panic!("Got non-channel-failing result from free_holding_cell_htlcs");
23562366
}
23572367
},
2358-
Ok(Some((commitment_update, channel_monitor))) => return Ok((resend_funding_locked, required_revoke, Some(commitment_update), Some(channel_monitor), order)),
2359-
Ok(None) => return Ok((resend_funding_locked, required_revoke, None, None, order)),
2368+
Ok(Some((commitment_update, channel_monitor))) => return Ok((resend_funding_locked, required_revoke, Some(commitment_update), Some(channel_monitor), order, shutdown_msg)),
2369+
Ok(None) => return Ok((resend_funding_locked, required_revoke, None, None, order, shutdown_msg)),
23602370
}
23612371
} else {
2362-
return Ok((resend_funding_locked, required_revoke, None, None, order));
2372+
return Ok((resend_funding_locked, required_revoke, None, None, order, shutdown_msg));
23632373
}
23642374
} else if msg.next_local_commitment_number == our_next_remote_commitment_number - 1 {
23652375
if required_revoke.is_some() {
@@ -2373,10 +2383,10 @@ impl Channel {
23732383

23742384
if self.channel_state & (ChannelState::MonitorUpdateFailed as u32) != 0 {
23752385
self.monitor_pending_commitment_signed = true;
2376-
return Ok((resend_funding_locked, None, None, None, order));
2386+
return Ok((resend_funding_locked, None, None, None, order, shutdown_msg));
23772387
}
23782388

2379-
return Ok((resend_funding_locked, required_revoke, Some(self.get_last_commitment_update()), None, order));
2389+
return Ok((resend_funding_locked, required_revoke, Some(self.get_last_commitment_update()), None, order, shutdown_msg));
23802390
} else {
23812391
return Err(ChannelError::Close("Peer attempted to reestablish channel with a very old remote commitment transaction"));
23822392
}
@@ -2424,9 +2434,6 @@ impl Channel {
24242434
return Err(ChannelError::Close("Got shutdown with remote pending HTLCs"));
24252435
}
24262436
}
2427-
if (self.channel_state & ChannelState::RemoteShutdownSent as u32) == ChannelState::RemoteShutdownSent as u32 {
2428-
return Err(ChannelError::Ignore("Remote peer sent duplicate shutdown message"));
2429-
}
24302437
assert_eq!(self.channel_state & ChannelState::ShutdownComplete as u32, 0);
24312438

24322439
// 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)