From 22dabfd79ec14a14bae53e481ee437def8e09f87 Mon Sep 17 00:00:00 2001 From: orignal Date: Wed, 15 May 2024 13:31:31 -0400 Subject: [PATCH] use unordered_map for excluded routers. don't request to self --- libi2pd/Destination.cpp | 2 +- libi2pd/Destination.h | 6 +++--- libi2pd/I2NPProtocol.cpp | 4 ++-- libi2pd/I2NPProtocol.h | 6 +++--- libi2pd/NetDb.cpp | 18 +++++++++--------- libi2pd/NetDb.hpp | 17 ++++++----------- libi2pd/NetDbRequests.cpp | 16 +++++++--------- libi2pd/NetDbRequests.h | 11 ++++++----- libi2pd/RouterContext.h | 4 ++-- libi2pd/SSU2.cpp | 4 ++-- libi2pd/SSU2.h | 3 ++- libi2pd/Transports.cpp | 4 ++-- 12 files changed, 45 insertions(+), 50 deletions(-) diff --git a/libi2pd/Destination.cpp b/libi2pd/Destination.cpp index 3d5893c7..eeaa6fa2 100644 --- a/libi2pd/Destination.cpp +++ b/libi2pd/Destination.cpp @@ -801,7 +801,7 @@ namespace client void LeaseSetDestination::RequestLeaseSet (const i2p::data::IdentHash& dest, RequestComplete requestComplete, std::shared_ptr requestedBlindedKey) { - std::set excluded; + std::unordered_set excluded; auto floodfill = i2p::data::netdb.GetClosestFloodfill (dest, excluded); if (floodfill) { diff --git a/libi2pd/Destination.h b/libi2pd/Destination.h index a066c64c..9600594d 100644 --- a/libi2pd/Destination.h +++ b/libi2pd/Destination.h @@ -15,7 +15,7 @@ #include #include #include -#include +#include #include #include #include @@ -97,7 +97,7 @@ namespace client struct LeaseSetRequest { LeaseSetRequest (boost::asio::io_service& service): requestTime (0), requestTimeoutTimer (service) {}; - std::set excluded; + std::unordered_set excluded; uint64_t requestTime; boost::asio::deadline_timer requestTimeoutTimer; std::list requestComplete; @@ -195,7 +195,7 @@ namespace client bool m_IsPublic; uint32_t m_PublishReplyToken; uint64_t m_LastSubmissionTime; // in seconds - std::set m_ExcludedFloodfills; // for publishing + std::unordered_set m_ExcludedFloodfills; // for publishing boost::asio::deadline_timer m_PublishConfirmationTimer, m_PublishVerificationTimer, m_PublishDelayTimer, m_CleanupTimer; diff --git a/libi2pd/I2NPProtocol.cpp b/libi2pd/I2NPProtocol.cpp index bea3f93f..e968daf9 100644 --- a/libi2pd/I2NPProtocol.cpp +++ b/libi2pd/I2NPProtocol.cpp @@ -147,7 +147,7 @@ namespace i2p } std::shared_ptr CreateRouterInfoDatabaseLookupMsg (const uint8_t * key, const uint8_t * from, - uint32_t replyTunnelID, bool exploratory, std::set * excludedPeers) + uint32_t replyTunnelID, bool exploratory, std::unordered_set * excludedPeers) { int cnt = excludedPeers ? excludedPeers->size () : 0; auto m = cnt > 7 ? NewI2NPMessage () : NewI2NPShortMessage (); @@ -192,7 +192,7 @@ namespace i2p } std::shared_ptr CreateLeaseSetDatabaseLookupMsg (const i2p::data::IdentHash& dest, - const std::set& excludedFloodfills, + const std::unordered_set& excludedFloodfills, std::shared_ptr replyTunnel, const uint8_t * replyKey, const uint8_t * replyTag, bool replyECIES) { diff --git a/libi2pd/I2NPProtocol.h b/libi2pd/I2NPProtocol.h index f110100e..1acd4242 100644 --- a/libi2pd/I2NPProtocol.h +++ b/libi2pd/I2NPProtocol.h @@ -11,7 +11,7 @@ #include #include -#include +#include #include #include #include "Crypto.h" @@ -294,9 +294,9 @@ namespace tunnel std::shared_ptr CreateTunnelTestMsg (uint32_t msgID); std::shared_ptr CreateDeliveryStatusMsg (uint32_t msgID); std::shared_ptr CreateRouterInfoDatabaseLookupMsg (const uint8_t * key, const uint8_t * from, - uint32_t replyTunnelID, bool exploratory = false, std::set * excludedPeers = nullptr); + uint32_t replyTunnelID, bool exploratory = false, std::unordered_set * excludedPeers = nullptr); std::shared_ptr CreateLeaseSetDatabaseLookupMsg (const i2p::data::IdentHash& dest, - const std::set& excludedFloodfills, + const std::unordered_set& excludedFloodfills, std::shared_ptr replyTunnel, const uint8_t * replyKey, const uint8_t * replyTag, bool replyECIES = false); std::shared_ptr CreateDatabaseSearchReply (const i2p::data::IdentHash& ident, std::vector routers); diff --git a/libi2pd/NetDb.cpp b/libi2pd/NetDb.cpp index 3b74ff6e..ea687ffa 100644 --- a/libi2pd/NetDb.cpp +++ b/libi2pd/NetDb.cpp @@ -1004,7 +1004,7 @@ namespace data auto dest = m_Requests->FindRequest (ident); if (dest && dest->IsActive ()) { - if (!dest->IsExploratory () && (num > 0 || dest->GetNumExcludedPeers () < 3)) // before 3-rd attempt might be just bad luck + if (!dest->IsExploratory () && (num > 0 || dest->GetNumAttempts () < 3)) // before 3-rd attempt might be just bad luck { // try to send next requests if (!m_Requests->SendNextRequest (dest)) @@ -1094,7 +1094,7 @@ namespace data return; } LogPrint (eLogInfo, "NetDb: Exploratory close to ", key, " ", numExcluded, " excluded"); - std::set excludedRouters; + std::unordered_set excludedRouters; const uint8_t * excluded_ident = excluded; for (int i = 0; i < numExcluded; i++) { @@ -1146,7 +1146,7 @@ namespace data if (!replyMsg) { - std::set excludedRouters; + std::unordered_set excludedRouters; const uint8_t * exclude_ident = excluded; for (int i = 0; i < numExcluded; i++) { @@ -1265,7 +1265,7 @@ namespace data void NetDb::Flood (const IdentHash& ident, std::shared_ptr floodMsg) { - std::set excluded; + std::unordered_set excluded; excluded.insert (i2p::context.GetIdentHash ()); // don't flood to itself excluded.insert (ident); // don't flood back for (int i = 0; i < 3; i++) @@ -1306,7 +1306,7 @@ namespace data }); } - std::shared_ptr NetDb::GetRandomSSU2PeerTestRouter (bool v4, const std::set& excluded) const + std::shared_ptr NetDb::GetRandomSSU2PeerTestRouter (bool v4, const std::unordered_set& excluded) const { return GetRandomRouter ( [v4, &excluded](std::shared_ptr router)->bool @@ -1316,7 +1316,7 @@ namespace data }); } - std::shared_ptr NetDb::GetRandomSSU2Introducer (bool v4, const std::set& excluded) const + std::shared_ptr NetDb::GetRandomSSU2Introducer (bool v4, const std::unordered_set& excluded) const { return GetRandomRouter ( [v4, &excluded](std::shared_ptr router)->bool @@ -1412,7 +1412,7 @@ namespace data } std::shared_ptr NetDb::GetClosestFloodfill (const IdentHash& destination, - const std::set& excluded) const + const std::unordered_set& excluded) const { IdentHash destKey = CreateRoutingKey (destination); std::lock_guard l(m_FloodfillsMutex); @@ -1424,7 +1424,7 @@ namespace data } std::vector NetDb::GetClosestFloodfills (const IdentHash& destination, size_t num, - std::set& excluded, bool closeThanUsOnly) const + std::unordered_set& excluded, bool closeThanUsOnly) const { std::vector res; IdentHash destKey = CreateRoutingKey (destination); @@ -1459,7 +1459,7 @@ namespace data } std::vector NetDb::GetExploratoryNonFloodfill (const IdentHash& destination, - size_t num, const std::set& excluded) + size_t num, const std::unordered_set& excluded) { std::vector ret; if (!num || m_RouterInfos.empty ()) return ret; // empty list diff --git a/libi2pd/NetDb.hpp b/libi2pd/NetDb.hpp index 50f6b033..1a6c357f 100644 --- a/libi2pd/NetDb.hpp +++ b/libi2pd/NetDb.hpp @@ -10,7 +10,7 @@ #define NETDB_H__ // this file is called NetDb.hpp to resolve conflict with libc's netdb.h on case insensitive fs #include -#include +#include #include #include #include @@ -95,12 +95,12 @@ namespace data std::shared_ptr GetRandomRouter () const; std::shared_ptr GetRandomRouter (std::shared_ptr compatibleWith, bool reverse, bool endpoint) const; std::shared_ptr GetHighBandwidthRandomRouter (std::shared_ptr compatibleWith, bool reverse, bool endpoint) const; - std::shared_ptr GetRandomSSU2PeerTestRouter (bool v4, const std::set& excluded) const; - std::shared_ptr GetRandomSSU2Introducer (bool v4, const std::set& excluded) const; - std::shared_ptr GetClosestFloodfill (const IdentHash& destination, const std::set& excluded) const; + std::shared_ptr GetRandomSSU2PeerTestRouter (bool v4, const std::unordered_set& excluded) const; + std::shared_ptr GetRandomSSU2Introducer (bool v4, const std::unordered_set& excluded) const; + std::shared_ptr GetClosestFloodfill (const IdentHash& destination, const std::unordered_set& excluded) const; std::vector GetClosestFloodfills (const IdentHash& destination, size_t num, - std::set& excluded, bool closeThanUsOnly = false) const; - std::vector GetExploratoryNonFloodfill (const IdentHash& destination, size_t num, const std::set& excluded); + std::unordered_set& excluded, bool closeThanUsOnly = false) const; + std::vector GetExploratoryNonFloodfill (const IdentHash& destination, size_t num, const std::unordered_set& excluded); std::shared_ptr GetRandomRouterInFamily (FamilyID fam) const; void SetUnreachable (const IdentHash& ident, bool unreachable); void ExcludeReachableTransports (const IdentHash& ident, RouterInfo::CompatibleTransports transports); @@ -139,8 +139,6 @@ namespace data std::shared_ptr NewIdentity (const uint8_t * buf, size_t len) { return m_IdentitiesPool.AcquireSharedMt (buf, len); }; std::shared_ptr NewRouterProfile () { return m_RouterProfilesPool.AcquireSharedMt (); }; - uint32_t GetPublishReplyToken () const { return m_PublishReplyToken; }; - private: void Load (); @@ -189,9 +187,6 @@ namespace data /** router info we are bootstrapping from or nullptr if we are not currently doing that*/ std::shared_ptr m_FloodfillBootstrap; - std::set m_PublishExcluded; - uint32_t m_PublishReplyToken = 0; - std::vector > m_ExploratorySelection; uint64_t m_LastExploratorySelectionUpdateTime; // in monotonic seconds diff --git a/libi2pd/NetDbRequests.cpp b/libi2pd/NetDbRequests.cpp index 85e3249f..08d5536c 100644 --- a/libi2pd/NetDbRequests.cpp +++ b/libi2pd/NetDbRequests.cpp @@ -12,6 +12,7 @@ #include "NetDb.hpp" #include "NetDbRequests.h" #include "ECIESX25519AEADRatchetSession.h" +#include "RouterContext.h" namespace i2p { @@ -19,8 +20,10 @@ namespace data { RequestedDestination::RequestedDestination (const IdentHash& destination, bool isExploratory, bool direct): m_Destination (destination), m_IsExploratory (isExploratory), m_IsDirect (direct), m_IsActive (true), - m_CreationTime (i2p::util::GetSecondsSinceEpoch ()), m_LastRequestTime (0) + m_CreationTime (i2p::util::GetSecondsSinceEpoch ()), m_LastRequestTime (0), m_NumAttempts (0) { + if (i2p::context.IsFloodfill ()) + m_ExcludedPeers.insert (i2p::context.GetIdentHash ()); // exclude self if floodfill } RequestedDestination::~RequestedDestination () @@ -42,6 +45,7 @@ namespace data if(router) m_ExcludedPeers.insert (router->GetIdentHash ()); m_LastRequestTime = i2p::util::GetSecondsSinceEpoch (); + m_NumAttempts++; return msg; } @@ -67,17 +71,11 @@ namespace data m_ExcludedPeers.clear (); } - std::set RequestedDestination::GetExcludedPeers () const + std::unordered_set RequestedDestination::GetExcludedPeers () const { std::lock_guard l (m_ExcludedPeersMutex); return m_ExcludedPeers; } - - size_t RequestedDestination::GetNumExcludedPeers () const - { - std::lock_guard l (m_ExcludedPeersMutex); - return m_ExcludedPeers.size (); - } void RequestedDestination::Success (std::shared_ptr r) { @@ -231,7 +229,7 @@ namespace data { if (!dest || !dest->IsActive ()) return false; bool ret = true; - auto count = dest->GetNumExcludedPeers (); + auto count = dest->GetNumAttempts (); if (!dest->IsExploratory () && count < MAX_NUM_REQUEST_ATTEMPTS) { auto nextFloodfill = netdb.GetClosestFloodfill (dest->GetDestination (), dest->GetExcludedPeers ()); diff --git a/libi2pd/NetDbRequests.h b/libi2pd/NetDbRequests.h index 2b398fd5..1f78fde6 100644 --- a/libi2pd/NetDbRequests.h +++ b/libi2pd/NetDbRequests.h @@ -11,7 +11,7 @@ #include #include -#include +#include #include #include "Identity.h" #include "RouterInfo.h" @@ -21,7 +21,7 @@ namespace i2p { namespace data { - const size_t MAX_NUM_REQUEST_ATTEMPTS = 5; + const int MAX_NUM_REQUEST_ATTEMPTS = 5; const uint64_t MANAGE_REQUESTS_INTERVAL = 1; // in seconds const uint64_t MIN_REQUEST_TIME = 5; // in seconds const uint64_t MAX_REQUEST_TIME = MAX_NUM_REQUEST_ATTEMPTS * (MIN_REQUEST_TIME + MANAGE_REQUESTS_INTERVAL); @@ -39,8 +39,8 @@ namespace data ~RequestedDestination (); const IdentHash& GetDestination () const { return m_Destination; }; - size_t GetNumExcludedPeers () const; - std::set GetExcludedPeers () const; + std::unordered_set GetExcludedPeers () const; + int GetNumAttempts () const { return m_NumAttempts; }; void ClearExcludedPeers (); bool IsExploratory () const { return m_IsExploratory; }; bool IsDirect () const { return m_IsDirect; }; @@ -62,9 +62,10 @@ namespace data IdentHash m_Destination; bool m_IsExploratory, m_IsDirect, m_IsActive; mutable std::mutex m_ExcludedPeersMutex; - std::set m_ExcludedPeers; + std::unordered_set m_ExcludedPeers; uint64_t m_CreationTime, m_LastRequestTime; // in seconds RequestComplete m_RequestComplete; + int m_NumAttempts; }; class NetDbRequests: public std::enable_shared_from_this diff --git a/libi2pd/RouterContext.h b/libi2pd/RouterContext.h index 941974b2..2a1f4ef6 100644 --- a/libi2pd/RouterContext.h +++ b/libi2pd/RouterContext.h @@ -12,7 +12,7 @@ #include #include #include -#include +#include #include #include "Identity.h" #include "RouterInfo.h" @@ -255,7 +255,7 @@ namespace garlic // publish std::unique_ptr m_Service; std::unique_ptr m_PublishTimer, m_CongestionUpdateTimer, m_CleanupTimer; - std::set m_PublishExcluded; + std::unordered_set m_PublishExcluded; uint32_t m_PublishReplyToken; bool m_IsHiddenMode; // not publish }; diff --git a/libi2pd/SSU2.cpp b/libi2pd/SSU2.cpp index 576b9cec..15d54395 100644 --- a/libi2pd/SSU2.cpp +++ b/libi2pd/SSU2.cpp @@ -1047,7 +1047,7 @@ namespace transport } std::list > SSU2Server::FindIntroducers (int maxNumIntroducers, - bool v4, const std::set& excluded) const + bool v4, const std::unordered_set& excluded) const { std::list > ret; for (const auto& s : m_Sessions) @@ -1078,7 +1078,7 @@ namespace transport uint32_t ts = i2p::util::GetSecondsSinceEpoch (); std::list newList, impliedList; auto& introducers = v4 ? m_Introducers : m_IntroducersV6; - std::set excluded; + std::unordered_set excluded; for (const auto& it : introducers) { std::shared_ptr session; diff --git a/libi2pd/SSU2.h b/libi2pd/SSU2.h index be643272..d81acf6f 100644 --- a/libi2pd/SSU2.h +++ b/libi2pd/SSU2.h @@ -10,6 +10,7 @@ #define SSU2_H__ #include +#include #include #include "util.h" #include "SSU2Session.h" @@ -127,7 +128,7 @@ namespace transport void ConnectThroughIntroducer (std::shared_ptr session); std::list > FindIntroducers (int maxNumIntroducers, - bool v4, const std::set& excluded) const; + bool v4, const std::unordered_set& excluded) const; void UpdateIntroducers (bool v4); void ScheduleIntroducersUpdateTimer (); void HandleIntroducersUpdateTimer (const boost::system::error_code& ecode, bool v4); diff --git a/libi2pd/Transports.cpp b/libi2pd/Transports.cpp index e4191fd2..9a10ca72 100644 --- a/libi2pd/Transports.cpp +++ b/libi2pd/Transports.cpp @@ -708,7 +708,7 @@ namespace transport if (ipv4 && i2p::context.SupportsV4 ()) { LogPrint (eLogInfo, "Transports: Started peer test IPv4"); - std::set excluded; + std::unordered_set excluded; excluded.insert (i2p::context.GetIdentHash ()); // don't pick own router int testDelay = 0; for (int i = 0; i < 5; i++) @@ -746,7 +746,7 @@ namespace transport if (ipv6 && i2p::context.SupportsV6 ()) { LogPrint (eLogInfo, "Transports: Started peer test IPv6"); - std::set excluded; + std::unordered_set excluded; excluded.insert (i2p::context.GetIdentHash ()); // don't pick own router int testDelay = 0; for (int i = 0; i < 5; i++)