merge bitcoin#24662: Use system time instead of adjusted network time

This commit is contained in:
Kittywhiskers Van Gogh 2022-03-24 19:56:00 +01:00
parent bc3ec30144
commit 492654db49
No known key found for this signature in database
GPG Key ID: 30CD0C065E5C4AAD
9 changed files with 29 additions and 30 deletions

View File

@ -14,10 +14,10 @@
#include <random.h> #include <random.h>
#include <serialize.h> #include <serialize.h>
#include <streams.h> #include <streams.h>
#include <timedata.h>
#include <tinyformat.h> #include <tinyformat.h>
#include <uint256.h> #include <uint256.h>
#include <util/check.h> #include <util/check.h>
#include <util/time.h>
#include <cmath> #include <cmath>
#include <optional> #include <optional>
@ -567,7 +567,7 @@ bool AddrManImpl::AddSingle(const CAddress& addr, const CNetAddr& source, std::c
if (pinfo) { if (pinfo) {
// periodically update nTime // 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}; const auto update_interval{currently_online ? 1h : 24h};
if (pinfo->nTime < addr.nTime - update_interval - time_penalty) { if (pinfo->nTime < addr.nTime - update_interval - time_penalty) {
pinfo->nTime = std::max(NodeSeconds{0s}, addr.nTime - time_penalty); pinfo->nTime = std::max(NodeSeconds{0s}, addr.nTime - time_penalty);
@ -821,7 +821,7 @@ std::vector<CAddress> AddrManImpl::GetAddr_(size_t max_addresses, size_t max_pct
} }
// gather a list of random nodes, skipping those of low quality // gather a list of random nodes, skipping those of low quality
const auto now{AdjustedTime()}; const auto now{Now<NodeSeconds>()};
std::vector<CAddress> addresses; std::vector<CAddress> addresses;
for (unsigned int n = 0; n < vRandom.size(); n++) { for (unsigned int n = 0; n < vRandom.size(); n++) {
if (addresses.size() >= nNodes) if (addresses.size() >= nNodes)
@ -918,7 +918,7 @@ void AddrManImpl::ResolveCollisions_()
int id_old = vvTried[tried_bucket][tried_bucket_pos]; int id_old = vvTried[tried_bucket][tried_bucket_pos];
AddrInfo& info_old = mapInfo[id_old]; AddrInfo& info_old = mapInfo[id_old];
const auto current_time{AdjustedTime()}; const auto current_time{Now<NodeSeconds>()};
// Has successfully connected in last X hours // Has successfully connected in last X hours
if (current_time - info_old.m_last_success < ADDRMAN_REPLACEMENT) { if (current_time - info_old.m_last_success < ADDRMAN_REPLACEMENT) {
@ -942,7 +942,7 @@ void AddrManImpl::ResolveCollisions_()
erase_collision = true; erase_collision = true;
} }
} else { // Collision is not actually a collision anymore } else { // Collision is not actually a collision anymore
Good_(info_new, false, AdjustedTime()); Good_(info_new, false, Now<NodeSeconds>());
erase_collision = true; erase_collision = true;
} }
} }

View File

@ -10,7 +10,6 @@
#include <netgroup.h> #include <netgroup.h>
#include <protocol.h> #include <protocol.h>
#include <streams.h> #include <streams.h>
#include <timedata.h>
#include <util/time.h> #include <util/time.h>
#include <cstdint> #include <cstdint>
@ -138,10 +137,10 @@ public:
* @param[in] time The time that we were last connected to this peer. * @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. * @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<NodeSeconds>());
//! Mark an entry as connection attempted to. //! 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<NodeSeconds>());
//! See if any to-be-evicted tried table entries have been tested and if so resolve the collisions. //! See if any to-be-evicted tried table entries have been tested and if so resolve the collisions.
void ResolveCollisions(); void ResolveCollisions();
@ -191,7 +190,7 @@ public:
* @param[in] addr The address of the peer we were connected to * @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 * @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<NodeSeconds>());
//! Update an entry's service bits. //! Update an entry's service bits.
void SetServices(const CService& addr, ServiceFlags nServices); void SetServices(const CService& addr, ServiceFlags nServices);

View File

@ -93,10 +93,10 @@ public:
int GetBucketPosition(const uint256 &nKey, bool fNew, int bucket) const; 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 //! 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<NodeSeconds>()) const;
//! Calculate the relative chance this entry should be given when selecting nodes to connect to //! 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<NodeSeconds>()) const;
}; };
class AddrManImpl class AddrManImpl

View File

@ -42,7 +42,7 @@ static void CreateAddresses()
CAddress ret(CService(addr, port), NODE_NETWORK); CAddress ret(CService(addr, port), NODE_NETWORK);
ret.nTime = AdjustedTime(); ret.nTime = Now<NodeSeconds>();
return ret; return ret;
}; };

View File

