-
Notifications
You must be signed in to change notification settings - Fork 407
Deduplicate PendingHTLCsForwardable
events on generation
#2026
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
TheBlueMatt
merged 3 commits into
lightningdevkit:main
from
valentinewallace:2023-02-dedup-pending-forwardable-evs
Feb 19, 2023
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3737,16 +3737,19 @@ where | |
// being fully configured. See the docs for `ChannelManagerReadArgs` for more. | ||
match source { | ||
HTLCSource::OutboundRoute { ref path, ref session_priv, ref payment_id, ref payment_params, .. } => { | ||
self.pending_outbound_payments.fail_htlc(source, payment_hash, onion_error, path, session_priv, payment_id, payment_params, self.probing_cookie_secret, &self.secp_ctx, &self.pending_events, &self.logger); | ||
if self.pending_outbound_payments.fail_htlc(source, payment_hash, onion_error, path, | ||
session_priv, payment_id, payment_params, self.probing_cookie_secret, &self.secp_ctx, | ||
&self.pending_events, &self.logger) | ||
{ self.push_pending_forwards_ev(); } | ||
}, | ||
HTLCSource::PreviousHopData(HTLCPreviousHopData { ref short_channel_id, ref htlc_id, ref incoming_packet_shared_secret, ref phantom_shared_secret, ref outpoint }) => { | ||
log_trace!(self.logger, "Failing HTLC with payment_hash {} backwards from us with {:?}", log_bytes!(payment_hash.0), onion_error); | ||
let err_packet = onion_error.get_encrypted_failure_packet(incoming_packet_shared_secret, phantom_shared_secret); | ||
|
||
let mut forward_event = None; | ||
let mut push_forward_ev = false; | ||
let mut forward_htlcs = self.forward_htlcs.lock().unwrap(); | ||
if forward_htlcs.is_empty() { | ||
forward_event = Some(Duration::from_millis(MIN_HTLC_RELAY_HOLDING_CELL_MILLIS)); | ||
push_forward_ev = true; | ||
} | ||
match forward_htlcs.entry(*short_channel_id) { | ||
hash_map::Entry::Occupied(mut entry) => { | ||
|
@@ -3757,12 +3760,8 @@ where | |
} | ||
} | ||
mem::drop(forward_htlcs); | ||
if push_forward_ev { self.push_pending_forwards_ev(); } | ||
let mut pending_events = self.pending_events.lock().unwrap(); | ||
if let Some(time) = forward_event { | ||
pending_events.push(events::Event::PendingHTLCsForwardable { | ||
time_forwardable: time | ||
}); | ||
} | ||
pending_events.push(events::Event::HTLCHandlingFailed { | ||
prev_channel_id: outpoint.to_channel_id(), | ||
failed_next_destination: destination, | ||
|
@@ -4839,7 +4838,7 @@ where | |
#[inline] | ||
fn forward_htlcs(&self, per_source_pending_forwards: &mut [(u64, OutPoint, u128, Vec<(PendingHTLCInfo, u64)>)]) { | ||
for &mut (prev_short_channel_id, prev_funding_outpoint, prev_user_channel_id, ref mut pending_forwards) in per_source_pending_forwards { | ||
let mut forward_event = None; | ||
let mut push_forward_event = false; | ||
let mut new_intercept_events = Vec::new(); | ||
let mut failed_intercept_forwards = Vec::new(); | ||
if !pending_forwards.is_empty() { | ||
|
@@ -4897,7 +4896,7 @@ where | |
// We don't want to generate a PendingHTLCsForwardable event if only intercepted | ||
// payments are being processed. | ||
if forward_htlcs_empty { | ||
forward_event = Some(Duration::from_millis(MIN_HTLC_RELAY_HOLDING_CELL_MILLIS)); | ||
push_forward_event = true; | ||
} | ||
entry.insert(vec!(HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo { | ||
prev_short_channel_id, prev_funding_outpoint, prev_htlc_id, prev_user_channel_id, forward_info }))); | ||
|
@@ -4915,16 +4914,21 @@ where | |
let mut events = self.pending_events.lock().unwrap(); | ||
events.append(&mut new_intercept_events); | ||
} | ||
if push_forward_event { self.push_pending_forwards_ev() } | ||
} | ||
} | ||
|
||
match forward_event { | ||
Some(time) => { | ||
let mut pending_events = self.pending_events.lock().unwrap(); | ||
pending_events.push(events::Event::PendingHTLCsForwardable { | ||
time_forwardable: time | ||
}); | ||
} | ||
None => {}, | ||
} | ||
// We only want to push a PendingHTLCsForwardable event if no others are queued. | ||
fn push_pending_forwards_ev(&self) { | ||
let mut pending_events = self.pending_events.lock().unwrap(); | ||
let forward_ev_exists = pending_events.iter() | ||
.find(|ev| if let events::Event::PendingHTLCsForwardable { .. } = ev { true } else { false }) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lol if our MSRV was just one minor version up (1.42) we could just .find(|ev| matches!(ev, events::Event::PendingHTLCsForwardable)) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Soclose 🥲 |
||
.is_some(); | ||
if !forward_ev_exists { | ||
pending_events.push(events::Event::PendingHTLCsForwardable { | ||
time_forwardable: | ||
Duration::from_millis(MIN_HTLC_RELAY_HOLDING_CELL_MILLIS), | ||
}); | ||
} | ||
} | ||
|
||
|
@@ -7526,7 +7530,8 @@ where | |
} | ||
} | ||
|
||
if !forward_htlcs.is_empty() { | ||
let pending_outbounds = OutboundPayments { pending_outbound_payments: Mutex::new(pending_outbound_payments.unwrap()), retry_lock: Mutex::new(()) }; | ||
if !forward_htlcs.is_empty() || pending_outbounds.needs_abandon() { | ||
// If we have pending HTLCs to forward, assume we either dropped a | ||
// `PendingHTLCsForwardable` or the user received it but never processed it as they | ||
// shut down before the timer hit. Either way, set the time_forwardable to a small | ||
|
@@ -7694,7 +7699,7 @@ where | |
|
||
inbound_payment_key: expanded_inbound_key, | ||
pending_inbound_payments: Mutex::new(pending_inbound_payments), | ||
pending_outbound_payments: OutboundPayments { pending_outbound_payments: Mutex::new(pending_outbound_payments.unwrap()), retry_lock: Mutex::new(()), }, | ||
pending_outbound_payments: pending_outbounds, | ||
pending_intercepted_htlcs: Mutex::new(pending_intercepted_htlcs.unwrap()), | ||
|
||
forward_htlcs: Mutex::new(forward_htlcs), | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, oops, this check isn't sufficient - I mean it reduces duplication, but the issue is we send the user a
PendingHTLCsForwardable
event, then they wait (during which time we don't have one lying around here) and then they callpending_htlcs_forwardable
. Avoiding duplicating entirely is gonna be a bit hard, but I think we could at least only have two in-flight ones at once by filtering theoutbound_payment
-generated one onneeds_abandon
(but under the same lock as the update in fail_htlc).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok I see the case we're not covering. I don't think we'd want
needs_abandon
, though, seems we'd want a newneeds_retry
check IIUC. Will add this. (Note thatneeds_abandon
should only apply to restart since we set all payments as non-retryable on deser.)