Skip to content

Commit 42fb58a

Browse files
committed
Pull out the HTLC forwarding loop into a function
1 parent 2f761af commit 42fb58a

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
@@ -1860,8 +1860,41 @@ impl ChannelManager {
18601860
Ok((revoke_and_ack, commitment_signed))
18611861
}
18621862

1863+
#[inline]
1864+
fn forward_htlcs(&self, per_source_pending_forwards: &mut [(u64, Vec<(PendingForwardHTLCInfo, u64)>)]) {
1865+
for &mut (prev_short_channel_id, ref mut pending_forwards) in per_source_pending_forwards {
1866+
let mut forward_event = None;
1867+
if !pending_forwards.is_empty() {
1868+
let mut channel_state = self.channel_state.lock().unwrap();
1869+
if channel_state.forward_htlcs.is_empty() {
1870+
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));
1871+
channel_state.next_forward = forward_event.unwrap();
1872+
}
1873+
for (forward_info, prev_htlc_id) in pending_forwards.drain(..) {
1874+
match channel_state.forward_htlcs.entry(forward_info.short_channel_id) {
1875+
hash_map::Entry::Occupied(mut entry) => {
1876+
entry.get_mut().push(HTLCForwardInfo { prev_short_channel_id, prev_htlc_id, forward_info });
1877+
},
1878+
hash_map::Entry::Vacant(entry) => {
1879+
entry.insert(vec!(HTLCForwardInfo { prev_short_channel_id, prev_htlc_id, forward_info }));
1880+
}
1881+
}
1882+
}
1883+
}
1884+
match forward_event {
1885+
Some(time) => {
1886+
let mut pending_events = self.pending_events.lock().unwrap();
1887+
pending_events.push(events::Event::PendingHTLCsForwardable {
1888+
time_forwardable: time
1889+
});
1890+
}
1891+
None => {},
1892+
}
1893+
}
1894+
}
1895+
18631896
fn internal_revoke_and_ack(&self, their_node_id: &PublicKey, msg: &msgs::RevokeAndACK) -> Result<Option<msgs::CommitmentUpdate>, MsgHandleErrInternal> {
1864-
let ((res, mut pending_forwards, mut pending_failures), short_channel_id) = {
1897+
let ((res, pending_forwards, mut pending_failures), short_channel_id) = {
18651898
let mut channel_state = self.channel_state.lock().unwrap();
18661899
match channel_state.by_id.get_mut(&msg.channel_id) {
18671900
Some(chan) => {
@@ -1881,34 +1914,7 @@ impl ChannelManager {
18811914
for failure in pending_failures.drain(..) {
18821915
self.fail_htlc_backwards_internal(self.channel_state.lock().unwrap(), failure.0, &failure.1, failure.2);
18831916
}
1884-
1885-
let mut forward_event = None;
1886-
if !pending_forwards.is_empty() {
1887-
let mut channel_state = self.channel_state.lock().unwrap();
1888-
if channel_state.forward_htlcs.is_empty() {
1889-
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));
1890-
channel_state.next_forward = forward_event.unwrap();
1891-
}
1892-
for (forward_info, prev_htlc_id) in pending_forwards.drain(..) {
1893-
match channel_state.forward_htlcs.entry(forward_info.short_channel_id) {
1894-
hash_map::Entry::Occupied(mut entry) => {
1895-
entry.get_mut().push(HTLCForwardInfo { prev_short_channel_id: short_channel_id, prev_htlc_id, forward_info });
1896-
},
1897-
hash_map::Entry::Vacant(entry) => {
1898-
entry.insert(vec!(HTLCForwardInfo { prev_short_channel_id: short_channel_id, prev_htlc_id, forward_info }));
1899-
}
1900-
}
1901-
}
1902-
}
1903-
match forward_event {
1904-
Some(time) => {
1905-
let mut pending_events = self.pending_events.lock().unwrap();
1906-
pending_events.push(events::Event::PendingHTLCsForwardable {
1907-
time_forwardable: time
1908-
});
1909-
}
1910-
None => {},
1911-
}
1917+
self.forward_htlcs(&mut [(short_channel_id, pending_forwards)]);
19121918

19131919
Ok(res)
19141920
}

0 commit comments

Comments
 (0)