Skip to content

Commit b587f82

Browse files
committed
Prefactor: Switch TIMERs to be Durations
Previously, all `TIMER` constants were `u64` implictly assumed to represent seconds. Here, we switch them over to be `Duration`s, which allows for the introduction of sub-second timers. Moreover, it avoids any future confusions due to the implicitly assumed units.
1 parent 14ca59f commit b587f82

File tree

1 file changed

+37
-41
lines changed
  • lightning-background-processor/src

1 file changed

+37
-41
lines changed

lightning-background-processor/src/lib.rs

Lines changed: 37 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -110,59 +110,59 @@ pub struct BackgroundProcessor {
110110
}
111111

112112
#[cfg(not(test))]
113-
const FRESHNESS_TIMER: u64 = 60;
113+
const FRESHNESS_TIMER: Duration = Duration::from_secs(60);
114114
#[cfg(test)]
115-
const FRESHNESS_TIMER: u64 = 1;
115+
const FRESHNESS_TIMER: Duration = Duration::from_secs(1);
116116

117117
#[cfg(all(not(test), not(debug_assertions)))]
118-
const PING_TIMER: u64 = 10;
118+
const PING_TIMER: Duration = Duration::from_secs(10);
119119
/// Signature operations take a lot longer without compiler optimisations.
120120
/// Increasing the ping timer allows for this but slower devices will be disconnected if the
121121
/// timeout is reached.
122122
#[cfg(all(not(test), debug_assertions))]
123-
const PING_TIMER: u64 = 30;
123+
const PING_TIMER: Duration = Duration::from_secs(30);
124124
#[cfg(test)]
125-
const PING_TIMER: u64 = 1;
125+
const PING_TIMER: Duration = Duration::from_secs(1);
126126

127127
#[cfg(not(test))]
128-
const ONION_MESSAGE_HANDLER_TIMER: u64 = 10;
128+
const ONION_MESSAGE_HANDLER_TIMER: Duration = Duration::from_secs(10);
129129
#[cfg(test)]
130-
const ONION_MESSAGE_HANDLER_TIMER: u64 = 1;
130+
const ONION_MESSAGE_HANDLER_TIMER: Duration = Duration::from_secs(1);
131131

132132
/// Prune the network graph of stale entries hourly.
133-
const NETWORK_PRUNE_TIMER: u64 = 60 * 60;
133+
const NETWORK_PRUNE_TIMER: Duration = Duration::from_secs(60 * 60);
134134

135135
#[cfg(not(test))]
136-
const SCORER_PERSIST_TIMER: u64 = 60 * 5;
136+
const SCORER_PERSIST_TIMER: Duration = Duration::from_secs(60 * 5);
137137
#[cfg(test)]
138-
const SCORER_PERSIST_TIMER: u64 = 1;
138+
const SCORER_PERSIST_TIMER: Duration = Duration::from_secs(1);
139139

140140
#[cfg(not(test))]
141-
const FIRST_NETWORK_PRUNE_TIMER: u64 = 60;
141+
const FIRST_NETWORK_PRUNE_TIMER: Duration = Duration::from_secs(60);
142142
#[cfg(test)]
143-
const FIRST_NETWORK_PRUNE_TIMER: u64 = 1;
143+
const FIRST_NETWORK_PRUNE_TIMER: Duration = Duration::from_secs(1);
144144

145145
#[cfg(not(test))]
146-
const REBROADCAST_TIMER: u64 = 30;
146+
const REBROADCAST_TIMER: Duration = Duration::from_secs(30);
147147
#[cfg(test)]
148-
const REBROADCAST_TIMER: u64 = 1;
148+
const REBROADCAST_TIMER: Duration = Duration::from_secs(1);
149149

150150
#[cfg(not(test))]
151-
const SWEEPER_TIMER: u64 = 30;
151+
const SWEEPER_TIMER: Duration = Duration::from_secs(30);
152152
#[cfg(test)]
153-
const SWEEPER_TIMER: u64 = 1;
153+
const SWEEPER_TIMER: Duration = Duration::from_secs(1);
154154

