addrman, refactor: improve stochastic test in AddSingle

This commit changes this algo to be O(1) instead of O(n) by using `<<`.
This commit is contained in:
brunoerg committed 2023-09-27 14:33:09 -03:00
1 parent c9f288244b
commit e064487ca2
1 file changed
+4 -5
+4 -5
View File
@@ -585,11 +585,10 @@ bool AddrManImpl::AddSingle(const CAddress& addr, const CNetAddr& source, std::c
return false;
// stochastic test: previous nRefCount == N: 2^N times harder to increase it
int nFactor = 1;
for (int n = 0; n < pinfo->nRefCount; n++)
nFactor *= 2;
if (nFactor > 1 && (insecure_rand.randrange(nFactor) != 0))
return false;
if (pinfo->nRefCount > 0) {
const int nFactor{1 << pinfo->nRefCount};
if (insecure_rand.randrange(nFactor) != 0) return false;
}
} else {
pinfo = Create(addr, source, &nId);
pinfo->nTime = std::max(NodeSeconds{0s}, pinfo->nTime - time_penalty);