Skip to content

Commit efee1db

Browse files
committed
scripted-diff: use insecure_rand256/randrange more
-BEGIN VERIFY SCRIPT- sed -i "s/\<GetRandHash(/insecure_rand256(/" src/test/*_tests.cpp sed -i "s/\<GetRand(/insecure_randrange(/" src/test/*_tests.cpp src/test/test_bitcoin.cpp sed -i 's/\<insecure_rand() % \([0-9]\+\)/insecure_randrange(\1)/g' src/test/*_tests.cpp -END VERIFY SCRIPT-
1 parent 1119927 commit efee1db

15 files changed

+72
-72
lines changed

src/test/DoS_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ BOOST_AUTO_TEST_CASE(DoS_bantime)
129129
CTransactionRef RandomOrphan()
130130
{
131131
std::map<uint256, COrphanTx>::iterator it;
132-
it = mapOrphanTransactions.lower_bound(GetRandHash());
132+
it = mapOrphanTransactions.lower_bound(insecure_rand256());
133133
if (it == mapOrphanTransactions.end())
134134
it = mapOrphanTransactions.begin();
135135
return it->second.tx;
@@ -148,7 +148,7 @@ BOOST_AUTO_TEST_CASE(DoS_mapOrphans)
148148
CMutableTransaction tx;
149149
tx.vin.resize(1);
150150
tx.vin[0].prevout.n = 0;
151-
tx.vin[0].prevout.hash = GetRandHash();
151+
tx.vin[0].prevout.hash = insecure_rand256();
152152
tx.vin[0].scriptSig << OP_1;
153153
tx.vout.resize(1);
154154
tx.vout[0].nValue = 1*CENT;

src/test/blockencodings_tests.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,16 @@ static CBlock BuildBlockTestCase() {
3030
block.vtx.resize(3);
3131
block.vtx[0] = MakeTransactionRef(tx);
3232
block.nVersion = 42;
33-
block.hashPrevBlock = GetRandHash();
33+
block.hashPrevBlock = insecure_rand256();
3434
block.nBits = 0x207fffff;
3535

36-
tx.vin[0].prevout.hash = GetRandHash();
36+
tx.vin[0].prevout.hash = insecure_rand256();
3737
tx.vin[0].prevout.n = 0;
3838
block.vtx[1] = MakeTransactionRef(tx);
3939

4040
tx.vin.resize(10);
4141
for (size_t i = 0; i < tx.vin.size(); i++) {
42-
tx.vin[i].prevout.hash = GetRandHash();
42+
tx.vin[i].prevout.hash = insecure_rand256();
4343
tx.vin[i].prevout.n = 0;
4444
}
4545
block.vtx[2] = MakeTransactionRef(tx);
@@ -283,7 +283,7 @@ BOOST_AUTO_TEST_CASE(EmptyBlockRoundTripTest)
283283
block.vtx.resize(1);
284284
block.vtx[0] = MakeTransactionRef(std::move(coinbase));
285285
block.nVersion = 42;
286-
block.hashPrevBlock = GetRandHash();
286+
block.hashPrevBlock = insecure_rand256();
287287
block.nBits = 0x207fffff;
288288

289289
bool mutated;
@@ -316,7 +316,7 @@ BOOST_AUTO_TEST_CASE(EmptyBlockRoundTripTest)
316316

317317
BOOST_AUTO_TEST_CASE(TransactionsRequestSerializationTest) {
318318
BlockTransactionsRequest req1;
319-
req1.blockhash = GetRandHash();
319+
req1.blockhash = insecure_rand256();
320320
req1.indexes.resize(4);
321321
req1.indexes[0] = 0;
322322
req1.indexes[1] = 1;

src/test/bloom_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ BOOST_AUTO_TEST_CASE(merkle_block_4_test_update_none)
463463

464464
static std::vector<unsigned char> RandomData()
465465
{
466-
uint256 r = GetRandHash();
466+
uint256 r = insecure_rand256();
467467
return std::vector<unsigned char>(r.begin(), r.end());
468468
}
469469

src/test/checkqueue_tests.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ void Correct_Queue_range(std::vector<size_t> range)
160160
FakeCheckCheckCompletion::n_calls = 0;
161161
CCheckQueueControl<FakeCheckCheckCompletion> control(small_queue.get());
162162
while (total) {
163-
vChecks.resize(std::min(total, (size_t) GetRand(10)));
163+
vChecks.resize(std::min(total, (size_t) insecure_randrange(10)));
164164
total -= vChecks.size();
165165
control.Add(vChecks);
166166
}
@@ -204,7 +204,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_Correct_Random)
204204
{
205205
std::vector<size_t> range;
206206
range.reserve(100000/1000);
207-
for (size_t i = 2; i < 100000; i += std::max((size_t)1, (size_t)GetRand(std::min((size_t)1000, ((size_t)100000) - i))))
207+
for (size_t i = 2; i < 100000; i += std::max((size_t)1, (size_t)insecure_randrange(std::min((size_t)1000, ((size_t)100000) - i))))
208208
range.push_back(i);
209209
Correct_Queue_range(range);
210210
}
@@ -224,7 +224,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_Catches_Failure)
224224
CCheckQueueControl<FailingCheck> control(fail_queue.get());
225225
size_t remaining = i;
226226
while (remaining) {
227-
size_t r = GetRand(10);
227+
size_t r = insecure_randrange(10);
228228

229229
std::vector<FailingCheck> vChecks;
230230
vChecks.reserve(r);
@@ -286,7 +286,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_UniqueCheck)
286286
{
287287
CCheckQueueControl<UniqueCheck> control(queue.get());
288288
while (total) {
289-
size_t r = GetRand(10);
289+
size_t r = insecure_randrange(10);
290290
std::vector<UniqueCheck> vChecks;
291291
for (size_t k = 0; k < r && total; k++)
292292
vChecks.emplace_back(--total);
@@ -320,7 +320,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_Memory)
320320
{
321321
CCheckQueueControl<MemoryCheck> control(queue.get());
322322
while (total) {
323-
size_t r = GetRand(10);
323+
size_t r = insecure_randrange(10);
324324
std::vector<MemoryCheck> vChecks;
325325
for (size_t k = 0; k < r && total; k++) {
326326
total--;

src/test/coins_tests.cpp

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class CCoinsViewTest : public CCoinsView
4343
return false;
4444
}
4545
coin = it->second;
46-
if (coin.IsSpent() && insecure_rand() % 2 == 0) {
46+
if (coin.IsSpent() && insecure_randrange(2) == 0) {
4747
// Randomly return false in case of an empty entry.
4848
return false;
4949
}
@@ -64,7 +64,7 @@ class CCoinsViewTest : public CCoinsView
6464
if (it->second.flags & CCoinsCacheEntry::DIRTY) {
6565
// Same optimization used in CCoinsViewDB is to only write dirty entries.
6666
map_[it->first] = it->second.coin;
67-
if (it->second.coin.IsSpent() && insecure_rand() % 3 == 0) {
67+
if (it->second.coin.IsSpent() && insecure_randrange(3) == 0) {
6868
// Randomly delete empty entries on write.
6969
map_.erase(it->first);
7070
}
@@ -139,22 +139,22 @@ BOOST_AUTO_TEST_CASE(coins_cache_simulation_test)
139139
std::vector<uint256> txids;
140140
txids.resize(NUM_SIMULATION_ITERATIONS / 8);
141141
for (unsigned int i = 0; i < txids.size(); i++) {
142-
txids[i] = GetRandHash();
142+
txids[i] = insecure_rand256();
143143
}
144144

145145
for (unsigned int i = 0; i < NUM_SIMULATION_ITERATIONS; i++) {
146146
// Do a random modification.
147147
{
148148
uint256 txid = txids[insecure_rand() % txids.size()]; // txid we're going to modify in this iteration.
149149
Coin& coin = result[COutPoint(txid, 0)];
150-
const Coin& entry = (insecure_rand() % 500 == 0) ? AccessByTxid(*stack.back(), txid) : stack.back()->AccessCoin(COutPoint(txid, 0));
150+
const Coin& entry = (insecure_randrange(500) == 0) ? AccessByTxid(*stack.back(), txid) : stack.back()->AccessCoin(COutPoint(txid, 0));
151151
BOOST_CHECK(coin == entry);
152152

153-
if (insecure_rand() % 5 == 0 || coin.IsSpent()) {
153+
if (insecure_randrange(5) == 0 || coin.IsSpent()) {
154154
Coin newcoin;
155155
newcoin.out.nValue = insecure_rand();
156156
newcoin.nHeight = 1;
157-
if (insecure_rand() % 16 == 0 && coin.IsSpent()) {
157+
if (insecure_randrange(16) == 0 && coin.IsSpent()) {
158158
newcoin.out.scriptPubKey.assign(1 + (insecure_rand() & 0x3F), OP_RETURN);
159159
BOOST_CHECK(newcoin.out.scriptPubKey.IsUnspendable());
160160
added_an_unspendable_entry = true;
@@ -172,15 +172,15 @@ BOOST_AUTO_TEST_CASE(coins_cache_simulation_test)
172172
}
173173

174174
// One every 10 iterations, remove a random entry from the cache
175-
if (insecure_rand() % 10 == 0) {
175+
if (insecure_randrange(10) == 0) {
176176
COutPoint out(txids[insecure_rand() % txids.size()], 0);
177177
int cacheid = insecure_rand() % stack.size();
178178
stack[cacheid]->Uncache(out);
179179
uncached_an_entry |= !stack[cacheid]->HaveCoinInCache(out);
180180
}
181181

182182
// Once every 1000 iterations and at the end, verify the full cache.
183-
if (insecure_rand() % 1000 == 1 || i == NUM_SIMULATION_ITERATIONS - 1) {
183+
if (insecure_randrange(1000) == 1 || i == NUM_SIMULATION_ITERATIONS - 1) {
184184
for (auto it = result.begin(); it != result.end(); it++) {
185185
bool have = stack.back()->HaveCoin(it->first);
186186
const Coin& coin = stack.back()->AccessCoin(it->first);
@@ -198,22 +198,22 @@ BOOST_AUTO_TEST_CASE(coins_cache_simulation_test)
198198
}
199199
}
200200

201-
if (insecure_rand() % 100 == 0) {
201+
if (insecure_randrange(100) == 0) {
202202
// Every 100 iterations, flush an intermediate cache
203-
if (stack.size() > 1 && insecure_rand() % 2 == 0) {
203+
if (stack.size() > 1 && insecure_randrange(2) == 0) {
204204
unsigned int flushIndex = insecure_rand() % (stack.size() - 1);
205205
stack[flushIndex]->Flush();
206206
}
207207
}
208-
if (insecure_rand() % 100 == 0) {
208+
if (insecure_randrange(100) == 0) {
209209
// Every 100 iterations, change the cache stack.
210-
if (stack.size() > 0 && insecure_rand() % 2 == 0) {
210+
if (stack.size() > 0 && insecure_randrange(2) == 0) {
211211
//Remove the top cache
212212
stack.back()->Flush();
213213
delete stack.back();
214214
stack.pop_back();
215215
}
216-
if (stack.size() == 0 || (stack.size() < 4 && insecure_rand() % 2)) {
216+
if (stack.size() == 0 || (stack.size() < 4 && insecure_randrange(2))) {
217217
//Add a new cache
218218
CCoinsView* tip = &base;
219219
if (stack.size() > 0) {
@@ -253,7 +253,7 @@ UtxoData utxoData;
253253

254254
UtxoData::iterator FindRandomFrom(const std::set<COutPoint> &utxoSet) {
255255
assert(utxoSet.size());
256-
auto utxoSetIt = utxoSet.lower_bound(COutPoint(GetRandHash(), 0));
256+
auto utxoSetIt = utxoSet.lower_bound(COutPoint(insecure_rand256(), 0));
257257
if (utxoSetIt == utxoSet.end()) {
258258
utxoSetIt = utxoSet.begin();
259259
}
@@ -301,7 +301,7 @@ BOOST_AUTO_TEST_CASE(updatecoins_simulation_test)
301301
// 2/20 times create a new coinbase
302302
if (randiter % 20 < 2 || coinbase_coins.size() < 10) {
303303
// 1/10 of those times create a duplicate coinbase
304-
if (insecure_rand() % 10 == 0 && coinbase_coins.size()) {
304+
if (insecure_randrange(10) == 0 && coinbase_coins.size()) {
305305
auto utxod = FindRandomFrom(coinbase_coins);
306306
// Reuse the exact same coinbase
307307
tx = std::get<0>(utxod->second);
@@ -411,7 +411,7 @@ BOOST_AUTO_TEST_CASE(updatecoins_simulation_test)
411411
}
412412

413413
// Once every 1000 iterations and at the end, verify the full cache.
414-
if (insecure_rand() % 1000 == 1 || i == NUM_SIMULATION_ITERATIONS - 1) {
414+
if (insecure_randrange(1000) == 1 || i == NUM_SIMULATION_ITERATIONS - 1) {
415415
for (auto it = result.begin(); it != result.end(); it++) {
416416
bool have = stack.back()->HaveCoin(it->first);
417417
const Coin& coin = stack.back()->AccessCoin(it->first);
@@ -421,31 +421,31 @@ BOOST_AUTO_TEST_CASE(updatecoins_simulation_test)
421421
}
422422

423423
// One every 10 iterations, remove a random entry from the cache
424-
if (utxoset.size() > 1 && insecure_rand() % 30 == 0) {
424+
if (utxoset.size() > 1 && insecure_randrange(30) == 0) {
425425
stack[insecure_rand() % stack.size()]->Uncache(FindRandomFrom(utxoset)->first);
426426
}
427-
if (disconnected_coins.size() > 1 && insecure_rand() % 30 == 0) {
427+
if (disconnected_coins.size() > 1 && insecure_randrange(30) == 0) {
428428
stack[insecure_rand() % stack.size()]->Uncache(FindRandomFrom(disconnected_coins)->first);
429429
}
430-
if (duplicate_coins.size() > 1 && insecure_rand() % 30 == 0) {
430+
if (duplicate_coins.size() > 1 && insecure_randrange(30) == 0) {
431431
stack[insecure_rand() % stack.size()]->Uncache(FindRandomFrom(duplicate_coins)->first);
432432
}
433433

434-
if (insecure_rand() % 100 == 0) {
434+
if (insecure_randrange(100) == 0) {
435435
// Every 100 iterations, flush an intermediate cache
436-
if (stack.size() > 1 && insecure_rand() % 2 == 0) {
436+
if (stack.size() > 1 && insecure_randrange(2) == 0) {
437437
unsigned int flushIndex = insecure_rand() % (stack.size() - 1);
438438
stack[flushIndex]->Flush();
439439
}
440440
}
441-
if (insecure_rand() % 100 == 0) {
441+
if (insecure_randrange(100) == 0) {
442442
// Every 100 iterations, change the cache stack.
443-
if (stack.size() > 0 && insecure_rand() % 2 == 0) {
443+
if (stack.size() > 0 && insecure_randrange(2) == 0) {
444444
stack.back()->Flush();
445445
delete stack.back();
446446
stack.pop_back();
447447
}
448-
if (stack.size() == 0 || (stack.size() < 4 && insecure_rand() % 2)) {
448+
if (stack.size() == 0 || (stack.size() < 4 && insecure_randrange(2))) {
449449
CCoinsView* tip = &base;
450450
if (stack.size() > 0) {
451451
tip = stack.back();

src/test/dbwrapper_tests.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper)
3131
fs::path ph = fs::temp_directory_path() / fs::unique_path();
3232
CDBWrapper dbw(ph, (1 << 20), true, false, obfuscate);
3333
char key = 'k';
34-
uint256 in = GetRandHash();
34+
uint256 in = insecure_rand256();
3535
uint256 res;
3636

3737
// Ensure that we're doing real obfuscation when obfuscate=true
@@ -53,11 +53,11 @@ BOOST_AUTO_TEST_CASE(dbwrapper_batch)
5353
CDBWrapper dbw(ph, (1 << 20), true, false, obfuscate);
5454

5555
char key = 'i';
56-
uint256 in = GetRandHash();
56+
uint256 in = insecure_rand256();
5757
char key2 = 'j';
58-
uint256 in2 = GetRandHash();
58+
uint256 in2 = insecure_rand256();
5959
char key3 = 'k';
60-
uint256 in3 = GetRandHash();
60+
uint256 in3 = insecure_rand256();
6161

6262
uint256 res;
6363
CDBBatch batch(dbw);
@@ -91,10 +91,10 @@ BOOST_AUTO_TEST_CASE(dbwrapper_iterator)
9191

9292
// The two keys are intentionally chosen for ordering
9393
char key = 'j';
94-
uint256 in = GetRandHash();
94+
uint256 in = insecure_rand256();
9595
BOOST_CHECK(dbw.Write(key, in));
9696
char key2 = 'k';
97-
uint256 in2 = GetRandHash();
97+
uint256 in2 = insecure_rand256();
9898
BOOST_CHECK(dbw.Write(key2, in2));
9999

100100
std::unique_ptr<CDBIterator> it(const_cast<CDBWrapper*>(&dbw)->NewIterator());
@@ -132,7 +132,7 @@ BOOST_AUTO_TEST_CASE(existing_data_no_obfuscate)
132132
// Set up a non-obfuscated wrapper to write some initial data.
133133
CDBWrapper* dbw = new CDBWrapper(ph, (1 << 10), false, false, false);
134134
char key = 'k';
135-
uint256 in = GetRandHash();
135+
uint256 in = insecure_rand256();
136136
uint256 res;
137137

138138
BOOST_CHECK(dbw->Write(key, in));
@@ -155,7 +155,7 @@ BOOST_AUTO_TEST_CASE(existing_data_no_obfuscate)
155155
BOOST_CHECK(!odbw.IsEmpty()); // There should be existing data
156156
BOOST_CHECK(is_null_key(dbwrapper_private::GetObfuscateKey(odbw))); // The key should be an empty string
157157

158-
uint256 in2 = GetRandHash();
158+
uint256 in2 = insecure_rand256();
159159
uint256 res3;
160160

161161
// Check that we can write successfully
@@ -174,7 +174,7 @@ BOOST_AUTO_TEST_CASE(existing_data_reindex)
174174
// Set up a non-obfuscated wrapper to write some initial data.
175175
CDBWrapper* dbw = new CDBWrapper(ph, (1 << 10), false, false, false);
176176
char key = 'k';
177-
uint256 in = GetRandHash();
177+
uint256 in = insecure_rand256();
178178
uint256 res;
179179

180180
BOOST_CHECK(dbw->Write(key, in));
@@ -193,7 +193,7 @@ BOOST_AUTO_TEST_CASE(existing_data_reindex)
193193
BOOST_CHECK(!odbw.Read(key, res2));
194194
BOOST_CHECK(!is_null_key(dbwrapper_private::GetObfuscateKey(odbw)));
195195

196-
uint256 in2 = GetRandHash();
196+
uint256 in2 = insecure_rand256();
197197
uint256 res3;
198198

199199
// Check that we can write successfully

src/test/hash_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ BOOST_AUTO_TEST_CASE(siphash)
134134
for (int i = 0; i < 16; ++i) {
135135
uint64_t k1 = ctx.rand64();
136136
uint64_t k2 = ctx.rand64();
137-
uint256 x = GetRandHash();
137+
uint256 x = insecure_rand256();
138138
uint32_t n = ctx.rand32();
139139
uint8_t nb[4];
140140
WriteLE32(nb, n);

src/test/merkle_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ BOOST_AUTO_TEST_CASE(merkle_test)
6767
{
6868
for (int i = 0; i < 32; i++) {
6969
// Try 32 block sizes: all sizes from 0 to 16 inclusive, and then 15 random sizes.
70-
int ntx = (i <= 16) ? i : 17 + (insecure_rand() % 4000);
70+
int ntx = (i <= 16) ? i : 17 + (insecure_randrange(4000));
7171
// Try up to 3 mutations.
7272
for (int mutate = 0; mutate <= 3; mutate++) {
7373
int duplicate1 = mutate >= 1 ? 1 << ctz(ntx) : 0; // The last how many transactions to duplicate first.

src/test/miner_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
372372
while (chainActive.Tip()->nHeight < 209999) {
373373
CBlockIndex* prev = chainActive.Tip();
374374
CBlockIndex* next = new CBlockIndex();
375-
next->phashBlock = new uint256(GetRandHash());
375+
next->phashBlock = new uint256(insecure_rand256());
376376
pcoinsTip->SetBestBlock(next->GetBlockHash());
377377
next->pprev = prev;
378378
next->nHeight = prev->nHeight + 1;
@@ -384,7 +384,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
384384
while (chainActive.Tip()->nHeight < 210000) {
385385
CBlockIndex* prev = chainActive.Tip();
386386
CBlockIndex* next = new CBlockIndex();
387-
next->phashBlock = new uint256(GetRandHash());
387+
next->phashBlock = new uint256(insecure_rand256());
388388
pcoinsTip->SetBestBlock(next->GetBlockHash());
389389
next->pprev = prev;
390390
next->nHeight = prev->nHeight + 1;

src/test/pmt_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class CPartialMerkleTreeTester : public CPartialMerkleTree
2222
// flip one bit in one of the hashes - this should break the authentication
2323
void Damage() {
2424
unsigned int n = insecure_rand() % vHash.size();
25-
int bit = insecure_rand() % 256;
25+
int bit = insecure_randrange(256);
2626
*(vHash[n].begin() + (bit>>3)) ^= 1<<(bit&7);
2727
}
2828
};

0 commit comments

Comments
 (0)