Skip to content

Commit a8722d1

Browse files
Deduplicate PendingHTLCsForwardable events when generating
1 parent 30b9d9f commit a8722d1

File tree

3 files changed

+33
-35
lines changed

3 files changed

+33
-35
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3745,16 +3745,19 @@ where
37453745
// being fully configured. See the docs for `ChannelManagerReadArgs` for more.
37463746
match source {
37473747
HTLCSource::OutboundRoute { ref path, ref session_priv, ref payment_id, ref payment_params, .. } => {
3748-
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);
3748+
if self.pending_outbound_payments.fail_htlc(source, payment_hash, onion_error, path,
3749+
session_priv, payment_id, payment_params, self.probing_cookie_secret, &self.secp_ctx,
3750+
&self.pending_events, &self.logger)
3751+
{ self.push_pending_forwards_ev(); }
37493752
},
37503753
HTLCSource::PreviousHopData(HTLCPreviousHopData { ref short_channel_id, ref htlc_id, ref incoming_packet_shared_secret, ref phantom_shared_secret, ref outpoint }) => {
37513754
log_trace!(self.logger, "Failing HTLC with payment_hash {} backwards from us with {:?}", log_bytes!(payment_hash.0), onion_error);
37523755
let err_packet = onion_error.get_encrypted_failure_packet(incoming_packet_shared_secret, phantom_shared_secret);
37533756

3754-
let mut forward_event = None;
3757+
let mut push_forward_ev = false;
37553758
let mut forward_htlcs = self.forward_htlcs.lock().unwrap();
37563759
if forward_htlcs.is_empty() {
3757-
forward_event = Some(Duration::from_millis(MIN_HTLC_RELAY_HOLDING_CELL_MILLIS));
3760+
push_forward_ev = true;
37583761
}
37593762
match forward_htlcs.entry(*short_channel_id) {
37603763
hash_map::Entry::Occupied(mut entry) => {
@@ -3765,12 +3768,8 @@ where
37653768
}
37663769
}
37673770
mem::drop(forward_htlcs);
3771+
if push_forward_ev { self.push_pending_forwards_ev(); }
37683772
let mut pending_events = self.pending_events.lock().unwrap();
3769-
if let Some(time) = forward_event {
3770-
pending_events.push(events::Event::PendingHTLCsForwardable {
3771-
time_forwardable: time
3772-
});
3773-
}
37743773
pending_events.push(events::Event::HTLCHandlingFailed {
37753774
prev_channel_id: outpoint.to_channel_id(),
37763775
failed_next_destination: destination,
@@ -4847,7 +4846,7 @@ where
48474846
#[inline]
48484847
fn forward_htlcs(&self, per_source_pending_forwards: &mut [(u64, OutPoint, u128, Vec<(PendingHTLCInfo, u64)>)]) {
48494848
for &mut (prev_short_channel_id, prev_funding_outpoint, prev_user_channel_id, ref mut pending_forwards) in per_source_pending_forwards {
4850-
let mut forward_event = None;
4849+
let mut push_forward_event = false;
48514850
let mut new_intercept_events = Vec::new();
48524851
let mut failed_intercept_forwards = Vec::new();
48534852
if !pending_forwards.is_empty() {
@@ -4905,7 +4904,7 @@ where
49054904
// We don't want to generate a PendingHTLCsForwardable event if only intercepted
49064905
// payments are being processed.
49074906
if forward_htlcs_empty {
4908-
forward_event = Some(Duration::from_millis(MIN_HTLC_RELAY_HOLDING_CELL_MILLIS));
4907+
push_forward_event = true;
49094908
}
49104909
entry.insert(vec!(HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
49114910
prev_short_channel_id, prev_funding_outpoint, prev_htlc_id, prev_user_channel_id, forward_info })));
@@ -4923,16 +4922,21 @@ where
49234922
let mut events = self.pending_events.lock().unwrap();
49244923
events.append(&mut new_intercept_events);
49254924
}
4925+
if push_forward_event { self.push_pending_forwards_ev() }
4926+
}
4927+
}
49264928

4927-
match forward_event {
4928-
Some(time) => {
4929-
let mut pending_events = self.pending_events.lock().unwrap();
4930-
pending_events.push(events::Event::PendingHTLCsForwardable {
4931-
time_forwardable: time
4932-
});
4933-
}
4934-
None => {},
4935-
}
4929+
// We only want to push a PendingHTLCsForwardable event if no others are queued.
4930+
fn push_pending_forwards_ev(&self) {
4931+
let mut pending_events = self.pending_events.lock().unwrap();
4932+
let forward_ev_exists = pending_events.iter()
4933+
.find(|ev| if let events::Event::PendingHTLCsForwardable { .. } = ev { true } else { false })
4934+
.is_some();
4935+
if !forward_ev_exists {
4936+
pending_events.push(events::Event::PendingHTLCsForwardable {
4937+
time_forwardable:
4938+
Duration::from_millis(MIN_HTLC_RELAY_HOLDING_CELL_MILLIS),
4939+
});
49364940
}
49374941
}
49384942

lightning/src/ln/outbound_payment.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use bitcoin::secp256k1::{self, Secp256k1, SecretKey};
1515

1616
use crate::chain::keysinterface::{EntropySource, NodeSigner, Recipient};
1717
use crate::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
18-
use crate::ln::channelmanager::{ChannelDetails, HTLCSource, IDEMPOTENCY_TIMEOUT_TICKS, MIN_HTLC_RELAY_HOLDING_CELL_MILLIS, PaymentId};
18+
use crate::ln::channelmanager::{ChannelDetails, HTLCSource, IDEMPOTENCY_TIMEOUT_TICKS, PaymentId};
1919
use crate::ln::channelmanager::MIN_FINAL_CLTV_EXPIRY_DELTA as LDK_DEFAULT_MIN_FINAL_CLTV_EXPIRY_DELTA;
2020
use crate::ln::msgs::DecodeError;
2121
use crate::ln::onion_utils::HTLCFailReason;
@@ -30,7 +30,6 @@ use crate::util::time::tests::SinceEpoch;
3030
use core::cmp;
3131
use core::fmt::{self, Display, Formatter};
3232
use core::ops::Deref;
33-
use core::time::Duration;
3433

3534
use crate::prelude::*;
3635
use crate::sync::Mutex;
@@ -1003,12 +1002,13 @@ impl OutboundPayments {
10031002
});
10041003
}
10051004

