Skip to content

Commit fadd8b2

Browse files
author
MarcoFalke
committed
addrman: Use system time instead of adjusted network time
1 parent ab3db34 commit fadd8b2

File tree

9 files changed

+28
-29
lines changed

9 files changed

+28
-29
lines changed

src/addrman.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
#include <random.h>
1515
#include <serialize.h>
1616
#include <streams.h>
17-
#include <timedata.h>
1817
#include <tinyformat.h>
1918
#include <uint256.h>
2019
#include <util/check.h>
20+
#include <util/time.h>
2121

2222
#include <cmath>
2323
#include <optional>
@@ -560,7 +560,7 @@ bool AddrManImpl::AddSingle(const CAddress& addr, const CNetAddr& source, std::c
560560

561561
if (pinfo) {
562562
// periodically update nTime
563-
const bool currently_online{AdjustedTime() - addr.nTime < 24h};
563+
const bool currently_online{NodeClock::now() - addr.nTime < 24h};
564564
const auto update_interval{currently_online ? 1h : 24h};
565565
if (pinfo->nTime < addr.nTime - update_interval - time_penalty) {
566566
pinfo->nTime = std::max(NodeSeconds{0s}, addr.nTime - time_penalty);
@@ -788,7 +788,7 @@ std::vector<CAddress> AddrManImpl::GetAddr_(size_t max_addresses, size_t max_pct
788788
}
789789

790790
// gather a list of random nodes, skipping those of low quality
791-
const auto now{AdjustedTime()};
791+
const auto now{Now<NodeSeconds>()};
792792
std::vector<CAddress> addresses;
793793
for (unsigned int n = 0; n < vRandom.size(); n++) {
794794
if (addresses.size() >= nNodes)
@@ -874,7 +874,7 @@ void AddrManImpl::ResolveCollisions_()
874874
int id_old = vvTried[tried_bucket][tried_bucket_pos];
875875
AddrInfo& info_old = mapInfo[id_old];
876876

877-
const auto current_time{AdjustedTime()};
877+
const auto current_time{Now<NodeSeconds>()};
878878

879879
// Has successfully connected in last X hours
880880
if (current_time - info_old.m_last_success < ADDRMAN_REPLACEMENT) {
@@ -898,7 +898,7 @@ void AddrManImpl::ResolveCollisions_()
898898
erase_collision = true;
899899
}
900900
} else { // Collision is not actually a collision anymore
901-
Good_(info_new, false, AdjustedTime());
901+
Good_(info_new, false, Now<NodeSeconds>());
902902
erase_collision = true;
903903
}
904904
}

src/addrman.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include <netgroup.h>
1111
#include <protocol.h>
1212
#include <streams.h>
13-
#include <timedata.h>
1413
#include <util/time.h>
1514

1615
#include <cstdint>
@@ -121,10 +120,10 @@ class AddrMan
121120
* @param[in] time The time that we were last connected to this peer.
122121
* @return true if the address is successfully moved from the new table to the tried table.
123122
*/
124-
bool Good(const CService& addr, NodeSeconds time = AdjustedTime());
123+
bool Good(const CService& addr, NodeSeconds time = Now<NodeSeconds>());
125124

126125
//! Mark an entry as connection attempted to.
127-
void Attempt(const CService& addr, bool fCountFailure, NodeSeconds time = AdjustedTime());
126+
void Attempt(const CService& addr, bool fCountFailure, NodeSeconds time = Now<NodeSeconds>());
128127

129128
//! See if any to-be-evicted tried table entries have been tested and if so resolve the collisions.
130129
void ResolveCollisions();
@@ -169,7 +168,7 @@ class AddrMan
169168
* @param[in] addr The address of the peer we were connected to
170169
* @param[in] time The time that we were last connected to this peer
171170
*/
172-
void Connected(const CService& addr, NodeSeconds time = AdjustedTime());
171+
void Connected(const CService& addr, NodeSeconds time = Now<NodeSeconds>());
173172

174173
//! Update an entry's service bits.
175174
void SetServices(const CService& addr, ServiceFlags nServices);

src/addrman_impl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@ class AddrInfo : public CAddress
9393
int GetBucketPosition(const uint256 &nKey, bool fNew, int nBucket) const;
9494

9595
//! Determine whether the statistics about this entry are bad enough so that it can just be deleted
96-
bool IsTerrible(NodeSeconds now = AdjustedTime()) const;
96+
bool IsTerrible(NodeSeconds now = Now<NodeSeconds>()) const;
9797

9898
//! Calculate the relative chance this entry should be given when selecting nodes to connect to
99-
double GetChance(NodeSeconds now = AdjustedTime()) const;
99+
double GetChance(NodeSeconds now = Now<NodeSeconds>()) const;
100100
};
101101

102102
class AddrManImpl

src/bench/addrman.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ static void CreateAddresses()
4343

4444
CAddress ret(CService(addr, port), NODE_NETWORK);
4545

46-
ret.nTime = AdjustedTime();
46+
ret.nTime = Now<NodeSeconds>();
4747

4848
return ret;
4949
};

