Skip to content

Commit 78232f2

Browse files
committed
Fail incoming HTLCs sent after we start shutdown, not the chan
1 parent a11e27d commit 78232f2

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/ln/channel.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1553,6 +1553,12 @@ impl Channel {
15531553

15541554
//TODO: Check msg.cltv_expiry further? Do this in channel manager?
15551555

1556+
if self.channel_state & ChannelState::LocalShutdownSent as u32 != 0 {
1557+
if let PendingHTLCStatus::Forward(_) = pending_forward_state {
1558+
panic!("ChannelManager shouldn't be trying to add a forwardable HTLC after we've started closing");
1559+
}
1560+
}
1561+
15561562
// Now update local state:
15571563
self.next_remote_htlc_id += 1;
15581564
self.pending_inbound_htlcs.push(InboundHTLCOutput {

src/ln/channelmanager.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1929,7 +1929,7 @@ impl ChannelManager {
19291929
//encrypted with the same key. Its not immediately obvious how to usefully exploit that,
19301930
//but we should prevent it anyway.
19311931

1932-
let (pending_forward_info, mut channel_state_lock) = self.decode_update_add_htlc_onion(msg);
1932+
let (mut pending_forward_info, mut channel_state_lock) = self.decode_update_add_htlc_onion(msg);
19331933
let channel_state = channel_state_lock.borrow_parts();
19341934

19351935
match channel_state.by_id.get_mut(&msg.channel_id) {
@@ -1939,7 +1939,16 @@ impl ChannelManager {
19391939
return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!", msg.channel_id));
19401940
}
19411941
if !chan.is_usable() {
1942-
return Err(MsgHandleErrInternal::from_no_close(HandleError{err: "Channel not yet available for receiving HTLCs", action: Some(msgs::ErrorAction::IgnoreError)}));
1942+
// If the update_add is completely bogus, the call will Err and we will close,
1943+
// but if we've sent a shutdown and they haven't acknowledged it yet, we just
1944+
// want to reject the new HTLC and fail it backwards instead of forwarding.
1945+
if let PendingHTLCStatus::Forward(PendingForwardHTLCInfo { incoming_shared_secret, .. }) = pending_forward_info {
1946+
pending_forward_info = PendingHTLCStatus::Fail(HTLCFailureMsg::Relay(msgs::UpdateFailHTLC {
1947+
channel_id: msg.channel_id,
1948+
htlc_id: msg.htlc_id,
1949+
reason: ChannelManager::build_first_hop_failure_packet(&incoming_shared_secret, 0x1000|20, &self.get_channel_update(chan).unwrap().encode_with_len()[..]),
1950+
}));
1951+
}
19431952
}
19441953
chan.update_add_htlc(&msg, pending_forward_info).map_err(|e| MsgHandleErrInternal::from_maybe_close(e))
19451954
},

0 commit comments

Comments
 (0)