Skip to content

Commit ddc184d

Browse files
committed
random: get rid of GetRand by inlining
1 parent e2d1f84 commit ddc184d

14 files changed

+40
-49
lines changed

src/addrdb.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ template <typename Data>
5353
bool SerializeFileDB(const std::string& prefix, const fs::path& path, const Data& data)
5454
{
5555
// Generate random temporary filename
56-
const uint16_t randv{GetRand<uint16_t>()};
56+
const uint16_t randv{FastRandomContext().rand<uint16_t>()};
5757
std::string tmpfn = strprintf("%s.%04x", prefix, randv);
5858

5959
// open temp output file

src/common/bloom.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ bool CRollingBloomFilter::contains(Span<const unsigned char> vKey) const
239239

240240
void CRollingBloomFilter::reset()
241241
{
242-
nTweak = GetRand<unsigned int>();
242+
nTweak = FastRandomContext().rand<unsigned int>();
243243
nEntriesThisGeneration = 0;
244244
nGeneration = 1;
245245
std::fill(data.begin(), data.end(), 0);

src/headerssync.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ static_assert(sizeof(CompressedHeader) == 48);
2525

2626
HeadersSyncState::HeadersSyncState(NodeId id, const Consensus::Params& consensus_params,
2727
const CBlockIndex* chain_start, const arith_uint256& minimum_required_work) :
28-
m_commit_offset(GetRand<unsigned>(HEADER_COMMITMENT_PERIOD)),
28+
m_commit_offset(FastRandomContext().randrange<unsigned>(HEADER_COMMITMENT_PERIOD)),
2929
m_id(id), m_consensus_params(consensus_params),
3030
m_chain_start(chain_start),
3131
m_minimum_required_work(minimum_required_work),

src/init.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,11 +1273,12 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
12731273
node.addrman = std::move(*addrman);
12741274
}
12751275

1276+
FastRandomContext rng;
12761277
assert(!node.banman);
12771278
node.banman = std::make_unique<BanMan>(args.GetDataDirNet() / "banlist", &uiInterface, args.GetIntArg("-bantime", DEFAULT_MISBEHAVING_BANTIME));
12781279
assert(!node.connman);
1279-
node.connman = std::make_unique<CConnman>(GetRand<uint64_t>(),
1280-
GetRand<uint64_t>(),
1280+
node.connman = std::make_unique<CConnman>(rng.rand64(),
1281+
rng.rand64(),
12811282
*node.addrman, *node.netgroupman, chainparams, args.GetBoolArg("-networkactive", true));
12821283

12831284
assert(!node.fee_estimator);

src/net_processing.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2124,7 +2124,7 @@ void PeerManagerImpl::BlockDisconnected(const std::shared_ptr<const CBlock> &blo
21242124
*/
21252125
void PeerManagerImpl::NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock>& pblock)
21262126
{
2127-
auto pcmpctblock = std::make_shared<const CBlockHeaderAndShortTxIDs>(*pblock, GetRand<uint64_t>());
2127+
auto pcmpctblock = std::make_shared<const CBlockHeaderAndShortTxIDs>(*pblock, FastRandomContext().rand64());
21282128

21292129
LOCK(cs_main);
21302130

@@ -2522,7 +2522,7 @@ void PeerManagerImpl::ProcessGetBlockData(CNode& pfrom, Peer& peer, const CInv&
25222522
if (a_recent_compact_block && a_recent_compact_block->header.GetHash() == pindex->GetBlockHash()) {
25232523
MakeAndPushMessage(pfrom, NetMsgType::CMPCTBLOCK, *a_recent_compact_block);
25242524
} else {
2525-
CBlockHeaderAndShortTxIDs cmpctblock{*pblock, GetRand<uint64_t>()};
2525+
CBlockHeaderAndShortTxIDs cmpctblock{*pblock, FastRandomContext().rand64()};
25262526
MakeAndPushMessage(pfrom, NetMsgType::CMPCTBLOCK, cmpctblock);
25272527
}
25282528
} else {
@@ -5617,7 +5617,7 @@ void PeerManagerImpl::MaybeSendPing(CNode& node_to, Peer& peer, std::chrono::mic
56175617
if (pingSend) {
56185618
uint64_t nonce;
56195619
do {
5620-
nonce = GetRand<uint64_t>();
5620+
nonce = FastRandomContext().rand64();
56215621
} while (nonce == 0);
56225622
peer.m_ping_queued = false;
56235623
peer.m_ping_start = now;
@@ -5984,7 +5984,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
59845984
CBlock block;
59855985
const bool ret{m_chainman.m_blockman.ReadBlockFromDisk(block, *pBestIndex)};
59865986
assert(ret);
5987-
CBlockHeaderAndShortTxIDs cmpctblock{block, GetRand<uint64_t>()};
5987+
CBlockHeaderAndShortTxIDs cmpctblock{block, m_rng.rand64()};
59885988
MakeAndPushMessage(*pto, NetMsgType::CMPCTBLOCK, cmpctblock);
59895989
}
59905990
state.pindexBestHeaderSent = pBestIndex;

src/netaddress.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -567,8 +567,8 @@ class CServiceHash
567567
{
568568
public:
569569
CServiceHash()
570-
: m_salt_k0{GetRand<uint64_t>()},
571-
m_salt_k1{GetRand<uint64_t>()}
570+
: m_salt_k0{FastRandomContext().rand64()},
571+
m_salt_k1{FastRandomContext().rand64()}
572572
{
573573
}
574574

src/node/txreconciliation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class TxReconciliationTracker::Impl
8585
LOCK(m_txreconciliation_mutex);
8686

8787
LogPrintLevel(BCLog::TXRECONCILIATION, BCLog::Level::Debug, "Pre-register peer=%d\n", peer_id);
88-
const uint64_t local_salt{GetRand(UINT64_MAX)};
88+
const uint64_t local_salt{FastRandomContext().rand64()};
8989

9090
// We do this exactly once per peer (which are unique by NodeId, see GetNewNodeId) so it's
9191
// safe to assume we don't have this record yet.

src/random.h

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -426,20 +426,6 @@ void Shuffle(I first, I last, R&& rng)
426426
}
427427
}
428428

429-
/** Generate a uniform random integer of type T in the range [0..nMax)
430-
* Precondition: nMax > 0, T is an integral type, no larger than uint64_t
431-
*/
432-
template<typename T>
433-
T GetRand(T nMax) noexcept {
434-
return T(FastRandomContext().randrange(nMax));
435-
}
436-
437-
/** Generate a uniform random integer of type T in its entire non-negative range. */
438-
template<typename T>
439-
T GetRand() noexcept {
440-
return T(FastRandomContext().rand<T>());
441-
}
442-
443429
/** Generate a uniform random duration in the range [0..max). Precondition: max.count() > 0 */
444430
template <typename D>
445431
D GetRandomDuration(typename std::common_type<D>::type max) noexcept
@@ -448,7 +434,7 @@ D GetRandomDuration(typename std::common_type<D>::type max) noexcept
448434
// type than the function argument. So std::common_type is used to force the
449435
// call site to specify the type of the return value.
450436
{
451-
return D{GetRand(max.count())};
437+
return D{FastRandomContext().randrange(max.count())};
452438
};
453439
constexpr auto GetRandMicros = GetRandomDuration<std::chrono::microseconds>;
454440
constexpr auto GetRandMillis = GetRandomDuration<std::chrono::milliseconds>;

src/test/random_tests.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,18 @@ BOOST_AUTO_TEST_CASE(fastrandom_tests_deterministic)
2828
FastRandomContext ctx2{true};
2929

3030
{
31-
BOOST_CHECK_EQUAL(GetRand<uint64_t>(), uint64_t{9330418229102544152u});
32-
BOOST_CHECK_EQUAL(GetRand<int>(), int{618925161});
31+
BOOST_CHECK_EQUAL(FastRandomContext().rand<uint64_t>(), uint64_t{9330418229102544152u});
32+
BOOST_CHECK_EQUAL(FastRandomContext().rand<int>(), int{618925161});
3333
BOOST_CHECK_EQUAL(GetRandMicros(std::chrono::hours{1}).count(), 1271170921);
3434
BOOST_CHECK_EQUAL(GetRandMillis(std::chrono::hours{1}).count(), 2803534);
3535

36-
BOOST_CHECK_EQUAL(GetRand<uint64_t>(), uint64_t{10170981140880778086u});
37-
BOOST_CHECK_EQUAL(GetRand<int>(), int{1689082725});
36+
BOOST_CHECK_EQUAL(FastRandomContext().rand<uint64_t>(), uint64_t{10170981140880778086u});
37+
BOOST_CHECK_EQUAL(FastRandomContext().rand<int>(), int{1689082725});
3838
BOOST_CHECK_EQUAL(GetRandMicros(std::chrono::hours{1}).count(), 2464643716);
3939
BOOST_CHECK_EQUAL(GetRandMillis(std::chrono::hours{1}).count(), 2312205);
4040

41-
BOOST_CHECK_EQUAL(GetRand<uint64_t>(), uint64_t{5689404004456455543u});
42-
BOOST_CHECK_EQUAL(GetRand<int>(), int{785839937});
41+
BOOST_CHECK_EQUAL(FastRandomContext().rand<uint64_t>(), uint64_t{5689404004456455543u});
42+
BOOST_CHECK_EQUAL(FastRandomContext().rand<int>(), int{785839937});
4343
BOOST_CHECK_EQUAL(GetRandMicros(std::chrono::hours{1}).count(), 93558804);
4444
BOOST_CHECK_EQUAL(GetRandMillis(std::chrono::hours{1}).count(), 507022);
4545
}
@@ -82,18 +82,18 @@ BOOST_AUTO_TEST_CASE(fastrandom_tests_nondeterministic)
8282
{
8383
// Check that a nondeterministic ones are not
8484
{
85-
BOOST_CHECK(GetRand<uint64_t>() != uint64_t{9330418229102544152u});
86-
BOOST_CHECK(GetRand<int>() != int{618925161});
85+
BOOST_CHECK(FastRandomContext().rand<uint64_t>() != uint64_t{9330418229102544152u});
86+
BOOST_CHECK(FastRandomContext().rand<int>() != int{618925161});
8787
BOOST_CHECK(GetRandMicros(std::chrono::hours{1}).count() != 1271170921);
8888
BOOST_CHECK(GetRandMillis(std::chrono::hours{1}).count() != 2803534);
8989

90-
BOOST_CHECK(GetRand<uint64_t>() != uint64_t{10170981140880778086u});
91-
BOOST_CHECK(GetRand<int>() != int{1689082725});
90+
BOOST_CHECK(FastRandomContext().rand<uint64_t>() != uint64_t{10170981140880778086u});
91+
BOOST_CHECK(FastRandomContext().rand<int>() != int{1689082725});
9292
BOOST_CHECK(GetRandMicros(std::chrono::hours{1}).count() != 2464643716);
9393
BOOST_CHECK(GetRandMillis(std::chrono::hours{1}).count() != 2312205);
9494

95-
BOOST_CHECK(GetRand<uint64_t>() != uint64_t{5689404004456455543u});
96-
BOOST_CHECK(GetRand<int>() != int{785839937});
95+
BOOST_CHECK(FastRandomContext().rand<uint64_t>() != uint64_t{5689404004456455543u});
96+
BOOST_CHECK(FastRandomContext().rand<int>() != int{785839937});
9797
BOOST_CHECK(GetRandMicros(std::chrono::hours{1}).count() != 93558804);
9898
BOOST_CHECK(GetRandMillis(std::chrono::hours{1}).count() != 507022);
9999
}

src/txmempool.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ void CTxMemPool::check(const CCoinsViewCache& active_coins_tip, int64_t spendhei
657657
{
658658
if (m_opts.check_ratio == 0) return;
659659

660-
if (GetRand(m_opts.check_ratio) >= 1) return;
660+
if (FastRandomContext().randrange(m_opts.check_ratio) >= 1) return;
661661

662662
AssertLockHeld(::cs_main);
663663
LOCK(cs);

0 commit comments

Comments
 (0)