Skip to content

Commit 4ac2fa1

Browse files
committed
Consider pending decode_update_add_htlcs when pushing forward event
Since decoding pending `update_add_htlc` onions will go through the HTLC forwarding path, we'll want to make sure we don't queue more events than necessary if we have both HTLCs to forward/fail and pending `update_add_htlc` onions to decode.
1 parent 08aa8b7 commit 4ac2fa1

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

lightning/src/ln/channelmanager.rs

+21-7
Original file line numberDiff line numberDiff line change
@@ -5406,11 +5406,9 @@ where
54065406
}
54075407
};
54085408

5409-
let mut push_forward_ev = false;
5409+
let mut push_forward_ev = self.decode_update_add_htlcs.lock().unwrap().is_empty();
54105410
let mut forward_htlcs = self.forward_htlcs.lock().unwrap();
5411-
if forward_htlcs.is_empty() {
5412-
push_forward_ev = true;
5413-
}
5411+
push_forward_ev &= forward_htlcs.is_empty();
54145412
match forward_htlcs.entry(*short_channel_id) {
54155413
hash_map::Entry::Occupied(mut entry) => {
54165414
entry.get_mut().push(failure);
@@ -6985,14 +6983,17 @@ where
69856983
}
69866984

69876985
fn decode_update_add_htlcs(&self, update_add_htlcs: (u64, Vec<msgs::UpdateAddHTLC>)) {
6986+
let mut push_forward_event = self.forward_htlcs.lock().unwrap().is_empty();
69886987
let mut decode_update_add_htlcs = self.decode_update_add_htlcs.lock().unwrap();
6988+
push_forward_event &= decode_update_add_htlcs.is_empty();
69896989
let scid = update_add_htlcs.0;
69906990
for update_add_htlc in update_add_htlcs.1 {
69916991
match decode_update_add_htlcs.entry(scid) {
69926992
hash_map::Entry::Occupied(mut e) => { e.get_mut().push(update_add_htlc); },
69936993
hash_map::Entry::Vacant(e) => { e.insert(vec![update_add_htlc]); },
69946994
}
69956995
}
6996+
if push_forward_event { self.push_pending_forwards_ev(); }
69966997
}
69976998

69986999
#[inline]
@@ -7011,6 +7012,7 @@ where
70117012
// Pull this now to avoid introducing a lock order with `forward_htlcs`.
70127013
let is_our_scid = self.short_to_chan_info.read().unwrap().contains_key(&scid);
70137014

7015+
let decode_update_add_htlcs_empty = self.decode_update_add_htlcs.lock().unwrap().is_empty();
70147016
let mut forward_htlcs = self.forward_htlcs.lock().unwrap();
70157017
let forward_htlcs_empty = forward_htlcs.is_empty();
70167018
match forward_htlcs.entry(scid) {
@@ -7059,7 +7061,7 @@ where
70597061
} else {
70607062
// We don't want to generate a PendingHTLCsForwardable event if only intercepted
70617063
// payments are being processed.
7062-
if forward_htlcs_empty {
7064+
if forward_htlcs_empty && decode_update_add_htlcs_empty {
70637065
push_forward_event = true;
70647066
}
70657067
entry.insert(vec!(HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
@@ -10827,7 +10829,7 @@ where
1082710829
(13, claimable_htlc_onion_fields, optional_vec),
1082810830
(14, decode_update_add_htlcs, option),
1082910831
});
10830-
let decode_update_add_htlcs = decode_update_add_htlcs.unwrap_or(new_hash_map());
10832+
let mut decode_update_add_htlcs = decode_update_add_htlcs.unwrap_or(new_hash_map());
1083110833
if fake_scid_rand_bytes.is_none() {
1083210834
fake_scid_rand_bytes = Some(args.entropy_source.get_secure_random_bytes());
1083310835
}
@@ -11047,6 +11049,18 @@ where
1104711049
// still have an entry for this HTLC in `forward_htlcs` or
1104811050
// `pending_intercepted_htlcs`, we were apparently not persisted after
1104911051
// the monitor was when forwarding the payment.
11052+
decode_update_add_htlcs.retain(|scid, update_add_htlcs| {
11053+
update_add_htlcs.retain(|update_add_htlc| {
11054+
let matches = *scid == prev_hop_data.short_channel_id &&
11055+
update_add_htlc.htlc_id == prev_hop_data.htlc_id;
11056+
if matches {
11057+
log_info!(logger, "Removing pending to-decode HTLC with hash {} as it was forwarded to the closed channel {}",
11058+
&htlc.payment_hash, &monitor.channel_id());
11059+
}
11060+
!matches
11061+
});
11062+
!update_add_htlcs.is_empty()
11063+
});
1105011064
forward_htlcs.retain(|_, forwards| {
1105111065
forwards.retain(|forward| {
1105211066
if let HTLCForwardInfo::AddHTLC(htlc_info) = forward {
@@ -11128,7 +11142,7 @@ where
1112811142
}
1112911143
}
1113011144

11131-
if !forward_htlcs.is_empty() || pending_outbounds.needs_abandon() {
11145+
if !forward_htlcs.is_empty() || !decode_update_add_htlcs.is_empty() || pending_outbounds.needs_abandon() {
1113211146
// If we have pending HTLCs to forward, assume we either dropped a
1113311147
// `PendingHTLCsForwardable` or the user received it but never processed it as they
1113411148
// shut down before the timer hit. Either way, set the time_forwardable to a small

0 commit comments

Comments
 (0)