Skip to content

Commit 7fe2582

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 2edc5b2 commit 7fe2582

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
@@ -5414,11 +5414,9 @@ where
54145414
}
54155415
};
54165416

5417-
let mut push_forward_ev = false;
5417+
let mut push_forward_ev = self.decode_update_add_htlcs.lock().unwrap().is_empty();
54185418
let mut forward_htlcs = self.forward_htlcs.lock().unwrap();
5419-
if forward_htlcs.is_empty() {
5420-
push_forward_ev = true;
5421-
}
5419+
push_forward_ev &= forward_htlcs.is_empty();
54225420
match forward_htlcs.entry(*short_channel_id) {
54235421
hash_map::Entry::Occupied(mut entry) => {
54245422
entry.get_mut().push(failure);
@@ -7005,12 +7003,15 @@ where
70057003
}
70067004

70077005
fn push_decode_update_add_htlcs(&self, mut update_add_htlcs: (u64, Vec<msgs::UpdateAddHTLC>)) {
7006+
let mut push_forward_event = self.forward_htlcs.lock().unwrap().is_empty();
70087007
let mut decode_update_add_htlcs = self.decode_update_add_htlcs.lock().unwrap();
7008+
push_forward_event &= decode_update_add_htlcs.is_empty();
70097009
let scid = update_add_htlcs.0;
70107010
match decode_update_add_htlcs.entry(scid) {
70117011
hash_map::Entry::Occupied(mut e) => { e.get_mut().append(&mut update_add_htlcs.1); },
70127012
hash_map::Entry::Vacant(e) => { e.insert(update_add_htlcs.1); },
70137013
}
7014+
if push_forward_event { self.push_pending_forwards_ev(); }
70147015
}
70157016

70167017
#[inline]
@@ -7029,6 +7030,7 @@ where
70297030
// Pull this now to avoid introducing a lock order with `forward_htlcs`.
70307031
let is_our_scid = self.short_to_chan_info.read().unwrap().contains_key(&scid);
70317032

7033+
let decode_update_add_htlcs_empty = self.decode_update_add_htlcs.lock().unwrap().is_empty();
70327034
let mut forward_htlcs = self.forward_htlcs.lock().unwrap();
70337035
let forward_htlcs_empty = forward_htlcs.is_empty();
70347036
match forward_htlcs.entry(scid) {
@@ -7077,7 +7079,7 @@ where
70777079
} else {
70787080
// We don't want to generate a PendingHTLCsForwardable event if only intercepted
70797081
// payments are being processed.
7080-
if forward_htlcs_empty {
7082+
if forward_htlcs_empty && decode_update_add_htlcs_empty {
70817083
push_forward_event = true;
70827084
}
70837085
entry.insert(vec!(HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
@@ -10874,7 +10876,7 @@ where
1087410876
(13, claimable_htlc_onion_fields, optional_vec),
1087510877
(14, decode_update_add_htlcs, option),
1087610878
});
10877-
let decode_update_add_htlcs = decode_update_add_htlcs.unwrap_or_else(|| new_hash_map());
10879+
let mut decode_update_add_htlcs = decode_update_add_htlcs.unwrap_or_else(|| new_hash_map());
1087810880
if fake_scid_rand_bytes.is_none() {
1087910881
fake_scid_rand_bytes = Some(args.entropy_source.get_secure_random_bytes());
1088010882
}
@@ -11094,6 +11096,18 @@ where
1109411096
// still have an entry for this HTLC in `forward_htlcs` or
1109511097
// `pending_intercepted_htlcs`, we were apparently not persisted after
1109611098
// the monitor was when forwarding the payment.
11099+
decode_update_add_htlcs.retain(|scid, update_add_htlcs| {
11100+
update_add_htlcs.retain(|update_add_htlc| {
11101+
let matches = *scid == prev_hop_data.short_channel_id &&
11102+
update_add_htlc.htlc_id == prev_hop_data.htlc_id;
11103+
if matches {
11104+
log_info!(logger, "Removing pending to-decode HTLC with hash {} as it was forwarded to the closed channel {}",
11105+
&htlc.payment_hash, &monitor.channel_id());
11106+
}
11107+
!matches
11108+
});
11109+
!update_add_htlcs.is_empty()
11110+
});
1109711111
forward_htlcs.retain(|_, forwards| {
1109811112
forwards.retain(|forward| {
1109911113
if let HTLCForwardInfo::AddHTLC(htlc_info) = forward {
@@ -11175,7 +11189,7 @@ where
1117511189
}
1117611190
}
1117711191

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

0 commit comments

Comments
 (0)