From 492654db493f6c1aa868f7d843431be5cb5da3a6 Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Thu, 24 Mar 2022 19:56:00 +0100 Subject: [PATCH] merge bitcoin#24662: 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 | 8 ++++---- src/net_processing.cpp | 6 +++--- src/rpc/net.cpp | 3 ++- src/test/addrman_tests.cpp | 18 +++++++++--------- src/timedata.h | 1 - 9 files changed, 29 insertions(+), 30 deletions(-) diff --git a/src/addrman.cpp b/src/addrman.cpp index e0b1e3270a..3a54ebbf70 100644 --- a/src/addrman.cpp +++ b/src/addrman.cpp @@ -14,10 +14,10 @@ #include #include #include -#include #include #include #include +#include #include #include @@ -567,7 +567,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); @@ -821,7 +821,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) @@ -918,7 +918,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) { @@ -942,7 +942,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 111b38f153..d927232b2b 100644 --- a/src/addrman.h +++ b/src/addrman.h @@ -10,7 +10,6 @@ #include #include #include -#include #include #include @@ -138,10 +137,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(); @@ -191,7 +190,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 825f33567f..08c597e667 100644 --- a/src/addrman_impl.h +++ b/src/addrman_impl.h @@ -93,10 +93,10 @@ public: int GetBucketPosition(const uint256 &nKey, bool fNew, int bucket) 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 94cf6fe702..575f90766a 100644 --- a/src/bench/addrman.cpp +++ b/src/bench/addrman.cpp @@ -42,7 +42,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 8965f784c3..3df32e1881 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -512,11 +512,11 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "trying %s connection %s lastseen=%.1fhrs\n", use_v2transport ? "v2" : "v1", pszDest ? pszDest : addrConnect.ToStringAddrPort(), - Ticks(pszDest ? 0h : AdjustedTime() - addrConnect.nTime)); + Ticks(pszDest ? 0h : Now() - addrConnect.nTime)); } else { LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "trying %s connection lastseen=%.1fhrs\n", use_v2transport ? "v2" : "v1", - Ticks(pszDest ? 0h : AdjustedTime() - addrConnect.nTime)); + Ticks(pszDest ? 0h : Now() - addrConnect.nTime)); } // Resolve @@ -3450,7 +3450,7 @@ void CConnman::ThreadOpenConnections(const std::vector connect, CDe auto mnList = dmnman.GetListAtChainTip(); - const auto nANow{AdjustedTime()}; + const auto current_time{NodeClock::now()}; int nTries = 0; while (!interruptNet) { @@ -3529,7 +3529,7 @@ void CConnman::ThreadOpenConnections(const std::vector connect, CDe if (onion_only && !addr.IsTor()) 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 1830729924..ceedd4ad9c 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -3696,7 +3696,7 @@ void PeerManagerImpl::ProcessMessage( // indicate to the peer that we will participate in addr relay. if (fListen && !m_chainman.ActiveChainstate().IsInitialBlockDownload()) { - CAddress addr{GetLocalAddress(pfrom), peer->m_our_services, AdjustedTime()}; + CAddress addr{GetLocalAddress(pfrom), peer->m_our_services, Now()}; FastRandomContext insecure_rand; if (addr.IsRoutable()) { @@ -3983,7 +3983,7 @@ void PeerManagerImpl::ProcessMessage( // 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()}; @@ -5668,7 +5668,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 922edef604..a8206e1f7e 100644 --- a/src/rpc/net.cpp +++ b/src/rpc/net.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -1015,7 +1016,7 @@ static RPCHelpMan addpeeraddress() if (net_addr.has_value()) { CAddress address{{net_addr.value(), port}, ServiceFlags{NODE_NETWORK}}; - 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 78d6ec9feb..ce9a2bca82 100644 --- a/src/test/addrman_tests.cpp +++ b/src/test/addrman_tests.cpp @@ -309,7 +309,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 @@ -379,15 +379,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"); @@ -413,7 +413,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); @@ -906,7 +906,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(); @@ -1053,7 +1053,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 8c036f1c1a..3779486c1f 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); /**