From e5efe793ca622ad2088137c6ea792127b86fe734 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Thu, 3 Nov 2022 09:05:33 -0400 Subject: [PATCH 1/2] llarp::service::Endpoint::ReadyToDoLookup() previously we had a checking style function that passes in an optional defaulting to nullopt as a micro optimzation, this makes the code unnessarily obtuse. simplify this by splitting up into 2 functions, one for getting the unique endpoints and one for checking if the number of them is above the minimum. add overload for ReadyToDoLookup() that checks against constant but can do more in the future if desired to reduce the burden on future contributors. --- llarp/service/endpoint.cpp | 45 ++++++++++++++++++-------------------- llarp/service/endpoint.hpp | 21 +++++++++++++----- 2 files changed, 37 insertions(+), 29 deletions(-) diff --git a/llarp/service/endpoint.cpp b/llarp/service/endpoint.cpp index d835f816d..14fc2adfb 100644 --- a/llarp/service/endpoint.cpp +++ b/llarp/service/endpoint.cpp @@ -40,16 +40,10 @@ #include #include -namespace -{ - constexpr size_t MIN_ENDPOINTS_FOR_LNS_LOOKUP = 2; -} // namespace - namespace llarp { namespace service { - static auto logcat = log::Cat("endpoint"); Endpoint::Endpoint(AbstractRouter* r, Context* parent) @@ -317,7 +311,7 @@ namespace llarp auto obj = path::Builder::ExtractStatus(); obj["exitMap"] = m_ExitMap.ExtractStatus(); obj["identity"] = m_Identity.pub.Addr().ToString(); - obj["networkReady"] = ReadyToDoLookup(); + obj["networkReady"] = ReadyForNetwork(); util::StatusObject authCodes; for (const auto& [service, info] : m_RemoteAuthInfos) @@ -958,20 +952,28 @@ namespace llarp return not m_ExitMap.Empty(); } + path::Path::UniqueEndpointSet_t + Endpoint::GetUniqueEndpointsForLookup() const + { + path::Path::UniqueEndpointSet_t paths; + ForEachPath([&paths](auto path) { + if (path and path->IsReady()) + paths.insert(path); + }); + return paths; + } + bool - Endpoint::ReadyToDoLookup(std::optional numPaths) const + Endpoint::ReadyForNetwork() const { - if (not numPaths) - { - path::Path::UniqueEndpointSet_t paths; - ForEachPath([&paths](auto path) { - if (path and path->IsReady()) - paths.insert(path); - }); - numPaths = paths.size(); - } + return IsReady() and ReadyToDoLookup(GetUniqueEndpointsForLookup().size()); + } - return numPaths >= MIN_ENDPOINTS_FOR_LNS_LOOKUP; + bool + Endpoint::ReadyToDoLookup(size_t num_paths) const + { + // Currently just checks the number of paths, but could do more checks in the future. (jason) + return num_paths >= MIN_ENDPOINTS_FOR_LNS_LOOKUP; } void @@ -992,12 +994,7 @@ namespace llarp return; } LogInfo(Name(), " looking up LNS name: ", name); - path::Path::UniqueEndpointSet_t paths; - ForEachPath([&paths](auto path) { - if (path and path->IsReady()) - paths.insert(path); - }); - + auto paths = GetUniqueEndpointsForLookup(); // not enough paths if (not ReadyToDoLookup(paths.size())) { diff --git a/llarp/service/endpoint.hpp b/llarp/service/endpoint.hpp index 09b960957..dd61c74b9 100644 --- a/llarp/service/endpoint.hpp +++ b/llarp/service/endpoint.hpp @@ -48,13 +48,16 @@ namespace llarp struct OutboundContext; /// minimum interval for publishing introsets - static constexpr auto IntrosetPublishInterval = path::intro_path_spread / 2; + inline constexpr auto IntrosetPublishInterval = path::intro_path_spread / 2; /// how agressively should we retry publishing introset on failure - static constexpr auto IntrosetPublishRetryCooldown = 1s; + inline constexpr auto IntrosetPublishRetryCooldown = 1s; /// how aggressively should we retry looking up introsets - static constexpr auto IntrosetLookupCooldown = 250ms; + inline constexpr auto IntrosetLookupCooldown = 250ms; + + /// number of unique snodes we want to talk to do to ons lookups + inline constexpr size_t MIN_ENDPOINTS_FOR_LNS_LOOKUP = 2; struct Endpoint : public path::Builder, public ILookupHolder, @@ -64,7 +67,9 @@ namespace llarp Endpoint(AbstractRouter* r, Context* parent); ~Endpoint() override; - /// return true if we are ready to recv packets from the void + /// return true if we are ready to recv packets from the void. + /// really should be ReadyForInboundTraffic() but the diff is HUGE and we need to rewrite this + /// component anyways. bool IsReady() const; @@ -521,10 +526,16 @@ namespace llarp return false; } + /// return true if we are ready to do outbound and inbound traffic bool - ReadyToDoLookup(std::optional numPaths = std::nullopt) const; + ReadyForNetwork() const; protected: + bool + ReadyToDoLookup(size_t num_paths) const; + path::Path::UniqueEndpointSet_t + GetUniqueEndpointsForLookup() const; + IDataHandler* m_DataHandler = nullptr; Identity m_Identity; net::IPRangeMap m_ExitMap; From 29da2a9943283f28d51b5140924355464a386a52 Mon Sep 17 00:00:00 2001 From: majestrate Date: Thu, 3 Nov 2022 10:49:57 -0400 Subject: [PATCH 2/2] Update llarp/service/endpoint.cpp Co-authored-by: Jason Rhinelander --- llarp/service/endpoint.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llarp/service/endpoint.cpp b/llarp/service/endpoint.cpp index 14fc2adfb..24839ff4d 100644 --- a/llarp/service/endpoint.cpp +++ b/llarp/service/endpoint.cpp @@ -972,7 +972,7 @@ namespace llarp bool Endpoint::ReadyToDoLookup(size_t num_paths) const { - // Currently just checks the number of paths, but could do more checks in the future. (jason) + // Currently just checks the number of paths, but could do more checks in the future. return num_paths >= MIN_ENDPOINTS_FOR_LNS_LOOKUP; }