Skip to content

Commit 2bd0bf7

Browse files
committed
Merge bitcoin/bitcoin#27319: addrman, refactor: improve stochastic test in AddSingle
e064487 addrman, refactor: improve stochastic test in `AddSingle` (brunoerg) Pull request description: This PR changes this algorithm to be O(1) instead of O(n). Also, in the current implementation, if `pinfo->nRefCount` is 0, we created an unnecessary variable (`nFactor`), this changes it. the change is relatively simple and does not cause conflicts. ACKs for top commit: achow101: ACK e064487 amitiuttarwar: ACK e064487 stratospher: ACK e064487. simple use of << instead of a loop, didn't observe any behaviour difference before and after. Tree-SHA512: ff0a65155e47f65d2ce3cb5a3fd7a86efef1861181143df13a9d8e59cb16aee9be2f8801457bba8478b17fac47b015bff5cc656f6fac2ccc071ee7178a38d291
2 parents ecbf4ba + e064487 commit 2bd0bf7

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

src/addrman.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -585,11 +585,10 @@ bool AddrManImpl::AddSingle(const CAddress& addr, const CNetAddr& source, std::c
585585
return false;
586586

587587
// stochastic test: previous nRefCount == N: 2^N times harder to increase it
588-
int nFactor = 1;
589-
for (int n = 0; n < pinfo->nRefCount; n++)
590-
nFactor *= 2;
591-
if (nFactor > 1 && (insecure_rand.randrange(nFactor) != 0))
592-
return false;
588+
if (pinfo->nRefCount > 0) {
589+
const int nFactor{1 << pinfo->nRefCount};
590+
if (insecure_rand.randrange(nFactor) != 0) return false;
591+
}
593592
} else {
594593
pinfo = Create(addr, source, &nId);
595594
pinfo->nTime = std::max(NodeSeconds{0s}, pinfo->nTime - time_penalty);

0 commit comments

Comments
 (0)