Skip to content

Commit ab1ea29

Browse files
refactor: make GetRand a template, remove GetRandInt
1 parent 505ba39 commit ab1ea29

File tree

12 files changed

+34
-31
lines changed

12 files changed

+34
-31
lines changed

src/addrdb.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ template <typename Data>
4949
bool SerializeFileDB(const std::string& prefix, const fs::path& path, const Data& data, int version)
5050
{
5151
// Generate random temporary filename
52-
uint16_t randv = 0;
53-
GetRandBytes({(unsigned char*)&randv, sizeof(randv)});
52+
const uint16_t randv{GetRand<uint16_t>()};
5453
std::string tmpfn = strprintf("%s.%04x", prefix, randv);
5554

5655
// open temp output file, and associate with CAutoFile

src/blockencodings.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include <unordered_map>
1818

1919
CBlockHeaderAndShortTxIDs::CBlockHeaderAndShortTxIDs(const CBlock& block, bool fUseWTXID) :
20-
nonce(GetRand(std::numeric_limits<uint64_t>::max())),
20+
nonce(GetRand<uint64_t>()),
2121
shorttxids(block.vtx.size() - 1), prefilledtxn(1), header(block) {
2222
FillShortTxIDSelector();
2323
//TODO: Use our mempool prior to block acceptance to predictively fill more than just the coinbase

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(std::numeric_limits<unsigned int>::max());
242+
nTweak = GetRand<unsigned int>();
243243
nEntriesThisGeneration = 0;
244244
nGeneration = 1;
245245
std::fill(data.begin(), data.end(), 0);

src/init.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,8 +1268,8 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
12681268
assert(!node.banman);
12691269
node.banman = std::make_unique<BanMan>(gArgs.GetDataDirNet() / "banlist", &uiInterface, args.GetIntArg("-bantime", DEFAULT_MISBEHAVING_BANTIME));
12701270
assert(!node.connman);
1271-
node.connman = std::make_unique<CConnman>(GetRand(std::numeric_limits<uint64_t>::max()),
1272-
GetRand(std::numeric_limits<uint64_t>::max()),
1271+
node.connman = std::make_unique<CConnman>(GetRand<uint64_t>(),
1272+
GetRand<uint64_t>(),
12731273
*node.addrman, *node.netgroupman, args.GetBoolArg("-networkactive", true));
12741274

12751275
assert(!node.fee_estimator);

src/net.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2154,7 +2154,7 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
21542154

21552155
if (fFeeler) {
21562156
// Add small amount of random noise before connection to avoid synchronization.
2157-
int randsleep = GetRandInt(FEELER_SLEEP_WINDOW * 1000);
2157+
int randsleep = GetRand<int>(FEELER_SLEEP_WINDOW * 1000);
21582158
if (!interruptNet.sleep_for(std::chrono::milliseconds(randsleep)))
21592159
return;
21602160
LogPrint(BCLog::NET, "Making feeler connection to %s\n", addrConnect.ToString());

src/net_processing.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4470,10 +4470,10 @@ void PeerManagerImpl::MaybeSendPing(CNode& node_to, Peer& peer, std::chrono::mic
44704470
}
44714471

44724472
if (pingSend) {
4473-
uint64_t nonce = 0;
4474-
while (nonce == 0) {
4475-
GetRandBytes({(unsigned char*)&nonce, sizeof(nonce)});
4476-
}
4473+
uint64_t nonce;
4474+
do {
4475+
nonce = GetRand<uint64_t>();
4476+
} while (nonce == 0);
44774477
peer.m_ping_queued = false;
44784478
peer.m_ping_start = now;
44794479
if (node_to.GetCommonVersion() > BIP0031_VERSION) {

src/netaddress.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -556,8 +556,8 @@ class CServiceHash
556556
{
557557
public:
558558
CServiceHash()
559-
: m_salt_k0{GetRand(std::numeric_limits<uint64_t>::max())},
560-
m_salt_k1{GetRand(std::numeric_limits<uint64_t>::max())}
559+
: m_salt_k0{GetRand<uint64_t>()},
560+
m_salt_k1{GetRand<uint64_t>()}
561561
{
562562
}
563563

src/random.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -586,16 +586,11 @@ void RandAddEvent(const uint32_t event_info) noexcept { GetRNGState().AddEvent(e
586586

587587
bool g_mock_deterministic_tests{false};
588588

589-
uint64_t GetRand(uint64_t nMax) noexcept
589+
uint64_t GetRandInternal(uint64_t nMax) noexcept
590590
{
591591
return FastRandomContext(g_mock_deterministic_tests).randrange(nMax);
592592
}
593593

594-
int GetRandInt(int nMax) noexcept
595-
{
596-
return GetRand(nMax);
597-
}
598-
599594
uint256 GetRandHash() noexcept
600595
{
601596
uint256 hash;

src/random.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,17 @@
6969
*/
7070
void GetRandBytes(Span<unsigned char> bytes) noexcept;
7171
/** Generate a uniform random integer in the range [0..range). Precondition: range > 0 */
72-
uint64_t GetRand(uint64_t nMax) noexcept;
72+
uint64_t GetRandInternal(uint64_t nMax) noexcept;
73+
/** Generate a uniform random integer of type T in the range [0..nMax)
74+
* nMax defaults to std::numeric_limits<T>::max()
75+
* Precondition: nMax > 0, T is an integral type, no larger than uint64_t
76+
*/
77+
template<typename T>
78+
T GetRand(T nMax=std::numeric_limits<T>::max()) noexcept {
79+
static_assert(std::is_integral<T>(), "T must be integral");
80+
static_assert(std::numeric_limits<T>::max() <= std::numeric_limits<uint64_t>::max(), "GetRand only supports up to uint64_t");
81+
return T(GetRandInternal(nMax));
82+
}
7383
/** Generate a uniform random duration in the range [0..max). Precondition: max.count() > 0 */
7484
template <typename D>
7585
D GetRandomDuration(typename std::common_type<D>::type max) noexcept
@@ -95,7 +105,6 @@ constexpr auto GetRandMillis = GetRandomDuration<std::chrono::milliseconds>;
95105
* */
96106
std::chrono::microseconds GetExponentialRand(std::chrono::microseconds now, std::chrono::seconds average_interval);
97107

98-
int GetRandInt(int nMax) noexcept;
99108
uint256 GetRandHash() noexcept;
100109

101110
/**

src/test/random_tests.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ BOOST_AUTO_TEST_CASE(fastrandom_tests)
2626
FastRandomContext ctx2(true);
2727

2828
for (int i = 10; i > 0; --i) {
29-
BOOST_CHECK_EQUAL(GetRand(std::numeric_limits<uint64_t>::max()), uint64_t{10393729187455219830U});
30-
BOOST_CHECK_EQUAL(GetRandInt(std::numeric_limits<int>::max()), int{769702006});
29+
BOOST_CHECK_EQUAL(GetRand<uint64_t>(), uint64_t{10393729187455219830U});
30+
BOOST_CHECK_EQUAL(GetRand<int>(), int{769702006});
3131
BOOST_CHECK_EQUAL(GetRandMicros(std::chrono::hours{1}).count(), 2917185654);
3232
BOOST_CHECK_EQUAL(GetRandMillis(std::chrono::hours{1}).count(), 2144374);
3333
}
@@ -47,8 +47,8 @@ BOOST_AUTO_TEST_CASE(fastrandom_tests)
4747
// Check that a nondeterministic ones are not
4848
g_mock_deterministic_tests = false;
4949
for (int i = 10; i > 0; --i) {
50-
BOOST_CHECK(GetRand(std::numeric_limits<uint64_t>::max()) != uint64_t{10393729187455219830U});
51-
BOOST_CHECK(GetRandInt(std::numeric_limits<int>::max()) != int{769702006});
50+
BOOST_CHECK(GetRand<uint64_t>() != uint64_t{10393729187455219830U});
51+
BOOST_CHECK(GetRand<int>() != int{769702006});
5252
BOOST_CHECK(GetRandMicros(std::chrono::hours{1}) != std::chrono::microseconds{2917185654});
5353
BOOST_CHECK(GetRandMillis(std::chrono::hours{1}) != std::chrono::milliseconds{2144374});
5454
}

0 commit comments

Comments
 (0)