Skip to content

Commit fa64dd6

Browse files
author
MarcoFalke
committed
refactor: Use type-safe std::chrono for addrman time
1 parent fa2ae37 commit fa64dd6

File tree

13 files changed

+162
-151
lines changed

13 files changed

+162
-151
lines changed

src/addrman.cpp

Lines changed: 68 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,19 @@ static constexpr uint32_t ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP{64};
2929
/** Maximum number of times an address can occur in the new table */
3030
static constexpr int32_t ADDRMAN_NEW_BUCKETS_PER_ADDRESS{8};
3131
/** How old addresses can maximally be */
32-
static constexpr int64_t ADDRMAN_HORIZON_DAYS{30};
32+
static constexpr auto ADDRMAN_HORIZON{30 * 24h};
3333
/** After how many failed attempts we give up on a new node */
3434
static constexpr int32_t ADDRMAN_RETRIES{3};
3535
/** How many successive failures are allowed ... */
3636
static constexpr int32_t ADDRMAN_MAX_FAILURES{10};
37-
/** ... in at least this many days */
38-
static constexpr int64_t ADDRMAN_MIN_FAIL_DAYS{7};
37+
/** ... in at least this duration */
38+
static constexpr auto ADDRMAN_MIN_FAIL{7 * 24h};
3939
/** How recent a successful connection should be before we allow an address to be evicted from tried */
40-
static constexpr int64_t ADDRMAN_REPLACEMENT_HOURS{4};
40+
static constexpr auto ADDRMAN_REPLACEMENT{4h};
4141
/** The maximum number of tried addr collisions to store */
4242
static constexpr size_t ADDRMAN_SET_TRIED_COLLISION_SIZE{10};
43-
/** The maximum time we'll spend trying to resolve a tried table collision, in seconds */
44-
static constexpr int64_t ADDRMAN_TEST_WINDOW{40*60}; // 40 minutes
43+
/** The maximum time we'll spend trying to resolve a tried table collision */
44+
static constexpr auto ADDRMAN_TEST_WINDOW{40min};
4545

4646
int AddrInfo::GetTriedBucket(const uint256& nKey, const NetGroupManager& netgroupman) const
4747
{
@@ -64,34 +64,37 @@ int AddrInfo::GetBucketPosition(const uint256& nKey, bool fNew, int nBucket) con
6464
return hash1 % ADDRMAN_BUCKET_SIZE;
6565
}
6666