@ -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", LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "trying %s connection %s lastseen=%.1fhrs\n",
use_v2transport ? "v2" : "v1", use_v2transport ? "v2" : "v1",
pszDest ? pszDest : addrConnect.ToStringAddrPort(), pszDest ? pszDest : addrConnect.ToStringAddrPort(),
Ticks<HoursDouble>(pszDest ? 0h : AdjustedTime() - addrConnect.nTime)); Ticks<HoursDouble>(pszDest ? 0h : Now<NodeSeconds>() - addrConnect.nTime));
} else { } else {
LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "trying %s connection lastseen=%.1fhrs\n", LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "trying %s connection lastseen=%.1fhrs\n",
use_v2transport ? "v2" : "v1", use_v2transport ? "v2" : "v1",
Ticks<HoursDouble>(pszDest ? 0h : AdjustedTime() - addrConnect.nTime)); Ticks<HoursDouble>(pszDest ? 0h : Now<NodeSeconds>() - addrConnect.nTime));
} }
// Resolve // Resolve
@ -3450,7 +3450,7 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect, CDe
auto mnList = dmnman.GetListAtChainTip(); auto mnList = dmnman.GetListAtChainTip();
const auto nANow{AdjustedTime()}; const auto current_time{NodeClock::now()};
int nTries = 0; int nTries = 0;
while (!interruptNet) while (!interruptNet)
{ {
@ -3529,7 +3529,7 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect, CDe
if (onion_only && !addr.IsTor()) continue; if (onion_only && !addr.IsTor()) continue;
// only consider very recently tried nodes after 30 failed attempts // 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; continue;
} }

View File

