mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-12 06:58:57 +01:00
refactor: Use type-safe std::chrono for addrman time
This commit is contained in:
133
src/addrman.cpp
133
src/addrman.cpp
@@ -29,19 +29,19 @@ static constexpr uint32_t ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP{64};
|
||||
/** Maximum number of times an address can occur in the new table */
|
||||
static constexpr int32_t ADDRMAN_NEW_BUCKETS_PER_ADDRESS{8};
|
||||
/** How old addresses can maximally be */
|
||||
static constexpr int64_t ADDRMAN_HORIZON_DAYS{30};
|
||||
static constexpr auto ADDRMAN_HORIZON{30 * 24h};
|
||||
/** After how many failed attempts we give up on a new node */
|
||||
static constexpr int32_t ADDRMAN_RETRIES{3};
|
||||
/** How many successive failures are allowed ... */
|
||||
static constexpr int32_t ADDRMAN_MAX_FAILURES{10};
|
||||
/** ... in at least this many days */
|
||||
static constexpr int64_t ADDRMAN_MIN_FAIL_DAYS{7};
|
||||
/** ... in at least this duration */
|
||||
static constexpr auto ADDRMAN_MIN_FAIL{7 * 24h};
|
||||
/** How recent a successful connection should be before we allow an address to be evicted from tried */
|
||||
static constexpr int64_t ADDRMAN_REPLACEMENT_HOURS{4};
|
||||
static constexpr auto ADDRMAN_REPLACEMENT{4h};
|
||||
/** The maximum number of tried addr collisions to store */
|
||||
static constexpr size_t ADDRMAN_SET_TRIED_COLLISION_SIZE{10};
|
||||
/** The maximum time we'll spend trying to resolve a tried table collision, in seconds */
|
||||
static constexpr int64_t ADDRMAN_TEST_WINDOW{40*60}; // 40 minutes
|
||||
/** The maximum time we'll spend trying to resolve a tried table collision */
|
||||
static constexpr auto ADDRMAN_TEST_WINDOW{40min};
|
||||
|
||||
int AddrInfo::GetTriedBucket(const uint256& nKey, const NetGroupManager& netgroupman) const
|
||||
{
|
||||
@@ -64,34 +64,37 @@ int AddrInfo::GetBucketPosition(const uint256& nKey, bool fNew, int nBucket) con
|
||||
return hash1 % ADDRMAN_BUCKET_SIZE;
|
||||
}
|
||||
|
||||
bool AddrInfo::IsTerrible(int64_t nNow) const
|
||||
bool AddrInfo::IsTerrible(NodeSeconds now) const
|
||||
{
|
||||
if (nNow - m_last_try <= 60) { // never remove things tried in the last minute
|
||||
if (now - m_last_try <= 1min) { // never remove things tried in the last minute
|
||||
return false;
|
||||
}
|
||||
|
||||
if (nTime > nNow + 10 * 60) // came in a flying DeLorean
|
||||
return true;
|
||||
|
||||
if (nNow - nTime > ADDRMAN_HORIZON_DAYS * 24 * 60 * 60) { // not seen in recent history
|
||||
if (nTime > now + 10min) { // came in a flying DeLorean
|
||||
return true;
|
||||
}
|
||||
|
||||
if (m_last_success == 0 && nAttempts >= ADDRMAN_RETRIES) // tried N times and never a success
|
||||
if (now - nTime > ADDRMAN_HORIZON) { // not seen in recent history
|
||||
return true;
|
||||
}
|
||||
|
||||
if (nNow - m_last_success > ADDRMAN_MIN_FAIL_DAYS * 24 * 60 * 60 && nAttempts >= ADDRMAN_MAX_FAILURES) // N successive failures in the last week
|
||||
if (TicksSinceEpoch<std::chrono::seconds>(m_last_success) == 0 && nAttempts >= ADDRMAN_RETRIES) { // tried N times and never a success
|
||||
return true;
|
||||
}
|
||||
|
||||
if (now - m_last_success > ADDRMAN_MIN_FAIL && nAttempts >= ADDRMAN_MAX_FAILURES) { // N successive failures in the last week
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
double AddrInfo::GetChance(int64_t nNow) const
|
||||
double AddrInfo::GetChance(NodeSeconds now) const
|
||||
{
|
||||
double fChance = 1.0;
|
||||
|
||||
// deprioritize very recent attempts away
|
||||
if (nNow - m_last_try < 60 * 10) {
|
||||
if (now - m_last_try < 10min) {
|
||||
fChance *= 0.01;
|
||||
}
|
||||
|
||||
@@ -540,7 +543,7 @@ void AddrManImpl::MakeTried(AddrInfo& info, int nId)
|
||||
info.fInTried = true;
|
||||
}
|
||||
|
||||
bool AddrManImpl::AddSingle(const CAddress& addr, const CNetAddr& source, int64_t time_penalty)
|
||||
bool AddrManImpl::AddSingle(const CAddress& addr, const CNetAddr& source, std::chrono::seconds time_penalty)
|
||||
{
|
||||
AssertLockHeld(cs);
|
||||
|
||||
@@ -552,15 +555,15 @@ bool AddrManImpl::AddSingle(const CAddress& addr, const CNetAddr& source, int64_
|
||||
|
||||
// Do not set a penalty for a source's self-announcement
|
||||
if (addr == source) {
|
||||
time_penalty = 0;
|
||||
time_penalty = 0s;
|
||||
}
|
||||
|
||||
if (pinfo) {
|
||||
// periodically update nTime
|
||||
bool currently_online = (GetAdjustedTime() - addr.nTime < 24 * 60 * 60);
|
||||
int64_t update_interval = (currently_online ? 60 * 60 : 24 * 60 * 60);
|
||||
const bool currently_online{AdjustedTime() - addr.nTime < 24h};
|
||||
const auto update_interval{currently_online ? 1h : 24h};
|
||||
if (pinfo->nTime < addr.nTime - update_interval - time_penalty) {
|
||||
pinfo->nTime = std::max((int64_t)0, addr.nTime - time_penalty);
|
||||
pinfo->nTime = std::max(NodeSeconds{0s}, addr.nTime - time_penalty);
|
||||
}
|
||||
|
||||
// add services
|
||||
@@ -587,7 +590,7 @@ bool AddrManImpl::AddSingle(const CAddress& addr, const CNetAddr& source, int64_
|
||||
return false;
|
||||
} else {
|
||||
pinfo = Create(addr, source, &nId);
|
||||
pinfo->nTime = std::max((int64_t)0, (int64_t)pinfo->nTime - time_penalty);
|
||||
pinfo->nTime = std::max(NodeSeconds{0s}, pinfo->nTime - time_penalty);
|
||||
nNew++;
|
||||
}
|
||||
|
||||
@@ -617,13 +620,13 @@ bool AddrManImpl::AddSingle(const CAddress& addr, const CNetAddr& source, int64_
|
||||
return fInsert;
|
||||
}
|
||||
|
||||
bool AddrManImpl::Good_(const CService& addr, bool test_before_evict, int64_t nTime)
|
||||
bool AddrManImpl::Good_(const CService& addr, bool test_before_evict, NodeSeconds time)
|
||||
{
|
||||
AssertLockHeld(cs);
|
||||
|
||||
int nId;
|
||||
|
||||
m_last_good = nTime;
|
||||
m_last_good = time;
|
||||
|
||||
AddrInfo* pinfo = Find(addr, &nId);
|
||||
|
||||
@@ -633,8 +636,8 @@ bool AddrManImpl::Good_(const CService& addr, bool test_before_evict, int64_t nT
|
||||
AddrInfo& info = *pinfo;
|
||||
|
||||
// update info
|
||||
info.m_last_success = nTime;
|
||||
info.m_last_try = nTime;
|
||||
info.m_last_success = time;
|
||||
info.m_last_try = time;
|
||||
info.nAttempts = 0;
|
||||
// nTime is not updated here, to avoid leaking information about
|
||||
// currently-connected peers.
|
||||
@@ -671,7 +674,7 @@ bool AddrManImpl::Good_(const CService& addr, bool test_before_evict, int64_t nT
|
||||
}
|
||||
}
|
||||
|
||||
bool AddrManImpl::Add_(const std::vector<CAddress> &vAddr, const CNetAddr& source, int64_t time_penalty)
|
||||
bool AddrManImpl::Add_(const std::vector<CAddress>& vAddr, const CNetAddr& source, std::chrono::seconds time_penalty)
|
||||
{
|
||||
int added{0};
|
||||
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
|
||||
return added > 0;
|
||||
}
|
||||
|
||||
void AddrManImpl::Attempt_(const CService& addr, bool fCountFailure, int64_t nTime)
|
||||
void AddrManImpl::Attempt_(const CService& addr, bool fCountFailure, NodeSeconds time)
|
||||
{
|
||||
AssertLockHeld(cs);
|
||||
|
||||
@@ -696,14 +699,14 @@ void AddrManImpl::Attempt_(const CService& addr, bool fCountFailure, int64_t nTi
|
||||
AddrInfo& info = *pinfo;
|
||||
|
||||
// update info
|
||||
info.m_last_try = nTime;
|
||||
info.m_last_try = time;
|
||||
if (fCountFailure && info.m_last_count_attempt < m_last_good) {
|
||||
info.m_last_count_attempt = nTime;
|
||||
info.m_last_count_attempt = time;
|
||||
info.nAttempts++;
|
||||
}
|
||||
}
|
||||
|
||||
std::pair<CAddress, int64_t> AddrManImpl::Select_(bool newOnly) const
|
||||
std::pair<CAddress, NodeSeconds> AddrManImpl::Select_(bool newOnly) const
|
||||
{
|
||||
AssertLockHeld(cs);
|
||||
|
||||
@@ -785,7 +788,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
|
||||
const int64_t now{GetAdjustedTime()};
|
||||
const auto now{AdjustedTime()};
|
||||
std::vector<CAddress> addresses;
|
||||
for (unsigned int n = 0; n < vRandom.size(); n++) {
|
||||
if (addresses.size() >= nNodes)
|
||||
@@ -810,7 +813,7 @@ std::vector<CAddress> AddrManImpl::GetAddr_(size_t max_addresses, size_t max_pct
|
||||
return addresses;
|
||||
}
|
||||
|
||||
void AddrManImpl::Connected_(const CService& addr, int64_t nTime)
|
||||
void AddrManImpl::Connected_(const CService& addr, NodeSeconds time)
|
||||
{
|
||||
AssertLockHeld(cs);
|
||||
|
||||
@@ -823,9 +826,10 @@ void AddrManImpl::Connected_(const CService& addr, int64_t nTime)
|
||||
AddrInfo& info = *pinfo;
|
||||
|
||||
// update info
|
||||
int64_t update_interval = 20 * 60;
|
||||
if (nTime - info.nTime > update_interval)
|
||||
info.nTime = nTime;
|
||||
const auto update_interval{20min};
|
||||
if (time - info.nTime > update_interval) {
|
||||
info.nTime = time;
|
||||
}
|
||||
}
|
||||
|
||||
void AddrManImpl::SetServices_(const CService& addr, ServiceFlags nServices)
|
||||
@@ -870,15 +874,15 @@ void AddrManImpl::ResolveCollisions_()
|
||||
int id_old = vvTried[tried_bucket][tried_bucket_pos];
|
||||
AddrInfo& info_old = mapInfo[id_old];
|
||||
|
||||
const auto current_time{GetAdjustedTime()};
|
||||
const auto current_time{AdjustedTime()};
|
||||
|
||||
// Has successfully connected in last X hours
|
||||
if (current_time - info_old.m_last_success < ADDRMAN_REPLACEMENT_HOURS*(60*60)) {
|
||||
if (current_time - info_old.m_last_success < ADDRMAN_REPLACEMENT) {
|
||||
erase_collision = true;
|
||||
} else if (current_time - info_old.m_last_try < ADDRMAN_REPLACEMENT_HOURS*(60*60)) { // attempted to connect and failed in last X hours
|
||||
} else if (current_time - info_old.m_last_try < ADDRMAN_REPLACEMENT) { // attempted to connect and failed in last X hours
|
||||
|
||||
// Give address at least 60 seconds to successfully connect
|
||||
if (current_time - info_old.m_last_try > 60) {
|
||||
if (current_time - info_old.m_last_try > 60s) {
|
||||
LogPrint(BCLog::ADDRMAN, "Replacing %s with %s in tried table\n", info_old.ToString(), info_new.ToString());
|
||||
|
||||
// Replaces an existing address already in the tried table with the new address
|
||||
@@ -894,7 +898,7 @@ void AddrManImpl::ResolveCollisions_()
|
||||
erase_collision = true;
|
||||
}
|
||||
} else { // Collision is not actually a collision anymore
|
||||
Good_(info_new, false, GetAdjustedTime());
|
||||
Good_(info_new, false, AdjustedTime());
|
||||
erase_collision = true;
|
||||
}
|
||||
}
|
||||
@@ -907,7 +911,7 @@ void AddrManImpl::ResolveCollisions_()
|
||||
}
|
||||
}
|
||||
|
||||
std::pair<CAddress, int64_t> AddrManImpl::SelectTriedCollision_()
|
||||
std::pair<CAddress, NodeSeconds> AddrManImpl::SelectTriedCollision_()
|
||||
{
|
||||
AssertLockHeld(cs);
|
||||
|
||||
@@ -990,8 +994,9 @@ int AddrManImpl::CheckAddrman() const
|
||||
int n = entry.first;
|
||||
const AddrInfo& info = entry.second;
|
||||
if (info.fInTried) {
|
||||
if (!info.m_last_success)
|
||||
if (!TicksSinceEpoch<std::chrono::seconds>(info.m_last_success)) {
|
||||
return -1;
|
||||
}
|
||||
if (info.nRefCount)
|
||||
return -2;
|
||||
setTried.insert(n);
|
||||
@@ -1008,10 +1013,12 @@ int AddrManImpl::CheckAddrman() const
|
||||
}
|
||||
if (info.nRandomPos < 0 || (size_t)info.nRandomPos >= vRandom.size() || vRandom[info.nRandomPos] != n)
|
||||
return -14;
|
||||
if (info.m_last_try < 0)
|
||||
if (info.m_last_try < NodeSeconds{0s}) {
|
||||
return -6;
|
||||
if (info.m_last_success < 0)
|
||||
}
|
||||
if (info.m_last_success < NodeSeconds{0s}) {
|
||||
return -8;
|
||||
}
|
||||
}
|
||||
|
||||
if (setTried.size() != (size_t)nTried)
|
||||
@@ -1067,7 +1074,7 @@ size_t AddrManImpl::size() const
|
||||
return vRandom.size();
|
||||
}
|
||||
|
||||
bool AddrManImpl::Add(const std::vector<CAddress>& vAddr, const CNetAddr& source, int64_t time_penalty)
|
||||
bool AddrManImpl::Add(const std::vector<CAddress>& vAddr, const CNetAddr& source, std::chrono::seconds time_penalty)
|
||||
{
|
||||
LOCK(cs);
|
||||
Check();
|
||||
@@ -1076,20 +1083,20 @@ bool AddrManImpl::Add(const std::vector<CAddress>& vAddr, const CNetAddr& source
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool AddrManImpl::Good(const CService& addr, int64_t nTime)
|
||||
bool AddrManImpl::Good(const CService& addr, NodeSeconds time)
|
||||
{
|
||||
LOCK(cs);
|
||||
Check();
|
||||
auto ret = Good_(addr, /*test_before_evict=*/true, nTime);
|
||||
auto ret = Good_(addr, /*test_before_evict=*/true, time);
|
||||
Check();
|
||||
return ret;
|
||||
}
|
||||
|
||||
void AddrManImpl::Attempt(const CService& addr, bool fCountFailure, int64_t nTime)
|
||||
void AddrManImpl::Attempt(const CService& addr, bool fCountFailure, NodeSeconds time)
|
||||
{
|
||||
LOCK(cs);
|
||||
Check();
|
||||
Attempt_(addr, fCountFailure, nTime);
|
||||
Attempt_(addr, fCountFailure, time);
|
||||
Check();
|
||||
}
|
||||
|
||||
@@ -1101,7 +1108,7 @@ void AddrManImpl::ResolveCollisions()
|
||||
Check();
|
||||
}
|
||||
|
||||
std::pair<CAddress, int64_t> AddrManImpl::SelectTriedCollision()
|
||||
std::pair<CAddress, NodeSeconds> AddrManImpl::SelectTriedCollision()
|
||||
{
|
||||
LOCK(cs);
|
||||
Check();
|
||||
@@ -1110,7 +1117,7 @@ std::pair<CAddress, int64_t> AddrManImpl::SelectTriedCollision()
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::pair<CAddress, int64_t> AddrManImpl::Select(bool newOnly) const
|
||||
std::pair<CAddress, NodeSeconds> AddrManImpl::Select(bool newOnly) const
|
||||
{
|
||||
LOCK(cs);
|
||||
Check();
|
||||
@@ -1128,11 +1135,11 @@ std::vector<CAddress> AddrManImpl::GetAddr(size_t max_addresses, size_t max_pct,
|
||||
return addresses;
|
||||
}
|
||||
|
||||
void AddrManImpl::Connected(const CService& addr, int64_t nTime)
|
||||
void AddrManImpl::Connected(const CService& addr, NodeSeconds time)
|
||||
{
|
||||
LOCK(cs);
|
||||
Check();
|
||||
Connected_(addr, nTime);
|
||||
Connected_(addr, time);
|
||||
Check();
|
||||
}
|
||||
|
||||
@@ -1184,19 +1191,19 @@ size_t AddrMan::size() const
|
||||
return m_impl->size();
|
||||
}
|
||||
|
||||
bool AddrMan::Add(const std::vector<CAddress>& vAddr, const CNetAddr& source, int64_t time_penalty)
|
||||
bool AddrMan::Add(const std::vector<CAddress>& vAddr, const CNetAddr& source, std::chrono::seconds time_penalty)
|
||||
{
|
||||
return m_impl->Add(vAddr, source, time_penalty);
|
||||
}
|
||||
|
||||
bool AddrMan::Good(const CService& addr, int64_t nTime)
|
||||
bool AddrMan::Good(const CService& addr, NodeSeconds time)
|
||||
{
|
||||
return m_impl->Good(addr, nTime);
|
||||
return m_impl->Good(addr, time);
|
||||
}
|
||||
|
||||
void AddrMan::Attempt(const CService& addr, bool fCountFailure, int64_t nTime)
|
||||
void AddrMan::Attempt(const CService& addr, bool fCountFailure, NodeSeconds time)
|
||||
{
|
||||
m_impl->Attempt(addr, fCountFailure, nTime);
|
||||
m_impl->Attempt(addr, fCountFailure, time);
|
||||
}
|
||||
|
||||
void AddrMan::ResolveCollisions()
|
||||
@@ -1204,12 +1211,12 @@ void AddrMan::ResolveCollisions()
|
||||
m_impl->ResolveCollisions();
|
||||
}
|
||||
|
||||
std::pair<CAddress, int64_t> AddrMan::SelectTriedCollision()
|
||||
std::pair<CAddress, NodeSeconds> AddrMan::SelectTriedCollision()
|
||||
{
|
||||
return m_impl->SelectTriedCollision();
|
||||
}
|
||||
|
||||
std::pair<CAddress, int64_t> AddrMan::Select(bool newOnly) const
|
||||
std::pair<CAddress, NodeSeconds> AddrMan::Select(bool newOnly) const
|
||||
{
|
||||
return m_impl->Select(newOnly);
|
||||
}
|
||||
@@ -1219,9 +1226,9 @@ std::vector<CAddress> AddrMan::GetAddr(size_t max_addresses, size_t max_pct, std
|
||||
return m_impl->GetAddr(max_addresses, max_pct, network);
|
||||
}
|
||||
|
||||
void AddrMan::Connected(const CService& addr, int64_t nTime)
|
||||
void AddrMan::Connected(const CService& addr, NodeSeconds time)
|
||||
{
|
||||
m_impl->Connected(addr, nTime);
|
||||
m_impl->Connected(addr, time);
|
||||
}
|
||||
|
||||
void AddrMan::SetServices(const CService& addr, ServiceFlags nServices)
|
||||
|
||||
Reference in New Issue
Block a user