Skip to content

Commit c0aeb61

Browse files
committed
Stop failing back HTLCs on peer disconnection
Previously, if we got disconnected from a peer while there were HTLCs pending forwarding in the holding cell, we'd clear them and fail them all backwards. This is largely fine, but since we now have support for handling such HTLCs on reconnect, we might as well not, instead relying on our timeout logic to fail them backwards if it takes too long to forward them.
1 parent f30b654 commit c0aeb61

File tree

2 files changed

+17
-61
lines changed

2 files changed

+17
-61
lines changed

lightning/src/ln/channel.rs

Lines changed: 12 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2691,13 +2691,11 @@ impl<Signer: Sign> Channel<Signer> {
26912691
/// implicitly dropping) and the payment_hashes of HTLCs we tried to add but are dropping.
26922692
/// No further message handling calls may be made until a channel_reestablish dance has
26932693
/// completed.
2694-
pub fn remove_uncommitted_htlcs_and_mark_paused<L: Deref>(&mut self, logger: &L) -> Vec<(HTLCSource, PaymentHash)> where L::Target: Logger {
2695-
let mut outbound_drops = Vec::new();
2696-
2694+
pub fn remove_uncommitted_htlcs_and_mark_paused<L: Deref>(&mut self, logger: &L) where L::Target: Logger {
26972695
assert_eq!(self.channel_state & ChannelState::ShutdownComplete as u32, 0);
26982696
if self.channel_state < ChannelState::FundingSent as u32 {
26992697
self.channel_state = ChannelState::ShutdownComplete as u32;
2700-
return outbound_drops;
2698+
return;
27012699
}
27022700
// Upon reconnect we have to start the closing_signed dance over, but shutdown messages
27032701
// will be retransmitted.
@@ -2740,23 +2738,8 @@ impl<Signer: Sign> Channel<Signer> {
27402738
}
27412739
}
27422740

2743-
self.holding_cell_htlc_updates.retain(|htlc_update| {
2744-
match htlc_update {
2745-
// Note that currently on channel reestablish we assert that there are
2746-
// no holding cell HTLC update_adds, so if in the future we stop
2747-
// dropping added HTLCs here and failing them backwards, then there will
2748-
// need to be corresponding changes made in the Channel's re-establish
2749-
// logic.
2750-
&HTLCUpdateAwaitingACK::AddHTLC { ref payment_hash, ref source, .. } => {
2751-
outbound_drops.push((source.clone(), payment_hash.clone()));
2752-
false
2753-
},
2754-
&HTLCUpdateAwaitingACK::ClaimHTLC {..} | &HTLCUpdateAwaitingACK::FailHTLC {..} => true,
2755-
}
2756-
});
27572741
self.channel_state |= ChannelState::PeerDisconnected as u32;
2758-
log_debug!(logger, "Peer disconnection resulted in {} remote-announced HTLC drops and {} waiting-to-locally-announced HTLC drops on channel {}", outbound_drops.len(), inbound_drop_count, log_bytes!(self.channel_id()));
2759-
outbound_drops
2742+
log_debug!(logger, "Peer disconnection resulted in {} waiting-to-locally-announced HTLC drops on channel {}", inbound_drop_count, log_bytes!(self.channel_id()));
27602743
}
27612744

27622745
/// Indicates that a ChannelMonitor update failed to be stored by the client and further
@@ -2914,7 +2897,7 @@ impl<Signer: Sign> Channel<Signer> {
29142897

29152898
/// May panic if some calls other than message-handling calls (which will all Err immediately)
29162899
/// have been called between remove_uncommitted_htlcs_and_mark_paused and this call.
2917-
pub fn channel_reestablish<L: Deref>(&mut self, msg: &msgs::ChannelReestablish, logger: &L) -> Result<(Option<msgs::FundingLocked>, Option<msgs::RevokeAndACK>, Option<msgs::CommitmentUpdate>, Option<ChannelMonitorUpdate>, RAACommitmentOrder, Option<msgs::Shutdown>), ChannelError> where L::Target: Logger {
2900+
pub fn channel_reestablish<L: Deref>(&mut self, msg: &msgs::ChannelReestablish, logger: &L) -> Result<(Option<msgs::FundingLocked>, Option<msgs::RevokeAndACK>, Option<msgs::CommitmentUpdate>, Option<ChannelMonitorUpdate>, RAACommitmentOrder, Vec<(HTLCSource, PaymentHash)>, Option<msgs::Shutdown>), ChannelError> where L::Target: Logger {
29182901
if self.channel_state & (ChannelState::PeerDisconnected as u32) == 0 {
29192902
// While BOLT 2 doesn't indicate explicitly we should error this channel here, it
29202903
// almost certainly indicates we are going to end up out-of-sync in some way, so we
@@ -2965,15 +2948,15 @@ impl<Signer: Sign> Channel<Signer> {
29652948
return Err(ChannelError::Close("Peer claimed they saw a revoke_and_ack but we haven't sent funding_locked yet".to_owned()));
29662949
}
29672950
// Short circuit the whole handler as there is nothing we can resend them
2968-
return Ok((None, None, None, None, RAACommitmentOrder::CommitmentFirst, shutdown_msg));
2951+
return Ok((None, None, None, None, RAACommitmentOrder::CommitmentFirst, Vec::new(), shutdown_msg));
29692952
}
29702953

29712954
// We have OurFundingLocked set!
29722955
let next_per_commitment_point = self.holder_signer.get_per_commitment_point(self.cur_holder_commitment_transaction_number, &self.secp_ctx);
29732956
return Ok((Some(msgs::FundingLocked {
29742957
channel_id: self.channel_id(),
29752958
next_per_commitment_point,
2976-
}), None, None, None, RAACommitmentOrder::CommitmentFirst, shutdown_msg));
2959+
}), None, None, None, RAACommitmentOrder::CommitmentFirst, Vec::new(), shutdown_msg));
29772960
}
29782961

