Skip to content

Commit e2a9ed7

Browse files
committed
Drop system clock calls for PendingHTLCsForwardable events.
Instead, return a Duration and let the user do the work of waiting. This is one of only a handful of steps to make us mostly-syscall-free, at least enough to run in WASM according to elichai.
1 parent 0dfd20b commit e2a9ed7

File tree

5 files changed

+6
-27
lines changed

5 files changed

+6
-27
lines changed

src/ln/chanmon_update_fail_tests.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ use util::errors::APIError;
1313
use bitcoin_hashes::sha256::Hash as Sha256;
1414
use bitcoin_hashes::Hash;
1515

16-
use std::time::Instant;
17-
1816
use ln::functional_test_utils::*;
1917

2018
#[test]
@@ -1495,7 +1493,6 @@ fn test_monitor_update_on_pending_forwards() {
14951493
Event::PendingHTLCsForwardable { .. } => { },
14961494
_ => panic!("Unexpected event"),
14971495
};
1498-
nodes[0].node.channel_state.lock().unwrap().next_forward = Instant::now();
14991496
nodes[0].node.process_pending_htlc_forwards();
15001497
expect_payment_received!(nodes[0], payment_hash_2, 1000000);
15011498

src/ln/channelmanager.rs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use std::collections::{HashMap, hash_map, HashSet};
4646
use std::io::Cursor;
4747
use std::sync::{Arc, Mutex, MutexGuard, RwLock};
4848
use std::sync::atomic::{AtomicUsize, Ordering};
49-
use std::time::{Instant,Duration};
49+
use std::time::Duration;
5050

