@@ -29,19 +29,19 @@ static constexpr uint32_t ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP{64};
29
29
/* * Maximum number of times an address can occur in the new table */
30
30
static constexpr int32_t ADDRMAN_NEW_BUCKETS_PER_ADDRESS{8 };
31
31
/* * How old addresses can maximally be */
32
- static constexpr int64_t ADDRMAN_HORIZON_DAYS {30 };
32
+ static constexpr auto ADDRMAN_HORIZON {30 * 24h };
33
33
/* * After how many failed attempts we give up on a new node */
34
34
static constexpr int32_t ADDRMAN_RETRIES{3 };
35
35
/* * How many successive failures are allowed ... */
36
36
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 };
39
39
/* * 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 };
41
41
/* * The maximum number of tried addr collisions to store */
42
42
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};
45
45
46
46
int AddrInfo::GetTriedBucket (const uint256& nKey, const NetGroupManager& netgroupman) const
47
47
{
@@ -64,34 +64,37 @@ int AddrInfo::GetBucketPosition(const uint256& nKey, bool fNew, int nBucket) con
64
64
return hash1 % ADDRMAN_BUCKET_SIZE;
65
65
}
66
66
67
- bool AddrInfo::IsTerrible (int64_t nNow ) const
67
+ bool AddrInfo::IsTerrible (NodeSeconds now ) const
68
68
{
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
70
70
return false ;
71
71
}
72
72
73
- if (nTime > nNow + 10 * 60 ) // came in a flying DeLorean
73
+ if (nTime > now + 10min) { // came in a flying DeLorean
74
74
return true ;
75
+ }
75
76
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
77
78
return true ;
78
79
}
79
80
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
81
82
return true ;
83
+ }
82
84
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
84
86
return true ;
87
+ }
85
88
86
89
return false ;
87
90
}
88
91
89
- double AddrInfo::GetChance (int64_t nNow ) const
92
+ double AddrInfo::GetChance (NodeSeconds now ) const
90
93
{
91
94
double fChance = 1.0 ;
92
95
93
96
// deprioritize very recent attempts away
94
- if (nNow - m_last_try < 60 * 10 ) {
97
+ if (now - m_last_try < 10min ) {
95
98
fChance *= 0.01 ;
96
99
}
97
100
@@ -540,7 +543,7 @@ void AddrManImpl::MakeTried(AddrInfo& info, int nId)
540
543
info.fInTried = true ;
541
544
}
542
545
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)
544
547
{
545
548
AssertLockHeld (cs);
546
549
@@ -552,15 +555,15 @@ bool AddrManImpl::AddSingle(const CAddress& addr, const CNetAddr& source, int64_
552
555
553
556
// Do not set a penalty for a source's self-announcement
554
557
if (addr == source) {
555
- time_penalty = 0 ;
558
+ time_penalty = 0s ;
556
559
}
557
560
558
561
if (pinfo) {
559
562
// 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} ;
562
565
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);
564
567
}
565
568
566
569
// add services
@@ -587,7 +590,7 @@ bool AddrManImpl::AddSingle(const CAddress& addr, const CNetAddr& source, int64_
587
590
return false ;
588
591
} else {
589
592
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);
591
594
nNew++;
592
595
}
593
596
@@ -617,13 +620,13 @@ bool AddrManImpl::AddSingle(const CAddress& addr, const CNetAddr& source, int64_
617
620
return fInsert ;
618
621
}
619
622
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 )
621
624
{
622
625
AssertLockHeld (cs);
623
626
624
627
int nId;
625
628
626
- m_last_good = nTime ;
629
+ m_last_good = time ;
627
630
628
631
AddrInfo* pinfo = Find (addr, &nId);
629
632
@@ -633,8 +636,8 @@ bool AddrManImpl::Good_(const CService& addr, bool test_before_evict, int64_t nT
633
636
AddrInfo& info = *pinfo;
634
637
635
638
// 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 ;
638
641
info.nAttempts = 0 ;
639
642
// nTime is not updated here, to avoid leaking information about
640
643
// currently-connected peers.
@@ -671,7 +674,7 @@ bool AddrManImpl::Good_(const CService& addr, bool test_before_evict, int64_t nT
671
674
}
672
675
}
673
676
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)
675
678
{
676
679
int added{0 };
677
680
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
683
686
return added > 0 ;
684
687
}
685
688
686
- void AddrManImpl::Attempt_ (const CService& addr, bool fCountFailure , int64_t nTime )
689
+ void AddrManImpl::Attempt_ (const CService& addr, bool fCountFailure , NodeSeconds time )
687
690
{
688
691
AssertLockHeld (cs);
689
692
@@ -696,14 +699,14 @@ void AddrManImpl::Attempt_(const CService& addr, bool fCountFailure, int64_t nTi
696
699
AddrInfo& info = *pinfo;
697
700
698
701
// update info
699
- info.m_last_try = nTime ;
702
+ info.m_last_try = time ;
700
703
if (fCountFailure && info.m_last_count_attempt < m_last_good) {
701
- info.m_last_count_attempt = nTime ;
704
+ info.m_last_count_attempt = time ;
702
705
info.nAttempts ++;
703
706
}
704
707
}
705
708
706
- std::pair<CAddress, int64_t > AddrManImpl::Select_ (bool newOnly) const
709
+ std::pair<CAddress, NodeSeconds > AddrManImpl::Select_ (bool newOnly) const
707
710
{
708
711
AssertLockHeld (cs);
709
712
@@ -785,7 +788,7 @@ std::vector<CAddress> AddrManImpl::GetAddr_(size_t max_addresses, size_t max_pct
785
788
}
786
789
787
790
// gather a list of random nodes, skipping those of low quality
788
- const int64_t now{GetAdjustedTime ()};
791
+ const auto now{AdjustedTime ()};
789
792
std::vector<CAddress> addresses;
790
793
for (unsigned int n = 0 ; n < vRandom.size (); n++) {
791
794
if (addresses.size () >= nNodes)
@@ -810,7 +813,7 @@ std::vector<CAddress> AddrManImpl::GetAddr_(size_t max_addresses, size_t max_pct
810
813
return addresses;
811
814
}
812
815
813
- void AddrManImpl::Connected_ (const CService& addr, int64_t nTime )
816
+ void AddrManImpl::Connected_ (const CService& addr, NodeSeconds time )
814
817
{
815
818
AssertLockHeld (cs);
816
819
@@ -823,9 +826,10 @@ void AddrManImpl::Connected_(const CService& addr, int64_t nTime)
823
826
AddrInfo& info = *pinfo;
824
827
825
828
// 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
+ }
829
833
}
830
834
831
835
void AddrManImpl::SetServices_ (const CService& addr, ServiceFlags nServices)
@@ -870,15 +874,15 @@ void AddrManImpl::ResolveCollisions_()
870
874
int id_old = vvTried[tried_bucket][tried_bucket_pos];
871
875
AddrInfo& info_old = mapInfo[id_old];
872
876
873
- const auto current_time{GetAdjustedTime ()};
877
+ const auto current_time{AdjustedTime ()};
874
878
875
879
// 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 ) {
877
881
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
879
883
880
884
// 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 ) {
882
886
LogPrint (BCLog::ADDRMAN, " Replacing %s with %s in tried table\n " , info_old.ToString (), info_new.ToString ());
883
887
884
888
// Replaces an existing address already in the tried table with the new address
@@ -894,7 +898,7 @@ void AddrManImpl::ResolveCollisions_()
894
898
erase_collision = true ;
895
899
}
896
900
} else { // Collision is not actually a collision anymore
897
- Good_ (info_new, false , GetAdjustedTime ());
901
+ Good_ (info_new, false , AdjustedTime ());
898
902
erase_collision = true ;
899
903
}
900
904
}
@@ -907,7 +911,7 @@ void AddrManImpl::ResolveCollisions_()
907
911
}
908
912
}
909
913
910
- std::pair<CAddress, int64_t > AddrManImpl::SelectTriedCollision_ ()
914
+ std::pair<CAddress, NodeSeconds > AddrManImpl::SelectTriedCollision_ ()
911
915
{
912
916
AssertLockHeld (cs);
913
917
@@ -990,8 +994,9 @@ int AddrManImpl::CheckAddrman() const
990
994
int n = entry.first ;
991
995
const AddrInfo& info = entry.second ;
992
996
if (info.fInTried ) {
993
- if (!info.m_last_success )
997
+ if (!TicksSinceEpoch<std::chrono::seconds>( info.m_last_success )) {
994
998
return -1 ;
999
+ }
995
1000
if (info.nRefCount )
996
1001
return -2 ;
997
1002
setTried.insert (n);
@@ -1008,10 +1013,12 @@ int AddrManImpl::CheckAddrman() const
1008
1013
}
1009
1014
if (info.nRandomPos < 0 || (size_t )info.nRandomPos >= vRandom.size () || vRandom[info.nRandomPos ] != n)
1010
1015
return -14 ;
1011
- if (info.m_last_try < 0 )
1016
+ if (info.m_last_try < NodeSeconds{0s}) {
1012
1017
return -6 ;
1013
- if (info.m_last_success < 0 )
1018
+ }
1019
+ if (info.m_last_success < NodeSeconds{0s}) {
1014
1020
return -8 ;
1021
+ }
1015
1022
}
1016
1023
1017
1024
if (setTried.size () != (size_t )nTried)
@@ -1067,7 +1074,7 @@ size_t AddrManImpl::size() const
1067
1074
return vRandom.size ();
1068
1075
}
1069
1076
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)
1071
1078
{
1072
1079
LOCK (cs);
1073
1080
Check ();
@@ -1076,20 +1083,20 @@ bool AddrManImpl::Add(const std::vector<CAddress>& vAddr, const CNetAddr& source
1076
1083
return ret;
1077
1084
}
1078
1085
1079
- bool AddrManImpl::Good (const CService& addr, int64_t nTime )
1086
+ bool AddrManImpl::Good (const CService& addr, NodeSeconds time )
1080
1087
{
1081
1088
LOCK (cs);
1082
1089
Check ();
1083
- auto ret = Good_ (addr, /* test_before_evict=*/ true , nTime );
1090
+ auto ret = Good_ (addr, /* test_before_evict=*/ true , time );
1084
1091
Check ();
1085
1092
return ret;
1086
1093
}
1087
1094
1088
- void AddrManImpl::Attempt (const CService& addr, bool fCountFailure , int64_t nTime )
1095
+ void AddrManImpl::Attempt (const CService& addr, bool fCountFailure , NodeSeconds time )
1089
1096
{
1090
1097
LOCK (cs);
1091
1098
Check ();
1092
- Attempt_ (addr, fCountFailure , nTime );
1099
+ Attempt_ (addr, fCountFailure , time );
1093
1100
Check ();
1094
1101
}
1095
1102
@@ -1101,7 +1108,7 @@ void AddrManImpl::ResolveCollisions()
1101
1108
Check ();
1102
1109
}
1103
1110
1104
- std::pair<CAddress, int64_t > AddrManImpl::SelectTriedCollision ()
1111
+ std::pair<CAddress, NodeSeconds > AddrManImpl::SelectTriedCollision ()
1105
1112
{
1106
1113
LOCK (cs);
1107
1114
Check ();
@@ -1110,7 +1117,7 @@ std::pair<CAddress, int64_t> AddrManImpl::SelectTriedCollision()
1110
1117
return ret;
1111
1118
}
1112
1119
1113
- std::pair<CAddress, int64_t > AddrManImpl::Select (bool newOnly) const
1120
+ std::pair<CAddress, NodeSeconds > AddrManImpl::Select (bool newOnly) const
1114
1121
{
1115
1122
LOCK (cs);
1116
1123
Check ();
@@ -1128,11 +1135,11 @@ std::vector<CAddress> AddrManImpl::GetAddr(size_t max_addresses, size_t max_pct,
1128
1135
return addresses;
1129
1136
}
1130
1137
1131
- void AddrManImpl::Connected (const CService& addr, int64_t nTime )
1138
+ void AddrManImpl::Connected (const CService& addr, NodeSeconds time )
1132
1139
{
1133
1140
LOCK (cs);
1134
1141
Check ();
1135
- Connected_ (addr, nTime );
1142
+ Connected_ (addr, time );
1136
1143
Check ();
1137
1144
}
1138
1145
@@ -1184,32 +1191,32 @@ size_t AddrMan::size() const
1184
1191
return m_impl->size ();
1185
1192
}
1186
1193
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)
1188
1195
{
1189
1196
return m_impl->Add (vAddr, source, time_penalty);
1190
1197
}
1191
1198
1192
- bool AddrMan::Good (const CService& addr, int64_t nTime )
1199
+ bool AddrMan::Good (const CService& addr, NodeSeconds time )
1193
1200
{
1194
- return m_impl->Good (addr, nTime );
1201
+ return m_impl->Good (addr, time );
1195
1202
}
1196
1203
1197
- void AddrMan::Attempt (const CService& addr, bool fCountFailure , int64_t nTime )
1204
+ void AddrMan::Attempt (const CService& addr, bool fCountFailure , NodeSeconds time )
1198
1205
{
1199
- m_impl->Attempt (addr, fCountFailure , nTime );
1206
+ m_impl->Attempt (addr, fCountFailure , time );
1200
1207
}
1201
1208
1202
1209
void AddrMan::ResolveCollisions ()
1203
1210
{
1204
1211
m_impl->ResolveCollisions ();
1205
1212
}
1206
1213
1207
- std::pair<CAddress, int64_t > AddrMan::SelectTriedCollision ()
1214
+ std::pair<CAddress, NodeSeconds > AddrMan::SelectTriedCollision ()
1208
1215
{
1209
1216
return m_impl->SelectTriedCollision ();
1210
1217
}
1211
1218
1212
- std::pair<CAddress, int64_t > AddrMan::Select (bool newOnly) const
1219
+ std::pair<CAddress, NodeSeconds > AddrMan::Select (bool newOnly) const
1213
1220
{
1214
1221
return m_impl->Select (newOnly);
1215
1222
}
@@ -1219,9 +1226,9 @@ std::vector<CAddress> AddrMan::GetAddr(size_t max_addresses, size_t max_pct, std
1219
1226
return m_impl->GetAddr (max_addresses, max_pct, network);
1220
1227
}
1221
1228
1222
- void AddrMan::Connected (const CService& addr, int64_t nTime )
1229
+ void AddrMan::Connected (const CService& addr, NodeSeconds time )
1223
1230
{
1224
- m_impl->Connected (addr, nTime );
1231
+ m_impl->Connected (addr, time );
1225
1232
}
1226
1233
1227
1234
void AddrMan::SetServices (const CService& addr, ServiceFlags nServices)
0 commit comments