Skip to content

Commit

Permalink
scripted-diff: [test] Use g_rng/m_rng directly
Browse files Browse the repository at this point in the history
-BEGIN VERIFY SCRIPT-

 # Use m_rng in unit test files
 ren() { sed -i "s:\<$1\>:$2:g" $( git grep -l "$1" src/test/*.cpp src/wallet/test/*.cpp src/test/util/setup_common.cpp ) ; }
 ren InsecureRand32                m_rng.rand32
 ren InsecureRand256               m_rng.rand256
 ren InsecureRandBits              m_rng.randbits
 ren InsecureRandRange             m_rng.randrange
 ren InsecureRandBool              m_rng.randbool
 ren g_insecure_rand_ctx           m_rng
 ren g_insecure_rand_ctx_temp_path g_rng_temp_path

-END VERIFY SCRIPT-
  • Loading branch information
MarcoFalke committed Aug 26, 2024
1 parent fa54cab commit fa0fe08
Show file tree
Hide file tree
Showing 40 changed files with 286 additions and 286 deletions.
10 changes: 5 additions & 5 deletions src/test/base58_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,14 @@ BOOST_AUTO_TEST_CASE(base58_DecodeBase58)
BOOST_AUTO_TEST_CASE(base58_random_encode_decode)
{
for (int n = 0; n < 1000; ++n) {
unsigned int len = 1 + InsecureRandBits(8);
unsigned int zeroes = InsecureRandBool() ? InsecureRandRange(len + 1) : 0;
auto data = Cat(std::vector<unsigned char>(zeroes, '\000'), g_insecure_rand_ctx.randbytes(len - zeroes));
unsigned int len = 1 + m_rng.randbits(8);
unsigned int zeroes = m_rng.randbool() ? m_rng.randrange(len + 1) : 0;
auto data = Cat(std::vector<unsigned char>(zeroes, '\000'), m_rng.randbytes(len - zeroes));
auto encoded = EncodeBase58Check(data);
std::vector<unsigned char> decoded;
auto ok_too_small = DecodeBase58Check(encoded, decoded, InsecureRandRange(len));
auto ok_too_small = DecodeBase58Check(encoded, decoded, m_rng.randrange(len));
BOOST_CHECK(!ok_too_small);
auto ok = DecodeBase58Check(encoded, decoded, len + InsecureRandRange(257 - len));
auto ok = DecodeBase58Check(encoded, decoded, len + m_rng.randrange(257 - len));
BOOST_CHECK(ok);
BOOST_CHECK(data == decoded);
}
Expand Down
6 changes: 3 additions & 3 deletions src/test/bip324_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ void TestBIP324PacketVector(

// Seek to the numbered packet.
if (in_idx == 0 && error == 12) continue;
uint32_t dec_idx = in_idx ^ (error == 12 ? (1U << InsecureRandRange(16)) : 0);
uint32_t dec_idx = in_idx ^ (error == 12 ? (1U << m_rng.randrange(16)) : 0);
for (uint32_t i = 0; i < dec_idx; ++i) {
unsigned use_idx = i < in_idx ? i : 0;
bool dec_ignore{false};
Expand All @@ -128,7 +128,7 @@ void TestBIP324PacketVector(
// Decrypt length
auto to_decrypt = ciphertext;
if (error >= 2 && error <= 9) {
to_decrypt[InsecureRandRange(to_decrypt.size())] ^= std::byte(1U << (error - 2));
to_decrypt[m_rng.randrange(to_decrypt.size())] ^= std::byte(1U << (error - 2));
}

// Decrypt length and resize ciphertext to accommodate.
Expand All @@ -139,7 +139,7 @@ void TestBIP324PacketVector(
auto dec_aad = in_aad;
if (error == 10) {
if (in_aad.size() == 0) continue;
dec_aad[InsecureRandRange(dec_aad.size())] ^= std::byte(1U << InsecureRandRange(8));
dec_aad[m_rng.randrange(dec_aad.size())] ^= std::byte(1U << m_rng.randrange(8));
}
if (error == 11) dec_aad.push_back({});

Expand Down
6 changes: 3 additions & 3 deletions src/test/blockencodings_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ BOOST_AUTO_TEST_CASE(ReceiveWithExtraTransactions) {

BOOST_AUTO_TEST_CASE(TransactionsRequestSerializationTest) {
BlockTransactionsRequest req1;
req1.blockhash = InsecureRand256();
req1.blockhash = m_rng.rand256();
req1.indexes.resize(4);
req1.indexes[0] = 0;
req1.indexes[1] = 1;
Expand All @@ -380,7 +380,7 @@ BOOST_AUTO_TEST_CASE(TransactionsRequestSerializationTest) {
BOOST_AUTO_TEST_CASE(TransactionsRequestDeserializationMaxTest) {
// Check that the highest legal index is decoded correctly
BlockTransactionsRequest req0;
req0.blockhash = InsecureRand256();
req0.blockhash = m_rng.rand256();
req0.indexes.resize(1);
req0.indexes[0] = 0xffff;
DataStream stream{};
Expand All @@ -398,7 +398,7 @@ BOOST_AUTO_TEST_CASE(TransactionsRequestDeserializationOverflowTest) {
// a request cannot be created by serializing a real BlockTransactionsRequest
// due to the overflow, so here we'll serialize from raw deltas.
BlockTransactionsRequest req0;
req0.blockhash = InsecureRand256();
req0.blockhash = m_rng.rand256();
req0.indexes.resize(3);
req0.indexes[0] = 0x7000;
req0.indexes[1] = 0x10000 - 0x7000 - 2;
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)

std::vector<unsigned char> BloomTest::RandomData()
{
uint256 r = InsecureRand256();
uint256 r = m_rng.rand256();
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 @@ -170,7 +170,7 @@ void CheckQueueTest::Correct_Queue_range(std::vector<size_t> range)
CCheckQueueControl<FakeCheckCheckCompletion> control(small_queue.get());
while (total) {
vChecks.clear();
vChecks.resize(std::min<size_t>(total, InsecureRandRange(10)));
vChecks.resize(std::min<size_t>(total, m_rng.randrange(10)));
total -= vChecks.size();
control.Add(std::move(vChecks));
}
Expand Down Expand Up @@ -211,7 +211,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)InsecureRandRange(std::min((size_t)1000, ((size_t)100000) - i))))
for (size_t i = 2; i < 100000; i += std::max((size_t)1, (size_t)m_rng.randrange(std::min((size_t)1000, ((size_t)100000) - i))))
range.push_back(i);
Correct_Queue_range(range);
}
Expand All @@ -225,7 +225,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_Catches_Failure)
CCheckQueueControl<FailingCheck> control(fail_queue.get());
size_t remaining = i;
while (remaining) {
size_t r = InsecureRandRange(10);
size_t r = m_rng.randrange(10);

std::vector<FailingCheck> vChecks;
vChecks.reserve(r);
Expand Down Expand Up @@ -272,7 +272,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_UniqueCheck)
{
CCheckQueueControl<UniqueCheck> control(queue.get());
while (total) {
size_t r = InsecureRandRange(10);
size_t r = m_rng.randrange(10);
std::vector<UniqueCheck> vChecks;
for (size_t k = 0; k < r && total; k++)
vChecks.emplace_back(--total);
Expand Down Expand Up @@ -304,7 +304,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_Memory)
{
CCheckQueueControl<MemoryCheck> control(queue.get());
while (total) {
size_t r = InsecureRandRange(10);
size_t r = m_rng.randrange(10);
std::vector<MemoryCheck> vChecks;
for (size_t k = 0; k < r && total; k++) {
total--;
Expand Down
100 changes: 50 additions & 50 deletions src/test/coins_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class CCoinsViewTest : public CCoinsView
return false;
}
coin = it->second;
if (coin.IsSpent() && InsecureRandBool() == 0) {
if (coin.IsSpent() && m_rng.randbool() == 0) {
// Randomly return false in case of an empty entry.
return false;
}
Expand All @@ -64,7 +64,7 @@ class CCoinsViewTest : public CCoinsView
if (it->second.IsDirty()) {
// Same optimization used in CCoinsViewDB is to only write dirty entries.
map_[it->first] = it->second.coin;
if (it->second.coin.IsSpent() && InsecureRandRange(3) == 0) {
if (it->second.coin.IsSpent() && m_rng.randrange(3) == 0) {
// Randomly delete empty entries on write.
map_.erase(it->first);
}
Expand Down Expand Up @@ -148,26 +148,26 @@ void SimulationTest(CCoinsView* base, bool fake_best_block)
std::vector<Txid> txids;
txids.resize(NUM_SIMULATION_ITERATIONS / 8);
for (unsigned int i = 0; i < txids.size(); i++) {
txids[i] = Txid::FromUint256(InsecureRand256());
txids[i] = Txid::FromUint256(m_rng.rand256());
}

for (unsigned int i = 0; i < NUM_SIMULATION_ITERATIONS; i++) {
// Do a random modification.
{
auto txid = txids[InsecureRandRange(txids.size())]; // txid we're going to modify in this iteration.
auto txid = txids[m_rng.randrange(txids.size())]; // txid we're going to modify in this iteration.
Coin& coin = result[COutPoint(txid, 0)];

// Determine whether to test HaveCoin before or after Access* (or both). As these functions
// can influence each other's behaviour by pulling things into the cache, all combinations
// are tested.
bool test_havecoin_before = InsecureRandBits(2) == 0;
bool test_havecoin_after = InsecureRandBits(2) == 0;
bool test_havecoin_before = m_rng.randbits(2) == 0;
bool test_havecoin_after = m_rng.randbits(2) == 0;

bool result_havecoin = test_havecoin_before ? stack.back()->HaveCoin(COutPoint(txid, 0)) : false;

// Infrequently, test usage of AccessByTxid instead of AccessCoin - the
// former just delegates to the latter and returns the first unspent in a txn.
const Coin& entry = (InsecureRandRange(500) == 0) ?
const Coin& entry = (m_rng.randrange(500) == 0) ?
AccessByTxid(*stack.back(), txid) : stack.back()->AccessCoin(COutPoint(txid, 0));
BOOST_CHECK(coin == entry);

Expand All @@ -180,23 +180,23 @@ void SimulationTest(CCoinsView* base, bool fake_best_block)
BOOST_CHECK(ret == !entry.IsSpent());
}

if (InsecureRandRange(5) == 0 || coin.IsSpent()) {
if (m_rng.randrange(5) == 0 || coin.IsSpent()) {
Coin newcoin;
newcoin.out.nValue = RandMoney(m_rng);
newcoin.nHeight = 1;

// Infrequently test adding unspendable coins.
if (InsecureRandRange(16) == 0 && coin.IsSpent()) {
newcoin.out.scriptPubKey.assign(1 + InsecureRandBits(6), OP_RETURN);
if (m_rng.randrange(16) == 0 && coin.IsSpent()) {
newcoin.out.scriptPubKey.assign(1 + m_rng.randbits(6), OP_RETURN);
BOOST_CHECK(newcoin.out.scriptPubKey.IsUnspendable());
added_an_unspendable_entry = true;
} else {
// Random sizes so we can test memory usage accounting
newcoin.out.scriptPubKey.assign(InsecureRandBits(6), 0);
newcoin.out.scriptPubKey.assign(m_rng.randbits(6), 0);
(coin.IsSpent() ? added_an_entry : updated_an_entry) = true;
coin = newcoin;
}
bool is_overwrite = !coin.IsSpent() || InsecureRand32() & 1;
bool is_overwrite = !coin.IsSpent() || m_rng.rand32() & 1;
stack.back()->AddCoin(COutPoint(txid, 0), std::move(newcoin), is_overwrite);
} else {
// Spend the coin.
Expand All @@ -207,15 +207,15 @@ void SimulationTest(CCoinsView* base, bool fake_best_block)
}

// Once every 10 iterations, remove a random entry from the cache
if (InsecureRandRange(10) == 0) {
COutPoint out(txids[InsecureRand32() % txids.size()], 0);
int cacheid = InsecureRand32() % stack.size();
if (m_rng.randrange(10) == 0) {
COutPoint out(txids[m_rng.rand32() % txids.size()], 0);
int cacheid = m_rng.rand32() % stack.size();
stack[cacheid]->Uncache(out);
uncached_an_entry |= !stack[cacheid]->HaveCoinInCache(out);
}

// Once every 1000 iterations and at the end, verify the full cache.
if (InsecureRandRange(1000) == 1 || i == NUM_SIMULATION_ITERATIONS - 1) {
if (m_rng.randrange(1000) == 1 || i == NUM_SIMULATION_ITERATIONS - 1) {
for (const auto& entry : result) {
bool have = stack.back()->HaveCoin(entry.first);
const Coin& coin = stack.back()->AccessCoin(entry.first);
Expand All @@ -233,27 +233,27 @@ void SimulationTest(CCoinsView* base, bool fake_best_block)
}
}

if (InsecureRandRange(100) == 0) {
if (m_rng.randrange(100) == 0) {
// Every 100 iterations, flush an intermediate cache
if (stack.size() > 1 && InsecureRandBool() == 0) {
unsigned int flushIndex = InsecureRandRange(stack.size() - 1);
if (fake_best_block) stack[flushIndex]->SetBestBlock(InsecureRand256());
bool should_erase = InsecureRandRange(4) < 3;
if (stack.size() > 1 && m_rng.randbool() == 0) {
unsigned int flushIndex = m_rng.randrange(stack.size() - 1);
if (fake_best_block) stack[flushIndex]->SetBestBlock(m_rng.rand256());
bool should_erase = m_rng.randrange(4) < 3;
BOOST_CHECK(should_erase ? stack[flushIndex]->Flush() : stack[flushIndex]->Sync());
flushed_without_erase |= !should_erase;
}
}
if (InsecureRandRange(100) == 0) {
if (m_rng.randrange(100) == 0) {
// Every 100 iterations, change the cache stack.
if (stack.size() > 0 && InsecureRandBool() == 0) {
if (stack.size() > 0 && m_rng.randbool() == 0) {
//Remove the top cache
if (fake_best_block) stack.back()->SetBestBlock(InsecureRand256());
bool should_erase = InsecureRandRange(4) < 3;
if (fake_best_block) stack.back()->SetBestBlock(m_rng.rand256());
bool should_erase = m_rng.randrange(4) < 3;
BOOST_CHECK(should_erase ? stack.back()->Flush() : stack.back()->Sync());
flushed_without_erase |= !should_erase;
stack.pop_back();
}
if (stack.size() == 0 || (stack.size() < 4 && InsecureRandBool())) {
if (stack.size() == 0 || (stack.size() < 4 && m_rng.randbool())) {
//Add a new cache
CCoinsView* tip = base;
if (stack.size() > 0) {
Expand Down Expand Up @@ -300,7 +300,7 @@ UtxoData utxoData;

UtxoData::iterator FindRandomFrom(const std::set<COutPoint> &utxoSet) {
assert(utxoSet.size());
auto utxoSetIt = utxoSet.lower_bound(COutPoint(Txid::FromUint256(InsecureRand256()), 0));
auto utxoSetIt = utxoSet.lower_bound(COutPoint(Txid::FromUint256(m_rng.rand256()), 0));
if (utxoSetIt == utxoSet.end()) {
utxoSetIt = utxoSet.begin();
}
Expand Down Expand Up @@ -336,22 +336,22 @@ BOOST_FIXTURE_TEST_CASE(updatecoins_simulation_test, UpdateTest)
std::set<COutPoint> utxoset;

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

// 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
tx.vout[0].scriptPubKey.assign(InsecureRand32() & 0x3F, 0); // Random sizes so we can test memory usage accounting
const int height{int(InsecureRand32() >> 1)};
tx.vout[0].scriptPubKey.assign(m_rng.rand32() & 0x3F, 0); // Random sizes so we can test memory usage accounting
const int height{int(m_rng.rand32() >> 1)};
Coin old_coin;

// 2/20 times create a new coinbase
if (randiter % 20 < 2 || coinbase_coins.size() < 10) {
// 1/10 of those times create a duplicate coinbase
if (InsecureRandRange(10) == 0 && coinbase_coins.size()) {
if (m_rng.randrange(10) == 0 && coinbase_coins.size()) {
auto utxod = FindRandomFrom(coinbase_coins);
// Reuse the exact same coinbase
tx = CMutableTransaction{std::get<0>(utxod->second)};
Expand Down Expand Up @@ -461,7 +461,7 @@ BOOST_FIXTURE_TEST_CASE(updatecoins_simulation_test, UpdateTest)
}

// Once every 1000 iterations and at the end, verify the full cache.
if (InsecureRandRange(1000) == 1 || i == NUM_SIMULATION_ITERATIONS - 1) {
if (m_rng.randrange(1000) == 1 || i == NUM_SIMULATION_ITERATIONS - 1) {
for (const auto& entry : result) {
bool have = stack.back()->HaveCoin(entry.first);
const Coin& coin = stack.back()->AccessCoin(entry.first);
Expand All @@ -471,30 +471,30 @@ BOOST_FIXTURE_TEST_CASE(updatecoins_simulation_test, UpdateTest)
}

// One every 10 iterations, remove a random entry from the cache
if (utxoset.size() > 1 && InsecureRandRange(30) == 0) {
stack[InsecureRand32() % stack.size()]->Uncache(FindRandomFrom(utxoset)->first);
if (utxoset.size() > 1 && m_rng.randrange(30) == 0) {
stack[m_rng.rand32() % stack.size()]->Uncache(FindRandomFrom(utxoset)->first);
}
if (disconnected_coins.size() > 1 && InsecureRandRange(30) == 0) {
stack[InsecureRand32() % stack.size()]->Uncache(FindRandomFrom(disconnected_coins)->first);
if (disconnected_coins.size() > 1 && m_rng.randrange(30) == 0) {
stack[m_rng.rand32() % stack.size()]->Uncache(FindRandomFrom(disconnected_coins)->first);
}
if (duplicate_coins.size() > 1 && InsecureRandRange(30) == 0) {
stack[InsecureRand32() % stack.size()]->Uncache(FindRandomFrom(duplicate_coins)->first);
if (duplicate_coins.size() > 1 && m_rng.randrange(30) == 0) {
stack[m_rng.rand32() % stack.size()]->Uncache(FindRandomFrom(duplicate_coins)->first);
}

if (InsecureRandRange(100) == 0) {
if (m_rng.randrange(100) == 0) {
// Every 100 iterations, flush an intermediate cache
if (stack.size() > 1 && InsecureRandBool() == 0) {
unsigned int flushIndex = InsecureRandRange(stack.size() - 1);
if (stack.size() > 1 && m_rng.randbool() == 0) {
unsigned int flushIndex = m_rng.randrange(stack.size() - 1);
BOOST_CHECK(stack[flushIndex]->Flush());
}
}
if (InsecureRandRange(100) == 0) {
if (m_rng.randrange(100) == 0) {
// Every 100 iterations, change the cache stack.
if (stack.size() > 0 && InsecureRandBool() == 0) {
if (stack.size() > 0 && m_rng.randbool() == 0) {
BOOST_CHECK(stack.back()->Flush());
stack.pop_back();
}
if (stack.size() == 0 || (stack.size() < 4 && InsecureRandBool())) {
if (stack.size() == 0 || (stack.size() < 4 && m_rng.randbool())) {
CCoinsView* tip = &base;
if (stack.size() > 0) {
tip = stack.back().get();
Expand Down Expand Up @@ -899,8 +899,8 @@ struct FlushTest : BasicTestingSetup {
Coin MakeCoin()
{
Coin coin;
coin.out.nValue = InsecureRand32();
coin.nHeight = InsecureRandRange(4096);
coin.out.nValue = m_rng.rand32();
coin.nHeight = m_rng.randrange(4096);
coin.fCoinBase = 0;
return coin;
}
Expand Down Expand Up @@ -934,12 +934,12 @@ void TestFlushBehavior(
cache->SanityCheck();
// hashBlock must be filled before flushing to disk; value is
// unimportant here. This is normally done during connect/disconnect block.
cache->SetBestBlock(InsecureRand256());
cache->SetBestBlock(m_rng.rand256());
erase ? cache->Flush() : cache->Sync();
}
};

Txid txid = Txid::FromUint256(InsecureRand256());
Txid txid = Txid::FromUint256(m_rng.rand256());
COutPoint outp = COutPoint(txid, 0);
Coin coin = MakeCoin();
// Ensure the coins views haven't seen this coin before.
Expand Down Expand Up @@ -1030,7 +1030,7 @@ void TestFlushBehavior(
// --- Bonus check: ensure that a coin added to the base view via one cache
// can be spent by another cache which has never seen it.
//
txid = Txid::FromUint256(InsecureRand256());
txid = Txid::FromUint256(m_rng.rand256());
outp = COutPoint(txid, 0);
coin = MakeCoin();
BOOST_CHECK(!base.HaveCoin(outp));
Expand All @@ -1053,7 +1053,7 @@ void TestFlushBehavior(

// --- Bonus check 2: ensure that a FRESH, spent coin is deleted by Sync()
//
txid = Txid::FromUint256(InsecureRand256());
txid = Txid::FromUint256(m_rng.rand256());
outp = COutPoint(txid, 0);
coin = MakeCoin();
CAmount coin_val = coin.out.nValue;
Expand Down
Loading

0 comments on commit fa0fe08

Please sign in to comment.