29792962
let required_revoke = if msg.next_remote_commitment_number + 1 == INITIAL_COMMITMENT_NUMBER - self.cur_holder_commitment_transaction_number {
@@ -3014,14 +2997,6 @@ impl<Signer: Sign> Channel<Signer> {
30142997
}
30152998

30162999
if (self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32 | ChannelState::MonitorUpdateFailed as u32)) == 0 {
3017-
// Note that if in the future we no longer drop holding cell update_adds on peer
3018-
// disconnect, this logic will need to be updated.
3019-
for htlc_update in self.holding_cell_htlc_updates.iter() {
3020-
if let &HTLCUpdateAwaitingACK::AddHTLC { .. } = htlc_update {
3021-
debug_assert!(false, "There shouldn't be any add-HTLCs in the holding cell now because they should have been dropped on peer disconnect. Panic here because said HTLCs won't be handled correctly.");
3022-
}
3023-
}
3024-
30253000
// We're up-to-date and not waiting on a remote revoke (if we are our
30263001
// channel_reestablish should result in them sending a revoke_and_ack), but we may
30273002
// have received some updates while we were disconnected. Free the holding cell
@@ -3030,20 +3005,14 @@ impl<Signer: Sign> Channel<Signer> {
30303005
Err(ChannelError::Close(msg)) => return Err(ChannelError::Close(msg)),
30313006
Err(ChannelError::Ignore(_)) | Err(ChannelError::CloseDelayBroadcast(_)) => panic!("Got non-channel-failing result from free_holding_cell_htlcs"),
30323007
Ok((Some((commitment_update, monitor_update)), htlcs_to_fail)) => {
3033-
// If in the future we no longer drop holding cell update_adds on peer
3034-
// disconnect, we may be handed some HTLCs to fail backwards here.
3035-
assert!(htlcs_to_fail.is_empty());
3036-
return Ok((resend_funding_locked, required_revoke, Some(commitment_update), Some(monitor_update), self.resend_order.clone(), shutdown_msg));
3008+
return Ok((resend_funding_locked, required_revoke, Some(commitment_update), Some(monitor_update), self.resend_order.clone(), htlcs_to_fail, shutdown_msg));
30373009
},
30383010
Ok((None, htlcs_to_fail)) => {
3039-
// If in the future we no longer drop holding cell update_adds on peer
3040-
// disconnect, we may be handed some HTLCs to fail backwards here.
3041-
assert!(htlcs_to_fail.is_empty());
3042-
return Ok((resend_funding_locked, required_revoke, None, None, self.resend_order.clone(), shutdown_msg));
3011+
return Ok((resend_funding_locked, required_revoke, None, None, self.resend_order.clone(), htlcs_to_fail, shutdown_msg));
30433012
},
30443013
}
30453014
} else {
3046-
return Ok((resend_funding_locked, required_revoke, None, None, self.resend_order.clone(), shutdown_msg));
3015+
return Ok((resend_funding_locked, required_revoke, None, None, self.resend_order.clone(), Vec::new(), shutdown_msg));
30473016
}
30483017
} else if msg.next_local_commitment_number == next_counterparty_commitment_number - 1 {
30493018
if required_revoke.is_some() {
@@ -3054,10 +3023,10 @@ impl<Signer: Sign> Channel<Signer> {
30543023

30553024
if self.channel_state & (ChannelState::MonitorUpdateFailed as u32) != 0 {
30563025
self.monitor_pending_commitment_signed = true;
3057-
return Ok((resend_funding_locked, None, None, None, self.resend_order.clone(), shutdown_msg));
3026+
return Ok((resend_funding_locked, None, None, None, self.resend_order.clone(), Vec::new(), shutdown_msg));
30583027
}
30593028

3060-
return Ok((resend_funding_locked, required_revoke, Some(self.get_last_commitment_update(logger)), None, self.resend_order.clone(), shutdown_msg));
3029+
return Ok((resend_funding_locked, required_revoke, Some(self.get_last_commitment_update(logger)), None, self.resend_order.clone(), Vec::new(), shutdown_msg));
30613030
} else {
30623031
return Err(ChannelError::Close("Peer attempted to reestablish channel with a very old remote commitment transaction".to_owned()));
30633032
}
@@ -4312,7 +4281,7 @@ impl Readable for InboundHTLCRemovalReason {
43124281
impl<Signer: Sign> Writeable for Channel<Signer> {
43134282
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
43144283
// Note that we write out as if remove_uncommitted_htlcs_and_mark_paused had just been
4315-
// called but include holding cell updates (and obviously we don't modify self).
4284+
// called.
43164285

43174286
writer.write_all(&[SERIALIZATION_VERSION; 1])?;
43184287
writer.write_all(&[MIN_SERIALIZATION_VERSION; 1])?;

lightning/src/ln/channelmanager.rs

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3103,7 +3103,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
31033103
}
31043104

31053105
fn internal_channel_reestablish(&self, counterparty_node_id: &PublicKey, msg: &msgs::ChannelReestablish) -> Result<(), MsgHandleErrInternal> {
3106-
let chan_restoration_res = {
3106+
let (htlcs_failed_forward, chan_restoration_res) = {
31073107
let mut channel_state_lock = self.channel_state.lock().unwrap();
31083108
let channel_state = &mut *channel_state_lock;
31093109

@@ -3116,20 +3116,21 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
31163116
// disconnect, so Channel's reestablish will never hand us any holding cell
31173117
// freed HTLCs to fail backwards. If in the future we no longer drop pending
31183118
// add-HTLCs on disconnect, we may be handed HTLCs to fail backwards here.
3119-
let (funding_locked, revoke_and_ack, commitment_update, monitor_update_opt, order, shutdown) =
3119+
let (funding_locked, revoke_and_ack, commitment_update, monitor_update_opt, order, htlcs_failed_forward, shutdown) =
31203120
try_chan_entry!(self, chan.get_mut().channel_reestablish(msg, &self.logger), channel_state, chan);
31213121
if let Some(msg) = shutdown {
31223122
channel_state.pending_msg_events.push(events::MessageSendEvent::SendShutdown {
31233123
node_id: counterparty_node_id.clone(),
31243124
msg,
31253125
});
31263126
}
3127-
handle_chan_restoration_locked!(self, channel_state_lock, channel_state, chan, revoke_and_ack, commitment_update, order, monitor_update_opt, Vec::new(), false, funding_locked)
3127+
(htlcs_failed_forward, handle_chan_restoration_locked!(self, channel_state_lock, channel_state, chan, revoke_and_ack, commitment_update, order, monitor_update_opt, Vec::new(), false, funding_locked))
31283128
},
31293129
hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel".to_owned(), msg.channel_id))
31303130
}
31313131
};
31323132
post_handle_chan_restoration!(self, chan_restoration_res);
3133+
self.fail_holding_cell_htlcs(htlcs_failed_forward, msg.channel_id);
31333134
Ok(())
31343135
}
31353136

@@ -3647,7 +3648,6 @@ impl<Signer: Sign, M: Deref + Sync + Send, T: Deref + Sync + Send, K: Deref + Sy
36473648
fn peer_disconnected(&self, counterparty_node_id: &PublicKey, no_connection_possible: bool) {
36483649
let _persistence_guard = PersistenceNotifierGuard::new(&self.total_consistency_lock, &self.persistence_notifier);
36493650
let mut failed_channels = Vec::new();
3650-
let mut failed_payments = Vec::new();
36513651
let mut no_channels_remain = true;
36523652
{
36533653
let mut channel_state_lock = self.channel_state.lock().unwrap();
@@ -3676,16 +3676,8 @@ impl<Signer: Sign, M: Deref + Sync + Send, T: Deref + Sync + Send, K: Deref + Sy
36763676
log_debug!(self.logger, "Marking channels with {} disconnected and generating channel_updates", log_pubkey!(counterparty_node_id));
36773677
channel_state.by_id.retain(|_, chan| {
36783678
if chan.get_counterparty_node_id() == *counterparty_node_id {
3679-
// Note that currently on channel reestablish we assert that there are no
3680-
// holding cell add-HTLCs, so if in the future we stop removing uncommitted HTLCs
3681-
// on peer disconnect here, there will need to be corresponding changes in
3682-
// reestablish logic.
3683-
let failed_adds = chan.remove_uncommitted_htlcs_and_mark_paused(&self.logger);
3679+
chan.remove_uncommitted_htlcs_and_mark_paused(&self.logger);
36843680
chan.to_disabled_marked();
3685-
if !failed_adds.is_empty() {
3686-
let chan_update = self.get_channel_update(&chan).map(|u| u.encode_with_len()).unwrap(); // Cannot add/recv HTLCs before we have a short_id so unwrap is safe
3687-
failed_payments.push((chan_update, failed_adds));
3688-
}
36893681
if chan.is_shutdown() {
36903682
if let Some(short_id) = chan.get_short_channel_id() {
36913683
short_to_id.remove(&short_id);
@@ -3729,11 +3721,6 @@ impl<Signer: Sign, M: Deref + Sync + Send, T: Deref + Sync + Send, K: Deref + Sy
37293721
for failure in failed_channels.drain(..) {
37303722
self.finish_force_close_channel(failure);
37313723
}
3732-
for (chan_update, mut htlc_sources) in failed_payments {
3733-
for (htlc_source, payment_hash) in htlc_sources.drain(..) {
3734-
self.fail_htlc_backwards_internal(self.channel_state.lock().unwrap(), htlc_source, &payment_hash, HTLCFailReason::Reason { failure_code: 0x1000 | 7, data: chan_update.clone() });
3735-
}
3736-
}
37373724
}
37383725

37393726
fn peer_connected(&self, counterparty_node_id: &PublicKey, init_msg: &msgs::Init) {

0 commit comments

Comments
 (0)