8
8
#include " undo.h"
9
9
#include " utilstrencodings.h"
10
10
#include " test/test_bitcoin.h"
11
- #include " test/test_random.h"
12
11
#include " validation.h"
13
12
#include " consensus/validation.h"
14
13
@@ -44,7 +43,7 @@ class CCoinsViewTest : public CCoinsView
44
43
return false ;
45
44
}
46
45
coin = it->second ;
47
- if (coin.IsSpent () && insecure_rand () % 2 == 0 ) {
46
+ if (coin.IsSpent () && InsecureRandBool () == 0 ) {
48
47
// Randomly return false in case of an empty entry.
49
48
return false ;
50
49
}
@@ -65,7 +64,7 @@ class CCoinsViewTest : public CCoinsView
65
64
if (it->second .flags & CCoinsCacheEntry::DIRTY) {
66
65
// Same optimization used in CCoinsViewDB is to only write dirty entries.
67
66
map_[it->first ] = it->second .coin ;
68
- if (it->second .coin .IsSpent () && insecure_rand () % 3 == 0 ) {
67
+ if (it->second .coin .IsSpent () && InsecureRandRange ( 3 ) == 0 ) {
69
68
// Randomly delete empty entries on write.
70
69
map_.erase (it->first );
71
70
}
@@ -140,31 +139,31 @@ BOOST_AUTO_TEST_CASE(coins_cache_simulation_test)
140
139
std::vector<uint256> txids;
141
140
txids.resize (NUM_SIMULATION_ITERATIONS / 8 );
142
141
for (unsigned int i = 0 ; i < txids.size (); i++) {
143
- txids[i] = GetRandHash ();
142
+ txids[i] = InsecureRand256 ();
144
143
}
145
144
146
145
for (unsigned int i = 0 ; i < NUM_SIMULATION_ITERATIONS; i++) {
147
146
// Do a random modification.
148
147
{
149
- uint256 txid = txids[insecure_rand () % txids.size ()]; // txid we're going to modify in this iteration.
148
+ uint256 txid = txids[InsecureRandRange ( txids.size () )]; // txid we're going to modify in this iteration.
150
149
Coin& coin = result[COutPoint (txid, 0 )];
151
- const Coin& entry = (insecure_rand () % 500 == 0 ) ? AccessByTxid (*stack.back (), txid) : stack.back ()->AccessCoin (COutPoint (txid, 0 ));
150
+ const Coin& entry = (InsecureRandRange ( 500 ) == 0 ) ? AccessByTxid (*stack.back (), txid) : stack.back ()->AccessCoin (COutPoint (txid, 0 ));
152
151
BOOST_CHECK (coin == entry);
153
152
154
- if (insecure_rand () % 5 == 0 || coin.IsSpent ()) {
153
+ if (InsecureRandRange ( 5 ) == 0 || coin.IsSpent ()) {
155
154
Coin newcoin;
156
- newcoin.out .nValue = insecure_rand ();
155
+ newcoin.out .nValue = InsecureRand32 ();
157
156
newcoin.nHeight = 1 ;
158
- if (insecure_rand () % 16 == 0 && coin.IsSpent ()) {
159
- newcoin.out .scriptPubKey .assign (1 + ( insecure_rand () & 0x3F ), OP_RETURN);
157
+ if (InsecureRandRange ( 16 ) == 0 && coin.IsSpent ()) {
158
+ newcoin.out .scriptPubKey .assign (1 + InsecureRandBits ( 6 ), OP_RETURN);
160
159
BOOST_CHECK (newcoin.out .scriptPubKey .IsUnspendable ());
161
160
added_an_unspendable_entry = true ;
162
161
} else {
163
- newcoin.out .scriptPubKey .assign (insecure_rand () & 0x3F , 0 ); // Random sizes so we can test memory usage accounting
162
+ newcoin.out .scriptPubKey .assign (InsecureRandBits ( 6 ) , 0 ); // Random sizes so we can test memory usage accounting
164
163
(coin.IsSpent () ? added_an_entry : updated_an_entry) = true ;
165
164
coin = newcoin;
166
165
}
167
- stack.back ()->AddCoin (COutPoint (txid, 0 ), std::move (newcoin), !coin.IsSpent () || insecure_rand () & 1 );
166
+ stack.back ()->AddCoin (COutPoint (txid, 0 ), std::move (newcoin), !coin.IsSpent () || InsecureRand32 () & 1 );
168
167
} else {
169
168
removed_an_entry = true ;
170
169
coin.Clear ();
@@ -173,15 +172,15 @@ BOOST_AUTO_TEST_CASE(coins_cache_simulation_test)
173
172
}
174
173
175
174
// One every 10 iterations, remove a random entry from the cache
176
- if (insecure_rand () % 10 == 0 ) {
177
- COutPoint out (txids[insecure_rand () % txids.size ()], 0 );
178
- int cacheid = insecure_rand () % stack.size ();
175
+ if (InsecureRandRange ( 10 ) == 0 ) {
176
+ COutPoint out (txids[InsecureRand32 () % txids.size ()], 0 );
177
+ int cacheid = InsecureRand32 () % stack.size ();
179
178
stack[cacheid]->Uncache (out);
180
179
uncached_an_entry |= !stack[cacheid]->HaveCoinInCache (out);
181
180
}
182
181
183
182
// Once every 1000 iterations and at the end, verify the full cache.
184
- if (insecure_rand () % 1000 == 1 || i == NUM_SIMULATION_ITERATIONS - 1 ) {
183
+ if (InsecureRandRange ( 1000 ) == 1 || i == NUM_SIMULATION_ITERATIONS - 1 ) {
185
184
for (auto it = result.begin (); it != result.end (); it++) {
186
185
bool have = stack.back ()->HaveCoin (it->first );
187
186
const Coin& coin = stack.back ()->AccessCoin (it->first );
@@ -199,22 +198,22 @@ BOOST_AUTO_TEST_CASE(coins_cache_simulation_test)
199
198
}
200
199
}
201
200
202
- if (insecure_rand () % 100 == 0 ) {
201
+ if (InsecureRandRange ( 100 ) == 0 ) {
203
202
// Every 100 iterations, flush an intermediate cache
204
- if (stack.size () > 1 && insecure_rand () % 2 == 0 ) {
205
- unsigned int flushIndex = insecure_rand () % (stack.size () - 1 );
203
+ if (stack.size () > 1 && InsecureRandBool () == 0 ) {
204
+ unsigned int flushIndex = InsecureRandRange (stack.size () - 1 );
206
205
stack[flushIndex]->Flush ();
207
206
}
208
207
}
209
- if (insecure_rand () % 100 == 0 ) {
208
+ if (InsecureRandRange ( 100 ) == 0 ) {
210
209
// Every 100 iterations, change the cache stack.
211
- if (stack.size () > 0 && insecure_rand () % 2 == 0 ) {
210
+ if (stack.size () > 0 && InsecureRandBool () == 0 ) {
212
211
// Remove the top cache
213
212
stack.back ()->Flush ();
214
213
delete stack.back ();
215
214
stack.pop_back ();
216
215
}
217
- if (stack.size () == 0 || (stack.size () < 4 && insecure_rand () % 2 )) {
216
+ if (stack.size () == 0 || (stack.size () < 4 && InsecureRandBool () )) {
218
217
// Add a new cache
219
218
CCoinsView* tip = &base;
220
219
if (stack.size () > 0 ) {
@@ -254,7 +253,7 @@ UtxoData utxoData;
254
253
255
254
UtxoData::iterator FindRandomFrom (const std::set<COutPoint> &utxoSet) {
256
255
assert (utxoSet.size ());
257
- auto utxoSetIt = utxoSet.lower_bound (COutPoint (GetRandHash (), 0 ));
256
+ auto utxoSetIt = utxoSet.lower_bound (COutPoint (InsecureRand256 (), 0 ));
258
257
if (utxoSetIt == utxoSet.end ()) {
259
258
utxoSetIt = utxoSet.begin ();
260
259
}
@@ -287,22 +286,22 @@ BOOST_AUTO_TEST_CASE(updatecoins_simulation_test)
287
286
std::set<COutPoint> utxoset;
288
287
289
288
for (unsigned int i = 0 ; i < NUM_SIMULATION_ITERATIONS; i++) {
290
- uint32_t randiter = insecure_rand ();
289
+ uint32_t randiter = InsecureRand32 ();
291
290
292
291
// 19/20 txs add a new transaction
293
292
if (randiter % 20 < 19 ) {
294
293
CMutableTransaction tx;
295
294
tx.vin .resize (1 );
296
295
tx.vout .resize (1 );
297
296
tx.vout [0 ].nValue = i; // Keep txs unique unless intended to duplicate
298
- tx.vout [0 ].scriptPubKey .assign (insecure_rand () & 0x3F , 0 ); // Random sizes so we can test memory usage accounting
299
- unsigned int height = insecure_rand ();
297
+ tx.vout [0 ].scriptPubKey .assign (InsecureRand32 () & 0x3F , 0 ); // Random sizes so we can test memory usage accounting
298
+ unsigned int height = InsecureRand32 ();
300
299
Coin old_coin;
301
300
302
301
// 2/20 times create a new coinbase
303
302
if (randiter % 20 < 2 || coinbase_coins.size () < 10 ) {
304
303
// 1/10 of those times create a duplicate coinbase
305
- if (insecure_rand () % 10 == 0 && coinbase_coins.size ()) {
304
+ if (InsecureRandRange ( 10 ) == 0 && coinbase_coins.size ()) {
306
305
auto utxod = FindRandomFrom (coinbase_coins);
307
306
// Reuse the exact same coinbase
308
307
tx = std::get<0 >(utxod->second );
@@ -412,7 +411,7 @@ BOOST_AUTO_TEST_CASE(updatecoins_simulation_test)
412
411
}
413
412
414
413
// Once every 1000 iterations and at the end, verify the full cache.
415
- if (insecure_rand () % 1000 == 1 || i == NUM_SIMULATION_ITERATIONS - 1 ) {
414
+ if (InsecureRandRange ( 1000 ) == 1 || i == NUM_SIMULATION_ITERATIONS - 1 ) {
416
415
for (auto it = result.begin (); it != result.end (); it++) {
417
416
bool have = stack.back ()->HaveCoin (it->first );
418
417
const Coin& coin = stack.back ()->AccessCoin (it->first );
@@ -422,31 +421,31 @@ BOOST_AUTO_TEST_CASE(updatecoins_simulation_test)
422
421
}
423
422
424
423
// One every 10 iterations, remove a random entry from the cache
425
- if (utxoset.size () > 1 && insecure_rand () % 30 == 0 ) {
426
- stack[insecure_rand () % stack.size ()]->Uncache (FindRandomFrom (utxoset)->first );
424
+ if (utxoset.size () > 1 && InsecureRandRange ( 30 ) == 0 ) {
425
+ stack[InsecureRand32 () % stack.size ()]->Uncache (FindRandomFrom (utxoset)->first );
427
426
}
428
- if (disconnected_coins.size () > 1 && insecure_rand () % 30 == 0 ) {
429
- stack[insecure_rand () % stack.size ()]->Uncache (FindRandomFrom (disconnected_coins)->first );
427
+ if (disconnected_coins.size () > 1 && InsecureRandRange ( 30 ) == 0 ) {
428
+ stack[InsecureRand32 () % stack.size ()]->Uncache (FindRandomFrom (disconnected_coins)->first );
430
429
}
431
- if (duplicate_coins.size () > 1 && insecure_rand () % 30 == 0 ) {
432
- stack[insecure_rand () % stack.size ()]->Uncache (FindRandomFrom (duplicate_coins)->first );
430
+ if (duplicate_coins.size () > 1 && InsecureRandRange ( 30 ) == 0 ) {
431
+ stack[InsecureRand32 () % stack.size ()]->Uncache (FindRandomFrom (duplicate_coins)->first );
433
432
}
434
433
435
- if (insecure_rand () % 100 == 0 ) {
434
+ if (InsecureRandRange ( 100 ) == 0 ) {
436
435
// Every 100 iterations, flush an intermediate cache
437
- if (stack.size () > 1 && insecure_rand () % 2 == 0 ) {
438
- unsigned int flushIndex = insecure_rand () % (stack.size () - 1 );
436
+ if (stack.size () > 1 && InsecureRandBool () == 0 ) {
437
+ unsigned int flushIndex = InsecureRandRange (stack.size () - 1 );
439
438
stack[flushIndex]->Flush ();
440
439
}
441
440
}
442
- if (insecure_rand () % 100 == 0 ) {
441
+ if (InsecureRandRange ( 100 ) == 0 ) {
443
442
// Every 100 iterations, change the cache stack.
444
- if (stack.size () > 0 && insecure_rand () % 2 == 0 ) {
443
+ if (stack.size () > 0 && InsecureRandBool () == 0 ) {
445
444
stack.back ()->Flush ();
446
445
delete stack.back ();
447
446
stack.pop_back ();
448
447
}
449
- if (stack.size () == 0 || (stack.size () < 4 && insecure_rand () % 2 )) {
448
+ if (stack.size () == 0 || (stack.size () < 4 && InsecureRandBool () )) {
450
449
CCoinsView* tip = &base;
451
450
if (stack.size () > 0 ) {
452
451
tip = stack.back ();
0 commit comments