5151
// We hold various information about HTLC relay in the HTLC objects in Channel itself:
5252
//
@@ -247,7 +247,6 @@ pub(super) enum RAACommitmentOrder {
247247
pub(super) struct ChannelHolder {
248248
pub(super) by_id: HashMap<[u8; 32], Channel>,
249249
pub(super) short_to_id: HashMap<u64, [u8; 32]>,
250-
pub(super) next_forward: Instant,
251250
/// short channel id -> forward infos. Key of 0 means payments received
252251
/// Note that while this is held in the same mutex as the channels themselves, no consistency
253252
/// guarantees are made about the existence of a channel with the short id here, nor the short
@@ -266,7 +265,6 @@ pub(super) struct ChannelHolder {
266265
pub(super) struct MutChannelHolder<'a> {
267266
pub(super) by_id: &'a mut HashMap<[u8; 32], Channel>,
268267
pub(super) short_to_id: &'a mut HashMap<u64, [u8; 32]>,
269-
pub(super) next_forward: &'a mut Instant,
270268
pub(super) forward_htlcs: &'a mut HashMap<u64, Vec<HTLCForwardInfo>>,
271269
pub(super) claimable_htlcs: &'a mut HashMap<PaymentHash, Vec<(u64, HTLCPreviousHopData)>>,
272270
pub(super) pending_msg_events: &'a mut Vec<events::MessageSendEvent>,
@@ -276,7 +274,6 @@ impl ChannelHolder {
276274
MutChannelHolder {
277275
by_id: &mut self.by_id,
278276
short_to_id: &mut self.short_to_id,
279-
next_forward: &mut self.next_forward,
280277
forward_htlcs: &mut self.forward_htlcs,
281278
claimable_htlcs: &mut self.claimable_htlcs,
282279
pending_msg_events: &mut self.pending_msg_events,
@@ -549,7 +546,6 @@ impl ChannelManager {
549546
channel_state: Mutex::new(ChannelHolder{
550547
by_id: HashMap::new(),
551548
short_to_id: HashMap::new(),
552-
next_forward: Instant::now(),
553549
forward_htlcs: HashMap::new(),
554550
claimable_htlcs: HashMap::new(),
555551
pending_msg_events: Vec::new(),
@@ -1184,10 +1180,6 @@ impl ChannelManager {
11841180
let mut channel_state_lock = self.channel_state.lock().unwrap();
11851181
let channel_state = channel_state_lock.borrow_parts();
11861182

1187-
if cfg!(not(feature = "fuzztarget")) && Instant::now() < *channel_state.next_forward {
1188-
return;
1189-
}
1190-
11911183
for (short_chan_id, mut pending_forwards) in channel_state.forward_htlcs.drain() {
11921184
if short_chan_id != 0 {
11931185
let forward_chan_id = match channel_state.short_to_id.get(&short_chan_id) {
@@ -1467,8 +1459,7 @@ impl ChannelManager {
14671459

14681460
let mut forward_event = None;
14691461
if channel_state_lock.forward_htlcs.is_empty() {
1470-
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));
1471-
channel_state_lock.next_forward = forward_event.unwrap();
1462+
forward_event = Some(Duration::from_millis(((rng::rand_f32() * 4.0 + 1.0) * MIN_HTLC_RELAY_HOLDING_CELL_MILLIS as f32) as u64));
14721463
}
14731464
match channel_state_lock.forward_htlcs.entry(short_channel_id) {
14741465
hash_map::Entry::Occupied(mut entry) => {
@@ -2077,8 +2068,7 @@ impl ChannelManager {
20772068
if !pending_forwards.is_empty() {
20782069
let mut channel_state = self.channel_state.lock().unwrap();
20792070
if channel_state.forward_htlcs.is_empty() {
2080-
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));
2081-
channel_state.next_forward = forward_event.unwrap();
2071+
forward_event = Some(Duration::from_millis(((rng::rand_f32() * 4.0 + 1.0) * MIN_HTLC_RELAY_HOLDING_CELL_MILLIS as f32) as u64));
20822072
}
20832073
for (forward_info, prev_htlc_id) in pending_forwards.drain(..) {
20842074
match channel_state.forward_htlcs.entry(forward_info.short_channel_id) {
@@ -3087,7 +3077,6 @@ impl<'a, R : ::std::io::Read> ReadableArgs<R, ChannelManagerReadArgs<'a>> for (S
30873077
channel_state: Mutex::new(ChannelHolder {
30883078
by_id,
30893079
short_to_id,
3090-
next_forward: Instant::now(),
30913080
forward_htlcs,
30923081
claimable_htlcs,
30933082
pending_msg_events: Vec::new(),

src/ln/functional_test_utils.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ use std::collections::HashMap;
3232
use std::default::Default;
3333
use std::rc::Rc;
3434
use std::sync::{Arc, Mutex};
35-
use std::time::Instant;
3635
use std::mem;
3736

3837
pub const CHAN_CONFIRM_DEPTH: u32 = 100;
@@ -536,8 +535,6 @@ macro_rules! expect_pending_htlcs_forwardable {
536535
Event::PendingHTLCsForwardable { .. } => { },
537536
_ => panic!("Unexpected event"),
538537
};
539-
let node_ref: &Node = &$node;
540-
node_ref.node.channel_state.lock().unwrap().next_forward = Instant::now();
541538
$node.node.process_pending_htlc_forwards();
542539
}}
543540
}

src/ln/functional_tests.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ use std::collections::{BTreeSet, HashMap, HashSet};
4343
use std::default::Default;
4444
use std::sync::Arc;
4545
use std::sync::atomic::Ordering;
46-
use std::time::Instant;
4746
use std::mem;
4847

4948
use ln::functional_test_utils::*;
@@ -2460,7 +2459,6 @@ fn do_test_commitment_revoked_fail_backward_exhaustive(deliver_bs_raa: bool, use
24602459
_ => panic!("Unexpected event"),
24612460
};
24622461
}
2463-
nodes[1].node.channel_state.lock().unwrap().next_forward = Instant::now();
24642462
nodes[1].node.process_pending_htlc_forwards();
24652463
check_added_monitors!(nodes[1], 1);
24662464

@@ -2813,7 +2811,6 @@ fn do_test_drop_messages_peer_disconnect(messages_delivered: u8) {
28132811
nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
28142812
reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
28152813

2816-
nodes[1].node.channel_state.lock().unwrap().next_forward = Instant::now();
28172814
nodes[1].node.process_pending_htlc_forwards();
28182815

28192816
let events_2 = nodes[1].node.get_and_clear_pending_events();
@@ -4463,7 +4460,6 @@ fn run_onion_failure_test_with_fail_intercept<F1,F2,F3>(_name: &str, test_case:
44634460
macro_rules! expect_htlc_forward {
44644461
($node: expr) => {{
44654462
expect_event!($node, Event::PendingHTLCsForwardable);
4466-
$node.node.channel_state.lock().unwrap().next_forward = Instant::now();
44674463
$node.node.process_pending_htlc_forwards();
44684464
}}
44694465
}

src/util/events.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use bitcoin::blockdata::script::Script;
2121

2222
use secp256k1::key::PublicKey;
2323

24-
use std::time::Instant;
24+
use std::time::Duration;
2525

2626
/// An Event which you should probably take some action in response to.
2727
pub enum Event {
@@ -92,8 +92,8 @@ pub enum Event {
9292
/// Used to indicate that ChannelManager::process_pending_htlc_forwards should be called at a
9393
/// time in the future.
9494
PendingHTLCsForwardable {
95-
/// The earliest time at which process_pending_htlc_forwards should be called.
96-
time_forwardable: Instant,
95+
/// The amount of time that should be waited prior to calling process_pending_htlc_forwards
96+
time_forwardable: Duration,
9797
},
9898
/// Used to indicate that an output was generated on-chain which you should know how to spend.
9999
/// Such an output will *not* ever be spent by rust-lightning, so you need to store them

0 commit comments

Comments
 (0)