* get rid of dht explore for service nodes

* add Time_t using std::chrono for future uses
* make decaying hashset constructor with llarp_time_t explicit
* add decaying hashset implicit constructor using Time_t
* add timeouts for gossiper replay
* allow regossip of our RC
pull/1073/head
Jeff Becker 4 years ago
parent ea3851d15f
commit 7ad47f2dba
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -14,7 +14,15 @@ namespace llarp
GossipRC(const RouterContact &rc) = 0; GossipRC(const RouterContact &rc) = 0;
virtual void virtual void
Decay(llarp_time_t now) = 0; Decay(Time_t now) = 0;
/// return true if we should gossip our RC now
virtual bool
ShouldGossipOurRC(Time_t now) const = 0;
/// return true if that rc is owned by us
virtual bool
IsOurRC(const RouterContact &rc) const = 0;
}; };
} // namespace llarp } // namespace llarp

@ -1,24 +1,47 @@
#include <router/rc_gossiper.hpp> #include <router/rc_gossiper.hpp>
#include <messages/dht_immediate.hpp> #include <messages/dht_immediate.hpp>
#include <dht/messages/gotrouter.hpp> #include <dht/messages/gotrouter.hpp>
#include <util/time.hpp>
namespace llarp namespace llarp
{ {
// 30 minutes
static constexpr auto RCGossipFilterDecayInterval = 30min;
// 60 - 5 minutes
static constexpr auto GossipOurRCInterval =
(RCGossipFilterDecayInterval * 2) - (5min);
RCGossiper::RCGossiper() RCGossiper::RCGossiper()
: I_RCGossiper(), m_Filter(RouterContact::UpdateInterval) : I_RCGossiper()
, m_Filter(
std::chrono::duration_cast< Time_t >(RCGossipFilterDecayInterval))
{ {
} }
void void
RCGossiper::Init(ILinkManager* l) RCGossiper::Init(ILinkManager* l, const RouterID& ourID)
{ {
m_OurRouterID = ourID;
m_LinkManager = l; m_LinkManager = l;
} }
bool
RCGossiper::ShouldGossipOurRC(Time_t now) const
{
return now >= (m_LastGossipedOurRC + GossipOurRCInterval);
}
bool
RCGossiper::IsOurRC(const RouterContact& rc) const
{
return rc.pubkey == m_OurRouterID;
}
void void
RCGossiper::Decay(llarp_time_t now) RCGossiper::Decay(Time_t now)
{ {
m_Filter.Decay(now); LogDebug("decay filter at ", now.count());
m_Filter.Decay(now.count());
} }
bool bool
@ -29,15 +52,31 @@ namespace llarp
return false; return false;
if(m_LinkManager == nullptr) if(m_LinkManager == nullptr)
return false; return false;
// check for filter hit const RouterID k(rc.pubkey);
if(not m_Filter.Insert(rc.pubkey)) // filter check
if(m_Filter.Contains(k))
return false; return false;
bool sent = false; m_Filter.Insert(k);
// unwarrented GRCM
const auto now = time_now();
// is this our rc?
if(IsOurRC(rc))
{
// should we gossip our rc?
if(not ShouldGossipOurRC(now))
{
// nah drop it
return false;
}
// ya pop it
m_LastGossipedOurRC = now;
}
// send unwarrented GRCM as gossip method
DHTImmediateMessage m; DHTImmediateMessage m;
m.msgs.emplace_back( m.msgs.emplace_back(
new dht::GotRouterMessage(dht::Key_t{}, 0, {rc}, false)); new dht::GotRouterMessage(dht::Key_t{}, 0, {rc}, false));
// send it to everyone
m_LinkManager->ForEachPeer([&](ILinkSession* s) { m_LinkManager->ForEachPeer([&](ILinkSession* s) {
// ensure connected session // ensure connected session
if(not(s && s->IsEstablished())) if(not(s && s->IsEstablished()))
@ -46,23 +85,17 @@ namespace llarp
const auto other_rc = s->GetRemoteRC(); const auto other_rc = s->GetRemoteRC();
if(not other_rc.IsPublicRouter()) if(not other_rc.IsPublicRouter())
return; return;
// dont send it to the owner
if(other_rc.pubkey == rc.pubkey)
return;
// encode message // encode message
ILinkSession::Message_t msg; ILinkSession::Message_t msg;
msg.resize(1024); msg.reserve(1024);
llarp_buffer_t buf(msg); llarp_buffer_t buf(msg);
if(not m.BEncode(&buf)) if(not m.BEncode(&buf))
return; return;
msg.resize(buf.cur - buf.base); msg.resize(buf.cur - buf.base);
// send message // send message
if(s->SendMessageBuffer(std::move(msg), nullptr)) s->SendMessageBuffer(std::move(msg), nullptr);
{
sent = true;
}
}); });
return sent; return true;
} }
} // namespace llarp } // namespace llarp

