Skip to content

Commit 0951b7f

Browse files
committed
Refactor push_htlc_failure out from fail_htlcs_backwards_internal
We plan to fail back HTLCs that have not been forwarded yet, so we refactored some of the failure event push logic out into it's own helper to reuse it.
1 parent 4ff8a39 commit 0951b7f

File tree

1 file changed

+33
-29
lines changed

1 file changed

+33
-29
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5283,15 +5283,6 @@ where
52835283
/// Fails an HTLC backwards to the sender of it to us.
52845284
/// Note that we do not assume that channels corresponding to failed HTLCs are still available.
52855285
fn fail_htlc_backwards_internal(&self, source: &HTLCSource, payment_hash: &PaymentHash, onion_error: &HTLCFailReason, destination: HTLCDestination) {
5286-
// Ensure that no peer state channel storage lock is held when calling this function.
5287-
// This ensures that future code doesn't introduce a lock-order requirement for
5288-
// `forward_htlcs` to be locked after the `per_peer_state` peer locks, which calling
5289-
// this function with any `per_peer_state` peer lock acquired would.
5290-
#[cfg(debug_assertions)]
5291-
for (_, peer) in self.per_peer_state.read().unwrap().iter() {
5292-
debug_assert_ne!(peer.held_by_thread(), LockHeldState::HeldByThread);
5293-
}
5294-
52955286
//TODO: There is a timing attack here where if a node fails an HTLC back to us they can
52965287
//identify whether we sent it or not based on the (I presume) very different runtime
52975288
//between the branches here. We should make this async and move it into the forward HTLCs
@@ -5339,28 +5330,41 @@ where
53395330
}
53405331
};
53415332

5342-
let mut push_forward_ev = false;
5343-
let mut forward_htlcs = self.forward_htlcs.lock().unwrap();
5344-
if forward_htlcs.is_empty() {
5345-
push_forward_ev = true;
5346-
}
5347-
match forward_htlcs.entry(*short_channel_id) {
5348-
hash_map::Entry::Occupied(mut entry) => {
5349-
entry.get_mut().push(failure);
5350-
},
5351-
hash_map::Entry::Vacant(entry) => {
5352-
entry.insert(vec!(failure));
5353-
}
5354-
}
5355-
mem::drop(forward_htlcs);
5356-
if push_forward_ev { self.push_pending_forwards_ev(); }
5357-
let mut pending_events = self.pending_events.lock().unwrap();
5358-
pending_events.push_back((events::Event::HTLCHandlingFailed {
5359-
prev_channel_id: outpoint.to_channel_id(),
5360-
failed_next_destination: destination,
5361-
}, None));
5333+
self.push_htlc_failure(*short_channel_id, outpoint.to_channel_id(), failure, destination);
5334+
}
5335+
}
5336+
}
5337+
5338+
fn push_htlc_failure(&self, short_channel_id: u64, channel_id: ChannelId, failure: HTLCForwardInfo, destination: HTLCDestination) {
5339+
// Ensure that no peer state channel storage lock is held when calling this function.
5340+
// This ensures that future code doesn't introduce a lock-order requirement for
5341+
// `forward_htlcs` to be locked after the `per_peer_state` peer locks, which calling
5342+
// this function with any `per_peer_state` peer lock acquired would.
5343+
#[cfg(debug_assertions)]
5344+
for (_, peer) in self.per_peer_state.read().unwrap().iter() {
5345+
debug_assert_ne!(peer.held_by_thread(), LockHeldState::HeldByThread);
5346+
}
5347+
5348+
let mut push_forward_ev = false;
5349+
let mut forward_htlcs = self.forward_htlcs.lock().unwrap();
5350+
if forward_htlcs.is_empty() {
5351+
push_forward_ev = true;
5352+
}
5353+
match forward_htlcs.entry(short_channel_id) {
5354+
hash_map::Entry::Occupied(mut entry) => {
5355+
entry.get_mut().push(failure);
53625356
},
5357+
hash_map::Entry::Vacant(entry) => {
5358+
entry.insert(vec!(failure));
5359+
}
53635360
}
5361+
mem::drop(forward_htlcs);
5362+
if push_forward_ev { self.push_pending_forwards_ev(); }
5363+
let mut pending_events = self.pending_events.lock().unwrap();
5364+
pending_events.push_back((events::Event::HTLCHandlingFailed {
5365+
prev_channel_id: channel_id,
5366+
failed_next_destination: destination,
5367+
}, None));
53645368
}
53655369

53665370
/// Provides a payment preimage in response to [`Event::PaymentClaimable`], generating any

0 commit comments

Comments
 (0)