Skip to content

Commit 5f0b04e

Browse files
committed
Replace rand() & ((1 << N) - 1) with randbits(N)
1 parent 3ecabae commit 5f0b04e

File tree

3 files changed

+9
-9
lines changed

3 files changed

+9
-9
lines changed

src/test/coins_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,11 @@ BOOST_AUTO_TEST_CASE(coins_cache_simulation_test)
155155
newcoin.out.nValue = insecure_rand();
156156
newcoin.nHeight = 1;
157157
if (insecure_randrange(16) == 0 && coin.IsSpent()) {
158-
newcoin.out.scriptPubKey.assign(1 + (insecure_rand() & 0x3F), OP_RETURN);
158+
newcoin.out.scriptPubKey.assign(1 + insecure_randbits(6), OP_RETURN);
159159
BOOST_CHECK(newcoin.out.scriptPubKey.IsUnspendable());
160160
added_an_unspendable_entry = true;
161161
} else {
162-
newcoin.out.scriptPubKey.assign(insecure_rand() & 0x3F, 0); // Random sizes so we can test memory usage accounting
162+
newcoin.out.scriptPubKey.assign(insecure_randbits(6), 0); // Random sizes so we can test memory usage accounting
163163
(coin.IsSpent() ? added_an_entry : updated_an_entry) = true;
164164
coin = newcoin;
165165
}

src/test/pmt_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ BOOST_AUTO_TEST_CASE(pmt_test1)
6262
std::vector<bool> vMatch(nTx, false);
6363
std::vector<uint256> vMatchTxid1;
6464
for (unsigned int j=0; j<nTx; j++) {
65-
bool fInclude = (insecure_rand() & ((1 << (att/2)) - 1)) == 0;
65+
bool fInclude = insecure_randbits(att / 2) == 0;
6666
vMatch[j] = fInclude;
6767
if (fInclude)
6868
vMatchTxid1.push_back(vTxid[j]);

src/test/versionbits_tests.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class VersionBitsTester
8080

8181
VersionBitsTester& TestStateSinceHeight(int height) {
8282
for (int i = 0; i < CHECKERS; i++) {
83-
if ((insecure_rand() & ((1 << i) - 1)) == 0) {
83+
if (insecure_randbits(i) == 0) {
8484
BOOST_CHECK_MESSAGE(checker[i].GetStateSinceHeightFor(vpblock.empty() ? NULL : vpblock.back()) == height, strprintf("Test %i for StateSinceHeight", num));
8585
}
8686
}
@@ -90,7 +90,7 @@ class VersionBitsTester
9090

9191
VersionBitsTester& TestDefined() {
9292
for (int i = 0; i < CHECKERS; i++) {
93-
if ((insecure_rand() & ((1 << i) - 1)) == 0) {
93+
if (insecure_randbits(i) == 0) {
9494
BOOST_CHECK_MESSAGE(checker[i].GetStateFor(vpblock.empty() ? NULL : vpblock.back()) == THRESHOLD_DEFINED, strprintf("Test %i for DEFINED", num));
9595
}
9696
}
@@ -100,7 +100,7 @@ class VersionBitsTester
100100

101101
VersionBitsTester& TestStarted() {
102102
for (int i = 0; i < CHECKERS; i++) {
103-
if ((insecure_rand() & ((1 << i) - 1)) == 0) {
103+
if (insecure_randbits(i) == 0) {
104104
BOOST_CHECK_MESSAGE(checker[i].GetStateFor(vpblock.empty() ? NULL : vpblock.back()) == THRESHOLD_STARTED, strprintf("Test %i for STARTED", num));
105105
}
106106
}
@@ -110,7 +110,7 @@ class VersionBitsTester
110110

111111
VersionBitsTester& TestLockedIn() {
112112
for (int i = 0; i < CHECKERS; i++) {
113-
if ((insecure_rand() & ((1 << i) - 1)) == 0) {
113+
if (insecure_randbits(i) == 0) {
114114
BOOST_CHECK_MESSAGE(checker[i].GetStateFor(vpblock.empty() ? NULL : vpblock.back()) == THRESHOLD_LOCKED_IN, strprintf("Test %i for LOCKED_IN", num));
115115
}
116116
}
@@ -120,7 +120,7 @@ class VersionBitsTester
120120

121121
VersionBitsTester& TestActive() {
122122
for (int i = 0; i < CHECKERS; i++) {
123-
if ((insecure_rand() & ((1 << i) - 1)) == 0) {
123+
if (insecure_randbits(i) == 0) {
124124
BOOST_CHECK_MESSAGE(checker[i].GetStateFor(vpblock.empty() ? NULL : vpblock.back()) == THRESHOLD_ACTIVE, strprintf("Test %i for ACTIVE", num));
125125
}
126126
}
@@ -130,7 +130,7 @@ class VersionBitsTester
130130

131131
VersionBitsTester& TestFailed() {
132132
for (int i = 0; i < CHECKERS; i++) {
133-
if ((insecure_rand() & ((1 << i) - 1)) == 0) {
133+
if (insecure_randbits(i) == 0) {
134134
BOOST_CHECK_MESSAGE(checker[i].GetStateFor(vpblock.empty() ? NULL : vpblock.back()) == THRESHOLD_FAILED, strprintf("Test %i for FAILED", num));
135135
}
136136
}

0 commit comments

Comments
 (0)