67-
bool AddrInfo::IsTerrible(int64_t nNow) const
67+
bool AddrInfo::IsTerrible(NodeSeconds now) const
6868
{
69-
if (nNow - m_last_try <= 60) { // never remove things tried in the last minute
69+
if (now - m_last_try <= 1min) { // never remove things tried in the last minute
7070
return false;
7171
}
7272

73-
if (nTime > nNow + 10 * 60) // came in a flying DeLorean
73+
if (nTime > now + 10min) { // came in a flying DeLorean
7474
return true;
75+
}
7576

76-
if (nNow - nTime > ADDRMAN_HORIZON_DAYS * 24 * 60 * 60) { // not seen in recent history
77+
if (now - nTime > ADDRMAN_HORIZON) { // not seen in recent history
7778
return true;
7879
}
7980

80-
if (m_last_success == 0 && nAttempts >= ADDRMAN_RETRIES) // tried N times and never a success
81+
if (TicksSinceEpoch<std::chrono::seconds>(m_last_success) == 0 && nAttempts >= ADDRMAN_RETRIES) { // tried N times and never a success
8182
return true;
83+
}
8284

83-
if (nNow - m_last_success > ADDRMAN_MIN_FAIL_DAYS * 24 * 60 * 60 && nAttempts >= ADDRMAN_MAX_FAILURES) // N successive failures in the last week
85+
if (now - m_last_success > ADDRMAN_MIN_FAIL && nAttempts >= ADDRMAN_MAX_FAILURES) { // N successive failures in the last week
8486
return true;
87+
}
8588

8689
return false;
8790
}
8891

89-
double AddrInfo::GetChance(int64_t nNow) const
92+
double AddrInfo::GetChance(NodeSeconds now) const
9093
{
9194
double fChance = 1.0;
9295

9396
// deprioritize very recent attempts away
94-
if (nNow - m_last_try < 60 * 10) {
97+
if (now - m_last_try < 10min) {
9598
fChance *= 0.01;
9699
}
97100

@@ -540,7 +543,7 @@ void AddrManImpl::MakeTried(AddrInfo& info, int nId)
540543
info.fInTried = true;
541544
}
542545

543-
bool AddrManImpl::AddSingle(const CAddress& addr, const CNetAddr& source, int64_t time_penalty)
546+
bool AddrManImpl::AddSingle(const CAddress& addr, const CNetAddr& source, std::chrono::seconds time_penalty)
544547
{
545548
AssertLockHeld(cs);
546549

@@ -552,15 +555,15 @@ bool AddrManImpl::AddSingle(const CAddress& addr, const CNetAddr& source, int64_
552555

553556
// Do not set a penalty for a source's self-announcement
554557
if (addr == source) {
555-
time_penalty = 0;
558+
time_penalty = 0s;
556559
}
557560

558561
if (pinfo) {
559562
// periodically update nTime
560-
bool currently_online = (GetAdjustedTime() - addr.nTime < 24 * 60 * 60);
561-
int64_t update_interval = (currently_online ? 60 * 60 : 24 * 60 * 60);
563+
const bool currently_online{AdjustedTime() - addr.nTime < 24h};
564+
const auto update_interval{currently_online ? 1h : 24h};
562565
if (pinfo->nTime < addr.nTime - update_interval - time_penalty) {
563-
pinfo->nTime = std::max((int64_t)0, addr.nTime - time_penalty);
566+
pinfo->nTime = std::max(NodeSeconds{0s}, addr.nTime - time_penalty);
564567
}
565568

566569
// add services
@@ -587,7 +590,7 @@ bool AddrManImpl::AddSingle(const CAddress& addr, const CNetAddr& source, int64_
587590
return false;
588591
} else {
589592
pinfo = Create(addr, source, &nId);
590-
pinfo->nTime = std::max((int64_t)0, (int64_t)pinfo->nTime - time_penalty);
593+
pinfo->nTime = std::max(NodeSeconds{0s}, pinfo->nTime - time_penalty);
591594
nNew++;
592595
}
593596

@@ -617,13 +620,13 @@ bool AddrManImpl::AddSingle(const CAddress& addr, const CNetAddr& source, int64_
617620
return fInsert;
618621
}
619622

620-
bool AddrManImpl::Good_(const CService& addr, bool test_before_evict, int64_t nTime)
623+
bool AddrManImpl::Good_(const CService& addr, bool test_before_evict, NodeSeconds time)
621624
{
622625
AssertLockHeld(cs);
623626

624627
int nId;
625628

626-
m_last_good = nTime;
629+
m_last_good = time;
627630

628631
AddrInfo* pinfo = Find(addr, &nId);
629632

@@ -633,8 +636,8 @@ bool AddrManImpl::Good_(const CService& addr, bool test_before_evict, int64_t nT
633636
AddrInfo& info = *pinfo;
634637

635638
// update info
636-
info.m_last_success = nTime;
637-
info.m_last_try = nTime;
639+
info.m_last_success = time;
640+
info.m_last_try = time;
638641
info.nAttempts = 0;
639642
// nTime is not updated here, to avoid leaking information about
640643
// currently-connected peers.
@@ -671,7 +674,7 @@ bool AddrManImpl::Good_(const CService& addr, bool test_before_evict, int64_t nT
671674
}
672675
}
673676

674-
bool AddrManImpl::Add_(const std::vector<CAddress> &vAddr, const CNetAddr& source, int64_t time_penalty)
677+
bool AddrManImpl::Add_(const std::vector<CAddress>& vAddr, const CNetAddr& source, std::chrono::seconds time_penalty)
675678
{
676679
int added{0};
677680
for (std::vector<CAddress>::const_iterator it = vAddr.begin(); it != vAddr.end(); it++) {
@@ -683,7 +686,7 @@ bool AddrManImpl::Add_(const std::vector<CAddress> &vAddr, const CNetAddr& sourc
683686
return added > 0;
684687
}
685688

686-
void AddrManImpl::Attempt_(const CService& addr, bool fCountFailure, int64_t nTime)
689+
void AddrManImpl::Attempt_(const CService& addr, bool fCountFailure, NodeSeconds time)
687690
{
688691
AssertLockHeld(cs);
689692

@@ -696,14 +699,14 @@ void AddrManImpl::Attempt_(const CService& addr, bool fCountFailure, int64_t nTi
696699
AddrInfo& info = *pinfo;
697700

698701
// update info
699-
info.m_last_try = nTime;
702+
info.m_last_try = time;
700703
if (fCountFailure && info.m_last_count_attempt < m_last_good) {
701-
info.m_last_count_attempt = nTime;
704+
info.m_last_count_attempt = time;
702705
info.nAttempts++;
703706
}
704707
}
705708

706-
std::pair<CAddress, int64_t> AddrManImpl::Select_(bool newOnly) const
709+
std::pair<CAddress, NodeSeconds> AddrManImpl::Select_(bool newOnly) const
707710
{
708711
AssertLockHeld(cs);
709712

@@ -785,7 +788,7 @@ std::vector<CAddress> AddrManImpl::GetAddr_(size_t max_addresses, size_t max_pct
785788
}
786789

787790
// gather a list of random nodes, skipping those of low quality
788-
const int64_t now{GetAdjustedTime()};
791+
const auto now{AdjustedTime()};
789792
std::vector<CAddress> addresses;
790793
for (unsigned int n = 0; n < vRandom.size(); n++) {
791794
if (addresses.size() >= nNodes)
@@ -810,7 +813,7 @@ std::vector<CAddress> AddrManImpl::GetAddr_(size_t max_addresses, size_t max_pct
810813
return addresses;
811814
}
812815

813-
void AddrManImpl::Connected_(const CService& addr, int64_t nTime)
816+
void AddrManImpl::Connected_(const CService& addr, NodeSeconds time)
814817
{
815818
AssertLockHeld(cs);
816819

@@ -823,9 +826,10 @@ void AddrManImpl::Connected_(const CService& addr, int64_t nTime)
823826
AddrInfo& info = *pinfo;
824827

825828
// update info
826-
int64_t update_interval = 20 * 60;
827-
if (nTime - info.nTime > update_interval)
828-
info.nTime = nTime;
829+
const auto update_interval{20min};
830+
if (time - info.nTime > update_interval) {
831+
info.nTime = time;
832+
}
829833
}
830834

831835
void AddrManImpl::SetServices_(const CService& addr, ServiceFlags nServices)
@@ -870,15 +874,15 @@ void AddrManImpl::ResolveCollisions_()
870874
int id_old = vvTried[tried_bucket][tried_bucket_pos];
871875
AddrInfo& info_old = mapInfo[id_old];
872876

873-
const auto current_time{GetAdjustedTime()};
877+
const auto current_time{AdjustedTime()};
874878

875879
// Has successfully connected in last X hours
876-
if (current_time - info_old.m_last_success < ADDRMAN_REPLACEMENT_HOURS*(60*60)) {
880+
if (current_time - info_old.m_last_success < ADDRMAN_REPLACEMENT) {
877881
erase_collision = true;
878-
} else if (current_time - info_old.m_last_try < ADDRMAN_REPLACEMENT_HOURS*(60*60)) { // attempted to connect and failed in last X hours
882+
} else if (current_time - info_old.m_last_try < ADDRMAN_REPLACEMENT) { // attempted to connect and failed in last X hours
879883

880884
// Give address at least 60 seconds to successfully connect
881-
if (current_time - info_old.m_last_try > 60) {
885+
if (current_time - info_old.m_last_try > 60s) {
882886
LogPrint(BCLog::ADDRMAN, "Replacing %s with %s in tried table\n", info_old.ToString(), info_new.ToString());
883887

884888
// Replaces an existing address already in the tried table with the new address
@@ -894,7 +898,7 @@ void AddrManImpl::ResolveCollisions_()
894898
erase_collision = true;
895899
}
896900
} else { // Collision is not actually a collision anymore
897-
Good_(info_new, false, GetAdjustedTime());
901+
Good_(info_new, false, AdjustedTime());
898902
erase_collision = true;
899903
}
900904
}
@@ -907,7 +911,7 @@ void AddrManImpl::ResolveCollisions_()
907911
}
908912
}
909913

910-
std::pair<CAddress, int64_t> AddrManImpl::SelectTriedCollision_()
914+
std::pair<CAddress, NodeSeconds> AddrManImpl::SelectTriedCollision_()
911915
{
912916
AssertLockHeld(cs);
913917

@@ -990,8 +994,9 @@ int AddrManImpl::CheckAddrman() const
990994
int n = entry.first;
991995
const AddrInfo& info = entry.second;
992996
if (info.fInTried) {
993-
if (!info.m_last_success)
997+
if (!TicksSinceEpoch<std::chrono::seconds>(info.m_last_success)) {
994998
return -1;
999+
}
9951000
if (info.nRefCount)
9961001
return -2;
9971002
setTried.insert(n);
@@ -1008,10 +1013,12 @@ int AddrManImpl::CheckAddrman() const
10081013
}
10091014
if (info.nRandomPos < 0 || (size_t)info.nRandomPos >= vRandom.size() || vRandom[info.nRandomPos] != n)
10101015
return -14;
1011-
if (info.m_last_try < 0)
1016+
if (info.m_last_try < NodeSeconds{0s}) {
10121017
return -6;
1013-
if (info.m_last_success < 0)
1018+
}
1019+
if (info.m_last_success < NodeSeconds{0s}) {
10141020
return -8;
1021+
}
10151022
}
10161023

10171024
if (setTried.size() != (size_t)nTried)
@@ -1067,7 +1074,7 @@ size_t AddrManImpl::size() const
10671074
return vRandom.size();
10681075
}
10691076

1070-
bool AddrManImpl::Add(const std::vector<CAddress>& vAddr, const CNetAddr& source, int64_t time_penalty)
1077+
bool AddrManImpl::Add(const std::vector<CAddress>& vAddr, const CNetAddr& source, std::chrono::seconds time_penalty)
10711078
{
10721079
LOCK(cs);
10731080
Check();
@@ -1076,20 +1083,20 @@ bool AddrManImpl::Add(const std::vector<CAddress>& vAddr, const CNetAddr& source
10761083
return ret;
10771084
}
10781085

1079-
bool AddrManImpl::Good(const CService& addr, int64_t nTime)
1086+
bool AddrManImpl::Good(const CService& addr, NodeSeconds time)
10801087
{
10811088
LOCK(cs);
10821089
Check();
1083-
auto ret = Good_(addr, /*test_before_evict=*/true, nTime);
1090+
auto ret = Good_(addr, /*test_before_evict=*/true, time);
10841091
Check();
10851092
return ret;
10861093
}
10871094

1088-
void AddrManImpl::Attempt(const CService& addr, bool fCountFailure, int64_t nTime)
1095+
void AddrManImpl::Attempt(const CService& addr, bool fCountFailure, NodeSeconds time)
10891096
{
10901097
LOCK(cs);
10911098
Check();
1092-
Attempt_(addr, fCountFailure, nTime);
1099+
Attempt_(addr, fCountFailure, time);
10931100
Check();
10941101
}
10951102

@@ -1101,7 +1108,7 @@ void AddrManImpl::ResolveCollisions()
11011108
Check();
11021109
}
11031110

1104-
std::pair<CAddress, int64_t> AddrManImpl::SelectTriedCollision()
1111+
std::pair<CAddress, NodeSeconds> AddrManImpl::SelectTriedCollision()
11051112
{
11061113
LOCK(cs);
11071114
Check();
@@ -1110,7 +1117,7 @@ std::pair<CAddress, int64_t> AddrManImpl::SelectTriedCollision()
11101117
return ret;
11111118
}
11121119

1113-
std::pair<CAddress, int64_t> AddrManImpl::Select(bool newOnly) const
1120+
std::pair<CAddress, NodeSeconds> AddrManImpl::Select(bool newOnly) const
11141121
{
11151122
LOCK(cs);
11161123
Check();
@@ -1128,11 +1135,11 @@ std::vector<CAddress> AddrManImpl::GetAddr(size_t max_addresses, size_t max_pct,
11281135
return addresses;
11291136
}
11301137

1131-
void AddrManImpl::Connected(const CService& addr, int64_t nTime)
1138+
void AddrManImpl::Connected(const CService& addr, NodeSeconds time)
11321139
{
11331140
LOCK(cs);
11341141
Check();
1135-
Connected_(addr, nTime);
1142+
Connected_(addr, time);
11361143
Check();
11371144
}
11381145

@@ -1184,32 +1191,32 @@ size_t AddrMan::size() const
11841191
return m_impl->size();
11851192
}
11861193

1187-
bool AddrMan::Add(const std::vector<CAddress>& vAddr, const CNetAddr& source, int64_t time_penalty)
1194+
bool AddrMan::Add(const std::vector<CAddress>& vAddr, const CNetAddr& source, std::chrono::seconds time_penalty)
11881195
{
11891196
return m_impl->Add(vAddr, source, time_penalty);
11901197
}
11911198

1192-
bool AddrMan::Good(const CService& addr, int64_t nTime)
1199+
bool AddrMan::Good(const CService& addr, NodeSeconds time)
11931200
{
1194-
return m_impl->Good(addr, nTime);
1201+
return m_impl->Good(addr, time);
11951202
}
11961203

1197-
void AddrMan::Attempt(const CService& addr, bool fCountFailure, int64_t nTime)
1204+
void AddrMan::Attempt(const CService& addr, bool fCountFailure, NodeSeconds time)
11981205
{
1199-
m_impl->Attempt(addr, fCountFailure, nTime);
1206+
m_impl->Attempt(addr, fCountFailure, time);
12001207
}
12011208

12021209
void AddrMan::ResolveCollisions()
12031210
{
12041211
m_impl->ResolveCollisions();
12051212
}
12061213

1207-
std::pair<CAddress, int64_t> AddrMan::SelectTriedCollision()
1214+
std::pair<CAddress, NodeSeconds> AddrMan::SelectTriedCollision()
12081215
{
12091216
return m_impl->SelectTriedCollision();
12101217
}
12111218

1212-
std::pair<CAddress, int64_t> AddrMan::Select(bool newOnly) const
1219+
std::pair<CAddress, NodeSeconds> AddrMan::Select(bool newOnly) const
12131220
{
12141221
return m_impl->Select(newOnly);
12151222
}
@@ -1219,9 +1226,9 @@ std::vector<CAddress> AddrMan::GetAddr(size_t max_addresses, size_t max_pct, std
12191226
return m_impl->GetAddr(max_addresses, max_pct, network);
12201227
}
12211228

1222-
void AddrMan::Connected(const CService& addr, int64_t nTime)
1229+
void AddrMan::Connected(const CService& addr, NodeSeconds time)
12231230
{
1224-
m_impl->Connected(addr, nTime);
1231+
m_impl->Connected(addr, time);
12251232
}
12261233

12271234
void AddrMan::SetServices(const CService& addr, ServiceFlags nServices)

0 commit comments

Comments
 (0)