src/net.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
454454

455455
LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "trying connection %s lastseen=%.1fhrs\n",
456456
pszDest ? pszDest : addrConnect.ToString(),
457-
Ticks<HoursDouble>(pszDest ? 0h : AdjustedTime() - addrConnect.nTime));
457+
Ticks<HoursDouble>(pszDest ? 0h : Now<NodeSeconds>() - addrConnect.nTime));
458458

459459
// Resolve
460460
const uint16_t default_port{pszDest != nullptr ? Params().GetDefaultPort(pszDest) :
@@ -1735,7 +1735,7 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
17351735

17361736
addrman.ResolveCollisions();
17371737

1738-
const auto nANow{AdjustedTime()};
1738+
const auto current_time{NodeClock::now()};
17391739
int nTries = 0;
17401740
while (!interruptNet)
17411741
{
@@ -1798,7 +1798,7 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
17981798
continue;
17991799

18001800
// only consider very recently tried nodes after 30 failed attempts
1801-
if (nANow - addr_last_try < 10min && nTries < 30) {
1801+
if (current_time - addr_last_try < 10min && nTries < 30) {
18021802
continue;
18031803
}
18041804

src/net_processing.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2930,7 +2930,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
29302930
// indicate to the peer that we will participate in addr relay.
29312931
if (fListen && !m_chainman.ActiveChainstate().IsInitialBlockDownload())
29322932
{
2933-
CAddress addr{GetLocalAddress(pfrom.addr), peer->m_our_services, AdjustedTime()};
2933+
CAddress addr{GetLocalAddress(pfrom.addr), peer->m_our_services, Now<NodeSeconds>()};
29342934
FastRandomContext insecure_rand;
29352935
if (addr.IsRoutable())
29362936
{
@@ -3135,7 +3135,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
31353135

31363136
// Store the new addresses
31373137
std::vector<CAddress> vAddrOk;
3138-
const auto current_a_time{AdjustedTime()};
3138+
const auto current_a_time{Now<NodeSeconds>()};
31393139

31403140
// Update/increment addr rate limiting bucket.
31413141
const auto current_time{GetTime<std::chrono::microseconds>()};
@@ -4683,7 +4683,7 @@ void PeerManagerImpl::MaybeSendAddr(CNode& node, Peer& peer, std::chrono::micros
46834683
peer.m_addr_known->reset();
46844684
}
46854685
if (std::optional<CService> local_service = GetLocalAddrForPeer(node)) {
4686-
CAddress local_addr{*local_service, peer.m_our_services, AdjustedTime()};
4686+
CAddress local_addr{*local_service, peer.m_our_services, Now<NodeSeconds>()};
46874687
FastRandomContext insecure_rand;
46884688
PushAddress(peer, local_addr, insecure_rand);
46894689
}

src/rpc/net.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <timedata.h>
2424
#include <util/strencodings.h>
2525
#include <util/string.h>
26+
#include <util/time.h>
2627
#include <util/translation.h>
2728
#include <validation.h>
2829
#include <version.h>
@@ -942,7 +943,7 @@ static RPCHelpMan addpeeraddress()
942943

943944
if (LookupHost(addr_string, net_addr, false)) {
944945
CAddress address{{net_addr, port}, ServiceFlags{NODE_NETWORK | NODE_WITNESS}};
945-
address.nTime = AdjustedTime();
946+
address.nTime = Now<NodeSeconds>();
946947
// The source address is set equal to the address. This is equivalent to the peer
947948
// announcing itself.
948949
if (node.addrman->Add({address}, address)) {

src/test/addrman_tests.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ BOOST_AUTO_TEST_CASE(addrman_new_multiplicity)
225225
{
226226
auto addrman = std::make_unique<AddrMan>(EMPTY_NETGROUPMAN, DETERMINISTIC, GetCheckRatio(m_node));
227227
CAddress addr{CAddress(ResolveService("253.3.3.3", 8333), NODE_NONE)};
228-
const auto start_time{AdjustedTime()};
228+
const auto start_time{Now<NodeSeconds>()};
229229
addr.nTime = start_time;
230230

231231
// test that multiplicity stays at 1 if nTime doesn't increase
@@ -295,15 +295,15 @@ BOOST_AUTO_TEST_CASE(addrman_getaddr)
295295
BOOST_CHECK_EQUAL(vAddr1.size(), 0U);
296296

297297
CAddress addr1 = CAddress(ResolveService("250.250.2.1", 8333), NODE_NONE);
298-
addr1.nTime = AdjustedTime(); // Set time so isTerrible = false
298+
addr1.nTime = Now<NodeSeconds>(); // Set time so isTerrible = false
299299
CAddress addr2 = CAddress(ResolveService("250.251.2.2", 9999), NODE_NONE);
300-
addr2.nTime = AdjustedTime();
300+
addr2.nTime = Now<NodeSeconds>();
301301
CAddress addr3 = CAddress(ResolveService("251.252.2.3", 8333), NODE_NONE);
302-
addr3.nTime = AdjustedTime();
302+
addr3.nTime = Now<NodeSeconds>();
303303
CAddress addr4 = CAddress(ResolveService("252.253.3.4", 8333), NODE_NONE);
304-
addr4.nTime = AdjustedTime();
304+
addr4.nTime = Now<NodeSeconds>();
305305
CAddress addr5 = CAddress(ResolveService("252.254.4.5", 8333), NODE_NONE);
306-
addr5.nTime = AdjustedTime();
306+
addr5.nTime = Now<NodeSeconds>();
307307
CNetAddr source1 = ResolveIP("250.1.2.1");
308308
CNetAddr source2 = ResolveIP("250.2.3.3");
309309

@@ -329,7 +329,7 @@ BOOST_AUTO_TEST_CASE(addrman_getaddr)
329329
CAddress addr = CAddress(ResolveService(strAddr), NODE_NONE);
330330

331331
// Ensure that for all addrs in addrman, isTerrible == false.
332-
addr.nTime = AdjustedTime();
332+
addr.nTime = Now<NodeSeconds>();
333333
addrman->Add({addr}, ResolveIP(strAddr));
334334
if (i % 8 == 0)
335335
addrman->Good(addr);
@@ -822,7 +822,7 @@ BOOST_AUTO_TEST_CASE(addrman_evictionworks)
822822
// Ensure test of address fails, so that it is evicted.
823823
// Update entry in tried by setting last good connection in the deep past.
824824
BOOST_CHECK(!addrman->Good(info, NodeSeconds{1s}));
825-
addrman->Attempt(info, /*fCountFailure=*/false, AdjustedTime() - 61s);
825+
addrman->Attempt(info, /*fCountFailure=*/false, Now<NodeSeconds>() - 61s);
826826

827827
// Should swap 36 for 19.
828828
addrman->ResolveCollisions();
@@ -966,7 +966,7 @@ BOOST_AUTO_TEST_CASE(addrman_update_address)
966966
CNetAddr source{ResolveIP("252.2.2.2")};
967967
CAddress addr{CAddress(ResolveService("250.1.1.1", 8333), NODE_NONE)};
968968

969-
const auto start_time{AdjustedTime() - 10000s};
969+
const auto start_time{Now<NodeSeconds>() - 10000s};
970970
addr.nTime = start_time;
971971
BOOST_CHECK(addrman->Add({addr}, source));
972972
BOOST_CHECK_EQUAL(addrman->size(), 1U);

src/timedata.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ class CMedianFilter
7676
/** Functions to keep track of adjusted P2P time */
7777
int64_t GetTimeOffset();
7878
int64_t GetAdjustedTime();
79-
inline NodeSeconds AdjustedTime() { return Now<NodeSeconds>() + std::chrono::seconds{GetTimeOffset()}; }
8079
void AddTimeData(const CNetAddr& ip, int64_t nTime);
8180

8281
/**

0 commit comments

Comments
 (0)