From c8c66f0a5f681da009e4bd91cd7d823bfd17732e Mon Sep 17 00:00:00 2001 From: Thomas Winget Date: Tue, 3 Mar 2020 14:56:33 -0500 Subject: [PATCH] some refactoring of tooling code, added RCGossipReceivedEvent --- llarp/CMakeLists.txt | 4 +- llarp/dht/messages/gotrouter.cpp | 8 ++- llarp/messages/relay_commit.cpp | 2 +- llarp/messages/relay_status.cpp | 2 +- llarp/path/pathbuilder.cpp | 2 +- llarp/router/rc_gossiper.cpp | 4 +- llarp/router/router.cpp | 4 ++ llarp/router_contact.hpp | 6 ++ llarp/tooling/path_event.cpp | 94 +++++++++++++++++++++++++++ llarp/tooling/path_event.hpp | 45 +++++++++++++ llarp/tooling/rc_event.cpp | 24 +++++++ llarp/tooling/rc_event.hpp | 21 ++++++ llarp/tooling/router_event.cpp | 88 ------------------------- llarp/tooling/router_event.hpp | 39 ----------- pybind/llarp/router_contact.cpp | 6 +- pybind/llarp/tooling/router_event.cpp | 6 ++ test/CMakeLists.txt | 3 +- test/hive/conftest.py | 25 ++++++- test/hive/hive.py | 10 +-- test/hive/test_path_builds.py | 6 +- 20 files changed, 252 insertions(+), 147 deletions(-) create mode 100644 llarp/tooling/path_event.cpp create mode 100644 llarp/tooling/path_event.hpp create mode 100644 llarp/tooling/rc_event.cpp create mode 100644 llarp/tooling/rc_event.hpp diff --git a/llarp/CMakeLists.txt b/llarp/CMakeLists.txt index 27b6e5e8c..3a45eef4b 100644 --- a/llarp/CMakeLists.txt +++ b/llarp/CMakeLists.txt @@ -198,8 +198,10 @@ set(LIB_SRC service/session.cpp service/tag_lookup_job.cpp service/tag.cpp - tooling/dht_event.cpp tooling/router_event.cpp + tooling/dht_event.cpp + tooling/path_event.cpp + tooling/rc_event.cpp ) if(TRACY_ROOT) diff --git a/llarp/dht/messages/gotrouter.cpp b/llarp/dht/messages/gotrouter.cpp index 03a1143b0..07e98ce04 100644 --- a/llarp/dht/messages/gotrouter.cpp +++ b/llarp/dht/messages/gotrouter.cpp @@ -5,6 +5,7 @@ #include #include #include +#include namespace llarp { @@ -124,9 +125,12 @@ namespace llarp { if(not dht.GetRouter()->rcLookupHandler().CheckRC(rc)) return false; - if(txid == 0) + if(txid == 0) // txid == 0 on gossip { - dht.GetRouter()->GossipRCIfNeeded(rc); + auto *router = dht.GetRouter(); + tooling::RouterEventPtr event = std::make_unique(router->pubkey(), rc); + router->NotifyRouterEvent(std::move(event)); + router->GossipRCIfNeeded(rc); } } return true; diff --git a/llarp/messages/relay_commit.cpp b/llarp/messages/relay_commit.cpp index 26fb574fd..5685687fb 100644 --- a/llarp/messages/relay_commit.cpp +++ b/llarp/messages/relay_commit.cpp @@ -13,7 +13,7 @@ #include #include #include -#include +#include #include #include diff --git a/llarp/messages/relay_status.cpp b/llarp/messages/relay_status.cpp index 7d839707e..829085ace 100644 --- a/llarp/messages/relay_status.cpp +++ b/llarp/messages/relay_status.cpp @@ -10,7 +10,7 @@ #include #include #include -#include +#include #include #include diff --git a/llarp/path/pathbuilder.cpp b/llarp/path/pathbuilder.cpp index 78849d8ac..d98fb9ca0 100644 --- a/llarp/path/pathbuilder.cpp +++ b/llarp/path/pathbuilder.cpp @@ -8,7 +8,7 @@ #include #include #include -#include +#include #include diff --git a/llarp/router/rc_gossiper.cpp b/llarp/router/rc_gossiper.cpp index 8930ebb06..8e2e17c67 100644 --- a/llarp/router/rc_gossiper.cpp +++ b/llarp/router/rc_gossiper.cpp @@ -29,7 +29,9 @@ namespace llarp bool RCGossiper::ShouldGossipOurRC(Time_t now) const { - return now >= (m_LastGossipedOurRC + GossipOurRCInterval); + bool should = now >= (m_LastGossipedOurRC + GossipOurRCInterval); + LogWarn("ShouldGossipOurRC: ", should); + return should; } bool diff --git a/llarp/router/router.cpp b/llarp/router/router.cpp index 8c7e443df..2feddad80 100644 --- a/llarp/router/router.cpp +++ b/llarp/router/router.cpp @@ -58,7 +58,11 @@ namespace llarp , _dht(llarp_dht_context_new(this)) , inbound_link_msg_parser(this) , _hiddenServiceContext(this) +#ifdef LOKINET_HIVE + , _randomStartDelay(std::chrono::milliseconds((llarp::randint() % 1250) + 1000)) +#else , _randomStartDelay(std::chrono::seconds((llarp::randint() % 30) + 10)) +#endif { m_keyManager = std::make_shared< KeyManager >(); diff --git a/llarp/router_contact.hpp b/llarp/router_contact.hpp index 442394032..475c473ce 100644 --- a/llarp/router_contact.hpp +++ b/llarp/router_contact.hpp @@ -116,6 +116,12 @@ namespace llarp return ExtractStatus(); } + std::string + ToString() const + { + return ToJson().dump(); + } + bool BEncode(llarp_buffer_t *buf) const; diff --git a/llarp/tooling/path_event.cpp b/llarp/tooling/path_event.cpp new file mode 100644 index 000000000..73c75b143 --- /dev/null +++ b/llarp/tooling/path_event.cpp @@ -0,0 +1,94 @@ +#include + +#include +#include + +namespace tooling +{ + + PathAttemptEvent::PathAttemptEvent(const llarp::RouterID& routerID, std::shared_ptr path) + : RouterEvent("PathAttemptEvent", routerID, false) + , hops(path->hops) + , pathid(path->hops[0].rxID) + { + } + + std::string + PathAttemptEvent::ToString() const + { + std::string result = RouterEvent::ToString(); + result += "---- ["; + + size_t i = 0; + for (const auto& hop : hops) + { + i++; + + result += llarp::RouterID(hop.rc.pubkey).ShortString(); + result += "]"; + + if (i != hops.size()) + { + result += " -> ["; + } + } + + return result; + } + + + PathRequestReceivedEvent::PathRequestReceivedEvent(const llarp::RouterID& routerID, std::shared_ptr hop) + : RouterEvent("PathRequestReceivedEvent", routerID, true) + , prevHop(hop->info.downstream) + , nextHop(hop->info.upstream) + , txid(hop->info.txID) + , rxid(hop->info.rxID) + { + isEndpoint = false; + if (routerID == nextHop) + { + isEndpoint = true; + } + } + + std::string + PathRequestReceivedEvent::ToString() const + { + std::string result = RouterEvent::ToString(); + result += "---- ["; + result += prevHop.ShortString(); + result += "] -> [*"; + result += routerID.ShortString(); + result += "] -> ["; + + if (isEndpoint) + { + result += "nowhere]"; + } + else + { + result += nextHop.ShortString(); + result += "]"; + } + + return result; + } + + PathStatusReceivedEvent::PathStatusReceivedEvent(const llarp::RouterID& routerID, const llarp::PathID_t rxid, uint64_t status) + : RouterEvent("PathStatusReceivedEvent", routerID, true) + , rxid(rxid) + , status(status) + { + } + + std::string + PathStatusReceivedEvent::ToString() const + { + std::string result = RouterEvent::ToString(); + result += "---- path rxid: " + rxid.ShortHex(); + result += ", status: " + std::to_string(status); + + return result; + } + +} // namespace tooling diff --git a/llarp/tooling/path_event.hpp b/llarp/tooling/path_event.hpp new file mode 100644 index 000000000..d4ce30272 --- /dev/null +++ b/llarp/tooling/path_event.hpp @@ -0,0 +1,45 @@ +#include "router_event.hpp" + +#include + +namespace tooling +{ + + struct PathAttemptEvent : public RouterEvent + { + PathAttemptEvent(const llarp::RouterID& routerID, std::shared_ptr path); + + std::string ToString() const override; + + std::vector hops; + + llarp::PathID_t pathid; + }; + + struct PathRequestReceivedEvent : public RouterEvent + { + PathRequestReceivedEvent(const llarp::RouterID& routerID, std::shared_ptr hop); + + std::string ToString() const override; + + llarp::RouterID prevHop; + llarp::RouterID nextHop; + + llarp::PathID_t txid; + llarp::PathID_t rxid; + + bool isEndpoint = false; + }; + + struct PathStatusReceivedEvent : public RouterEvent + { + PathStatusReceivedEvent(const llarp::RouterID& routerID, const llarp::PathID_t rxid, uint64_t status); + + std::string ToString() const override; + + llarp::PathID_t rxid; + + uint64_t status; + }; + +} // namespace tooling diff --git a/llarp/tooling/rc_event.cpp b/llarp/tooling/rc_event.cpp new file mode 100644 index 000000000..5907cf333 --- /dev/null +++ b/llarp/tooling/rc_event.cpp @@ -0,0 +1,24 @@ +#include + +namespace tooling +{ + RCGossipReceivedEvent::RCGossipReceivedEvent(const llarp::RouterID& routerID, const llarp::RouterContact& rc) + : RouterEvent("RCGossipReceivedEvent", routerID, true) + , rc(rc) + { + } + + std::string + RCGossipReceivedEvent::ToString() const + { + return RouterEvent::ToString() + " ---- other RouterID: " + llarp::RouterID(rc.pubkey).ShortString(); + } + + std::string + RCGossipReceivedEvent::LongString() const + { + return RouterEvent::ToString() + " ---- RC: " + rc.ToString(); + } + +} // namespace tooling + diff --git a/llarp/tooling/rc_event.hpp b/llarp/tooling/rc_event.hpp new file mode 100644 index 000000000..532b5df53 --- /dev/null +++ b/llarp/tooling/rc_event.hpp @@ -0,0 +1,21 @@ +#include + +#include + +namespace tooling +{ + struct RCGossipReceivedEvent : public RouterEvent + { + RCGossipReceivedEvent(const llarp::RouterID& routerID, const llarp::RouterContact& rc); + + std::string + ToString() const override; + + std::string + LongString() const; + + llarp::RouterContact rc; + }; + +} // namespace tooling + diff --git a/llarp/tooling/router_event.cpp b/llarp/tooling/router_event.cpp index 4f6144a95..ff74c68bb 100644 --- a/llarp/tooling/router_event.cpp +++ b/llarp/tooling/router_event.cpp @@ -1,8 +1,5 @@ #include -#include -#include - namespace tooling { @@ -22,89 +19,4 @@ namespace tooling return result; } - PathAttemptEvent::PathAttemptEvent(const llarp::RouterID& routerID, std::shared_ptr path) - : RouterEvent("PathAttemptEvent", routerID, false) - , hops(path->hops) - , pathid(path->hops[0].rxID) - { - } - - std::string - PathAttemptEvent::ToString() const - { - std::string result = RouterEvent::ToString(); - result += "---- ["; - - size_t i = 0; - for (const auto& hop : hops) - { - i++; - - result += llarp::RouterID(hop.rc.pubkey).ShortString(); - result += "]"; - - if (i != hops.size()) - { - result += " -> ["; - } - } - - return result; - } - - - PathRequestReceivedEvent::PathRequestReceivedEvent(const llarp::RouterID& routerID, std::shared_ptr hop) - : RouterEvent("PathRequestReceivedEvent", routerID, true) - , prevHop(hop->info.downstream) - , nextHop(hop->info.upstream) - , txid(hop->info.txID) - , rxid(hop->info.rxID) - { - isEndpoint = false; - if (routerID == nextHop) - { - isEndpoint = true; - } - } - - std::string - PathRequestReceivedEvent::ToString() const - { - std::string result = RouterEvent::ToString(); - result += "---- ["; - result += prevHop.ShortString(); - result += "] -> [*"; - result += routerID.ShortString(); - result += "] -> ["; - - if (isEndpoint) - { - result += "nowhere]"; - } - else - { - result += nextHop.ShortString(); - result += "]"; - } - - return result; - } - - PathStatusReceivedEvent::PathStatusReceivedEvent(const llarp::RouterID& routerID, const llarp::PathID_t rxid, uint64_t status) - : RouterEvent("PathStatusReceivedEvent", routerID, true) - , rxid(rxid) - , status(status) - { - } - - std::string - PathStatusReceivedEvent::ToString() const - { - std::string result = RouterEvent::ToString(); - result += "---- path rxid: " + rxid.ShortHex(); - result += ", status: " + std::to_string(status); - - return result; - } - } // namespace tooling diff --git a/llarp/tooling/router_event.hpp b/llarp/tooling/router_event.hpp index 175a67e22..a72b59132 100644 --- a/llarp/tooling/router_event.hpp +++ b/llarp/tooling/router_event.hpp @@ -1,7 +1,6 @@ #pragma once #include -#include #include #include @@ -46,42 +45,4 @@ namespace tooling using RouterEventPtr = std::unique_ptr; - - struct PathAttemptEvent : public RouterEvent - { - PathAttemptEvent(const llarp::RouterID& routerID, std::shared_ptr path); - - std::string ToString() const override; - - std::vector hops; - - llarp::PathID_t pathid; - }; - - struct PathRequestReceivedEvent : public RouterEvent - { - PathRequestReceivedEvent(const llarp::RouterID& routerID, std::shared_ptr hop); - - std::string ToString() const override; - - llarp::RouterID prevHop; - llarp::RouterID nextHop; - - llarp::PathID_t txid; - llarp::PathID_t rxid; - - bool isEndpoint = false; - }; - - struct PathStatusReceivedEvent : public RouterEvent - { - PathStatusReceivedEvent(const llarp::RouterID& routerID, const llarp::PathID_t rxid, uint64_t status); - - std::string ToString() const override; - - llarp::PathID_t rxid; - - uint64_t status; - }; - } // namespace tooling diff --git a/pybind/llarp/router_contact.cpp b/pybind/llarp/router_contact.cpp index 833a0c0b2..366e7f6b9 100644 --- a/pybind/llarp/router_contact.cpp +++ b/pybind/llarp/router_contact.cpp @@ -13,9 +13,9 @@ namespace llarp }) .def("ReadFile", &RouterContact::Read) .def("WriteFile", &RouterContact::Write) - .def("ToString", [](const RouterContact* const rc) -> std::string { - return rc->ToJson().dump(); - }) + .def("ToString", &RouterContact::ToString) + .def("__str__", &RouterContact::ToString) + .def("__repr__", &RouterContact::ToString) .def("Verify", [](const RouterContact* const rc) -> bool { const llarp_time_t now = llarp::time_now_ms(); return rc->Verify(now); diff --git a/pybind/llarp/tooling/router_event.cpp b/pybind/llarp/tooling/router_event.cpp index 3dd529ef9..dc3a4a167 100644 --- a/pybind/llarp/tooling/router_event.cpp +++ b/pybind/llarp/tooling/router_event.cpp @@ -3,6 +3,8 @@ #include "tooling/router_event.hpp" #include "tooling/dht_event.hpp" +#include "tooling/path_event.hpp" +#include "tooling/rc_event.hpp" #include #include @@ -46,6 +48,10 @@ namespace tooling .def_readonly("location", &GotIntroReceivedEvent::Introset) .def_readonly("relayOrder", &GotIntroReceivedEvent::RelayOrder) .def_readonly("txid", &GotIntroReceivedEvent::TxID); + + py::class_(mod, "RCGossipReceivedEvent") + .def_readonly("rc", &RCGossipReceivedEvent::rc) + .def("LongString", &RCGossipReceivedEvent::LongString); } } // namespace tooling diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 29e35b089..0a6db0109 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,10 +1,11 @@ if (WITH_HIVE) + add_custom_target(hive_build DEPENDS ${STATIC_LIB} pyllarp) add_custom_target(hive ${CMAKE_COMMAND} -E env PYTHONPATH="$ENV{PYTHONPATH}:${CMAKE_BINARY_DIR}/pybind" ${PYTHON_EXECUTABLE} -m pytest ${CMAKE_CURRENT_SOURCE_DIR}/hive DEPENDS - ${STATIC_LIB} pyllarp) + hive_build) endif() set(GTEST_EXE testAll) diff --git a/test/hive/conftest.py b/test/hive/conftest.py index 52f03a611..e44427ee8 100644 --- a/test/hive/conftest.py +++ b/test/hive/conftest.py @@ -3,10 +3,33 @@ import hive import pytest @pytest.fixture(scope="session") -def HiveTenTen(): +def HiveTenRTenC(): router_hive = hive.RouterHive(n_relays=10, n_clients=10, netid="hive") router_hive.Start() yield router_hive router_hive.Stop() + +@pytest.fixture(scope="session") +def HiveThirtyRTenC(): + router_hive = hive.RouterHive(n_relays=30, n_clients=10, netid="hive") + router_hive.Start() + + yield router_hive + + router_hive.Stop() + +@pytest.fixture() +def HiveArbitrary(): + router_hive = None + def _make(n_relays=10, n_clients=10, netid="hive"): + nonlocal router_hive + router_hive = hive.RouterHive(n_relays=30, n_clients=10, netid="hive") + router_hive.Start() + return router_hive + + yield _make + + router_hive.Stop() + diff --git a/test/hive/hive.py b/test/hive/hive.py index 5efed44c8..7b64600d7 100644 --- a/test/hive/hive.py +++ b/test/hive/hive.py @@ -161,15 +161,15 @@ class RouterHive(object): print("Starting relays") self.hive.StartRelays() - sleep(1) + sleep(0.2) self.hive.ForEachRelay(lambda r: self.MakeEndpoint(r, self.onGotEndpoint)) - print("Sleeping 5 seconds before starting clients") - sleep(5) + print("Sleeping 1 seconds before starting clients") + sleep(1) self.hive.StartClients() - sleep(1) + sleep(0.2) self.hive.ForEachClient(lambda r: self.MakeEndpoint(r, self.onGotEndpoint)) def Stop(self): @@ -241,4 +241,4 @@ if __name__ == '__main__': print_events = False parser.add_argument('--print-events', dest="print_events", action='store_true') args = parser.parse_args() - main(print_each_event = args.print_events) + main(n_relays=30, print_each_event = args.print_events) diff --git a/test/hive/test_path_builds.py b/test/hive/test_path_builds.py index 677f46303..74cd73a45 100644 --- a/test/hive/test_path_builds.py +++ b/test/hive/test_path_builds.py @@ -1,11 +1,11 @@ from time import time -def test_path_builds(HiveTenTen): - h = HiveTenTen +def test_path_builds(HiveArbitrary): + h = HiveArbitrary(n_relays=30, n_clients=10) start_time = time() cur_time = start_time - test_duration = 10 #seconds + test_duration = 5 #seconds log_attempts = True