Skip to content

Commit 9ba691c

Browse files
committed
WIP Drop PendingHTLCsForwardable event
1 parent 08550a2 commit 9ba691c

13 files changed

+63
-280
lines changed

lightning/src/events/mod.rs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ use bitcoin::script::ScriptBuf;
5050
use bitcoin::secp256k1::PublicKey;
5151
use bitcoin::{OutPoint, Transaction};
5252
use core::ops::Deref;
53-
use core::time::Duration;
5453

5554
#[allow(unused_imports)]
5655
use crate::prelude::*;
@@ -1174,21 +1173,6 @@ pub enum Event {
11741173
/// with channels in the public network graph.
11751174
short_channel_id: Option<u64>,
11761175
},
1177-
/// Used to indicate that [`ChannelManager::process_pending_htlc_forwards`] should be called at
1178-
/// a time in the future.
1179-
///
1180-
/// # Failure Behavior and Persistence
1181-
/// This event will eventually be replayed after failures-to-handle (i.e., the event handler
1182-
/// returning `Err(ReplayEvent ())`) and will be regenerated after restarts.
1183-
///
1184-
/// [`ChannelManager::process_pending_htlc_forwards`]: crate::ln::channelmanager::ChannelManager::process_pending_htlc_forwards
1185-
PendingHTLCsForwardable {
1186-
/// The minimum amount of time that should be waited prior to calling
1187-
/// process_pending_htlc_forwards. To increase the effort required to correlate payments,
1188-
/// you should wait a random amount of time in roughly the range (now + time_forwardable,
1189-
/// now + 5*time_forwardable).
1190-
time_forwardable: Duration,
1191-
},
11921176
/// Used to indicate that we've intercepted an HTLC forward. This event will only be generated if
11931177
/// you've encoded an intercept scid in the receiver's invoice route hints using
11941178
/// [`ChannelManager::get_intercept_scid`] and have set [`UserConfig::accept_intercept_htlcs`].
@@ -1721,11 +1705,7 @@ impl Writeable for Event {
17211705
(13, failure, required),
17221706
});
17231707
},
1724-
&Event::PendingHTLCsForwardable { time_forwardable: _ } => {
1725-
4u8.write(writer)?;
1726-
// Note that we now ignore these on the read end as we'll re-generate them in
1727-
// ChannelManager, we write them here only for backwards compatibility.
1728-
},
1708+
// 4u8 used to be `PendingHTLCsForwardable`
17291709
&Event::SpendableOutputs { ref outputs, channel_id } => {
17301710
5u8.write(writer)?;
17311711
write_tlv_fields!(writer, {

lightning/src/ln/blinded_payment_tests.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -636,10 +636,6 @@ fn do_forward_fail_in_process_pending_htlc_fwds(check: ProcessPendingHTLCsCheck,
636636
$curr_node.node.force_close_broadcasting_latest_txn(&$failed_chan_id, &$next_node.node.get_our_node_id(), error_message.to_string()).unwrap();
637637
let events = $curr_node.node.get_and_clear_pending_events();
638638
match events[0] {
639-
crate::events::Event::PendingHTLCsForwardable { .. } => {},
640-
_ => panic!("Unexpected event {:?}", events),
641-
};
642-
match events[1] {
643639
crate::events::Event::ChannelClosed { .. } => {},
644640
_ => panic!("Unexpected event {:?}", events),
645641
}
@@ -1162,18 +1158,14 @@ fn blinded_path_retries() {
11621158
do_commitment_signed_dance(&nodes[0], &$intro_node, &updates.commitment_signed, false, false);
11631159

11641160
let mut events = nodes[0].node.get_and_clear_pending_events();
1165-
assert_eq!(events.len(), 2);
1161+
assert_eq!(events.len(), 1);
11661162
match events[0] {
11671163
Event::PaymentPathFailed { payment_hash: ev_payment_hash, payment_failed_permanently, .. } => {
11681164
assert_eq!(payment_hash, ev_payment_hash);
11691165
assert_eq!(payment_failed_permanently, false);
11701166
},
11711167
_ => panic!("Unexpected event"),
11721168
}
1173-
match events[1] {
1174-
Event::PendingHTLCsForwardable { .. } => {},
1175-
_ => panic!("Unexpected event"),
1176-
}
11771169
nodes[0].node.process_pending_htlc_forwards();
11781170
}
11791171
}

lightning/src/ln/chanmon_update_fail_tests.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2011,23 +2011,19 @@ fn test_monitor_update_on_pending_forwards() {
20112011
commitment_signed_dance!(nodes[0], nodes[1], bs_updates.commitment_signed, false, true);
20122012

20132013
let events = nodes[0].node.get_and_clear_pending_events();
2014-
assert_eq!(events.len(), 3);
2015-
if let Event::PaymentPathFailed { payment_hash, payment_failed_permanently, .. } = events[1] {
2014+
assert_eq!(events.len(), 2);
2015+
if let Event::PaymentPathFailed { payment_hash, payment_failed_permanently, .. } = events[0] {
20162016
assert_eq!(payment_hash, payment_hash_1);
20172017
assert!(payment_failed_permanently);
20182018
} else {
20192019
panic!("Unexpected event!");
20202020
}
2021-
match events[2] {
2021+
match events[1] {
20222022
Event::PaymentFailed { payment_hash, .. } => {
20232023
assert_eq!(payment_hash, Some(payment_hash_1));
20242024
},
20252025
_ => panic!("Unexpected event"),
20262026
}
2027-
match events[0] {
2028-
Event::PendingHTLCsForwardable { .. } => {},
2029-
_ => panic!("Unexpected event"),
2030-
};
20312027
nodes[0].node.process_pending_htlc_forwards();
20322028
expect_payment_claimable!(nodes[0], payment_hash_2, payment_secret_2, 1000000);
20332029

@@ -2810,12 +2806,8 @@ fn do_channel_holding_cell_serialize(disconnect: bool, reload_a: bool) {
28102806
commitment_signed_dance!(nodes[1], nodes[0], (), false, true, false, false);
28112807

28122808
let events = nodes[1].node.get_and_clear_pending_events();
2813-
assert_eq!(events.len(), 2);
2809+
assert_eq!(events.len(), 1);
28142810
match events[0] {
2815-
Event::PendingHTLCsForwardable { .. } => {},
2816-
_ => panic!("Unexpected event"),
2817-
};
2818-
match events[1] {
28192811
Event::PaymentPathSuccessful { .. } => {},
28202812
_ => panic!("Unexpected event"),
28212813
};

lightning/src/ln/channelmanager.rs

Lines changed: 7 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -960,12 +960,6 @@ impl MsgHandleErrInternal {
960960
}
961961
}
962962

963-
/// We hold back HTLCs we intend to relay for a random interval greater than this (see
964-
/// Event::PendingHTLCsForwardable for the API guidelines indicating how long should be waited).
965-
/// This provides some limited amount of privacy. Ideally this would range from somewhere like one
966-
/// second to 30 seconds, but people expect lightning to be, you know, kinda fast, sadly.
967-
pub(super) const MIN_HTLC_RELAY_HOLDING_CELL_MILLIS: u64 = 100;
968-
969963
/// For events which result in both a RevokeAndACK and a CommitmentUpdate, by default they should
970964
/// be sent in the order they appear in the return value, however sometimes the order needs to be
971965
/// variable at runtime (eg FundedChannel::channel_reestablish needs to re-send messages in the order
@@ -6315,8 +6309,7 @@ where
63156309

63166310
/// Processes HTLCs which are pending waiting on random forward delay.
63176311
///
6318-
/// Should only really ever be called in response to a PendingHTLCsForwardable event.
6319-
/// Will likely generate further events.
6312+
/// Will regularly be called by the background processor.
63206313
pub fn process_pending_htlc_forwards(&self) {
63216314
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
63226315

@@ -7721,23 +7714,20 @@ where
77217714
&self, source: &HTLCSource, payment_hash: &PaymentHash, onion_error: &HTLCFailReason,
77227715
destination: HTLCHandlingFailureType,
77237716
) {
7724-
let push_forward_event = self.fail_htlc_backwards_internal_without_forward_event(
7717+
self.fail_htlc_backwards_internal_without_forward_event(
77257718
source,
77267719
payment_hash,
77277720
onion_error,
77287721
destination,
77297722
);
7730-
if push_forward_event {
7731-
self.push_pending_forwards_ev();
7732-
}
77337723
}
77347724

77357725
/// Fails an HTLC backwards to the sender of it to us.
77367726
/// Note that we do not assume that channels corresponding to failed HTLCs are still available.
77377727
fn fail_htlc_backwards_internal_without_forward_event(
77387728
&self, source: &HTLCSource, payment_hash: &PaymentHash, onion_error: &HTLCFailReason,
77397729
failure_type: HTLCHandlingFailureType,
7740-
) -> bool {
7730+
) {
77417731
// Ensure that no peer state channel storage lock is held when calling this function.
77427732
// This ensures that future code doesn't introduce a lock-order requirement for
77437733
// `forward_htlcs` to be locked after the `per_peer_state` peer locks, which calling
@@ -7755,10 +7745,9 @@ where
77557745
// Note that we MUST NOT end up calling methods on self.chain_monitor here - we're called
77567746
// from block_connected which may run during initialization prior to the chain_monitor
77577747
// being fully configured. See the docs for `ChannelManagerReadArgs` for more.
7758-
let mut push_forward_event;
77597748
match source {
77607749
HTLCSource::OutboundRoute { ref path, ref session_priv, ref payment_id, .. } => {
7761-
push_forward_event = self.pending_outbound_payments.fail_htlc(
7750+
self.pending_outbound_payments.fail_htlc(
77627751
source,
77637752
payment_hash,
77647753
onion_error,
@@ -7814,9 +7803,7 @@ where
78147803
},
78157804
};
78167805

7817-
push_forward_event = self.decode_update_add_htlcs.lock().unwrap().is_empty();
78187806
let mut forward_htlcs = self.forward_htlcs.lock().unwrap();
7819-
push_forward_event &= forward_htlcs.is_empty();
78207807
match forward_htlcs.entry(*short_channel_id) {
78217808
hash_map::Entry::Occupied(mut entry) => {
78227809
entry.get_mut().push(failure);
@@ -7837,7 +7824,6 @@ where
78377824
));
78387825
},
78397826
}
7840-
push_forward_event
78417827
}
78427828

78437829
/// Provides a payment preimage in response to [`Event::PaymentClaimable`], generating any
@@ -9978,9 +9964,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
99789964
}
99799965

99809966
fn push_decode_update_add_htlcs(&self, mut update_add_htlcs: (u64, Vec<msgs::UpdateAddHTLC>)) {
9981-
let mut push_forward_event = self.forward_htlcs.lock().unwrap().is_empty();
99829967
let mut decode_update_add_htlcs = self.decode_update_add_htlcs.lock().unwrap();
9983-
push_forward_event &= decode_update_add_htlcs.is_empty();
99849968
let scid = update_add_htlcs.0;
99859969
match decode_update_add_htlcs.entry(scid) {
99869970
hash_map::Entry::Occupied(mut e) => {
@@ -9990,25 +9974,17 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
99909974
e.insert(update_add_htlcs.1);
99919975
},
99929976
}
9993-
if push_forward_event {
9994-
self.push_pending_forwards_ev();
9995-
}
99969977
}
99979978

99989979
#[inline]
99999980
fn forward_htlcs(&self, per_source_pending_forwards: &mut [PerSourcePendingForward]) {
10000-
let push_forward_event =
10001-
self.forward_htlcs_without_forward_event(per_source_pending_forwards);
10002-
if push_forward_event {
10003-
self.push_pending_forwards_ev()
10004-
}
9981+
self.forward_htlcs_without_forward_event(per_source_pending_forwards);
100059982
}
100069983

100079984
#[inline]
100089985
fn forward_htlcs_without_forward_event(
100099986
&self, per_source_pending_forwards: &mut [PerSourcePendingForward],
10010-
) -> bool {
10011-
let mut push_forward_event = false;
9987+
) {
100129988
for &mut (
100139989
prev_short_channel_id,
100149990
prev_counterparty_node_id,
@@ -10031,10 +10007,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1003110007
// Pull this now to avoid introducing a lock order with `forward_htlcs`.
1003210008
let is_our_scid = self.short_to_chan_info.read().unwrap().contains_key(&scid);
1003310009

10034-
let decode_update_add_htlcs_empty =
10035-
self.decode_update_add_htlcs.lock().unwrap().is_empty();
1003610010
let mut forward_htlcs = self.forward_htlcs.lock().unwrap();
10037-
let forward_htlcs_empty = forward_htlcs.is_empty();
1003810011
match forward_htlcs.entry(scid) {
1003910012
hash_map::Entry::Occupied(mut entry) => {
1004010013
entry.get_mut().push(HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
@@ -10132,8 +10105,6 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1013210105
} else {
1013310106
// We don't want to generate a PendingHTLCsForwardable event if only intercepted
1013410107
// payments are being processed.
10135-
push_forward_event |=
10136-
forward_htlcs_empty && decode_update_add_htlcs_empty;
1013710108
entry.insert(vec![HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
1013810109
prev_short_channel_id,
1013910110
prev_counterparty_node_id,
@@ -10152,7 +10123,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1015210123
for (htlc_source, payment_hash, failure_reason, destination) in
1015310124
failed_intercept_forwards.drain(..)
1015410125
{
10155-
push_forward_event |= self.fail_htlc_backwards_internal_without_forward_event(
10126+
self.fail_htlc_backwards_internal_without_forward_event(
1015610127
&htlc_source,
1015710128
&payment_hash,
1015810129
&failure_reason,
@@ -10165,30 +10136,6 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1016510136
events.append(&mut new_intercept_events);
1016610137
}
1016710138
}
10168-
push_forward_event
10169-
}
10170-
10171-
fn push_pending_forwards_ev(&self) {
10172-
let mut pending_events = self.pending_events.lock().unwrap();
10173-
let is_processing_events = self.pending_events_processor.load(Ordering::Acquire);
10174-
let num_forward_events = pending_events
10175-
.iter()
10176-
.filter(|(ev, _)| matches!(ev, events::Event::PendingHTLCsForwardable { .. }))
10177-
.count();
10178-
// We only want to push a PendingHTLCsForwardable event if no others are queued. Processing
10179-
// events is done in batches and they are not removed until we're done processing each
10180-
// batch. Since handling a `PendingHTLCsForwardable` event will call back into the
10181-
// `ChannelManager`, we'll still see the original forwarding event not removed. Phantom
10182-
// payments will need an additional forwarding event before being claimed to make them look
10183-
// real by taking more time.
10184-
if (is_processing_events && num_forward_events <= 1) || num_forward_events < 1 {
10185-
pending_events.push_back((
10186-
Event::PendingHTLCsForwardable {
10187-
time_forwardable: Duration::from_millis(MIN_HTLC_RELAY_HOLDING_CELL_MILLIS),
10188-
},
10189-
None,
10190-
));
10191-
}
1019210139
}
1019310140

1019410141
/// Checks whether [`ChannelMonitorUpdate`]s generated by the receipt of a remote
@@ -15951,21 +15898,6 @@ where
1595115898
}
1595215899
}
1595315900

15954-
if !forward_htlcs.is_empty()
15955-
|| !decode_update_add_htlcs.is_empty()
15956-
|| pending_outbounds.needs_abandon()
15957-
{
15958-
// If we have pending HTLCs to forward, assume we either dropped a
15959-
// `PendingHTLCsForwardable` or the user received it but never processed it as they
15960-
// shut down before the timer hit. Either way, set the time_forwardable to a small
15961-
// constant as enough time has likely passed that we should simply handle the forwards
15962-
// now, or at least after the user gets a chance to reconnect to our peers.
15963-
pending_events_read.push_back((
15964-
events::Event::PendingHTLCsForwardable { time_forwardable: Duration::from_secs(2) },
15965-
None,
15966-
));
15967-
}
15968-
1596915901
let expanded_inbound_key = args.node_signer.get_inbound_payment_key();
1597015902

1597115903
let mut claimable_payments = hash_map_with_capacity(claimable_htlcs_list.len());

lightning/src/ln/functional_test_utils.rs

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1983,7 +1983,6 @@ macro_rules! expect_htlc_handling_failed_destinations {
19831983
let mut num_expected_failures = $expected_failures.len();
19841984
for event in $events {
19851985
match event {
1986-
$crate::events::Event::PendingHTLCsForwardable { .. } => { },
19871986
$crate::events::Event::HTLCHandlingFailed { ref failure_type, .. } => {
19881987
assert!($expected_failures.contains(&failure_type));
19891988
num_expected_failures -= 1;
@@ -1999,9 +1998,7 @@ macro_rules! expect_htlc_handling_failed_destinations {
19991998
/// there are any [`Event::HTLCHandlingFailed`] events their [`HTLCHandlingFailureType`] is included in the
20001999
/// `expected_failures` set.
20012000
pub fn expect_pending_htlcs_forwardable_conditions(events: Vec<Event>, expected_failures: &[HTLCHandlingFailureType]) {
2002-
let count = expected_failures.len() + 1;
2003-
assert_eq!(events.len(), count);
2004-
assert!(events.iter().find(|event| matches!(event, Event::PendingHTLCsForwardable { .. })).is_some());
2001+
assert_eq!(events.len(), expected_failures.len());
20052002
if expected_failures.len() > 0 {
20062003
expect_htlc_handling_failed_destinations!(events, expected_failures)
20072004
}
@@ -2052,23 +2049,6 @@ macro_rules! expect_pending_htlcs_forwardable_and_htlc_handling_failed {
20522049
}}
20532050
}
20542051

2055-
#[cfg(any(test, feature = "_externalize_tests"))]
2056-
macro_rules! expect_pending_htlcs_forwardable_from_events {
2057-
($node: expr, $events: expr, $ignore: expr) => {{
2058-
assert_eq!($events.len(), 1);
2059-
match $events[0] {
2060-
Event::PendingHTLCsForwardable { .. } => { },
2061-
_ => panic!("Unexpected event"),
2062-
};
2063-
if $ignore {
2064-
$node.node.process_pending_htlc_forwards();
2065-
2066-
// Ensure process_pending_htlc_forwards is idempotent.
2067-
$node.node.process_pending_htlc_forwards();
2068-
}
2069-
}}
2070-
}
2071-
20722052
#[macro_export]
20732053
/// Performs the "commitment signed dance" - the series of message exchanges which occur after a
20742054
/// commitment update.
@@ -2645,11 +2625,6 @@ pub fn expect_payment_failed_conditions_event<'a, 'b, 'c, 'd, 'e>(
26452625
}
26462626
_ => panic!("Unexpected second event"),
26472627
}
2648-
} else if conditions.retry_expected {
2649-
match &payment_failed_events[1] {
2650-
Event::PendingHTLCsForwardable { .. } => {},
2651-
_ => panic!("Unexpected second event"),
2652-
}
26532628
}
26542629
}
26552630

@@ -2830,11 +2805,7 @@ pub fn do_pass_along_path<'a, 'b, 'c>(args: PassAlongPathArgs) -> Option<Event>
28302805
}
28312806
event = Some(events_2[0].clone());
28322807
} else if let Some(ref failure) = expected_failure {
2833-
// If we successfully decode the HTLC onion but then fail later in
2834-
// process_pending_htlc_forwards, then we'll queue the failure and generate a new
2835-
// `ProcessPendingHTLCForwards` event. If we fail during the process of decoding the HTLC,
2836-
// we'll fail it immediately with no intermediate forwarding event.
2837-
assert!(events_2.len() == 1 || events_2.len() == 2);
2808+
assert!(events_2.len() == 1);
28382809
expect_htlc_handling_failed_destinations!(events_2, &[failure]);
28392810
node.node.process_pending_htlc_forwards();
28402811
check_added_monitors!(node, 1);

0 commit comments

Comments
 (0)