@@ -49,7 +49,7 @@ class CCoinsViewTest : public CCoinsView
49
49
return false ;
50
50
}
51
51
coin = it->second ;
52
- if (coin.IsSpent () && InsecureRandBool () == 0 ) {
52
+ if (coin.IsSpent () && m_rng. randbool () == 0 ) {
53
53
// Randomly return false in case of an empty entry.
54
54
return false ;
55
55
}
@@ -64,7 +64,7 @@ class CCoinsViewTest : public CCoinsView
64
64
if (it->second .IsDirty ()) {
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 () && InsecureRandRange (3 ) == 0 ) {
67
+ if (it->second .coin .IsSpent () && m_rng. randrange (3 ) == 0 ) {
68
68
// Randomly delete empty entries on write.
69
69
map_.erase (it->first );
70
70
}
@@ -148,26 +148,26 @@ void SimulationTest(CCoinsView* base, bool fake_best_block)
148
148
std::vector<Txid> txids;
149
149
txids.resize (NUM_SIMULATION_ITERATIONS / 8 );
150
150
for (unsigned int i = 0 ; i < txids.size (); i++) {
151
- txids[i] = Txid::FromUint256 (InsecureRand256 ());
151
+ txids[i] = Txid::FromUint256 (m_rng. rand256 ());
152
152
}
153
153
154
154
for (unsigned int i = 0 ; i < NUM_SIMULATION_ITERATIONS; i++) {
155
155
// Do a random modification.
156
156
{
157
- auto txid = txids[InsecureRandRange (txids.size ())]; // txid we're going to modify in this iteration.
157
+ auto txid = txids[m_rng. randrange (txids.size ())]; // txid we're going to modify in this iteration.
158
158
Coin& coin = result[COutPoint (txid, 0 )];
159
159
160
160
// Determine whether to test HaveCoin before or after Access* (or both). As these functions
161
161
// can influence each other's behaviour by pulling things into the cache, all combinations
162
162
// are tested.
163
- bool test_havecoin_before = InsecureRandBits (2 ) == 0 ;
164
- bool test_havecoin_after = InsecureRandBits (2 ) == 0 ;
163
+ bool test_havecoin_before = m_rng. randbits (2 ) == 0 ;
164
+ bool test_havecoin_after = m_rng. randbits (2 ) == 0 ;
165
165
166
166
bool result_havecoin = test_havecoin_before ? stack.back ()->HaveCoin (COutPoint (txid, 0 )) : false ;
167
167
168
168
// Infrequently, test usage of AccessByTxid instead of AccessCoin - the
169
169
// former just delegates to the latter and returns the first unspent in a txn.
170
- const Coin& entry = (InsecureRandRange (500 ) == 0 ) ?
170
+ const Coin& entry = (m_rng. randrange (500 ) == 0 ) ?
171
171
AccessByTxid (*stack.back (), txid) : stack.back ()->AccessCoin (COutPoint (txid, 0 ));
172
172
BOOST_CHECK (coin == entry);
173
173
@@ -180,23 +180,23 @@ void SimulationTest(CCoinsView* base, bool fake_best_block)
180
180
BOOST_CHECK (ret == !entry.IsSpent ());
181
181
}
182
182
183
- if (InsecureRandRange (5 ) == 0 || coin.IsSpent ()) {
183
+ if (m_rng. randrange (5 ) == 0 || coin.IsSpent ()) {
184
184
Coin newcoin;
185
185
newcoin.out .nValue = RandMoney (m_rng);
186
186
newcoin.nHeight = 1 ;
187
187
188
188
// Infrequently test adding unspendable coins.
189
- if (InsecureRandRange (16 ) == 0 && coin.IsSpent ()) {
190
- newcoin.out .scriptPubKey .assign (1 + InsecureRandBits (6 ), OP_RETURN);
189
+ if (m_rng. randrange (16 ) == 0 && coin.IsSpent ()) {
190
+ newcoin.out .scriptPubKey .assign (1 + m_rng. randbits (6 ), OP_RETURN);
191
191
BOOST_CHECK (newcoin.out .scriptPubKey .IsUnspendable ());
192
192
added_an_unspendable_entry = true ;
193
193
} else {
194
194
// Random sizes so we can test memory usage accounting
195
- newcoin.out .scriptPubKey .assign (InsecureRandBits (6 ), 0 );
195
+ newcoin.out .scriptPubKey .assign (m_rng. randbits (6 ), 0 );
196
196
(coin.IsSpent () ? added_an_entry : updated_an_entry) = true ;
197
197
coin = newcoin;
198
198
}
199
- bool is_overwrite = !coin.IsSpent () || InsecureRand32 () & 1 ;
199
+ bool is_overwrite = !coin.IsSpent () || m_rng. rand32 () & 1 ;
200
200
stack.back ()->AddCoin (COutPoint (txid, 0 ), std::move (newcoin), is_overwrite);
201
201
} else {
202
202
// Spend the coin.
@@ -207,15 +207,15 @@ void SimulationTest(CCoinsView* base, bool fake_best_block)
207
207
}
208
208
209
209
// Once every 10 iterations, remove a random entry from the cache
210
- if (InsecureRandRange (10 ) == 0 ) {
211
- COutPoint out (txids[InsecureRand32 () % txids.size ()], 0 );
212
- int cacheid = InsecureRand32 () % stack.size ();
210
+ if (m_rng. randrange (10 ) == 0 ) {
211
+ COutPoint out (txids[m_rng. rand32 () % txids.size ()], 0 );
212
+ int cacheid = m_rng. rand32 () % stack.size ();
213
213
stack[cacheid]->Uncache (out);
214
214
uncached_an_entry |= !stack[cacheid]->HaveCoinInCache (out);
215
215
}
216
216
217
217
// Once every 1000 iterations and at the end, verify the full cache.
218
- if (InsecureRandRange (1000 ) == 1 || i == NUM_SIMULATION_ITERATIONS - 1 ) {
218
+ if (m_rng. randrange (1000 ) == 1 || i == NUM_SIMULATION_ITERATIONS - 1 ) {
219
219
for (const auto & entry : result) {
220
220
bool have = stack.back ()->HaveCoin (entry.first );
221
221
const Coin& coin = stack.back ()->AccessCoin (entry.first );
@@ -233,27 +233,27 @@ void SimulationTest(CCoinsView* base, bool fake_best_block)
233
233
}
234
234
}
235
235
236
- if (InsecureRandRange (100 ) == 0 ) {
236
+ if (m_rng. randrange (100 ) == 0 ) {
237
237
// Every 100 iterations, flush an intermediate cache
238
- if (stack.size () > 1 && InsecureRandBool () == 0 ) {
239
- unsigned int flushIndex = InsecureRandRange (stack.size () - 1 );
240
- if (fake_best_block) stack[flushIndex]->SetBestBlock (InsecureRand256 ());
241
- bool should_erase = InsecureRandRange (4 ) < 3 ;
238
+ if (stack.size () > 1 && m_rng. randbool () == 0 ) {
239
+ unsigned int flushIndex = m_rng. randrange (stack.size () - 1 );
240
+ if (fake_best_block) stack[flushIndex]->SetBestBlock (m_rng. rand256 ());
241
+ bool should_erase = m_rng. randrange (4 ) < 3 ;
242
242
BOOST_CHECK (should_erase ? stack[flushIndex]->Flush () : stack[flushIndex]->Sync ());
243
243
flushed_without_erase |= !should_erase;
244
244
}
245
245
}
246
- if (InsecureRandRange (100 ) == 0 ) {
246
+ if (m_rng. randrange (100 ) == 0 ) {
247
247
// Every 100 iterations, change the cache stack.
248
- if (stack.size () > 0 && InsecureRandBool () == 0 ) {
248
+ if (stack.size () > 0 && m_rng. randbool () == 0 ) {
249
249
// Remove the top cache
250
- if (fake_best_block) stack.back ()->SetBestBlock (InsecureRand256 ());
251
- bool should_erase = InsecureRandRange (4 ) < 3 ;
250
+ if (fake_best_block) stack.back ()->SetBestBlock (m_rng. rand256 ());
251
+ bool should_erase = m_rng. randrange (4 ) < 3 ;
252
252
BOOST_CHECK (should_erase ? stack.back ()->Flush () : stack.back ()->Sync ());
253
253
flushed_without_erase |= !should_erase;
254
254
stack.pop_back ();
255
255
}
256
- if (stack.size () == 0 || (stack.size () < 4 && InsecureRandBool ())) {
256
+ if (stack.size () == 0 || (stack.size () < 4 && m_rng. randbool ())) {
257
257
// Add a new cache
258
258
CCoinsView* tip = base;
259
259
if (stack.size () > 0 ) {
@@ -300,7 +300,7 @@ UtxoData utxoData;
300
300
301
301
UtxoData::iterator FindRandomFrom (const std::set<COutPoint> &utxoSet) {
302
302
assert (utxoSet.size ());
303
- auto utxoSetIt = utxoSet.lower_bound (COutPoint (Txid::FromUint256 (InsecureRand256 ()), 0 ));
303
+ auto utxoSetIt = utxoSet.lower_bound (COutPoint (Txid::FromUint256 (m_rng. rand256 ()), 0 ));
304
304
if (utxoSetIt == utxoSet.end ()) {
305
305
utxoSetIt = utxoSet.begin ();
306
306
}
@@ -336,22 +336,22 @@ BOOST_FIXTURE_TEST_CASE(updatecoins_simulation_test, UpdateTest)
336
336
std::set<COutPoint> utxoset;
337
337
338
338
for (unsigned int i = 0 ; i < NUM_SIMULATION_ITERATIONS; i++) {
339
- uint32_t randiter = InsecureRand32 ();
339
+ uint32_t randiter = m_rng. rand32 ();
340
340
341
341
// 19/20 txs add a new transaction
342
342
if (randiter % 20 < 19 ) {
343
343
CMutableTransaction tx;
344
344
tx.vin .resize (1 );
345
345
tx.vout .resize (1 );
346
346
tx.vout [0 ].nValue = i; // Keep txs unique unless intended to duplicate
347
- tx.vout [0 ].scriptPubKey .assign (InsecureRand32 () & 0x3F , 0 ); // Random sizes so we can test memory usage accounting
348
- const int height{int (InsecureRand32 () >> 1 )};
347
+ tx.vout [0 ].scriptPubKey .assign (m_rng. rand32 () & 0x3F , 0 ); // Random sizes so we can test memory usage accounting
348
+ const int height{int (m_rng. rand32 () >> 1 )};
349
349
Coin old_coin;
350
350
351
351
// 2/20 times create a new coinbase
352
352
if (randiter % 20 < 2 || coinbase_coins.size () < 10 ) {
353
353
// 1/10 of those times create a duplicate coinbase
354
- if (InsecureRandRange (10 ) == 0 && coinbase_coins.size ()) {
354
+ if (m_rng. randrange (10 ) == 0 && coinbase_coins.size ()) {
355
355
auto utxod = FindRandomFrom (coinbase_coins);
356
356
// Reuse the exact same coinbase
357
357
tx = CMutableTransaction{std::get<0 >(utxod->second )};
@@ -461,7 +461,7 @@ BOOST_FIXTURE_TEST_CASE(updatecoins_simulation_test, UpdateTest)
461
461
}
462
462
463
463
// Once every 1000 iterations and at the end, verify the full cache.
464
- if (InsecureRandRange (1000 ) == 1 || i == NUM_SIMULATION_ITERATIONS - 1 ) {
464
+ if (m_rng. randrange (1000 ) == 1 || i == NUM_SIMULATION_ITERATIONS - 1 ) {
465
465
for (const auto & entry : result) {
466
466
bool have = stack.back ()->HaveCoin (entry.first );
467
467
const Coin& coin = stack.back ()->AccessCoin (entry.first );
@@ -471,30 +471,30 @@ BOOST_FIXTURE_TEST_CASE(updatecoins_simulation_test, UpdateTest)
471
471
}
472
472
473
473
// One every 10 iterations, remove a random entry from the cache
474
- if (utxoset.size () > 1 && InsecureRandRange (30 ) == 0 ) {
475
- stack[InsecureRand32 () % stack.size ()]->Uncache (FindRandomFrom (utxoset)->first );
474
+ if (utxoset.size () > 1 && m_rng. randrange (30 ) == 0 ) {
475
+ stack[m_rng. rand32 () % stack.size ()]->Uncache (FindRandomFrom (utxoset)->first );
476
476
}
477
- if (disconnected_coins.size () > 1 && InsecureRandRange (30 ) == 0 ) {
478
- stack[InsecureRand32 () % stack.size ()]->Uncache (FindRandomFrom (disconnected_coins)->first );
477
+ if (disconnected_coins.size () > 1 && m_rng. randrange (30 ) == 0 ) {
478
+ stack[m_rng. rand32 () % stack.size ()]->Uncache (FindRandomFrom (disconnected_coins)->first );
479
479
}
480
- if (duplicate_coins.size () > 1 && InsecureRandRange (30 ) == 0 ) {
481
- stack[InsecureRand32 () % stack.size ()]->Uncache (FindRandomFrom (duplicate_coins)->first );
480
+ if (duplicate_coins.size () > 1 && m_rng. randrange (30 ) == 0 ) {
481
+ stack[m_rng. rand32 () % stack.size ()]->Uncache (FindRandomFrom (duplicate_coins)->first );
482
482
}
483
483
484
- if (InsecureRandRange (100 ) == 0 ) {
484
+ if (m_rng. randrange (100 ) == 0 ) {
485
485
// Every 100 iterations, flush an intermediate cache
486
- if (stack.size () > 1 && InsecureRandBool () == 0 ) {
487
- unsigned int flushIndex = InsecureRandRange (stack.size () - 1 );
486
+ if (stack.size () > 1 && m_rng. randbool () == 0 ) {
487
+ unsigned int flushIndex = m_rng. randrange (stack.size () - 1 );
488
488
BOOST_CHECK (stack[flushIndex]->Flush ());
489
489
}
490
490
}
491
- if (InsecureRandRange (100 ) == 0 ) {
491
+ if (m_rng. randrange (100 ) == 0 ) {
492
492
// Every 100 iterations, change the cache stack.
493
- if (stack.size () > 0 && InsecureRandBool () == 0 ) {
493
+ if (stack.size () > 0 && m_rng. randbool () == 0 ) {
494
494
BOOST_CHECK (stack.back ()->Flush ());
495
495
stack.pop_back ();
496
496
}
497
- if (stack.size () == 0 || (stack.size () < 4 && InsecureRandBool ())) {
497
+ if (stack.size () == 0 || (stack.size () < 4 && m_rng. randbool ())) {
498
498
CCoinsView* tip = &base;
499
499
if (stack.size () > 0 ) {
500
500
tip = stack.back ().get ();
@@ -899,8 +899,8 @@ struct FlushTest : BasicTestingSetup {
899
899
Coin MakeCoin ()
900
900
{
901
901
Coin coin;
902
- coin.out .nValue = InsecureRand32 ();
903
- coin.nHeight = InsecureRandRange (4096 );
902
+ coin.out .nValue = m_rng. rand32 ();
903
+ coin.nHeight = m_rng. randrange (4096 );
904
904
coin.fCoinBase = 0 ;
905
905
return coin;
906
906
}
@@ -934,12 +934,12 @@ void TestFlushBehavior(
934
934
cache->SanityCheck ();
935
935
// hashBlock must be filled before flushing to disk; value is
936
936
// unimportant here. This is normally done during connect/disconnect block.
937
- cache->SetBestBlock (InsecureRand256 ());
937
+ cache->SetBestBlock (m_rng. rand256 ());
938
938
erase ? cache->Flush () : cache->Sync ();
939
939
}
940
940
};
941
941
942
- Txid txid = Txid::FromUint256 (InsecureRand256 ());
942
+ Txid txid = Txid::FromUint256 (m_rng. rand256 ());
943
943
COutPoint outp = COutPoint (txid, 0 );
944
944
Coin coin = MakeCoin ();
945
945
// Ensure the coins views haven't seen this coin before.
@@ -1030,7 +1030,7 @@ void TestFlushBehavior(
1030
1030
// --- Bonus check: ensure that a coin added to the base view via one cache
1031
1031
// can be spent by another cache which has never seen it.
1032
1032
//
1033
- txid = Txid::FromUint256 (InsecureRand256 ());
1033
+ txid = Txid::FromUint256 (m_rng. rand256 ());
1034
1034
outp = COutPoint (txid, 0 );
1035
1035
coin = MakeCoin ();
1036
1036
BOOST_CHECK (!base.HaveCoin (outp));
@@ -1053,7 +1053,7 @@ void TestFlushBehavior(
1053
1053
1054
1054
// --- Bonus check 2: ensure that a FRESH, spent coin is deleted by Sync()
1055
1055
//
1056
- txid = Txid::FromUint256 (InsecureRand256 ());
1056
+ txid = Txid::FromUint256 (m_rng. rand256 ());
1057
1057
outp = COutPoint (txid, 0 );
1058
1058
coin = MakeCoin ();
1059
1059
CAmount coin_val = coin.out .nValue ;
0 commit comments