From 181953b4a6597cf3db3507948e6485ce50293baf Mon Sep 17 00:00:00 2001 From: Jason Rhinelander Date: Tue, 9 Mar 2021 14:39:40 -0400 Subject: [PATCH] Replace ::Hash nested structs with std::hash specializations --- llarp/config/config.hpp | 4 +-- llarp/crypto/types.hpp | 7 +++++ llarp/dht/context.hpp | 6 ++-- llarp/dht/txholder.hpp | 28 ++++++++--------- llarp/dht/txowner.hpp | 26 +++++++++------- llarp/exit/context.hpp | 2 +- llarp/handlers/exit.hpp | 11 +++---- llarp/handlers/tun.hpp | 4 +-- llarp/iwp/linklayer.hpp | 4 +-- llarp/link/link_manager.cpp | 2 +- llarp/link/link_manager.hpp | 5 ++- llarp/link/server.cpp | 2 +- llarp/link/server.hpp | 8 ++--- llarp/net/address_info.hpp | 22 ++++++++------ llarp/net/ip_address.hpp | 24 +++++++++------ llarp/net/sock_addr.hpp | 26 +++++++++------- llarp/net/uint128.hpp | 6 ++++ llarp/path/path.hpp | 12 ++++---- llarp/path/path_context.hpp | 4 +-- llarp/path/path_types.hpp | 11 +++++-- llarp/path/pathset.hpp | 35 +++++++++------------ llarp/path/transit_hop.hpp | 37 +++++++++-------------- llarp/peerstats/peer_db.hpp | 2 +- llarp/router/outbound_message_handler.hpp | 5 ++- llarp/router/outbound_session_maker.hpp | 5 ++- llarp/router/rc_lookup_handler.hpp | 5 ++- llarp/router/router.cpp | 2 +- llarp/router_contact.hpp | 22 ++++++++------ llarp/router_id.hpp | 16 ++-------- llarp/rpc/endpoint_rpc.hpp | 2 +- llarp/service/address.hpp | 11 +------ llarp/service/endpoint.hpp | 4 +-- llarp/service/endpoint_state.hpp | 6 ++-- llarp/service/endpoint_types.hpp | 13 ++++---- llarp/service/handler.hpp | 3 +- llarp/service/intro.hpp | 22 ++++++++------ llarp/service/outbound_context.hpp | 2 +- llarp/service/tag.hpp | 9 ++++-- llarp/tooling/router_hive.hpp | 4 +-- llarp/util/aligned.hpp | 26 +++++++++------- llarp/util/decaying_hashset.hpp | 2 +- llarp/util/decaying_hashtable.hpp | 2 +- test/util/test_llarp_util_aligned.cpp | 2 +- 43 files changed, 231 insertions(+), 220 deletions(-) diff --git a/llarp/config/config.hpp b/llarp/config/config.hpp index 1ba181123..4b0ef3ce8 100644 --- a/llarp/config/config.hpp +++ b/llarp/config/config.hpp @@ -106,7 +106,7 @@ namespace llarp net::IPRangeMap m_ExitMap; net::IPRangeMap m_LNSExitMap; - std::unordered_map m_ExitAuths; + std::unordered_map m_ExitAuths; std::unordered_map m_LNSExitAuths; std::unordered_map m_mapAddrs; @@ -114,7 +114,7 @@ namespace llarp service::AuthType m_AuthType = service::AuthType::eAuthTypeNone; std::optional m_AuthUrl; std::optional m_AuthMethod; - std::unordered_set m_AuthWhitelist; + std::unordered_set m_AuthWhitelist; std::vector m_SRVRecords; diff --git a/llarp/crypto/types.hpp b/llarp/crypto/types.hpp index 2c0d3711a..225be8278 100644 --- a/llarp/crypto/types.hpp +++ b/llarp/crypto/types.hpp @@ -243,3 +243,10 @@ namespace llarp /// SH(result, body) using shorthash_func = std::function; } // namespace llarp + +namespace std +{ + template <> + struct hash : hash> + {}; +}; // namespace std diff --git a/llarp/dht/context.hpp b/llarp/dht/context.hpp index 9d00beec9..1b184555f 100644 --- a/llarp/dht/context.hpp +++ b/llarp/dht/context.hpp @@ -34,9 +34,9 @@ namespace llarp struct AbstractContext { - using PendingIntrosetLookups = TXHolder; - using PendingRouterLookups = TXHolder; - using PendingExploreLookups = TXHolder; + using PendingIntrosetLookups = TXHolder; + using PendingRouterLookups = TXHolder; + using PendingExploreLookups = TXHolder; virtual ~AbstractContext() = 0; diff --git a/llarp/dht/txholder.hpp b/llarp/dht/txholder.hpp index 2c7100146..b17586174 100644 --- a/llarp/dht/txholder.hpp +++ b/llarp/dht/txholder.hpp @@ -13,16 +13,16 @@ namespace llarp { namespace dht { - template + template struct TXHolder { using TXPtr = std::unique_ptr>; // tx who are waiting for a reply for each key - std::unordered_multimap waiting; + std::unordered_multimap waiting; // tx timesouts by key - std::unordered_map timeouts; + std::unordered_map timeouts; // maps remote peer with tx to handle reply from them - std::unordered_map tx; + std::unordered_map tx; const TX* GetPendingLookupFrom(const TXOwner& owner) const; @@ -106,9 +106,9 @@ namespace llarp Expire(llarp_time_t now); }; - template + template const TX* - TXHolder::GetPendingLookupFrom(const TXOwner& owner) const + TXHolder::GetPendingLookupFrom(const TXOwner& owner) const { auto itr = tx.find(owner); if (itr == tx.end()) @@ -119,9 +119,9 @@ namespace llarp return itr->second.get(); } - template + template void - TXHolder::NewTX( + TXHolder::NewTX( const TXOwner& askpeer, const TXOwner& whoasked, const K& k, @@ -144,9 +144,9 @@ namespace llarp } } - template + template void - TXHolder::NotFound(const TXOwner& from, const std::unique_ptr&) + TXHolder::NotFound(const TXOwner& from, const std::unique_ptr&) { auto txitr = tx.find(from); if (txitr == tx.end()) @@ -156,9 +156,9 @@ namespace llarp Inform(from, txitr->second->target, {}, true, true); } - template + template void - TXHolder::Inform( + TXHolder::Inform( TXOwner from, K key, std::vector values, bool sendreply, bool removeTimeouts) { auto range = waiting.equal_range(key); @@ -192,9 +192,9 @@ namespace llarp } } - template + template void - TXHolder::Expire(llarp_time_t now) + TXHolder::Expire(llarp_time_t now) { auto itr = timeouts.begin(); while (itr != timeouts.end()) diff --git a/llarp/dht/txowner.hpp b/llarp/dht/txowner.hpp index d334064bb..e21c878a1 100644 --- a/llarp/dht/txowner.hpp +++ b/llarp/dht/txowner.hpp @@ -44,17 +44,21 @@ namespace llarp { return std::tie(txid, node) < std::tie(other.txid, other.node); } - - struct Hash - { - std::size_t - operator()(const TXOwner& o) const noexcept - { - std::size_t sz2; - memcpy(&sz2, o.node.data(), sizeof(std::size_t)); - return o.txid ^ (sz2 << 1); - } - }; }; } // namespace dht } // namespace llarp + +namespace std +{ + template <> + struct hash + { + std::size_t + operator()(const llarp::dht::TXOwner& o) const noexcept + { + std::size_t sz2; + memcpy(&sz2, o.node.data(), sizeof(std::size_t)); + return o.txid ^ (sz2 << 1); + } + }; +} // namespace std diff --git a/llarp/exit/context.hpp b/llarp/exit/context.hpp index cfc71ea97..91398efa9 100644 --- a/llarp/exit/context.hpp +++ b/llarp/exit/context.hpp @@ -39,7 +39,7 @@ namespace llarp FindEndpointForPath(const PathID_t& path) const; /// calculate (pk, tx, rx) for all exit traffic - using TrafficStats = std::unordered_map, PubKey::Hash>; + using TrafficStats = std::unordered_map>; void CalculateExitTraffic(TrafficStats& stats); diff --git a/llarp/handlers/exit.hpp b/llarp/handlers/exit.hpp index cc408a3f7..7b066a2e4 100644 --- a/llarp/handlers/exit.hpp +++ b/llarp/handlers/exit.hpp @@ -134,13 +134,13 @@ namespace llarp bool m_ShouldInitTun; std::string m_Name; bool m_PermitExit; - std::unordered_map m_Paths; + std::unordered_map m_Paths; - std::unordered_map m_ChosenExits; + std::unordered_map m_ChosenExits; - std::unordered_multimap, PubKey::Hash> m_ActiveExits; + std::unordered_multimap> m_ActiveExits; - using KeyMap_t = std::unordered_map; + using KeyMap_t = std::unordered_map; KeyMap_t m_KeyToIP; @@ -148,8 +148,7 @@ namespace llarp /// set of pubkeys we treat as snodes SNodes_t m_SNodeKeys; - using SNodeSessions_t = - std::unordered_map, RouterID::Hash>; + using SNodeSessions_t = std::unordered_map>; /// snode sessions we are talking to directly SNodeSessions_t m_SNodeSessions; diff --git a/llarp/handlers/tun.hpp b/llarp/handlers/tun.hpp index 5dd2312ac..9c813d3f7 100644 --- a/llarp/handlers/tun.hpp +++ b/llarp/handlers/tun.hpp @@ -200,11 +200,11 @@ namespace llarp /// maps ip to key (host byte order) std::unordered_map> m_IPToAddr; /// maps key to ip (host byte order) - std::unordered_map, huint128_t, AlignedBuffer<32>::Hash> m_AddrToIP; + std::unordered_map, huint128_t> m_AddrToIP; /// maps key to true if key is a service node, maps key to false if key is /// a hidden service - std::unordered_map, bool, AlignedBuffer<32>::Hash> m_SNodes; + std::unordered_map, bool> m_SNodes; private: template diff --git a/llarp/iwp/linklayer.hpp b/llarp/iwp/linklayer.hpp index 0bdd5d7f5..52d81ee2e 100644 --- a/llarp/iwp/linklayer.hpp +++ b/llarp/iwp/linklayer.hpp @@ -61,8 +61,8 @@ namespace llarp::iwp HandleWakeupPlaintext(); const std::shared_ptr m_Wakeup; - std::unordered_map, SockAddr::Hash> m_PlaintextRecv; - std::unordered_map m_AuthedAddrs; + std::unordered_map> m_PlaintextRecv; + std::unordered_map m_AuthedAddrs; const bool permitInbound; }; diff --git a/llarp/link/link_manager.cpp b/llarp/link/link_manager.cpp index a680488ac..0d987c3b1 100644 --- a/llarp/link/link_manager.cpp +++ b/llarp/link/link_manager.cpp @@ -296,7 +296,7 @@ namespace llarp bool LinkManager::GetRandomConnectedRouter(RouterContact& router) const { - std::unordered_map connectedRouters; + std::unordered_map connectedRouters; ForEachPeer( [&connectedRouters](const ILinkSession* peer, bool unused) { diff --git a/llarp/link/link_manager.hpp b/llarp/link/link_manager.hpp index 8cf7d58ba..e05debb1e 100644 --- a/llarp/link/link_manager.hpp +++ b/llarp/link/link_manager.hpp @@ -104,10 +104,9 @@ namespace llarp LinkSet inboundLinks; // sessions to persist -> timestamp to end persist at - std::unordered_map m_PersistingSessions - GUARDED_BY(_mutex); + std::unordered_map m_PersistingSessions GUARDED_BY(_mutex); - std::unordered_map m_lastRouterStats; + std::unordered_map m_lastRouterStats; IOutboundSessionMaker* _sessionMaker; }; diff --git a/llarp/link/server.cpp b/llarp/link/server.cpp index 78f8099fe..6e40b0df4 100644 --- a/llarp/link/server.cpp +++ b/llarp/link/server.cpp @@ -172,7 +172,7 @@ namespace llarp void ILinkLayer::Pump() { - std::unordered_set closedSessions; + std::unordered_set closedSessions; std::vector> closedPending; auto _now = Now(); { diff --git a/llarp/link/server.hpp b/llarp/link/server.hpp index 6fe75c68b..cc3b5e84e 100644 --- a/llarp/link/server.hpp +++ b/llarp/link/server.hpp @@ -244,16 +244,14 @@ namespace llarp std::shared_ptr m_udp; SecretKey m_SecretKey; - using AuthedLinks = - std::unordered_multimap, RouterID::Hash>; - using Pending = - std::unordered_multimap, SockAddr::Hash>; + using AuthedLinks = std::unordered_multimap>; + using Pending = std::unordered_multimap>; mutable DECLARE_LOCK(Mutex_t, m_AuthedLinksMutex, ACQUIRED_BEFORE(m_PendingMutex)); AuthedLinks m_AuthedLinks GUARDED_BY(m_AuthedLinksMutex); mutable DECLARE_LOCK(Mutex_t, m_PendingMutex, ACQUIRED_AFTER(m_AuthedLinksMutex)); Pending m_Pending GUARDED_BY(m_PendingMutex); - std::unordered_map m_RecentlyClosed; + std::unordered_map m_RecentlyClosed; private: std::shared_ptr m_repeater_keepalive; diff --git a/llarp/net/address_info.hpp b/llarp/net/address_info.hpp index 0a972b011..5851ad07c 100644 --- a/llarp/net/address_info.hpp +++ b/llarp/net/address_info.hpp @@ -49,15 +49,6 @@ namespace llarp std::ostream& print(std::ostream& stream, int level, int spaces) const; - - struct Hash - { - size_t - operator()(const AddressInfo& addr) const - { - return AlignedBuffer::Hash()(addr.pubkey); - } - }; }; void @@ -76,3 +67,16 @@ namespace llarp operator<(const AddressInfo& lhs, const AddressInfo& rhs); } // namespace llarp + +namespace std +{ + template <> + struct hash + { + size_t + operator()(const llarp::AddressInfo& addr) const + { + return std::hash{}(addr.pubkey); + } + }; +} // namespace std diff --git a/llarp/net/ip_address.hpp b/llarp/net/ip_address.hpp index 3e7139645..2414f4f8e 100644 --- a/llarp/net/ip_address.hpp +++ b/llarp/net/ip_address.hpp @@ -132,7 +132,7 @@ namespace llarp // TODO: other utility functions left over from Addr which may be useful // IsBogon() const; // isPrivate() const; - // struct Hash + // std::hash // to string / stream / etc bool @@ -141,15 +141,6 @@ namespace llarp bool operator==(const IpAddress& other) const; - struct Hash - { - std::size_t - operator()(const IpAddress& address) const noexcept - { - return std::hash{}(address.toString()); - } - }; - private: bool m_empty = true; std::string m_ipAddress; @@ -160,3 +151,16 @@ namespace llarp operator<<(std::ostream& out, const IpAddress& address); } // namespace llarp + +namespace std +{ + template <> + struct hash + { + std::size_t + operator()(const llarp::IpAddress& address) const noexcept + { + return std::hash{}(address.toString()); + } + }; +} // namespace std diff --git a/llarp/net/sock_addr.hpp b/llarp/net/sock_addr.hpp index adfbdfcc7..303d71752 100644 --- a/llarp/net/sock_addr.hpp +++ b/llarp/net/sock_addr.hpp @@ -131,17 +131,6 @@ namespace llarp huint32_t asIPv4() const; - struct Hash - { - size_t - operator()(const SockAddr& addr) const noexcept - { - const std::hash port{}; - const std::hash ip{}; - return (port(addr.getPort()) << 3) ^ ip(addr.asIPv6()); - } - }; - private: bool m_empty = true; sockaddr_in6 m_addr; @@ -158,3 +147,18 @@ namespace llarp operator<<(std::ostream& out, const SockAddr& address); } // namespace llarp + +namespace std +{ + template <> + struct hash + { + size_t + operator()(const llarp::SockAddr& addr) const noexcept + { + const std::hash port{}; + const std::hash ip{}; + return (port(addr.getPort()) << 3) ^ ip(addr.asIPv6()); + } + }; +} // namespace std diff --git a/llarp/net/uint128.hpp b/llarp/net/uint128.hpp index 33aa8bd96..aa1c96d08 100644 --- a/llarp/net/uint128.hpp +++ b/llarp/net/uint128.hpp @@ -314,3 +314,9 @@ ntoh128(llarp::uint128_t i) return {loSwapped, hiSwapped}; #endif } + +inline llarp::uint128_t +hton128(llarp::uint128_t i) +{ + return ntoh128(i); // Same bit flipping as n-to-h +} diff --git a/llarp/path/path.hpp b/llarp/path/path.hpp index 867673a99..5253f5f26 100644 --- a/llarp/path/path.hpp +++ b/llarp/path/path.hpp @@ -115,16 +115,16 @@ namespace llarp const auto& tx = p.hops[0].txID; const auto& rx = p.hops[0].rxID; const auto& r = p.hops[0].upstream; - const size_t rhash = std::accumulate(r.begin(), r.end(), 0, std::bit_xor()); + const size_t rhash = std::accumulate(r.begin(), r.end(), 0, std::bit_xor{}); return std::accumulate( rx.begin(), rx.begin(), - std::accumulate(tx.begin(), tx.end(), rhash, std::bit_xor()), - std::bit_xor()); + std::accumulate(tx.begin(), tx.end(), rhash, std::bit_xor{}), + std::bit_xor{}); } }; - /// hash for std::shared_ptr + /// hash for std::shared_ptr struct Ptr_Hash { size_t @@ -136,7 +136,7 @@ namespace llarp } }; - /// hash for std::shared_ptr by path endpoint + /// hash for std::shared_ptr by path endpoint struct Endpoint_Hash { size_t @@ -144,7 +144,7 @@ namespace llarp { if (p == nullptr) return 0; - return RouterID::Hash{}(p->Endpoint()); + return std::hash{}(p->Endpoint()); } }; diff --git a/llarp/path/path_context.hpp b/llarp/path/path_context.hpp index a0eec0a5a..ad3c7f204 100644 --- a/llarp/path/path_context.hpp +++ b/llarp/path/path_context.hpp @@ -109,7 +109,7 @@ namespace llarp void RemovePathSet(PathSet_ptr set); - using TransitHopsMap_t = std::unordered_multimap; + using TransitHopsMap_t = std::unordered_multimap; struct SyncTransitMap_t { @@ -129,7 +129,7 @@ namespace llarp }; // maps path id -> pathset owner of path - using OwnedPathsMap_t = std::unordered_map; + using OwnedPathsMap_t = std::unordered_map; struct SyncOwnedPathsMap_t { diff --git a/llarp/path/path_types.hpp b/llarp/path/path_types.hpp index eb3bc5306..917379ad4 100644 --- a/llarp/path/path_types.hpp +++ b/llarp/path/path_types.hpp @@ -6,8 +6,13 @@ namespace llarp { struct PathID_t final : public AlignedBuffer - { - using Hash = AlignedBuffer::Hash; - }; + {}; } // namespace llarp + +namespace std +{ + template <> + struct hash : hash> + {}; +} // namespace std diff --git a/llarp/path/pathset.hpp b/llarp/path/pathset.hpp index 6105bc5b4..5cb787053 100644 --- a/llarp/path/pathset.hpp +++ b/llarp/path/pathset.hpp @@ -13,6 +13,19 @@ #include #include +namespace std +{ + template <> + struct hash> + { + size_t + operator()(const std::pair& i) const + { + return hash{}(i.first) ^ hash{}(i.second); + } + }; +} // namespace std + namespace llarp { struct RouterContact; @@ -294,29 +307,9 @@ namespace llarp void TickPaths(AbstractRouter* r); - using PathInfo_t = std::pair; - - struct PathInfoHash - { - size_t - operator()(const PathInfo_t& i) const - { - return RouterID::Hash()(i.first) ^ PathID_t::Hash()(i.second); - } - }; - - struct PathInfoEquals - { - bool - operator()(const PathInfo_t& left, const PathInfo_t& right) const - { - return left.first == right.first && left.second == right.second; - } - }; - using Mtx_t = util::NullMutex; using Lock_t = util::NullLock; - using PathMap_t = std::unordered_map; + using PathMap_t = std::unordered_map, Path_ptr>; mutable Mtx_t m_PathsMutex; PathMap_t m_Paths; }; diff --git a/llarp/path/transit_hop.hpp b/llarp/path/transit_hop.hpp index 052b8b9a1..315cf83d8 100644 --- a/llarp/path/transit_hop.hpp +++ b/llarp/path/transit_hop.hpp @@ -30,28 +30,6 @@ namespace llarp std::ostream& print(std::ostream& stream, int level, int spaces) const; - - struct PathIDHash - { - std::size_t - operator()(const PathID_t& a) const - { - return AlignedBuffer::Hash()(a); - } - }; - - struct Hash - { - std::size_t - operator()(TransitHopInfo const& a) const - { - std::size_t idx0 = RouterID::Hash()(a.upstream); - std::size_t idx1 = RouterID::Hash()(a.downstream); - std::size_t idx2 = PathIDHash()(a.txID); - std::size_t idx3 = PathIDHash()(a.rxID); - return idx0 ^ idx1 ^ idx2 ^ idx3; - } - }; }; inline bool @@ -235,3 +213,18 @@ namespace llarp } } // namespace path } // namespace llarp + +namespace std +{ + template <> + struct hash + { + std::size_t + operator()(llarp::path::TransitHopInfo const& a) const + { + hash RHash{}; + hash PHash{}; + return RHash(a.upstream) ^ RHash(a.downstream) ^ PHash(a.txID) ^ PHash(a.rxID); + } + }; +} // namespace std diff --git a/llarp/peerstats/peer_db.hpp b/llarp/peerstats/peer_db.hpp index ecbb739ab..b669b233c 100644 --- a/llarp/peerstats/peer_db.hpp +++ b/llarp/peerstats/peer_db.hpp @@ -127,7 +127,7 @@ namespace llarp ExtractStatus() const; private: - std::unordered_map m_peerStats; + std::unordered_map m_peerStats; mutable std::mutex m_statsLock; std::unique_ptr m_storage; diff --git a/llarp/router/outbound_message_handler.hpp b/llarp/router/outbound_message_handler.hpp index 95c31a95b..8769b80d6 100644 --- a/llarp/router/outbound_message_handler.hpp +++ b/llarp/router/outbound_message_handler.hpp @@ -128,10 +128,9 @@ namespace llarp mutable util::Mutex _mutex; // protects pendingSessionMessageQueues - std::unordered_map pendingSessionMessageQueues - GUARDED_BY(_mutex); + std::unordered_map pendingSessionMessageQueues GUARDED_BY(_mutex); - std::unordered_map outboundMessageQueues; + std::unordered_map outboundMessageQueues; std::queue roundRobinOrder; diff --git a/llarp/router/outbound_session_maker.hpp b/llarp/router/outbound_session_maker.hpp index da52fd474..1906868ee 100644 --- a/llarp/router/outbound_session_maker.hpp +++ b/llarp/router/outbound_session_maker.hpp @@ -100,11 +100,10 @@ namespace llarp mutable util::Mutex _mutex; // protects pendingSessions, pendingCallbacks - std::unordered_map, RouterID::Hash> pendingSessions + std::unordered_map> pendingSessions GUARDED_BY(_mutex); - std::unordered_map pendingCallbacks - GUARDED_BY(_mutex); + std::unordered_map pendingCallbacks GUARDED_BY(_mutex); AbstractRouter* _router = nullptr; ILinkManager* _linkManager = nullptr; diff --git a/llarp/router/rc_lookup_handler.hpp b/llarp/router/rc_lookup_handler.hpp index 8cc7cafe1..a3b88e924 100644 --- a/llarp/router/rc_lookup_handler.hpp +++ b/llarp/router/rc_lookup_handler.hpp @@ -114,8 +114,7 @@ namespace llarp std::set _bootstrapRCList; std::unordered_set _bootstrapRouterIDList; - std::unordered_map pendingCallbacks - GUARDED_BY(_mutex); + std::unordered_map pendingCallbacks GUARDED_BY(_mutex); bool useWhitelist = false; bool isServiceNode = false; @@ -123,7 +122,7 @@ namespace llarp std::unordered_set whitelistRouters GUARDED_BY(_mutex); using TimePoint = std::chrono::steady_clock::time_point; - std::unordered_map _routerLookupTimes; + std::unordered_map _routerLookupTimes; }; } // namespace llarp diff --git a/llarp/router/router.cpp b/llarp/router/router.cpp index abd7ed366..769565bdb 100644 --- a/llarp/router/router.cpp +++ b/llarp/router/router.cpp @@ -797,7 +797,7 @@ namespace llarp }); // find all deregistered relays - std::unordered_set closePeers; + std::unordered_set closePeers; _linkManager.ForEachPeer([&](auto session) { if (whitelistRouters and not gotWhitelist) diff --git a/llarp/router_contact.hpp b/llarp/router_contact.hpp index 2d93ed5a8..48442a202 100644 --- a/llarp/router_contact.hpp +++ b/llarp/router_contact.hpp @@ -83,15 +83,6 @@ namespace llarp Clear(); } - struct Hash - { - size_t - operator()(const RouterContact& r) const - { - return PubKey::Hash()(r.pubkey); - } - }; - // advertised addresses std::vector addrs; // network identifier @@ -240,3 +231,16 @@ namespace llarp using RouterLookupHandler = std::function&)>; } // namespace llarp + +namespace std +{ + template <> + struct hash + { + size_t + operator()(const llarp::RouterContact& r) const + { + return std::hash{}(r.pubkey); + } + }; +} // namespace std diff --git a/llarp/router_id.hpp b/llarp/router_id.hpp index ccc1c74c4..70c4dfbc3 100644 --- a/llarp/router_id.hpp +++ b/llarp/router_id.hpp @@ -11,8 +11,7 @@ namespace llarp using Data = std::array; - RouterID() - {} + RouterID() = default; RouterID(const byte_t* buf) : AlignedBuffer(buf) {} @@ -44,8 +43,6 @@ namespace llarp { return out << id.ToString(); } - - using Hash = AlignedBuffer::Hash; }; inline bool @@ -59,13 +56,6 @@ namespace llarp namespace std { template <> - struct hash - { - size_t - operator()(const llarp::RouterID& id) const - { - const llarp::RouterID::Hash h{}; - return h(id); - } - }; + struct hash : hash> + {}; } // namespace std diff --git a/llarp/rpc/endpoint_rpc.hpp b/llarp/rpc/endpoint_rpc.hpp index a7a699b28..c84cf6183 100644 --- a/llarp/rpc/endpoint_rpc.hpp +++ b/llarp/rpc/endpoint_rpc.hpp @@ -15,7 +15,7 @@ namespace llarp::rpc { using LMQ_ptr = std::shared_ptr; using Endpoint_ptr = std::shared_ptr; - using Whitelist_t = std::unordered_set; + using Whitelist_t = std::unordered_set; explicit EndpointAuthRPC( std::string url, diff --git a/llarp/service/address.hpp b/llarp/service/address.hpp index aed9a1c79..31f15cb76 100644 --- a/llarp/service/address.hpp +++ b/llarp/service/address.hpp @@ -89,15 +89,6 @@ namespace llarp { return RouterID(as_array()); } - - struct Hash - { - size_t - operator()(const Address& buf) const - { - return std::accumulate(buf.begin(), buf.end(), 0, std::bit_xor()); - } - }; }; } // namespace service @@ -111,7 +102,7 @@ namespace std size_t operator()(const llarp::service::Address& addr) const { - return llarp::service::Address::Hash{}(addr); + return std::accumulate(addr.begin(), addr.end(), 0, std::bit_xor{}); } }; } // namespace std diff --git a/llarp/service/endpoint.hpp b/llarp/service/endpoint.hpp index e0f8b2c82..cabde8dc9 100644 --- a/llarp/service/endpoint.hpp +++ b/llarp/service/endpoint.hpp @@ -442,7 +442,7 @@ namespace llarp bool m_PublishIntroSet = true; std::unique_ptr m_state; std::shared_ptr m_AuthPolicy; - std::unordered_map m_RemoteAuthInfos; + std::unordered_map m_RemoteAuthInfos; /// (lns name, optional exit range, optional auth info) for looking up on startup std::unordered_map, std::optional>> @@ -463,7 +463,7 @@ namespace llarp const IntroSet& introSet() const; IntroSet& introSet(); - using ConvoMap = std::unordered_map< ConvoTag, Session, ConvoTag::Hash >; + using ConvoMap = std::unordered_map; const ConvoMap& Sessions() const; ConvoMap& Sessions(); // clang-format on diff --git a/llarp/service/endpoint_state.hpp b/llarp/service/endpoint_state.hpp index 9bae738b9..f33e848b7 100644 --- a/llarp/service/endpoint_state.hpp +++ b/llarp/service/endpoint_state.hpp @@ -43,10 +43,10 @@ namespace llarp SNodeSessions m_SNodeSessions; - std::unordered_multimap m_PendingServiceLookups; - std::unordered_map m_LastServiceLookupTimes; + std::unordered_multimap m_PendingServiceLookups; + std::unordered_map m_LastServiceLookupTimes; - std::unordered_map m_ServiceLookupFails; + std::unordered_map m_ServiceLookupFails; PendingRouters m_PendingRouters; diff --git a/llarp/service/endpoint_types.hpp b/llarp/service/endpoint_types.hpp index 07be14333..7eee10a68 100644 --- a/llarp/service/endpoint_types.hpp +++ b/llarp/service/endpoint_types.hpp @@ -30,26 +30,25 @@ namespace llarp using SendMessageQueue_t = thread::Queue; using PendingBufferQueue = std::deque; - using PendingTraffic = std::unordered_map; + using PendingTraffic = std::unordered_map; using ProtocolMessagePtr = std::shared_ptr; using RecvPacketQueue_t = thread::Queue; - using PendingRouters = std::unordered_map; + using PendingRouters = std::unordered_map; using PendingLookups = std::unordered_map>; - using Sessions = - std::unordered_multimap, Address::Hash>; + using Sessions = std::unordered_multimap>; using SNodeSessionValue = std::pair, ConvoTag>; - using SNodeSessions = std::unordered_multimap; + using SNodeSessions = std::unordered_multimap; - using ConvoMap = std::unordered_map; + using ConvoMap = std::unordered_map; /// set of outbound addresses to maintain to - using OutboundSessions_t = std::unordered_set; + using OutboundSessions_t = std::unordered_set
; using PathEnsureHook = std::function; diff --git a/llarp/service/handler.hpp b/llarp/service/handler.hpp index f5c3528e6..d4d39370d 100644 --- a/llarp/service/handler.hpp +++ b/llarp/service/handler.hpp @@ -12,7 +12,8 @@ namespace llarp { namespace service { - using ConvoTag = AlignedBuffer<16>; + struct ConvoTag final : AlignedBuffer<16> + {}; struct ProtocolMessage; struct RecvDataEvent diff --git a/llarp/service/intro.hpp b/llarp/service/intro.hpp index 936af0738..1fa5fd790 100644 --- a/llarp/service/intro.hpp +++ b/llarp/service/intro.hpp @@ -70,15 +70,6 @@ namespace llarp { return pathID != other.pathID || router != other.router; } - - struct Hash - { - size_t - operator()(const Introduction& i) const - { - return PubKey::Hash()(i.router) ^ PathID_t::Hash()(i.pathID); - } - }; }; inline std::ostream& @@ -88,3 +79,16 @@ namespace llarp } } // namespace service } // namespace llarp + +namespace std +{ + template <> + struct hash + { + size_t + operator()(const llarp::service::Introduction& i) const + { + return std::hash{}(i.router) ^ std::hash{}(i.pathID); + } + }; +} // namespace std diff --git a/llarp/service/outbound_context.hpp b/llarp/service/outbound_context.hpp index af9896463..570f70f7d 100644 --- a/llarp/service/outbound_context.hpp +++ b/llarp/service/outbound_context.hpp @@ -135,7 +135,7 @@ namespace llarp uint64_t m_UpdateIntrosetTX = 0; IntroSet currentIntroSet; Introduction m_NextIntro; - std::unordered_map m_BadIntros; + std::unordered_map m_BadIntros; llarp_time_t lastShift = 0s; uint16_t m_LookupFails = 0; uint16_t m_BuildFails = 0; diff --git a/llarp/service/tag.hpp b/llarp/service/tag.hpp index 219885af9..a4e3dba4e 100644 --- a/llarp/service/tag.hpp +++ b/llarp/service/tag.hpp @@ -49,8 +49,13 @@ namespace llarp { return data()[0] == 0; } - - using Hash = AlignedBuffer::Hash; }; } // namespace service } // namespace llarp + +namespace std +{ + template <> + struct hash : hash> + {}; +} // namespace std diff --git a/llarp/tooling/router_hive.hpp b/llarp/tooling/router_hive.hpp index 5428bb790..54826a7f5 100644 --- a/llarp/tooling/router_hive.hpp +++ b/llarp/tooling/router_hive.hpp @@ -83,8 +83,8 @@ namespace tooling GetRelayRCs(); std::mutex routerMutex; - std::unordered_map relays; - std::unordered_map clients; + std::unordered_map relays; + std::unordered_map clients; std::vector routerMainThreads; diff --git a/llarp/util/aligned.hpp b/llarp/util/aligned.hpp index aba9b83e7..f2507e1ba 100644 --- a/llarp/util/aligned.hpp +++ b/llarp/util/aligned.hpp @@ -289,18 +289,22 @@ namespace llarp return stream; } - struct Hash - { - std::size_t - operator()(const AlignedBuffer& buf) const noexcept - { - std::size_t h = 0; - std::memcpy(&h, buf.data(), sizeof(std::size_t)); - return h; - } - }; - private: Data m_data; }; } // namespace llarp + +namespace std +{ + template + struct hash> + { + std::size_t + operator()(const llarp::AlignedBuffer& buf) const noexcept + { + std::size_t h = 0; + std::memcpy(&h, buf.data(), sizeof(std::size_t)); + return h; + } + }; +} // namespace std diff --git a/llarp/util/decaying_hashset.hpp b/llarp/util/decaying_hashset.hpp index a941440f5..82c395ef4 100644 --- a/llarp/util/decaying_hashset.hpp +++ b/llarp/util/decaying_hashset.hpp @@ -7,7 +7,7 @@ namespace llarp { namespace util { - template + template > struct DecayingHashSet { using Time_t = std::chrono::milliseconds; diff --git a/llarp/util/decaying_hashtable.hpp b/llarp/util/decaying_hashtable.hpp index 2f7ea7c03..09fc1c787 100644 --- a/llarp/util/decaying_hashtable.hpp +++ b/llarp/util/decaying_hashtable.hpp @@ -5,7 +5,7 @@ namespace llarp::util { - template + template > struct DecayingHashTable { DecayingHashTable(std::chrono::milliseconds cacheInterval = 1h) : m_CacheInterval(cacheInterval) diff --git a/test/util/test_llarp_util_aligned.cpp b/test/util/test_llarp_util_aligned.cpp index b84fa40a9..8cda24eff 100644 --- a/test/util/test_llarp_util_aligned.cpp +++ b/test/util/test_llarp_util_aligned.cpp @@ -162,7 +162,7 @@ TEMPLATE_LIST_TEST_CASE("AlignedBuffer", "[AlignedBuffer]", TestSizes) SECTION("TestHash") { - using Map_t = std::unordered_map; + using Map_t = std::unordered_map; Buffer k, other_k; k.Randomize();