diff --git a/llarp/router/i_gossiper.hpp b/llarp/router/i_gossiper.hpp index c22431578..c0d6fd51f 100644 --- a/llarp/router/i_gossiper.hpp +++ b/llarp/router/i_gossiper.hpp @@ -1,5 +1,7 @@ #pragma once #include +#include +#include namespace llarp { @@ -15,7 +17,7 @@ namespace llarp virtual bool GossipRC(const RouterContact& rc) = 0; - using Time_t = std::chrono::milliseconds; + using Time_t = Duration_t; virtual void Decay(Time_t now) = 0; @@ -31,5 +33,13 @@ namespace llarp /// forget the replay filter entry given pubkey virtual void Forget(const RouterID& router) = 0; + + /// returns the time point when we will send our next gossip at + virtual TimePoint_t + NextGossipAt() const = 0; + + /// returns the time point when we sent our last gossip at or nullopt if we never did + virtual std::optional + LastGossipAt() const = 0; }; } // namespace llarp diff --git a/llarp/router/rc_gossiper.cpp b/llarp/router/rc_gossiper.cpp index a88089bb5..bd9e99b7b 100644 --- a/llarp/router/rc_gossiper.cpp +++ b/llarp/router/rc_gossiper.cpp @@ -50,6 +50,22 @@ namespace llarp m_LastGossipedOurRC = 0s; } + TimePoint_t + RCGossiper::NextGossipAt() const + { + if (auto maybe = LastGossipAt()) + return *maybe + GossipOurRCInterval; + return DateClock_t::now(); + } + + std::optional + RCGossiper::LastGossipAt() const + { + if (m_LastGossipedOurRC == 0s) + return std::nullopt; + return DateClock_t::time_point{m_LastGossipedOurRC}; + } + bool RCGossiper::GossipRC(const RouterContact& rc) { diff --git a/llarp/router/rc_gossiper.hpp b/llarp/router/rc_gossiper.hpp index e3fb70d5b..948abad32 100644 --- a/llarp/router/rc_gossiper.hpp +++ b/llarp/router/rc_gossiper.hpp @@ -32,6 +32,12 @@ namespace llarp void Forget(const RouterID& router) override; + TimePoint_t + NextGossipAt() const override; + + std::optional + LastGossipAt() const override; + private: RouterID m_OurRouterID; Time_t m_LastGossipedOurRC = 0s; diff --git a/llarp/router/router.cpp b/llarp/router/router.cpp index 3e6c62f1e..43de835e5 100644 --- a/llarp/router/router.cpp +++ b/llarp/router/router.cpp @@ -869,7 +869,16 @@ namespace llarp ss << " snode | known/svc/clients: " << nodedb()->NumLoaded() << "/" << NumberOfConnectedRouters() << "/" << NumberOfConnectedClients() << " | " << pathContext().CurrentTransitPaths() << " active paths | " - << "block " << (m_lokidRpcClient ? m_lokidRpcClient->BlockHeight() : 0); + << "block " << (m_lokidRpcClient ? m_lokidRpcClient->BlockHeight() : 0) << " | gossip: " + << "(next/last) " << _rcGossiper.NextGossipAt() << "/"; + if (auto maybe = _rcGossiper.LastGossipAt()) + { + ss << *maybe; + } + else + { + ss << "never"; + } } else { diff --git a/llarp/util/time.cpp b/llarp/util/time.cpp index 4c9cde1f8..cd6a3b4fd 100644 --- a/llarp/util/time.cpp +++ b/llarp/util/time.cpp @@ -1,20 +1,24 @@ #include "time.hpp" #include +#include namespace llarp { - using Clock_t = std::chrono::system_clock; - - template - static Duration_t - time_since_epoch(std::chrono::time_point point) + namespace { - return std::chrono::duration_cast(point.time_since_epoch()); - } + using Clock_t = std::chrono::system_clock; + + template + static Duration_t + time_since_epoch(std::chrono::time_point point) + { + return std::chrono::duration_cast(point.time_since_epoch()); + } - const static auto started_at_system = Clock_t::now(); + const static auto started_at_system = Clock_t::now(); - const static auto started_at_steady = std::chrono::steady_clock::now(); + const static auto started_at_steady = std::chrono::steady_clock::now(); + } // namespace uint64_t ToMS(Duration_t ms) @@ -74,4 +78,11 @@ namespace llarp out.fill(old_fill); return out << "s"; } + + std::ostream& + operator<<(std::ostream& out, const TimePoint_t& tp) + { + auto t = TimePoint_t::clock::to_time_t(tp); + return out << std::put_time(std::localtime(&t), "%c %Z"); + } } // namespace llarp diff --git a/llarp/util/time.hpp b/llarp/util/time.hpp index d5f558f8c..0b3c20d3d 100644 --- a/llarp/util/time.hpp +++ b/llarp/util/time.hpp @@ -26,4 +26,7 @@ namespace llarp nlohmann::json to_json(const Duration_t& t); + std::ostream& + operator<<(std::ostream& out, const TimePoint_t& t); + } // namespace llarp diff --git a/llarp/util/types.hpp b/llarp/util/types.hpp index cd86b4d04..be6a0a286 100644 --- a/llarp/util/types.hpp +++ b/llarp/util/types.hpp @@ -14,6 +14,9 @@ namespace llarp /// convert to milliseconds uint64_t ToMS(Duration_t duration); + + using DateClock_t = std::chrono::system_clock; + using TimePoint_t = DateClock_t::time_point; } // namespace llarp using llarp_time_t = llarp::Duration_t;