Skip to content

Commit 3467c2d

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 37e8b8c commit 3467c2d

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,14 +7003,17 @@ where
70057003
}
70067004

70077005
fn decode_update_add_htlcs(&self, 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
for update_add_htlc in update_add_htlcs.1 {
70117011
match decode_update_add_htlcs.entry(scid) {
70127012
hash_map::Entry::Occupied(mut e) => { e.get_mut().push(update_add_htlc); },
70137013
hash_map::Entry::Vacant(e) => { e.insert(vec![update_add_htlc]); },
70147014
}
70157015
}
7016+
if push_forward_event { self.push_pending_forwards_ev(); }
70167017
}
70177018

70187019
#[inline]
@@ -7031,6 +7032,7 @@ where
70317032
// Pull this now to avoid introducing a lock order with `forward_htlcs`.
70327033
let is_our_scid = self.short_to_chan_info.read().unwrap().contains_key(&scid);
70337034

7035+
let decode_update_add_htlcs_empty = self.decode_update_add_htlcs.lock().unwrap().is_empty();
70347036
let mut forward_htlcs = self.forward_htlcs.lock().unwrap();
70357037
let forward_htlcs_empty = forward_htlcs.is_empty();
70367038
match forward_htlcs.entry(scid) {
@@ -7079,7 +7081,7 @@ where
70797081
} else {
70807082
// We don't want to generate a PendingHTLCsForwardable event if only intercepted
70817083
// payments are being processed.
7082-
if forward_htlcs_empty {
7084+
if forward_htlcs_empty && decode_update_add_htlcs_empty {
70837085
push_forward_event = true;
70847086
}
70857087
entry.insert(vec!(HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
@@ -10875,7 +10877,7 @@ where
1087510877
(13, claimable_htlc_onion_fields, optional_vec),
1087610878
(14, decode_update_add_htlcs, option),
1087710879
});
10878-
let decode_update_add_htlcs = decode_update_add_htlcs.unwrap_or_else(|| new_hash_map());
10880+
let mut decode_update_add_htlcs = decode_update_add_htlcs.unwrap_or_else(|| new_hash_map());
1087910881
if fake_scid_rand_bytes.is_none() {
1088010882
fake_scid_rand_bytes = Some(args.entropy_source.get_secure_random_bytes());
1088110883
}
@@ -11095,6 +11097,18 @@ where
1109511097
// still have an entry for this HTLC in `forward_htlcs` or
1109611098
// `pending_intercepted_htlcs`, we were apparently not persisted after
1109711099
// the monitor was when forwarding the payment.
11100+
decode_update_add_htlcs.retain(|scid, update_add_htlcs| {
11101+
update_add_htlcs.retain(|update_add_htlc| {
11102+
let matches = *scid == prev_hop_data.short_channel_id &&
11103+
update_add_htlc.htlc_id == prev_hop_data.htlc_id;
11104+
if matches {
11105+
log_info!(logger, "Removing pending to-decode HTLC with hash {} as it was forwarded to the closed channel {}",
11106+
&htlc.payment_hash, &monitor.channel_id());
11107+
}
11108+
!matches
11109+
});
11110+
!update_add_htlcs.is_empty()
11111+
});
1109811112
forward_htlcs.retain(|_, forwards| {
1109911113
forwards.retain(|forward| {
1110011114
if let HTLCForwardInfo::AddHTLC(htlc_info) = forward {
@@ -11176,7 +11190,7 @@ where
1117611190
}
1117711191
}
1117811192

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

0 commit comments

Comments
 (0)