@@ -49,7 +49,7 @@ class CCoinsViewTest : public CCoinsView
4949 return false ;
5050 }
5151 coin = it->second ;
52- if (coin.IsSpent () && InsecureRandBool () == 0 ) {
52+ if (coin.IsSpent () && m_rng. randbool () == 0 ) {
5353 // Randomly return false in case of an empty entry.
5454 return false ;
5555 }
@@ -64,7 +64,7 @@ class CCoinsViewTest : public CCoinsView
6464 if (it->second .IsDirty ()) {
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 () && InsecureRandRange (3 ) == 0 ) {
67+ if (it->second .coin .IsSpent () && m_rng. randrange (3 ) == 0 ) {
6868 // Randomly delete empty entries on write.
6969 map_.erase (it->first );
7070 }
@@ -148,26 +148,26 @@ void SimulationTest(CCoinsView* base, bool fake_best_block)
148148 std::vector<Txid> txids;
149149 txids.resize (NUM_SIMULATION_ITERATIONS / 8 );
150150 for (unsigned int i = 0 ; i < txids.size (); i++) {
151- txids[i] = Txid::FromUint256 (InsecureRand256 ());
151+ txids[i] = Txid::FromUint256 (m_rng. rand256 ());
152152 }
153153
154154 for (unsigned int i = 0 ; i < NUM_SIMULATION_ITERATIONS; i++) {
155155 // Do a random modification.
156156 {
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.
158158 Coin& coin = result[COutPoint (txid, 0 )];
159159
160160 // Determine whether to test HaveCoin before or after Access* (or both). As these functions
161161 // can influence each other's behaviour by pulling things into the cache, all combinations
162162 // 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 ;
165165
166166 bool result_havecoin = test_havecoin_before ? stack.back ()->HaveCoin (COutPoint (txid, 0 )) : false ;
167167
168168 // Infrequently, test usage of AccessByTxid instead of AccessCoin - the
169169 // 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 ) ?
171171 AccessByTxid (*stack.back (), txid) : stack.back ()->AccessCoin (COutPoint (txid, 0 ));
172172 BOOST_CHECK (coin == entry);
173173
@@ -180,23 +180,23 @@ void SimulationTest(CCoinsView* base, bool fake_best_block)
180180 BOOST_CHECK (ret == !entry.IsSpent ());
181181 }
182182
183- if (InsecureRandRange (5 ) == 0 || coin.IsSpent ()) {
183+ if (m_rng. randrange (5 ) == 0 || coin.IsSpent ()) {
184184 Coin newcoin;
185185 newcoin.out .nValue = RandMoney (m_rng);
186186 newcoin.nHeight = 1 ;
187187
188188 // 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);
191191 BOOST_CHECK (newcoin.out .scriptPubKey .IsUnspendable ());
192192 added_an_unspendable_entry = true ;
193193 } else {
194194 // 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 );
196196 (coin.IsSpent () ? added_an_entry : updated_an_entry) = true ;
197197 coin = newcoin;
198198 }
199- bool is_overwrite = !coin.IsSpent () || InsecureRand32 () & 1 ;
199+ bool is_overwrite = !coin.IsSpent () || m_rng. rand32 () & 1 ;
200200 stack.back ()->AddCoin (COutPoint (txid, 0 ), std::move (newcoin), is_overwrite);
201201 } else {
202202 // Spend the coin.
@@ -207,15 +207,15 @@ void SimulationTest(CCoinsView* base, bool fake_best_block)
207207 }
208208
209209 // 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 ();
213213 stack[cacheid]->Uncache (out);
214214 uncached_an_entry |= !stack[cacheid]->HaveCoinInCache (out);
215215 }
216216
217217 // 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 ) {
219219 for (const auto & entry : result) {
220220 bool have = stack.back ()->HaveCoin (entry.first );
221221 const Coin& coin = stack.back ()->AccessCoin (entry.first );
@@ -233,27 +233,27 @@ void SimulationTest(CCoinsView* base, bool fake_best_block)
233233 }
234234 }
235235
236- if (InsecureRandRange (100 ) == 0 ) {
236+ if (m_rng. randrange (100 ) == 0 ) {
237237 // 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 ;
242242 BOOST_CHECK (should_erase ? stack[flushIndex]->Flush () : stack[flushIndex]->Sync ());
243243 flushed_without_erase |= !should_erase;
244244 }
245245 }
246- if (InsecureRandRange (100 ) == 0 ) {
246+ if (m_rng. randrange (100 ) == 0 ) {
247247 // Every 100 iterations, change the cache stack.
248- if (stack.size () > 0 && InsecureRandBool () == 0 ) {
248+ if (stack.size () > 0 && m_rng. randbool () == 0 ) {
249249 // 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 ;
252252 BOOST_CHECK (should_erase ? stack.back ()->Flush () : stack.back ()->Sync ());
253253 flushed_without_erase |= !should_erase;
254254 stack.pop_back ();
255255 }
256- if (stack.size () == 0 || (stack.size () < 4 && InsecureRandBool ())) {
256+ if (stack.size () == 0 || (stack.size () < 4 && m_rng. randbool ())) {
257257 // Add a new cache
258258 CCoinsView* tip = base;
259259 if (stack.size () > 0 ) {
@@ -300,7 +300,7 @@ UtxoData utxoData;
300300
301301UtxoData::iterator FindRandomFrom (const std::set<COutPoint> &utxoSet) {
302302 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 ));
304304 if (utxoSetIt == utxoSet.end ()) {
305305 utxoSetIt = utxoSet.begin ();
306306 }
@@ -336,22 +336,22 @@ BOOST_FIXTURE_TEST_CASE(updatecoins_simulation_test, UpdateTest)
336336 std::set<COutPoint> utxoset;
337337
338338 for (unsigned int i = 0 ; i < NUM_SIMULATION_ITERATIONS; i++) {
339- uint32_t randiter = InsecureRand32 ();
339+ uint32_t randiter = m_rng. rand32 ();
340340
341341 // 19/20 txs add a new transaction
342342 if (randiter % 20 < 19 ) {
343343 CMutableTransaction tx;
344344 tx.vin .resize (1 );
345345 tx.vout .resize (1 );
346346 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 )};
349349 Coin old_coin;
350350
351351 // 2/20 times create a new coinbase
352352 if (randiter % 20 < 2 || coinbase_coins.size () < 10 ) {
353353 // 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 ()) {
355355 auto utxod = FindRandomFrom (coinbase_coins);
356356 // Reuse the exact same coinbase
357357 tx = CMutableTransaction{std::get<0 >(utxod->second )};
@@ -461,7 +461,7 @@ BOOST_FIXTURE_TEST_CASE(updatecoins_simulation_test, UpdateTest)
461461 }
462462
463463 // 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 ) {
465465 for (const auto & entry : result) {
466466 bool have = stack.back ()->HaveCoin (entry.first );
467467 const Coin& coin = stack.back ()->AccessCoin (entry.first );
@@ -471,30 +471,30 @@ BOOST_FIXTURE_TEST_CASE(updatecoins_simulation_test, UpdateTest)
471471 }
472472
473473 // 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 );
476476 }
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 );
479479 }
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 );
482482 }
483483
484- if (InsecureRandRange (100 ) == 0 ) {
484+ if (m_rng. randrange (100 ) == 0 ) {
485485 // 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 );
488488 BOOST_CHECK (stack[flushIndex]->Flush ());
489489 }
490490 }
491- if (InsecureRandRange (100 ) == 0 ) {
491+ if (m_rng. randrange (100 ) == 0 ) {
492492 // Every 100 iterations, change the cache stack.
493- if (stack.size () > 0 && InsecureRandBool () == 0 ) {
493+ if (stack.size () > 0 && m_rng. randbool () == 0 ) {
494494 BOOST_CHECK (stack.back ()->Flush ());
495495 stack.pop_back ();
496496 }
497- if (stack.size () == 0 || (stack.size () < 4 && InsecureRandBool ())) {
497+ if (stack.size () == 0 || (stack.size () < 4 && m_rng. randbool ())) {
498498 CCoinsView* tip = &base;
499499 if (stack.size () > 0 ) {
500500 tip = stack.back ().get ();
@@ -899,8 +899,8 @@ struct FlushTest : BasicTestingSetup {
899899Coin MakeCoin ()
900900{
901901 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 );
904904 coin.fCoinBase = 0 ;
905905 return coin;
906906}
@@ -934,12 +934,12 @@ void TestFlushBehavior(
934934 cache->SanityCheck ();
935935 // hashBlock must be filled before flushing to disk; value is
936936 // unimportant here. This is normally done during connect/disconnect block.
937- cache->SetBestBlock (InsecureRand256 ());
937+ cache->SetBestBlock (m_rng. rand256 ());
938938 erase ? cache->Flush () : cache->Sync ();
939939 }
940940 };
941941
942- Txid txid = Txid::FromUint256 (InsecureRand256 ());
942+ Txid txid = Txid::FromUint256 (m_rng. rand256 ());
943943 COutPoint outp = COutPoint (txid, 0 );
944944 Coin coin = MakeCoin ();
945945 // Ensure the coins views haven't seen this coin before.
@@ -1030,7 +1030,7 @@ void TestFlushBehavior(
10301030 // --- Bonus check: ensure that a coin added to the base view via one cache
10311031 // can be spent by another cache which has never seen it.
10321032 //
1033- txid = Txid::FromUint256 (InsecureRand256 ());
1033+ txid = Txid::FromUint256 (m_rng. rand256 ());
10341034 outp = COutPoint (txid, 0 );
10351035 coin = MakeCoin ();
10361036 BOOST_CHECK (!base.HaveCoin (outp));
@@ -1053,7 +1053,7 @@ void TestFlushBehavior(
10531053
10541054 // --- Bonus check 2: ensure that a FRESH, spent coin is deleted by Sync()
10551055 //
1056- txid = Txid::FromUint256 (InsecureRand256 ());
1056+ txid = Txid::FromUint256 (m_rng. rand256 ());
10571057 outp = COutPoint (txid, 0 );
10581058 coin = MakeCoin ();
10591059 CAmount coin_val = coin.out .nValue ;
0 commit comments