155155
/// core::cmp::min is not currently const, so we define a trivial (and equivalent) replacement
156-
const fn min_u64(a: u64, b: u64) -> u64 {
157-
if a < b {
156+
const fn min_duration(a: Duration, b: Duration) -> Duration {
157+
if a.as_nanos() < b.as_nanos() {
158158
a
159159
} else {
160160
b
161161
}
162162
}
163-
const FASTEST_TIMER: u64 = min_u64(
164-
min_u64(FRESHNESS_TIMER, PING_TIMER),
165-
min_u64(SCORER_PERSIST_TIMER, min_u64(FIRST_NETWORK_PRUNE_TIMER, REBROADCAST_TIMER)),
163+
const FASTEST_TIMER: Duration = min_duration(
164+
min_duration(FRESHNESS_TIMER, PING_TIMER),
165+
min_duration(SCORER_PERSIST_TIMER, min_duration(FIRST_NETWORK_PRUNE_TIMER, REBROADCAST_TIMER)),
166166
);
167167

168168
/// Either [`P2PGossipSync`] or [`RapidGossipSync`].
@@ -372,9 +372,9 @@ macro_rules! define_run_body {
372372
// We wait up to 100ms, but track how long it takes to detect being put to sleep,
373373
// see `await_start`'s use below.
374374
let mut await_start = None;
375-
if $check_slow_await { await_start = Some($get_timer(1)); }
375+
if $check_slow_await { await_start = Some($get_timer(Duration::from_secs(1))); }
376376
$await;
377-
let await_slow = if $check_slow_await { $timer_elapsed(&mut await_start.unwrap(), 1) } else { false };
377+
let await_slow = if $check_slow_await { $timer_elapsed(&mut await_start.unwrap(), Duration::from_secs(1)) } else { false };
378378

379379
// Exit the loop if the background processor was requested to stop.
380380
if $loop_exit_check {
@@ -904,7 +904,7 @@ where
904904
e: sleeper(if mobile_interruptable_platform {
905905
Duration::from_millis(100)
906906
} else {
907-
Duration::from_secs(FASTEST_TIMER)
907+
FASTEST_TIMER
908908
}),
909909
};
910910
match fut.await {
@@ -914,7 +914,7 @@ where
914914
},
915915
}
916916
},
917-
|t| sleeper(Duration::from_secs(t)),
917+
|t| sleeper(t),
918918
|fut: &mut SleepFuture, _| {
919919
let mut waker = dummy_waker();
920920
let mut ctx = task::Context::from_waker(&mut waker);
@@ -1097,7 +1097,7 @@ impl BackgroundProcessor {
10971097
sleeper.wait_timeout(Duration::from_millis(100));
10981098
},
10991099
|_| Instant::now(),
1100-
|time: &Instant, dur| time.elapsed().as_secs() > dur,
1100+
|time: &Instant, dur| time.elapsed() > dur,
11011101
false,
11021102
|| {
11031103
use std::time::SystemTime;
@@ -1215,7 +1215,8 @@ mod tests {
12151215
use std::time::Duration;
12161216
use std::{env, fs};
12171217

1218-
const EVENT_DEADLINE: u64 = 5 * FRESHNESS_TIMER;
1218+
const EVENT_DEADLINE: Duration =
1219+
Duration::from_millis(5 * (FRESHNESS_TIMER.as_millis() as u64));
12191220

12201221
#[derive(Clone, Hash, PartialEq, Eq)]
12211222
struct TestDescriptor {}
@@ -2253,7 +2254,7 @@ mod tests {
22532254
// Open a channel and check that the FundingGenerationReady event was handled.
22542255
begin_open_channel!(nodes[0], nodes[1], channel_value);
22552256
let (temporary_channel_id, funding_tx) = funding_generation_recv
2256-
.recv_timeout(Duration::from_secs(EVENT_DEADLINE))
2257+
.recv_timeout(EVENT_DEADLINE)
22572258
.expect("FundingGenerationReady not handled within deadline");
22582259
nodes[0]
22592260
.node
@@ -2265,7 +2266,7 @@ mod tests {
22652266
let msg_1 = get_event_msg!(nodes[1], MessageSendEvent::SendFundingSigned, node_0_id);
22662267
nodes[0].node.handle_funding_signed(node_1_id, &msg_1);
22672268
channel_pending_recv
2268-
.recv_timeout(Duration::from_secs(EVENT_DEADLINE))
2269+
.recv_timeout(EVENT_DEADLINE)
22692270
.expect("ChannelPending not handled within deadline");
22702271

22712272
// Confirm the funding transaction.
@@ -2327,9 +2328,8 @@ mod tests {
23272328
let commitment_tx = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().pop().unwrap();
23282329
confirm_transaction_depth(&mut nodes[0], &commitment_tx, BREAKDOWN_TIMEOUT as u32);
23292330

2330-
let event = receiver
2331-
.recv_timeout(Duration::from_secs(EVENT_DEADLINE))
2332-
.expect("Events not handled within deadline");
2331+
let event =
2332+
receiver.recv_timeout(EVENT_DEADLINE).expect("Events not handled within deadline");
23332333
match event {
23342334
Event::SpendableOutputs { outputs, channel_id } => {
23352335
nodes[0]
@@ -2481,8 +2481,8 @@ mod tests {
24812481

24822482
begin_open_channel!(nodes[0], nodes[1], channel_value);
24832483
assert_eq!(
2484-
first_event_recv.recv_timeout(Duration::from_secs(EVENT_DEADLINE)).unwrap(),
2485-
second_event_recv.recv_timeout(Duration::from_secs(EVENT_DEADLINE)).unwrap()
2484+
first_event_recv.recv_timeout(EVENT_DEADLINE).unwrap(),
2485+
second_event_recv.recv_timeout(EVENT_DEADLINE).unwrap()
24862486
);
24872487

24882488
if !std::thread::panicking() {
@@ -2609,7 +2609,7 @@ mod tests {
26092609

26102610
do_test_not_pruning_network_graph_until_graph_sync_completion!(
26112611
nodes,
2612-
receiver.recv_timeout(Duration::from_secs(super::FIRST_NETWORK_PRUNE_TIMER * 5)),
2612+
receiver.recv_timeout(super::FIRST_NETWORK_PRUNE_TIMER * 5),
26132613
std::thread::sleep(Duration::from_millis(1))
26142614
);
26152615

@@ -2658,8 +2658,7 @@ mod tests {
26582658
{
26592659
let mut i = 0;
26602660
loop {
2661-
tokio::time::sleep(Duration::from_secs(super::FIRST_NETWORK_PRUNE_TIMER))
2662-
.await;
2661+
tokio::time::sleep(super::FIRST_NETWORK_PRUNE_TIMER).await;
26632662
if let Ok(()) = receiver.try_recv() {
26642663
break Ok::<(), ()>(());
26652664
}
@@ -2806,10 +2805,7 @@ mod tests {
28062805
Some(Arc::clone(&nodes[0].scorer)),
28072806
);
28082807

2809-
do_test_payment_path_scoring!(
2810-
nodes,
2811-
receiver.recv_timeout(Duration::from_secs(EVENT_DEADLINE))
2812-
);
2808+
do_test_payment_path_scoring!(nodes, receiver.recv_timeout(EVENT_DEADLINE));
28132809

28142810
if !std::thread::panicking() {
28152811
bg_processor.stop().unwrap();

0 commit comments

Comments
 (0)