@ -3696,7 +3696,7 @@ void PeerManagerImpl::ProcessMessage(
// indicate to the peer that we will participate in addr relay. // indicate to the peer that we will participate in addr relay.
if (fListen && !m_chainman.ActiveChainstate().IsInitialBlockDownload()) if (fListen && !m_chainman.ActiveChainstate().IsInitialBlockDownload())
{ {
CAddress addr{GetLocalAddress(pfrom), peer->m_our_services, AdjustedTime()}; CAddress addr{GetLocalAddress(pfrom), peer->m_our_services, Now<NodeSeconds>()};
FastRandomContext insecure_rand; FastRandomContext insecure_rand;
if (addr.IsRoutable()) if (addr.IsRoutable())
{ {
@ -3983,7 +3983,7 @@ void PeerManagerImpl::ProcessMessage(
// Store the new addresses // Store the new addresses
std::vector<CAddress> vAddrOk; std::vector<CAddress> vAddrOk;
const auto current_a_time{AdjustedTime()}; const auto current_a_time{Now<NodeSeconds>()};
// Update/increment addr rate limiting bucket. // Update/increment addr rate limiting bucket.
const auto current_time{GetTime<std::chrono::microseconds>()}; const auto current_time{GetTime<std::chrono::microseconds>()};
@ -5668,7 +5668,7 @@ void PeerManagerImpl::MaybeSendAddr(CNode& node, Peer& peer, std::chrono::micros
peer.m_addr_known->reset(); peer.m_addr_known->reset();
} }
if (std::optional<CService> local_service = GetLocalAddrForPeer(node)) { if (std::optional<CService> local_service = GetLocalAddrForPeer(node)) {
CAddress local_addr{*local_service, peer.m_our_services, AdjustedTime()}; CAddress local_addr{*local_service, peer.m_our_services, Now<NodeSeconds>()};
FastRandomContext insecure_rand; FastRandomContext insecure_rand;
PushAddress(peer, local_addr, insecure_rand); PushAddress(peer, local_addr, insecure_rand);
} }

View File

@ -24,6 +24,7 @@
#include <timedata.h> #include <timedata.h>
#include <util/strencodings.h> #include <util/strencodings.h>
#include <util/string.h> #include <util/string.h>
#include <util/time.h>
#include <util/translation.h> #include <util/translation.h>
#include <validation.h> #include <validation.h>
#include <version.h> #include <version.h>
@ -1015,7 +1016,7 @@ static RPCHelpMan addpeeraddress()
if (net_addr.has_value()) { if (net_addr.has_value()) {
CAddress address{{net_addr.value(), port}, ServiceFlags{NODE_NETWORK}}; CAddress address{{net_addr.value(), port}, ServiceFlags{NODE_NETWORK}};
address.nTime = AdjustedTime(); address.nTime = Now<NodeSeconds>();
// The source address is set equal to the address. This is equivalent to the peer // The source address is set equal to the address. This is equivalent to the peer
// announcing itself. // announcing itself.
if (node.addrman->Add({address}, address)) { if (node.addrman->Add({address}, address)) {

View File

@ -309,7 +309,7 @@ BOOST_AUTO_TEST_CASE(addrman_new_multiplicity)
{ {
auto addrman = std::make_unique<AddrMan>(EMPTY_NETGROUPMAN, DETERMINISTIC, GetCheckRatio(m_node)); auto addrman = std::make_unique<AddrMan>(EMPTY_NETGROUPMAN, DETERMINISTIC, GetCheckRatio(m_node));
CAddress addr{CAddress(ResolveService("253.3.3.3", 8333), NODE_NONE)}; CAddress addr{CAddress(ResolveService("253.3.3.3", 8333), NODE_NONE)};
const auto start_time{AdjustedTime()}; const auto start_time{Now<NodeSeconds>()};
addr.nTime = start_time; addr.nTime = start_time;
// test that multiplicity stays at 1 if nTime doesn't increase // 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); BOOST_CHECK_EQUAL(vAddr1.size(), 0U);
CAddress addr1 = CAddress(ResolveService("250.250.2.1", 8333), NODE_NONE); CAddress addr1 = CAddress(ResolveService("250.250.2.1", 8333), NODE_NONE);
addr1.nTime = AdjustedTime(); // Set time so isTerrible = false addr1.nTime = Now<NodeSeconds>(); // Set time so isTerrible = false
CAddress addr2 = CAddress(ResolveService("250.251.2.2", 9999), NODE_NONE); CAddress addr2 = CAddress(ResolveService("250.251.2.2", 9999), NODE_NONE);
addr2.nTime = AdjustedTime(); addr2.nTime = Now<NodeSeconds>();
CAddress addr3 = CAddress(ResolveService("251.252.2.3", 8333), NODE_NONE); CAddress addr3 = CAddress(ResolveService("251.252.2.3", 8333), NODE_NONE);
addr3.nTime = AdjustedTime(); addr3.nTime = Now<NodeSeconds>();
CAddress addr4 = CAddress(ResolveService("252.253.3.4", 8333), NODE_NONE); CAddress addr4 = CAddress(ResolveService("252.253.3.4", 8333), NODE_NONE);
addr4.nTime = AdjustedTime(); addr4.nTime = Now<NodeSeconds>();
CAddress addr5 = CAddress(ResolveService("252.254.4.5", 8333), NODE_NONE); CAddress addr5 = CAddress(ResolveService("252.254.4.5", 8333), NODE_NONE);
addr5.nTime = AdjustedTime(); addr5.nTime = Now<NodeSeconds>();
CNetAddr source1 = ResolveIP("250.1.2.1"); CNetAddr source1 = ResolveIP("250.1.2.1");
CNetAddr source2 = ResolveIP("250.2.3.3"); CNetAddr source2 = ResolveIP("250.2.3.3");
@ -413,7 +413,7 @@ BOOST_AUTO_TEST_CASE(addrman_getaddr)
CAddress addr = CAddress(ResolveService(strAddr), NODE_NONE); CAddress addr = CAddress(ResolveService(strAddr), NODE_NONE);
// Ensure that for all addrs in addrman, isTerrible == false. // Ensure that for all addrs in addrman, isTerrible == false.
addr.nTime = AdjustedTime(); addr.nTime = Now<NodeSeconds>();
addrman->Add({addr}, ResolveIP(strAddr)); addrman->Add({addr}, ResolveIP(strAddr));
if (i % 8 == 0) if (i % 8 == 0)
addrman->Good(addr); addrman->Good(addr);
@ -906,7 +906,7 @@ BOOST_AUTO_TEST_CASE(addrman_evictionworks)
// Ensure test of address fails, so that it is evicted. // Ensure test of address fails, so that it is evicted.
// Update entry in tried by setting last good connection in the deep past. // Update entry in tried by setting last good connection in the deep past.
BOOST_CHECK(!addrman->Good(info, NodeSeconds{1s})); BOOST_CHECK(!addrman->Good(info, NodeSeconds{1s}));
addrman->Attempt(info, /*fCountFailure=*/false, AdjustedTime() - 61s); addrman->Attempt(info, /*fCountFailure=*/false, Now<NodeSeconds>() - 61s);
// Should swap 36 for 19. // Should swap 36 for 19.
addrman->ResolveCollisions(); addrman->ResolveCollisions();
@ -1053,7 +1053,7 @@ BOOST_AUTO_TEST_CASE(addrman_update_address)
CNetAddr source{ResolveIP("252.2.2.2")}; CNetAddr source{ResolveIP("252.2.2.2")};
CAddress addr{CAddress(ResolveService("250.1.1.1", 8333), NODE_NONE)}; CAddress addr{CAddress(ResolveService("250.1.1.1", 8333), NODE_NONE)};
const auto start_time{AdjustedTime() - 10000s}; const auto start_time{Now<NodeSeconds>() - 10000s};
addr.nTime = start_time; addr.nTime = start_time;
BOOST_CHECK(addrman->Add({addr}, source)); BOOST_CHECK(addrman->Add({addr}, source));
BOOST_CHECK_EQUAL(addrman->Size(), 1U); BOOST_CHECK_EQUAL(addrman->Size(), 1U);

View File

@ -76,7 +76,6 @@ public:
/** Functions to keep track of adjusted P2P time */ /** Functions to keep track of adjusted P2P time */
int64_t GetTimeOffset(); int64_t GetTimeOffset();
int64_t GetAdjustedTime(); int64_t GetAdjustedTime();
inline NodeSeconds AdjustedTime() { return Now<NodeSeconds>() + std::chrono::seconds{GetTimeOffset()}; }
void AddTimeData(const CNetAddr& ip, int64_t nTime); void AddTimeData(const CNetAddr& ip, int64_t nTime);
/** /**