Skip to content

Commit b683add

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 8e5b498 commit b683add

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5402,11 +5402,9 @@ where
54025402
}
54035403
};
54045404

5405-
let mut push_forward_ev = false;
5405+
let mut push_forward_ev = self.decode_update_add_htlcs.lock().unwrap().is_empty();
54065406
let mut forward_htlcs = self.forward_htlcs.lock().unwrap();
5407-
if forward_htlcs.is_empty() {
5408-
push_forward_ev = true;
5409-
}
5407+
push_forward_ev &= forward_htlcs.is_empty();
54105408
match forward_htlcs.entry(*short_channel_id) {
54115409
hash_map::Entry::Occupied(mut entry) => {
54125410
entry.get_mut().push(failure);
@@ -6981,14 +6979,17 @@ where
69816979
}
69826980

69836981
fn decode_update_add_htlcs(&self, update_add_htlcs: (u64, Vec<msgs::UpdateAddHTLC>)) {
6982+
let mut push_forward_event = self.forward_htlcs.lock().unwrap().is_empty();
69846983
let mut decode_update_add_htlcs = self.decode_update_add_htlcs.lock().unwrap();
6984+
push_forward_event &= decode_update_add_htlcs.is_empty();
69856985
let scid = update_add_htlcs.0;
69866986
for update_add_htlc in update_add_htlcs.1 {
69876987
match decode_update_add_htlcs.entry(scid) {
69886988
hash_map::Entry::Occupied(mut e) => { e.get_mut().push(update_add_htlc); },
69896989
hash_map::Entry::Vacant(e) => { e.insert(vec![update_add_htlc]); },
69906990
}
69916991
}
6992+
if push_forward_event { self.push_pending_forwards_ev(); }
69926993
}
69936994

69946995
#[inline]
@@ -7007,6 +7008,7 @@ where
70077008
// Pull this now to avoid introducing a lock order with `forward_htlcs`.
70087009
let is_our_scid = self.short_to_chan_info.read().unwrap().contains_key(&scid);
70097010

7011+
let decode_update_add_htlcs_empty = self.decode_update_add_htlcs.lock().unwrap().is_empty();
70107012
let mut forward_htlcs = self.forward_htlcs.lock().unwrap();
70117013
let forward_htlcs_empty = forward_htlcs.is_empty();
70127014
match forward_htlcs.entry(scid) {
@@ -7055,7 +7057,7 @@ where
70557057
} else {
70567058
// We don't want to generate a PendingHTLCsForwardable event if only intercepted
70577059
// payments are being processed.
7058-
if forward_htlcs_empty {
7060+
if forward_htlcs_empty && decode_update_add_htlcs_empty {
70597061
push_forward_event = true;
70607062
}
70617063
entry.insert(vec!(HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
@@ -10823,7 +10825,7 @@ where
1082310825
(13, claimable_htlc_onion_fields, optional_vec),
1082410826
(14, decode_update_add_htlcs, option),
1082510827
});
10826-
let decode_update_add_htlcs = decode_update_add_htlcs.unwrap_or(new_hash_map());
10828+
let mut decode_update_add_htlcs = decode_update_add_htlcs.unwrap_or(new_hash_map());
1082710829
if fake_scid_rand_bytes.is_none() {
1082810830
fake_scid_rand_bytes = Some(args.entropy_source.get_secure_random_bytes());
1082910831
}
@@ -11043,6 +11045,18 @@ where
1104311045
// still have an entry for this HTLC in `forward_htlcs` or
1104411046
// `pending_intercepted_htlcs`, we were apparently not persisted after
1104511047
// the monitor was when forwarding the payment.
11048+
decode_update_add_htlcs.retain(|scid, update_add_htlcs| {
11049+
update_add_htlcs.retain(|update_add_htlc| {
11050+
let matches = *scid == prev_hop_data.short_channel_id &&
11051+
update_add_htlc.htlc_id == prev_hop_data.htlc_id;
11052+
if matches {
11053+
log_info!(logger, "Removing pending to-decode HTLC with hash {} as it was forwarded to the closed channel {}",
11054+
&htlc.payment_hash, &monitor.channel_id());
11055+
}
11056+
!matches
11057+
});
11058+
!update_add_htlcs.is_empty()
11059+
});
1104611060
forward_htlcs.retain(|_, forwards| {
1104711061
forwards.retain(|forward| {
1104811062
if let HTLCForwardInfo::AddHTLC(htlc_info) = forward {
@@ -11124,7 +11138,7 @@ where
1112411138
}
1112511139
}
1112611140

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

0 commit comments

Comments
 (0)