From b2a72dd46a9fc026bb642693eec7eb858ba1180b Mon Sep 17 00:00:00 2001 From: Stephen Shelton Date: Thu, 11 Jun 2020 13:02:34 -0600 Subject: [PATCH] Initial test_peer_stats hive test --- llarp/iwp/session.cpp | 4 ++-- llarp/link/server.hpp | 2 +- llarp/router/router.cpp | 6 ++++-- llarp/router/router.hpp | 2 +- pybind/llarp/tooling/router_event.cpp | 5 +++++ test/hive/conftest.py | 12 ++++++++++++ 6 files changed, 25 insertions(+), 6 deletions(-) diff --git a/llarp/iwp/session.cpp b/llarp/iwp/session.cpp index 82c659354..06ba7dc41 100644 --- a/llarp/iwp/session.cpp +++ b/llarp/iwp/session.cpp @@ -76,7 +76,7 @@ namespace llarp GotLIM = util::memFn(&Session::GotRenegLIM, this); m_RemoteRC = msg->rc; m_Parent->MapAddr(m_RemoteRC.pubkey, this); - return m_Parent->SessionEstablished(this); + return m_Parent->SessionEstablished(this, true); } bool @@ -96,7 +96,7 @@ namespace llarp { self->m_State = State::Ready; self->m_Parent->MapAddr(self->m_RemoteRC.pubkey, self.get()); - self->m_Parent->SessionEstablished(self.get()); + self->m_Parent->SessionEstablished(self.get(), false); } }); return true; diff --git a/llarp/link/server.hpp b/llarp/link/server.hpp index ea8e30f78..55df4fa6f 100644 --- a/llarp/link/server.hpp +++ b/llarp/link/server.hpp @@ -43,7 +43,7 @@ namespace llarp /// return true to accept /// /// currently called in iwp::Session when a valid LIM is received. - using SessionEstablishedHandler = std::function; + using SessionEstablishedHandler = std::function; /// f(new, old) /// handler of session renegotiation diff --git a/llarp/router/router.cpp b/llarp/router/router.cpp index 4fc8599a0..0a1b5c667 100644 --- a/llarp/router/router.cpp +++ b/llarp/router/router.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include "tooling/router_event.hpp" #include "util/status.hpp" @@ -837,14 +838,15 @@ namespace llarp } bool - Router::ConnectionEstablished(ILinkSession* session) + Router::ConnectionEstablished(ILinkSession* session, bool inbound) { + RouterID id{session->GetPubKey()}; if (m_peerDb) { - RouterID id{session->GetPubKey()}; // TODO: make sure this is a public router (on whitelist)? m_peerDb->modifyPeerStats(id, [&](PeerStats& stats) { stats.numConnectionSuccesses++; }); } + NotifyRouterEvent(pubkey(), id, inbound); return _outboundSessionMaker.OnSessionEstablished(session); } diff --git a/llarp/router/router.hpp b/llarp/router/router.hpp index 5f99540bb..1be5a99c7 100644 --- a/llarp/router/router.hpp +++ b/llarp/router/router.hpp @@ -440,7 +440,7 @@ namespace llarp /// called by link when session is fully established bool - ConnectionEstablished(ILinkSession* session); + ConnectionEstablished(ILinkSession* session, bool inbound); /// call internal router ticker void diff --git a/pybind/llarp/tooling/router_event.cpp b/pybind/llarp/tooling/router_event.cpp index 8098d4283..906469044 100644 --- a/pybind/llarp/tooling/router_event.cpp +++ b/pybind/llarp/tooling/router_event.cpp @@ -5,6 +5,7 @@ #include "tooling/dht_event.hpp" #include "tooling/path_event.hpp" #include "tooling/rc_event.hpp" +#include "tooling/peer_stats_event.hpp" #include #include @@ -73,6 +74,10 @@ namespace tooling mod, "FindRouterReceivedEvent"); py::class_(mod, "FindRouterSentEvent"); + + py::class_(mod, "LinkSessionEstablishedEvent") + .def_readonly("remoteId", &LinkSessionEstablishedEvent::remoteId) + .def_readonly("inbound", &LinkSessionEstablishedEvent::inbound); } } // namespace tooling diff --git a/test/hive/conftest.py b/test/hive/conftest.py index e44427ee8..371f62740 100644 --- a/test/hive/conftest.py +++ b/test/hive/conftest.py @@ -33,3 +33,15 @@ def HiveArbitrary(): router_hive.Stop() +@pytest.fixture() +def HiveForPeerStats(): + router_hive = None + def _make(n_relays, n_clients, netid): + nonlocal router_hive + router_hive = hive.RouterHive(n_relays, n_clients, netid) + router_hive.Start() + return router_hive + + yield _make + + router_hive.Stop()