Skip to content

Commit

Permalink
scripted-diff: Use randbits/bool instead of randrange where possible
Browse files Browse the repository at this point in the history
-BEGIN VERIFY SCRIPT-
sed -i 's/insecure_randbits(1)/insecure_randbool()/g' src/test/*_tests.cpp
sed -i 's/insecure_randrange(2)/insecure_randbool()/g' src/test/*_tests.cpp
sed -i 's/insecure_randrange(4)/insecure_randbits(2)/g' src/test/*_tests.cpp
sed -i 's/insecure_randrange(32)/insecure_randbits(5)/g' src/test/*_tests.cpp
sed -i 's/insecure_randrange(256)/insecure_randbits(8)/g' src/test/*_tests.cpp
-END VERIFY SCRIPT-

Cherry-picked from: 2fcd9cc
  • Loading branch information
sipa authored and xanimo committed Apr 4, 2024
1 parent b38c4aa commit ef63819
Show file tree
Hide file tree
Showing 19 changed files with 127 additions and 127 deletions.
4 changes: 2 additions & 2 deletions src/test/DoS_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ BOOST_AUTO_TEST_CASE(DoS_bantime)
CTransactionRef RandomOrphan()
{
std::map<uint256, COrphanTx>::iterator it;
it = mapOrphanTransactions.lower_bound(insecure_rand256());
it = mapOrphanTransactions.lower_bound(InsecureRand256());
if (it == mapOrphanTransactions.end())
it = mapOrphanTransactions.begin();
return it->second.tx;
Expand All @@ -148,7 +148,7 @@ BOOST_AUTO_TEST_CASE(DoS_mapOrphans)
CMutableTransaction tx;
tx.vin.resize(1);
tx.vin[0].prevout.n = 0;
tx.vin[0].prevout.hash = insecure_rand256();
tx.vin[0].prevout.hash = InsecureRand256();
tx.vin[0].scriptSig << OP_1;
tx.vout.resize(1);
tx.vout[0].nValue = 1*CENT;
Expand Down
10 changes: 5 additions & 5 deletions src/test/blockencodings_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ static CBlock BuildBlockTestCase() {
block.vtx.resize(3);
block.vtx[0] = MakeTransactionRef(tx);
block.nVersion = 1;
block.hashPrevBlock = insecure_rand256();
block.hashPrevBlock = InsecureRand256();
block.nBits = 0x207fffff;

tx.vin[0].prevout.hash = insecure_rand256();
tx.vin[0].prevout.hash = InsecureRand256();
tx.vin[0].prevout.n = 0;
block.vtx[1] = MakeTransactionRef(tx);

tx.vin.resize(10);
for (size_t i = 0; i < tx.vin.size(); i++) {
tx.vin[i].prevout.hash = insecure_rand256();
tx.vin[i].prevout.hash = InsecureRand256();
tx.vin[i].prevout.n = 0;
}
block.vtx[2] = MakeTransactionRef(tx);
Expand Down Expand Up @@ -283,7 +283,7 @@ BOOST_AUTO_TEST_CASE(EmptyBlockRoundTripTest)
block.vtx.resize(1);
block.vtx[0] = MakeTransactionRef(std::move(coinbase));
block.nVersion = 1;
block.hashPrevBlock = insecure_rand256();
block.hashPrevBlock = InsecureRand256();
block.nBits = 0x207fffff;

bool mutated;
Expand Down Expand Up @@ -316,7 +316,7 @@ BOOST_AUTO_TEST_CASE(EmptyBlockRoundTripTest)

BOOST_AUTO_TEST_CASE(TransactionsRequestSerializationTest) {
BlockTransactionsRequest req1;
req1.blockhash = insecure_rand256();
req1.blockhash = InsecureRand256();
req1.indexes.resize(4);
req1.indexes[0] = 0;
req1.indexes[1] = 1;
Expand Down
2 changes: 1 addition & 1 deletion src/test/bloom_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ BOOST_AUTO_TEST_CASE(merkle_block_4_test_update_none)

static std::vector<unsigned char> RandomData()
{
uint256 r = insecure_rand256();
uint256 r = InsecureRand256();
return std::vector<unsigned char>(r.begin(), r.end());
}

Expand Down
10 changes: 5 additions & 5 deletions src/test/checkqueue_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ void Correct_Queue_range(std::vector<size_t> range)
FakeCheckCheckCompletion::n_calls = 0;
CCheckQueueControl<FakeCheckCheckCompletion> control(small_queue.get());
while (total) {
vChecks.resize(std::min(total, (size_t) insecure_randrange(10)));
vChecks.resize(std::min(total, (size_t) InsecureRandRange(10)));
total -= vChecks.size();
control.Add(vChecks);
}
Expand Down Expand Up @@ -204,7 +204,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_Correct_Random)
{
std::vector<size_t> range;
range.reserve(100000/1000);
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))))
for (size_t i = 2; i < 100000; i += std::max((size_t)1, (size_t)InsecureRandRange(std::min((size_t)1000, ((size_t)100000) - i))))
range.push_back(i);
Correct_Queue_range(range);
}
Expand All @@ -224,7 +224,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_Catches_Failure)
CCheckQueueControl<FailingCheck> control(fail_queue.get());
size_t remaining = i;
while (remaining) {
size_t r = insecure_randrange(10);
size_t r = InsecureRandRange(10);

std::vector<FailingCheck> vChecks;
vChecks.reserve(r);
Expand Down Expand Up @@ -286,7 +286,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_UniqueCheck)
{
CCheckQueueControl<UniqueCheck> control(queue.get());
while (total) {
size_t r = insecure_randrange(10);
size_t r = InsecureRandRange(10);
std::vector<UniqueCheck> vChecks;
for (size_t k = 0; k < r && total; k++)
vChecks.emplace_back(--total);
Expand Down Expand Up @@ -320,7 +320,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_Memory)
{
CCheckQueueControl<MemoryCheck> control(queue.get());
while (total) {
size_t r = insecure_randrange(10);
size_t r = InsecureRandRange(10);
std::vector<MemoryCheck> vChecks;
for (size_t k = 0; k < r && total; k++) {
total--;
Expand Down
50 changes: 25 additions & 25 deletions src/test/coins_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class CCoinsViewTest : public CCoinsView
return false;
}
coins = it->second;
if (coins.IsPruned() && insecure_randrange(2) == 0) {
if (coins.IsPruned() && InsecureRandBool() == 0) {
// Randomly return false in case of an empty entry.
return false;
}
Expand All @@ -55,7 +55,7 @@ class CCoinsViewTest : public CCoinsView
if (it->second.flags & CCoinsCacheEntry::DIRTY) {
// Same optimization used in CCoinsViewDB is to only write dirty entries.
map_[it->first] = it->second.coins;
if (it->second.coins.IsPruned() && insecure_randrange(3) == 0) {
if (it->second.coins.IsPruned() && InsecureRandRange(3) == 0) {
// Randomly delete empty entries on write.
map_.erase(it->first);
}
Expand Down Expand Up @@ -125,25 +125,25 @@ BOOST_AUTO_TEST_CASE(coins_cache_simulation_test)
std::vector<uint256> txids;
txids.resize(NUM_SIMULATION_ITERATIONS / 8);
for (unsigned int i = 0; i < txids.size(); i++) {
txids[i] = insecure_rand256();
txids[i] = InsecureRand256();
}

for (unsigned int i = 0; i < NUM_SIMULATION_ITERATIONS; i++) {
// Do a random modification.
{
uint256 txid = txids[insecure_randrange(500) % txids.size()]; // txid we're going to modify in this iteration.
uint256 txid = txids[InsecureRandRange(txids.size())]; // txid we're going to modify in this iteration.
CCoins& coins = result[txid];
CCoinsModifier entry = stack.back()->ModifyCoins(txid);
BOOST_CHECK(coins == *entry);
if (insecure_randrange(5) == 0 || coins.IsPruned()) {
if (InsecureRandRange(5) == 0 || coins.IsPruned()) {
if (coins.IsPruned()) {
added_an_entry = true;
} else {
updated_an_entry = true;
}
coins.nVersion = insecure_rand();
coins.nVersion = InsecureRand32();
coins.vout.resize(1);
coins.vout[0].nValue = insecure_rand();
coins.vout[0].nValue = InsecureRand32();
*entry = coins;
} else {
coins.Clear();
Expand All @@ -153,7 +153,7 @@ BOOST_AUTO_TEST_CASE(coins_cache_simulation_test)
}

// Once every 1000 iterations and at the end, verify the full cache.
if (insecure_randrange(1000) == 1 || i == NUM_SIMULATION_ITERATIONS - 1) {
if (InsecureRandRange(1000) == 1 || i == NUM_SIMULATION_ITERATIONS - 1) {
for (std::map<uint256, CCoins>::iterator it = result.begin(); it != result.end(); it++) {
const CCoins* coins = stack.back()->AccessCoins(it->first);
if (coins) {
Expand All @@ -169,22 +169,22 @@ BOOST_AUTO_TEST_CASE(coins_cache_simulation_test)
}
}

if (insecure_randrange(100) == 0) {
if (InsecureRandRange(100) == 0) {
// Every 100 iterations, flush an intermediate cache
if (stack.size() > 1 && insecure_randrange(2) == 0) {
unsigned int flushIndex = insecure_randrange(stack.size() - 1);
if (stack.size() > 1 && InsecureRandBool() == 0) {
unsigned int flushIndex = InsecureRandRange(stack.size() - 1);
stack[flushIndex]->Flush();
}
}
if (insecure_randrange(100) == 0) {
if (InsecureRandRange(100) == 0) {
// Every 100 iterations, change the cache stack.
if (stack.size() > 0 && insecure_randrange(2) == 0) {
if (stack.size() > 0 && InsecureRandBool() == 0) {
//Remove the top cache
stack.back()->Flush();
delete stack.back();
stack.pop_back();
}
if (stack.size() == 0 || (stack.size() < 4 && insecure_randrange(2))) {
if (stack.size() == 0 || (stack.size() < 4 && InsecureRandBool())) {
//Add a new cache
CCoinsView* tip = &base;
if (stack.size() > 0) {
Expand Down Expand Up @@ -222,7 +222,7 @@ std::map<uint256, TxData> alltxs;

TxData &FindRandomFrom(const std::set<uint256> &txidset) {
assert(txidset.size());
std::set<uint256>::iterator txIt = txidset.lower_bound(insecure_rand256());
std::set<uint256>::iterator txIt = txidset.lower_bound(InsecureRand256());
if (txIt == txidset.end()) {
txIt = txidset.begin();
}
Expand Down Expand Up @@ -255,21 +255,21 @@ BOOST_AUTO_TEST_CASE(updatecoins_simulation_test)
std::set<uint256> utxoset;

for (unsigned int i = 0; i < NUM_SIMULATION_ITERATIONS; i++) {
uint32_t randiter = insecure_rand();
uint32_t randiter = InsecureRand32();

// 19/20 txs add a new transaction
if (randiter % 20 < 19) {
CMutableTransaction tx;
tx.vin.resize(1);
tx.vout.resize(1);
tx.vout[0].nValue = i; //Keep txs unique unless intended to duplicate
unsigned int height = insecure_rand();
unsigned int height = InsecureRand32();
CCoins oldcoins;

// 2/20 times create a new coinbase
if (randiter % 20 < 2 || coinbaseids.size() < 10) {
// 1/10 of those times create a duplicate coinbase
if (insecure_randrange(10) == 0 && coinbaseids.size()) {
if (InsecureRandRange(10) == 0 && coinbaseids.size()) {
TxData &txd = FindRandomFrom(coinbaseids);
// Reuse the exact same coinbase
tx = std::get<0>(txd);
Expand Down Expand Up @@ -384,7 +384,7 @@ BOOST_AUTO_TEST_CASE(updatecoins_simulation_test)
}

// Once every 1000 iterations and at the end, verify the full cache.
if (insecure_randrange(1000) == 1 || i == NUM_SIMULATION_ITERATIONS - 1) {
if (InsecureRandRange(1000) == 1 || i == NUM_SIMULATION_ITERATIONS - 1) {
for (std::map<uint256, CCoins>::iterator it = result.begin(); it != result.end(); it++) {
const CCoins* coins = stack.back()->AccessCoins(it->first);
if (coins) {
Expand All @@ -395,21 +395,21 @@ BOOST_AUTO_TEST_CASE(updatecoins_simulation_test)
}
}

if (insecure_randrange(100) == 0) {
if (InsecureRandRange(100) == 0) {
// Every 100 iterations, flush an intermediate cache
if (stack.size() > 1 && insecure_randrange(2) == 0) {
unsigned int flushIndex = insecure_randrange(stack.size() - 1);
if (stack.size() > 1 && InsecureRandBool() == 0) {
unsigned int flushIndex = InsecureRandRange(stack.size() - 1);
stack[flushIndex]->Flush();
}
}
if (insecure_randrange(100) == 0) {
if (InsecureRandRange(100) == 0) {
// Every 100 iterations, change the cache stack.
if (stack.size() > 0 && insecure_randrange(2) == 0) {
if (stack.size() > 0 && InsecureRandBool() == 0) {
stack.back()->Flush();
delete stack.back();
stack.pop_back();
}
if (stack.size() == 0 || (stack.size() < 4 && insecure_randrange(2))) {
if (stack.size() == 0 || (stack.size() < 4 && InsecureRandBool())) {
CCoinsView* tip = &base;
if (stack.size() > 0) {
tip = stack.back();
Expand Down
2 changes: 1 addition & 1 deletion src/test/crypto_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void TestVector(const Hasher &h, const In &in, const Out &out) {
Hasher hasher(h);
size_t pos = 0;
while (pos < in.size()) {
size_t len = insecure_randrange((in.size() - pos + 1) / 2 + 1);
size_t len = InsecureRandRange((in.size() - pos + 1) / 2 + 1);
hasher.Write((unsigned char*)&in[pos], len);
pos += len;
if (pos > 0 && pos + 2 * out.size() > in.size() && pos < in.size()) {
Expand Down
20 changes: 10 additions & 10 deletions src/test/dbwrapper_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper)
fs::path ph = fs::temp_directory_path() / fs::unique_path();
CDBWrapper dbw(ph, (1 << 20), true, false, obfuscate);
char key = 'k';
uint256 in = insecure_rand256();
uint256 in = InsecureRand256();
uint256 res;

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

char key = 'i';
uint256 in = insecure_rand256();
uint256 in = InsecureRand256();
char key2 = 'j';
uint256 in2 = insecure_rand256();
uint256 in2 = InsecureRand256();
char key3 = 'k';
uint256 in3 = insecure_rand256();
uint256 in3 = InsecureRand256();

uint256 res;
CDBBatch batch(dbw);
Expand Down Expand Up @@ -93,10 +93,10 @@ BOOST_AUTO_TEST_CASE(dbwrapper_iterator)

// The two keys are intentionally chosen for ordering
char key = 'j';
uint256 in = insecure_rand256();
uint256 in = InsecureRand256();
BOOST_CHECK(dbw.Write(key, in));
char key2 = 'k';
uint256 in2 = insecure_rand256();
uint256 in2 = InsecureRand256();
BOOST_CHECK(dbw.Write(key2, in2));

std::unique_ptr<CDBIterator> it(const_cast<CDBWrapper*>(&dbw)->NewIterator());
Expand Down Expand Up @@ -134,7 +134,7 @@ BOOST_AUTO_TEST_CASE(existing_data_no_obfuscate)
// Set up a non-obfuscated wrapper to write some initial data.
CDBWrapper* dbw = new CDBWrapper(ph, (1 << 10), false, false, false);
char key = 'k';
uint256 in = insecure_rand256();
uint256 in = InsecureRand256();
uint256 res;

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

uint256 in2 = insecure_rand256();
uint256 in2 = InsecureRand256();
uint256 res3;

// Check that we can write successfully
Expand All @@ -175,7 +175,7 @@ BOOST_AUTO_TEST_CASE(existing_data_reindex)
// Set up a non-obfuscated wrapper to write some initial data.
CDBWrapper* dbw = new CDBWrapper(ph, (1 << 10), false, false, false);
char key = 'k';
uint256 in = insecure_rand256();
uint256 in = InsecureRand256();
uint256 res;

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

uint256 in2 = insecure_rand256();
uint256 in2 = InsecureRand256();
uint256 res3;

// Check that we can write successfully
Expand Down
4 changes: 2 additions & 2 deletions src/test/merkle_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ BOOST_AUTO_TEST_CASE(merkle_test)
{
for (int i = 0; i < 32; i++) {
// Try 32 block sizes: all sizes from 0 to 16 inclusive, and then 15 random sizes.
int ntx = (i <= 16) ? i : 17 + (insecure_randrange(4000));
int ntx = (i <= 16) ? i : 17 + (InsecureRandRange(4000));
// Try up to 3 mutations.
for (int mutate = 0; mutate <= 3; mutate++) {
int duplicate1 = mutate >= 1 ? 1 << ctz(ntx) : 0; // The last how many transactions to duplicate first.
Expand Down Expand Up @@ -120,7 +120,7 @@ BOOST_AUTO_TEST_CASE(merkle_test)
// If ntx <= 16, try all branches. Otherise, try 16 random ones.
int mtx = loop;
if (ntx > 16) {
mtx = insecure_randrange(ntx);
mtx = InsecureRandRange(ntx);
}
std::vector<uint256> newBranch = BlockMerkleBranch(block, mtx);
std::vector<uint256> oldBranch = BlockGetMerkleBranch(block, merkleTree, mtx);
Expand Down
4 changes: 2 additions & 2 deletions src/test/miner_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
while (chainActive.Tip()->nHeight < 209999) {
CBlockIndex* prev = chainActive.Tip();
CBlockIndex* next = new CBlockIndex();
next->phashBlock = new uint256(insecure_rand256());
next->phashBlock = new uint256(InsecureRand256());
pcoinsTip->SetBestBlock(next->GetBlockHash());
next->pprev = prev;
next->nHeight = prev->nHeight + 1;
Expand All @@ -375,7 +375,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
while (chainActive.Tip()->nHeight < 210000) {
CBlockIndex* prev = chainActive.Tip();
CBlockIndex* next = new CBlockIndex();
next->phashBlock = new uint256(insecure_rand256());
next->phashBlock = new uint256(InsecureRand256());
pcoinsTip->SetBestBlock(next->GetBlockHash());
next->pprev = prev;
next->nHeight = prev->nHeight + 1;
Expand Down
Loading

0 comments on commit ef63819

Please sign in to comment.