1005+
// Returns a bool indicating whether a PendingHTLCsForwardable event should be generated.
10061006
pub(super) fn fail_htlc<L: Deref>(
10071007
&self, source: &HTLCSource, payment_hash: &PaymentHash, onion_error: &HTLCFailReason,
10081008
path: &Vec<RouteHop>, session_priv: &SecretKey, payment_id: &PaymentId,
10091009
payment_params: &Option<PaymentParameters>, probing_cookie_secret: [u8; 32],
10101010
secp_ctx: &Secp256k1<secp256k1::All>, pending_events: &Mutex<Vec<events::Event>>, logger: &L
1011-
) where L::Target: Logger {
1011+
) -> bool where L::Target: Logger {
10121012
#[cfg(test)]
10131013
let (network_update, short_channel_id, payment_retryable, onion_error_code, onion_error_data) = onion_error.decode_onion_failure(secp_ctx, logger, &source);
10141014
#[cfg(not(test))]
@@ -1020,16 +1020,16 @@ impl OutboundPayments {
10201020
let mut outbounds = self.pending_outbound_payments.lock().unwrap();
10211021
let mut all_paths_failed = false;
10221022
let mut full_failure_ev = None;
1023-
let mut pending_retry_ev = None;
1023+
let mut pending_retry_ev = false;
10241024
let mut retry = None;
10251025
let attempts_remaining = if let hash_map::Entry::Occupied(mut payment) = outbounds.entry(*payment_id) {
10261026
if !payment.get_mut().remove(&session_priv_bytes, Some(&path)) {
10271027
log_trace!(logger, "Received duplicative fail for HTLC with payment_hash {}", log_bytes!(payment_hash.0));
1028-
return
1028+
return false
10291029
}
10301030
if payment.get().is_fulfilled() {
10311031
log_trace!(logger, "Received failure of HTLC with payment_hash {} after payment completion", log_bytes!(payment_hash.0));
1032-
return
1032+
return false
10331033
}
10341034
let mut is_retryable_now = payment.get().is_auto_retryable_now();
10351035
if let Some(scid) = short_channel_id {
@@ -1079,7 +1079,7 @@ impl OutboundPayments {
10791079
is_retryable_now
10801080
} else {
10811081
log_trace!(logger, "Received duplicative fail for HTLC with payment_hash {}", log_bytes!(payment_hash.0));
1082-
return
1082+
return false
10831083
};
10841084
core::mem::drop(outbounds);
10851085
log_trace!(logger, "Failing outbound payment HTLC with payment_hash {}", log_bytes!(payment_hash.0));
@@ -1111,9 +1111,7 @@ impl OutboundPayments {
11111111
// payment will sit in our outbounds forever.
11121112
if attempts_remaining {
11131113
debug_assert!(full_failure_ev.is_none());
1114-
pending_retry_ev = Some(events::Event::PendingHTLCsForwardable {
1115-
time_forwardable: Duration::from_millis(MIN_HTLC_RELAY_HOLDING_CELL_MILLIS),
1116-
});
1114+
pending_retry_ev = true;
11171115
}
11181116
events::Event::PaymentPathFailed {
11191117
payment_id: Some(*payment_id),
@@ -1134,7 +1132,7 @@ impl OutboundPayments {
11341132
let mut pending_events = pending_events.lock().unwrap();
11351133
pending_events.push(path_failure);
11361134
if let Some(ev) = full_failure_ev { pending_events.push(ev); }
1137-
if let Some(ev) = pending_retry_ev { pending_events.push(ev); }
1135+
pending_retry_ev
11381136
}
11391137

11401138
pub(super) fn abandon_payment(

lightning/src/ln/payment_tests.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2345,7 +2345,7 @@ fn no_extra_retries_on_back_to_back_fail() {
23452345
// Because we now retry payments as a batch, we simply return a single-path route in the
23462346
// second, batched, request, have that fail, ensure the payment was abandoned.
23472347
let mut events = nodes[0].node.get_and_clear_pending_events();
2348-
assert_eq!(events.len(), 4);
2348+
assert_eq!(events.len(), 3);
23492349
match events[0] {
23502350
Event::PaymentPathFailed { payment_hash: ev_payment_hash, payment_failed_permanently, .. } => {
23512351
assert_eq!(payment_hash, ev_payment_hash);
@@ -2364,10 +2364,6 @@ fn no_extra_retries_on_back_to_back_fail() {
23642364
},
23652365
_ => panic!("Unexpected event"),
23662366
}
2367-
match events[3] {
2368-
Event::PendingHTLCsForwardable { .. } => {},
2369-
_ => panic!("Unexpected event"),
2370-
}
23712367

23722368
nodes[0].node.process_pending_htlc_forwards();
23732369
let retry_htlc_updates = SendEvent::from_node(&nodes[0]);

0 commit comments

Comments
 (0)