monotonic time and run testnet at 20% realtime

pull/1070/head
Jeff Becker 4 years ago
parent 0edad0a817
commit 7aa1b2c27c
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -195,6 +195,8 @@ endif(USE_NETNS)
if(TESTNET)
add_definitions(-DTESTNET=1)
# 5 times slower than realtime
add_definitions(-DTESTNET_SPEED=5)
endif(TESTNET)
if(SHADOW)

@ -836,6 +836,9 @@ namespace libuv
{
if(m_Run)
{
#ifdef TESTNET_SPEED
ms *= TESTNET_SPEED;
#endif
uv_timer_start(m_TickTimer, &OnTickTimeout, ms, 0);
uv_run(&m_Impl, UV_RUN_ONCE);
}
@ -872,6 +875,9 @@ namespace libuv
Loop::call_after_delay(llarp_time_t delay_ms,
std::function< void(void) > callback)
{
#ifdef TESTNET_SPEED
delay_ms *= TESTNET_SPEED;
#endif
PendingTimer timer;
timer.delay_ms = delay_ms;
timer.callback = callback;

@ -6,15 +6,33 @@ namespace llarp
{
using Clock_t = std::chrono::system_clock;
template < typename Res >
template < typename Res, typename Clock >
static llarp_time_t
time_since_epoch()
{
return std::chrono::duration_cast< Res >(
llarp::Clock_t::now().time_since_epoch())
return std::chrono::duration_cast< Res >(Clock::now().time_since_epoch())
.count();
}
static llarp_time_t
time_started_at()
{
const static llarp_time_t started =
time_since_epoch< std::chrono::milliseconds, Clock_t >();
return started;
}
static llarp_time_t
time_since_started()
{
const static llarp_time_t started =
time_since_epoch< std::chrono::milliseconds,
std::chrono::steady_clock >();
return time_since_epoch< std::chrono::milliseconds,
std::chrono::steady_clock >()
- started;
}
// use std::chrono because otherwise the network breaks with Daylight Savings
// this time, it doesn't get truncated -despair
// that concern is what drove me back to the POSIX C time functions
@ -23,7 +41,12 @@ namespace llarp
time_now_ms()
{
static llarp_time_t lastTime = 0;
auto t = llarp::time_since_epoch< std::chrono::milliseconds >();
auto t = time_since_started();
#ifdef TESTNET_SPEED
t /= TESTNET_SPEED;
#endif
t += time_started_at();
if(t <= lastTime)
{
return lastTime;

Loading…
Cancel
Save