Skip to content

Commit 4cca933

Browse files
committed
Pull out the HTLC forwarding loop into a function
1 parent 695eec2 commit 4cca933

File tree

1 file changed

+35
-29
lines changed

1 file changed

+35
-29
lines changed

src/ln/channelmanager.rs

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2061,8 +2061,41 @@ impl ChannelManager {
20612061
Ok((revoke_and_ack, commitment_signed))
20622062
}
20632063

2064+
#[inline]
2065+
fn forward_htlcs(&self, per_source_pending_forwards: &mut [(u64, Vec<(PendingForwardHTLCInfo, u64)>)]) {
2066+
for &mut (prev_short_channel_id, ref mut pending_forwards) in per_source_pending_forwards {
2067+
let mut forward_event = None;
2068+
if !pending_forwards.is_empty() {
2069+
let mut channel_state = self.channel_state.lock().unwrap();
2070+
if channel_state.forward_htlcs.is_empty() {
2071+
forward_event = Some(Instant::now() + Duration::from_millis(((rng::rand_f32() * 4.0 + 1.0) * MIN_HTLC_RELAY_HOLDING_CELL_MILLIS as f32) as u64));
2072+
channel_state.next_forward = forward_event.unwrap();
2073+
}
2074+
for (forward_info, prev_htlc_id) in pending_forwards.drain(..) {
2075+
match channel_state.forward_htlcs.entry(forward_info.short_channel_id) {
2076+
hash_map::Entry::Occupied(mut entry) => {
2077+
entry.get_mut().push(HTLCForwardInfo { prev_short_channel_id, prev_htlc_id, forward_info });
2078+
},
2079+
hash_map::Entry::Vacant(entry) => {
2080+
entry.insert(vec!(HTLCForwardInfo { prev_short_channel_id, prev_htlc_id, forward_info }));
2081+
}
2082+
}
2083+
}
2084+
}
2085+
match forward_event {
2086+
Some(time) => {
2087+
let mut pending_events = self.pending_events.lock().unwrap();
2088+
pending_events.push(events::Event::PendingHTLCsForwardable {
2089+
time_forwardable: time
2090+
});
2091+
}
2092+
None => {},
2093+
}
2094+
}
2095+
}
2096+
20642097
fn internal_revoke_and_ack(&self, their_node_id: &PublicKey, msg: &msgs::RevokeAndACK) -> Result<Option<msgs::CommitmentUpdate>, MsgHandleErrInternal> {
2065-
let ((res, mut pending_forwards, mut pending_failures), short_channel_id) = {
2098+
let ((res, pending_forwards, mut pending_failures), short_channel_id) = {
20662099
let mut channel_state = self.channel_state.lock().unwrap();
20672100
match channel_state.by_id.get_mut(&msg.channel_id) {
20682101
Some(chan) => {
@@ -2082,34 +2115,7 @@ impl ChannelManager {
20822115
for failure in pending_failures.drain(..) {
20832116
self.fail_htlc_backwards_internal(self.channel_state.lock().unwrap(), failure.0, &failure.1, failure.2);
20842117
}
2085-
2086-
let mut forward_event = None;
2087-
if !pending_forwards.is_empty() {
2088-
let mut channel_state = self.channel_state.lock().unwrap();
2089-
if channel_state.forward_htlcs.is_empty() {
2090-
forward_event = Some(Instant::now() + Duration::from_millis(((rng::rand_f32() * 4.0 + 1.0) * MIN_HTLC_RELAY_HOLDING_CELL_MILLIS as f32) as u64));
2091-
channel_state.next_forward = forward_event.unwrap();
2092-
}
2093-
for (forward_info, prev_htlc_id) in pending_forwards.drain(..) {
2094-
match channel_state.forward_htlcs.entry(forward_info.short_channel_id) {
2095-
hash_map::Entry::Occupied(mut entry) => {
2096-
entry.get_mut().push(HTLCForwardInfo { prev_short_channel_id: short_channel_id, prev_htlc_id, forward_info });
2097-
},
2098-
hash_map::Entry::Vacant(entry) => {
2099-
entry.insert(vec!(HTLCForwardInfo { prev_short_channel_id: short_channel_id, prev_htlc_id, forward_info }));
2100-
}
2101-
}
2102-
}
2103-
}
2104-
match forward_event {
2105-
Some(time) => {
2106-
let mut pending_events = self.pending_events.lock().unwrap();
2107-
pending_events.push(events::Event::PendingHTLCsForwardable {
2108-
time_forwardable: time
2109-
});
2110-
}
2111-
None => {},
2112-
}
2118+
self.forward_htlcs(&mut [(short_channel_id, pending_forwards)]);
21132119

21142120
Ok(res)
21152121
}

0 commit comments

Comments
 (0)