From fadd8b2676f6d68ec87189871461c9a6a6aa3cac Mon Sep 17 00:00:00 2001 From: MarcoFalke Date: Thu, 24 Mar 2022 19:56:00 +0100 Subject: addrman: Use system time instead of adjusted network time --- src/addrman.cpp | 10 +++++----- src/addrman.h | 7 +++---- src/addrman_impl.h | 4 ++-- src/bench/addrman.cpp | 2 +- src/net.cpp | 6 +++--- src/net_processing.cpp | 6 +++--- src/rpc/net.cpp | 3 ++- src/test/addrman_tests.cpp | 18 +++++++++--------- src/timedata.h | 1 - 9 files changed, 28 insertions(+), 29 deletions(-) diff --git a/src/addrman.cpp b/src/addrman.cpp index 857ad73b2f..f16ff2230b 100644 --- a/src/addrman.cpp +++ b/src/addrman.cpp @@ -14,10 +14,10 @@ #include #include #include -#include #include #include #include +#include #include #include @@ -560,7 +560,7 @@ bool AddrManImpl::AddSingle(const CAddress& addr, const CNetAddr& source, std::c if (pinfo) { // periodically update nTime - const bool currently_online{AdjustedTime() - addr.nTime < 24h}; + const bool currently_online{NodeClock::now() - addr.nTime < 24h}; const auto update_interval{currently_online ? 1h : 24h}; if (pinfo->nTime < addr.nTime - update_interval - time_penalty) { pinfo->nTime = std::max(NodeSeconds{0s}, addr.nTime - time_penalty); @@ -788,7 +788,7 @@ std::vector AddrManImpl::GetAddr_(size_t max_addresses, size_t max_pct } // gather a list of random nodes, skipping those of low quality - const auto now{AdjustedTime()}; + const auto now{Now()}; std::vector addresses; for (unsigned int n = 0; n < vRandom.size(); n++) { if (addresses.size() >= nNodes) @@ -874,7 +874,7 @@ void AddrManImpl::ResolveCollisions_() int id_old = vvTried[tried_bucket][tried_bucket_pos]; AddrInfo& info_old = mapInfo[id_old]; - const auto current_time{AdjustedTime()}; + const auto current_time{Now()}; // Has successfully connected in last X hours if (current_time - info_old.m_last_success < ADDRMAN_REPLACEMENT) { @@ -898,7 +898,7 @@ void AddrManImpl::ResolveCollisions_() erase_collision = true; } } else { // Collision is not actually a collision anymore - Good_(info_new, false, AdjustedTime()); + Good_(info_new, false, Now()); erase_collision = true; } } diff --git a/src/addrman.h b/src/addrman.h index b70c6a48ad..5099c8c7a3 100644 --- a/src/addrman.h +++ b/src/addrman.h @@ -10,7 +10,6 @@ #include #include #include -#include #include #include @@ -121,10 +120,10 @@ public: * @param[in] time The time that we were last connected to this peer. * @return true if the address is successfully moved from the new table to the tried table. */ - bool Good(const CService& addr, NodeSeconds time = AdjustedTime()); + bool Good(const CService& addr, NodeSeconds time = Now()); //! Mark an entry as connection attempted to. - void Attempt(const CService& addr, bool fCountFailure, NodeSeconds time = AdjustedTime()); + void Attempt(const CService& addr, bool fCountFailure, NodeSeconds time = Now()); //! See if any to-be-evicted tried table entries have been tested and if so resolve the collisions. void ResolveCollisions(); @@ -169,7 +168,7 @@ public: * @param[in] addr The address of the peer we were connected to * @param[in] time The time that we were last connected to this peer */ - void Connected(const CService& addr, NodeSeconds time = AdjustedTime()); + void Connected(const CService& addr, NodeSeconds time = Now()); //! Update an entry's service bits. void SetServices(const CService& addr, ServiceFlags nServices); diff --git a/src/addrman_impl.h b/src/addrman_impl.h index a73a026940..376e79f49f 100644 --- a/src/addrman_impl.h +++ b/src/addrman_impl.h @@ -93,10 +93,10 @@ public: int GetBucketPosition(const uint256 &nKey, bool fNew, int nBucket) const; //! Determine whether the statistics about this entry are bad enough so that it can just be deleted - bool IsTerrible(NodeSeconds now = AdjustedTime()) const; + bool IsTerrible(NodeSeconds now = Now()) const; //! Calculate the relative chance this entry should be given when selecting nodes to connect to - double GetChance(NodeSeconds now = AdjustedTime()) const; + double GetChance(NodeSeconds now = Now()) const; }; class AddrManImpl diff --git a/src/bench/addrman.cpp b/src/bench/addrman.cpp index 2600b03022..f14d1f89b6 100644 --- a/src/bench/addrman.cpp +++ b/src/bench/addrman.cpp @@ -43,7 +43,7 @@ static void CreateAddresses() CAddress ret(CService(addr, port), NODE_NETWORK); - ret.nTime = AdjustedTime(); + ret.nTime = Now(); return ret; }; diff --git a/src/net.cpp b/src/net.cpp index c4aaac4986..e87ac079b5 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -454,7 +454,7 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "trying connection %s lastseen=%.1fhrs\n", pszDest ? pszDest : addrConnect.ToString(), - Ticks(pszDest ? 0h : AdjustedTime() - addrConnect.nTime)); + Ticks(pszDest ? 0h : Now() - addrConnect.nTime)); // Resolve const uint16_t default_port{pszDest != nullptr ? Params().GetDefaultPort(pszDest) : @@ -1735,7 +1735,7 @@ void CConnman::ThreadOpenConnections(const std::vector connect) addrman.ResolveCollisions(); - const auto nANow{AdjustedTime()}; + const auto current_time{NodeClock::now()}; int nTries = 0; while (!interruptNet) { @@ -1798,7 +1798,7 @@ void CConnman::ThreadOpenConnections(const std::vector connect) continue; // only consider very recently tried nodes after 30 failed attempts - if (nANow - addr_last_try < 10min && nTries < 30) { + if (current_time - addr_last_try < 10min && nTries < 30) { continue; } diff --git a/src/net_processing.cpp b/src/net_processing.cpp index 0e10fa5f9d..c657afa56a 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -2930,7 +2930,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type, // indicate to the peer that we will participate in addr relay. if (fListen && !m_chainman.ActiveChainstate().IsInitialBlockDownload()) { - CAddress addr{GetLocalAddress(pfrom.addr), peer->m_our_services, AdjustedTime()}; + CAddress addr{GetLocalAddress(pfrom.addr), peer->m_our_services, Now()}; FastRandomContext insecure_rand; if (addr.IsRoutable()) { @@ -3135,7 +3135,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type, // Store the new addresses std::vector vAddrOk; - const auto current_a_time{AdjustedTime()}; + const auto current_a_time{Now()}; // Update/increment addr rate limiting bucket. const auto current_time{GetTime()}; @@ -4683,7 +4683,7 @@ void PeerManagerImpl::MaybeSendAddr(CNode& node, Peer& peer, std::chrono::micros peer.m_addr_known->reset(); } if (std::optional local_service = GetLocalAddrForPeer(node)) { - CAddress local_addr{*local_service, peer.m_our_services, AdjustedTime()}; + CAddress local_addr{*local_service, peer.m_our_services, Now()}; FastRandomContext insecure_rand; PushAddress(peer, local_addr, insecure_rand); } diff --git a/src/rpc/net.cpp b/src/rpc/net.cpp index 059be61bb4..e2d727e0a6 100644 --- a/src/rpc/net.cpp +++ b/src/rpc/net.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -942,7 +943,7 @@ static RPCHelpMan addpeeraddress() if (LookupHost(addr_string, net_addr, false)) { CAddress address{{net_addr, port}, ServiceFlags{NODE_NETWORK | NODE_WITNESS}}; - address.nTime = AdjustedTime(); + address.nTime = Now(); // The source address is set equal to the address. This is equivalent to the peer // announcing itself. if (node.addrman->Add({address}, address)) { diff --git a/src/test/addrman_tests.cpp b/src/test/addrman_tests.cpp index b1372a3e98..b10d32ccec 100644 --- a/src/test/addrman_tests.cpp +++ b/src/test/addrman_tests.cpp @@ -225,7 +225,7 @@ BOOST_AUTO_TEST_CASE(addrman_new_multiplicity) { auto addrman = std::make_unique(EMPTY_NETGROUPMAN, DETERMINISTIC, GetCheckRatio(m_node)); CAddress addr{CAddress(ResolveService("253.3.3.3", 8333), NODE_NONE)}; - const auto start_time{AdjustedTime()}; + const auto start_time{Now()}; addr.nTime = start_time; // test that multiplicity stays at 1 if nTime doesn't increase @@ -295,15 +295,15 @@ BOOST_AUTO_TEST_CASE(addrman_getaddr) BOOST_CHECK_EQUAL(vAddr1.size(), 0U); CAddress addr1 = CAddress(ResolveService("250.250.2.1", 8333), NODE_NONE); - addr1.nTime = AdjustedTime(); // Set time so isTerrible = false + addr1.nTime = Now(); // Set time so isTerrible = false CAddress addr2 = CAddress(ResolveService("250.251.2.2", 9999), NODE_NONE); - addr2.nTime = AdjustedTime(); + addr2.nTime = Now(); CAddress addr3 = CAddress(ResolveService("251.252.2.3", 8333), NODE_NONE); - addr3.nTime = AdjustedTime(); + addr3.nTime = Now(); CAddress addr4 = CAddress(ResolveService("252.253.3.4", 8333), NODE_NONE); - addr4.nTime = AdjustedTime(); + addr4.nTime = Now(); CAddress addr5 = CAddress(ResolveService("252.254.4.5", 8333), NODE_NONE); - addr5.nTime = AdjustedTime(); + addr5.nTime = Now(); CNetAddr source1 = ResolveIP("250.1.2.1"); CNetAddr source2 = ResolveIP("250.2.3.3"); @@ -329,7 +329,7 @@ BOOST_AUTO_TEST_CASE(addrman_getaddr) CAddress addr = CAddress(ResolveService(strAddr), NODE_NONE); // Ensure that for all addrs in addrman, isTerrible == false. - addr.nTime = AdjustedTime(); + addr.nTime = Now(); addrman->Add({addr}, ResolveIP(strAddr)); if (i % 8 == 0) addrman->Good(addr); @@ -822,7 +822,7 @@ BOOST_AUTO_TEST_CASE(addrman_evictionworks) // Ensure test of address fails, so that it is evicted. // Update entry in tried by setting last good connection in the deep past. BOOST_CHECK(!addrman->Good(info, NodeSeconds{1s})); - addrman->Attempt(info, /*fCountFailure=*/false, AdjustedTime() - 61s); + addrman->Attempt(info, /*fCountFailure=*/false, Now() - 61s); // Should swap 36 for 19. addrman->ResolveCollisions(); @@ -966,7 +966,7 @@ BOOST_AUTO_TEST_CASE(addrman_update_address) CNetAddr source{ResolveIP("252.2.2.2")}; CAddress addr{CAddress(ResolveService("250.1.1.1", 8333), NODE_NONE)}; - const auto start_time{AdjustedTime() - 10000s}; + const auto start_time{Now() - 10000s}; addr.nTime = start_time; BOOST_CHECK(addrman->Add({addr}, source)); BOOST_CHECK_EQUAL(addrman->size(), 1U); diff --git a/src/timedata.h b/src/timedata.h index ed2d8639f7..cfe5111644 100644 --- a/src/timedata.h +++ b/src/timedata.h @@ -76,7 +76,6 @@ public: /** Functions to keep track of adjusted P2P time */ int64_t GetTimeOffset(); int64_t GetAdjustedTime(); -inline NodeSeconds AdjustedTime() { return Now() + std::chrono::seconds{GetTimeOffset()}; } void AddTimeData(const CNetAddr& ip, int64_t nTime); /** -- cgit v1.2.3