@@ -43,7 +43,7 @@ class CCoinsViewTest : public CCoinsView
43
43
return false ;
44
44
}
45
45
coin = it->second ;
46
- if (coin.IsSpent () && insecure_rand () % 2 == 0 ) {
46
+ if (coin.IsSpent () && insecure_randrange ( 2 ) == 0 ) {
47
47
// Randomly return false in case of an empty entry.
48
48
return false ;
49
49
}
@@ -64,7 +64,7 @@ class CCoinsViewTest : public CCoinsView
64
64
if (it->second .flags & CCoinsCacheEntry::DIRTY) {
65
65
// Same optimization used in CCoinsViewDB is to only write dirty entries.
66
66
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 ) {
68
68
// Randomly delete empty entries on write.
69
69
map_.erase (it->first );
70
70
}
@@ -139,22 +139,22 @@ BOOST_AUTO_TEST_CASE(coins_cache_simulation_test)
139
139
std::vector<uint256> txids;
140
140
txids.resize (NUM_SIMULATION_ITERATIONS / 8 );
141
141
for (unsigned int i = 0 ; i < txids.size (); i++) {
142
- txids[i] = GetRandHash ();
142
+ txids[i] = insecure_rand256 ();
143
143
}
144
144
145
145
for (unsigned int i = 0 ; i < NUM_SIMULATION_ITERATIONS; i++) {
146
146
// Do a random modification.
147
147
{
148
148
uint256 txid = txids[insecure_rand () % txids.size ()]; // txid we're going to modify in this iteration.
149
149
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 ));
151
151
BOOST_CHECK (coin == entry);
152
152
153
- if (insecure_rand () % 5 == 0 || coin.IsSpent ()) {
153
+ if (insecure_randrange ( 5 ) == 0 || coin.IsSpent ()) {
154
154
Coin newcoin;
155
155
newcoin.out .nValue = insecure_rand ();
156
156
newcoin.nHeight = 1 ;
157
- if (insecure_rand () % 16 == 0 && coin.IsSpent ()) {
157
+ if (insecure_randrange ( 16 ) == 0 && coin.IsSpent ()) {
158
158
newcoin.out .scriptPubKey .assign (1 + (insecure_rand () & 0x3F ), OP_RETURN);
159
159
BOOST_CHECK (newcoin.out .scriptPubKey .IsUnspendable ());
160
160
added_an_unspendable_entry = true ;
@@ -172,15 +172,15 @@ BOOST_AUTO_TEST_CASE(coins_cache_simulation_test)
172
172
}
173
173
174
174
// One every 10 iterations, remove a random entry from the cache
175
- if (insecure_rand () % 10 == 0 ) {
175
+ if (insecure_randrange ( 10 ) == 0 ) {
176
176
COutPoint out (txids[insecure_rand () % txids.size ()], 0 );
177
177
int cacheid = insecure_rand () % stack.size ();
178
178
stack[cacheid]->Uncache (out);
179
179
uncached_an_entry |= !stack[cacheid]->HaveCoinInCache (out);
180
180
}
181
181
182
182
// 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 ) {
184
184
for (auto it = result.begin (); it != result.end (); it++) {
185
185
bool have = stack.back ()->HaveCoin (it->first );
186
186
const Coin& coin = stack.back ()->AccessCoin (it->first );
@@ -198,22 +198,22 @@ BOOST_AUTO_TEST_CASE(coins_cache_simulation_test)
198
198
}
199
199
}
200
200
201
- if (insecure_rand () % 100 == 0 ) {
201
+ if (insecure_randrange ( 100 ) == 0 ) {
202
202
// 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 ) {
204
204
unsigned int flushIndex = insecure_rand () % (stack.size () - 1 );
205
205
stack[flushIndex]->Flush ();
206
206
}
207
207
}
208
- if (insecure_rand () % 100 == 0 ) {
208
+ if (insecure_randrange ( 100 ) == 0 ) {
209
209
// 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 ) {
211
211
// Remove the top cache
212
212
stack.back ()->Flush ();
213
213
delete stack.back ();
214
214
stack.pop_back ();
215
215
}
216
- if (stack.size () == 0 || (stack.size () < 4 && insecure_rand () % 2 )) {
216
+ if (stack.size () == 0 || (stack.size () < 4 && insecure_randrange ( 2 ) )) {
217
217
// Add a new cache
218
218
CCoinsView* tip = &base;
219
219
if (stack.size () > 0 ) {
@@ -253,7 +253,7 @@ UtxoData utxoData;
253
253
254
254
UtxoData::iterator FindRandomFrom (const std::set<COutPoint> &utxoSet) {
255
255
assert (utxoSet.size ());
256
- auto utxoSetIt = utxoSet.lower_bound (COutPoint (GetRandHash (), 0 ));
256
+ auto utxoSetIt = utxoSet.lower_bound (COutPoint (insecure_rand256 (), 0 ));
257
257
if (utxoSetIt == utxoSet.end ()) {
258
258
utxoSetIt = utxoSet.begin ();
259
259
}
@@ -301,7 +301,7 @@ BOOST_AUTO_TEST_CASE(updatecoins_simulation_test)
301
301
// 2/20 times create a new coinbase
302
302
if (randiter % 20 < 2 || coinbase_coins.size () < 10 ) {
303
303
// 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 ()) {
305
305
auto utxod = FindRandomFrom (coinbase_coins);
306
306
// Reuse the exact same coinbase
307
307
tx = std::get<0 >(utxod->second );
@@ -411,7 +411,7 @@ BOOST_AUTO_TEST_CASE(updatecoins_simulation_test)
411
411
}
412
412
413
413
// 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 ) {
415
415
for (auto it = result.begin (); it != result.end (); it++) {
416
416
bool have = stack.back ()->HaveCoin (it->first );
417
417
const Coin& coin = stack.back ()->AccessCoin (it->first );
@@ -421,31 +421,31 @@ BOOST_AUTO_TEST_CASE(updatecoins_simulation_test)
421
421
}
422
422
423
423
// 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 ) {
425
425
stack[insecure_rand () % stack.size ()]->Uncache (FindRandomFrom (utxoset)->first );
426
426
}
427
- if (disconnected_coins.size () > 1 && insecure_rand () % 30 == 0 ) {
427
+ if (disconnected_coins.size () > 1 && insecure_randrange ( 30 ) == 0 ) {
428
428
stack[insecure_rand () % stack.size ()]->Uncache (FindRandomFrom (disconnected_coins)->first );
429
429
}
430
- if (duplicate_coins.size () > 1 && insecure_rand () % 30 == 0 ) {
430
+ if (duplicate_coins.size () > 1 && insecure_randrange ( 30 ) == 0 ) {
431
431
stack[insecure_rand () % stack.size ()]->Uncache (FindRandomFrom (duplicate_coins)->first );
432
432
}
433
433
434
- if (insecure_rand () % 100 == 0 ) {
434
+ if (insecure_randrange ( 100 ) == 0 ) {
435
435
// 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 ) {
437
437
unsigned int flushIndex = insecure_rand () % (stack.size () - 1 );
438
438
stack[flushIndex]->Flush ();
439
439
}
440
440
}
441
- if (insecure_rand () % 100 == 0 ) {
441
+ if (insecure_randrange ( 100 ) == 0 ) {
442
442
// 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 ) {
444
444
stack.back ()->Flush ();
445
445
delete stack.back ();
446
446
stack.pop_back ();
447
447
}
448
- if (stack.size () == 0 || (stack.size () < 4 && insecure_rand () % 2 )) {
448
+ if (stack.size () == 0 || (stack.size () < 4 && insecure_randrange ( 2 ) )) {
449
449
CCoinsView* tip = &base;
450
450
if (stack.size () > 0 ) {
451
451
tip = stack.back ();
0 commit comments