Skip to content

Commit 8884830

Browse files
committed
Use C++11 thread-safe static initializers
1 parent c31b24f commit 8884830

File tree

4 files changed

+6
-17
lines changed

4 files changed

+6
-17
lines changed

src/coins.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,7 @@ void CCoinsViewBacked::SetBackend(CCoinsView &viewIn) { base = &viewIn; }
5656
bool CCoinsViewBacked::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) { return base->BatchWrite(mapCoins, hashBlock); }
5757
CCoinsViewCursor *CCoinsViewBacked::Cursor() const { return base->Cursor(); }
5858

59-
SaltedTxidHasher::SaltedTxidHasher()
60-
{
61-
GetRandBytes((unsigned char*)&k0, sizeof(k0));
62-
GetRandBytes((unsigned char*)&k1, sizeof(k1));
63-
}
59+
SaltedTxidHasher::SaltedTxidHasher() : k0(GetRand(std::numeric_limits<uint64_t>::max())), k1(GetRand(std::numeric_limits<uint64_t>::max())) {}
6460

6561
CCoinsViewCache::CCoinsViewCache(CCoinsView *baseIn) : CCoinsViewBacked(baseIn), hasModifier(false), cachedCoinsUsage(0) { }
6662

src/coins.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ class SaltedTxidHasher
269269
{
270270
private:
271271
/** Salt */
272-
uint64_t k0, k1;
272+
const uint64_t k0, k1;
273273

274274
public:
275275
SaltedTxidHasher();

src/main.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4784,11 +4784,8 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
47844784
LOCK(cs_vNodes);
47854785
// Use deterministic randomness to send to the same nodes for 24 hours
47864786
// at a time so the addrKnowns of the chosen nodes prevent repeats
4787-
static uint64_t salt0 = 0, salt1 = 0;
4788-
while (salt0 == 0 && salt1 == 0) {
4789-
GetRandBytes((unsigned char*)&salt0, sizeof(salt0));
4790-
GetRandBytes((unsigned char*)&salt1, sizeof(salt1));
4791-
}
4787+
static const uint64_t salt0 = GetRand(std::numeric_limits<uint64_t>::max());
4788+
static const uint64_t salt1 = GetRand(std::numeric_limits<uint64_t>::max());
47924789
uint64_t hashAddr = addr.GetHash();
47934790
multimap<uint64_t, CNode*> mapMix;
47944791
const CSipHasher hasher = CSipHasher(salt0, salt1).Write(hashAddr << 32).Write((GetTime() + hashAddr) / (24*60*60));

src/net.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2601,12 +2601,8 @@ int64_t PoissonNextSend(int64_t nNow, int average_interval_seconds) {
26012601

26022602
/* static */ uint64_t CNode::CalculateKeyedNetGroup(const CAddress& ad)
26032603
{
2604-
static uint64_t k0 = 0, k1 = 0;
2605-
while (k0 == 0 && k1 == 0) {
2606-
// Make sure this only runs on the first invocation.
2607-
GetRandBytes((unsigned char*)&k0, sizeof(k0));
2608-
GetRandBytes((unsigned char*)&k1, sizeof(k1));
2609-
}
2604+
static const uint64_t k0 = GetRand(std::numeric_limits<uint64_t>::max());
2605+
static const uint64_t k1 = GetRand(std::numeric_limits<uint64_t>::max());
26102606

26112607
std::vector<unsigned char> vchNetGroup(ad.GetGroup());
26122608

0 commit comments

Comments
 (0)