@ -18,12 +18,20 @@ namespace llarp
GossipRC(const RouterContact &rc) override; GossipRC(const RouterContact &rc) override;
void void
Decay(llarp_time_t now) override; Decay(Time_t now) override;
bool
ShouldGossipOurRC(Time_t now) const override;
bool
IsOurRC(const RouterContact &rc) const override;
void void
Init(ILinkManager *); Init(ILinkManager *, const RouterID &);
private: private:
RouterID m_OurRouterID;
Time_t m_LastGossipedOurRC = 0s;
ILinkManager *m_LinkManager = nullptr; ILinkManager *m_LinkManager = nullptr;
util::DecayingHashSet< RouterID > m_Filter; util::DecayingHashSet< RouterID > m_Filter;
}; };

@ -288,7 +288,10 @@ namespace llarp
GetRC(r, nullptr, true); GetRC(r, nullptr, true);
return; return;
} }
// TODO: only explore via random subset // service nodes gossip, not explore
if(_dht->impl->GetRouter()->IsServiceNode())
return;
// explore via every connected peer // explore via every connected peer
_linkManager->ForEachPeer([&](ILinkSession *s) { _linkManager->ForEachPeer([&](ILinkSession *s) {
if(!s->IsEstablished()) if(!s->IsEstablished())

@ -518,7 +518,6 @@ namespace llarp
_rcLookupHandler.Init(_dht, _nodedb, threadpool(), &_linkManager, _rcLookupHandler.Init(_dht, _nodedb, threadpool(), &_linkManager,
&_hiddenServiceContext, strictConnectPubkeys, &_hiddenServiceContext, strictConnectPubkeys,
bootstrapRCList, whitelistRouters, m_isServiceNode); bootstrapRCList, whitelistRouters, m_isServiceNode);
_rcGossiper.Init(&_linkManager);
if(!usingSNSeed) if(!usingSNSeed)
{ {
@ -687,7 +686,7 @@ namespace llarp
ReportStats(); ReportStats();
} }
_rcGossiper.Decay(now); _rcGossiper.Decay(time_now());
_rcLookupHandler.PeriodicUpdate(now); _rcLookupHandler.PeriodicUpdate(now);
@ -1012,9 +1011,10 @@ namespace llarp
LogError("Failed to initialize service node"); LogError("Failed to initialize service node");
return false; return false;
} }
RouterID us = pubkey(); const RouterID us = pubkey();
LogInfo("initalized service node: ", us); LogInfo("initalized service node: ", us);
// init gossiper here
_rcGossiper.Init(&_linkManager, us);
// relays do not use profiling // relays do not use profiling
routerProfiling().Disable(); routerProfiling().Disable();
} }

@ -11,11 +11,11 @@ namespace llarp
template < typename Val_t, typename Hash_t = typename Val_t::Hash > template < typename Val_t, typename Hash_t = typename Val_t::Hash >
struct DecayingHashSet struct DecayingHashSet
{ {
DecayingHashSet(std::chrono::milliseconds cacheInterval) DecayingHashSet(Time_t cacheInterval)
: DecayingHashSet(cacheInterval.count()) : DecayingHashSet(cacheInterval.count())
{ {
} }
DecayingHashSet(llarp_time_t cacheInterval = 5000) explicit DecayingHashSet(llarp_time_t cacheInterval = 5000)
: m_CacheInterval(cacheInterval) : m_CacheInterval(cacheInterval)
{ {
} }

@ -29,6 +29,12 @@ namespace llarp
- started_at_steady; - started_at_steady;
} }
Time_t
time_now()
{
return Time_t(time_now_ms());
}
llarp_time_t llarp_time_t
time_now_ms() time_now_ms()
{ {

@ -4,11 +4,21 @@
#include <util/types.hpp> #include <util/types.hpp>
#include <chrono> #include <chrono>
#include <chrono>
using namespace std::chrono_literals;
namespace llarp namespace llarp
{ {
/// get time right now as milliseconds, this is monotonic /// get time right now as milliseconds, this is monotonic
llarp_time_t llarp_time_t
time_now_ms(); time_now_ms();
using Time_t = std::chrono::milliseconds;
/// get time right now as a Time_t, monotonic
Time_t
time_now();
} // namespace llarp } // namespace llarp
#endif #endif

Loading…
Cancel
Save