Use a local FastRandomContext in a few more places in net

This commit is contained in:
Pieter Wuille 2018-10-31 15:02:24 -07:00
parent 9695f31d75
commit 8098379be5

View file

@ -134,11 +134,12 @@ static std::vector<CAddress> convertSeed6(const std::vector<SeedSpec6> &vSeedsIn
const int64_t nOneWeek = 7*24*60*60; const int64_t nOneWeek = 7*24*60*60;
std::vector<CAddress> vSeedsOut; std::vector<CAddress> vSeedsOut;
vSeedsOut.reserve(vSeedsIn.size()); vSeedsOut.reserve(vSeedsIn.size());
FastRandomContext rng;
for (const auto& seed_in : vSeedsIn) { for (const auto& seed_in : vSeedsIn) {
struct in6_addr ip; struct in6_addr ip;
memcpy(&ip, seed_in.addr, sizeof(ip)); memcpy(&ip, seed_in.addr, sizeof(ip));
CAddress addr(CService(ip, seed_in.port), GetDesirableServiceFlags(NODE_NONE)); CAddress addr(CService(ip, seed_in.port), GetDesirableServiceFlags(NODE_NONE));
addr.nTime = GetTime() - GetRand(nOneWeek) - nOneWeek; addr.nTime = GetTime() - rng.randrange(nOneWeek) - nOneWeek;
vSeedsOut.push_back(addr); vSeedsOut.push_back(addr);
} }
return vSeedsOut; return vSeedsOut;
@ -189,16 +190,16 @@ void AdvertiseLocal(CNode *pnode)
// If discovery is enabled, sometimes give our peer the address it // If discovery is enabled, sometimes give our peer the address it
// tells us that it sees us as in case it has a better idea of our // tells us that it sees us as in case it has a better idea of our
// address than we do. // address than we do.
FastRandomContext rng;
if (IsPeerAddrLocalGood(pnode) && (!addrLocal.IsRoutable() || if (IsPeerAddrLocalGood(pnode) && (!addrLocal.IsRoutable() ||
GetRand((GetnScore(addrLocal) > LOCAL_MANUAL) ? 8:2) == 0)) rng.randbits((GetnScore(addrLocal) > LOCAL_MANUAL) ? 3 : 1) == 0))
{ {
addrLocal.SetIP(pnode->GetAddrLocal()); addrLocal.SetIP(pnode->GetAddrLocal());
} }
if (addrLocal.IsRoutable() || gArgs.GetBoolArg("-addrmantest", false)) if (addrLocal.IsRoutable() || gArgs.GetBoolArg("-addrmantest", false))
{ {
LogPrint(BCLog::NET, "AdvertiseLocal: advertising address %s\n", addrLocal.ToString()); LogPrint(BCLog::NET, "AdvertiseLocal: advertising address %s\n", addrLocal.ToString());
FastRandomContext insecure_rand; pnode->PushAddress(addrLocal, rng);
pnode->PushAddress(addrLocal, insecure_rand);
} }
} }
} }