Skip to content

Commit 68f77dd

Browse files
ryanofskyMarcoFalke
authored andcommitted
test: refactor: Pass rng parameters to test functions
Add FastRandomContext parameter to the utility function AddTestCoin(), and a few local test functions and classes.
1 parent fa19af5 commit 68f77dd

12 files changed

+58
-40
lines changed

src/test/coins_tests.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,13 @@ bool operator==(const Coin &a, const Coin &b) {
3535

3636
class CCoinsViewTest : public CCoinsView
3737
{
38+
FastRandomContext& m_rng;
3839
uint256 hashBestBlock_;
3940
std::map<COutPoint, Coin> map_;
4041

4142
public:
43+
CCoinsViewTest(FastRandomContext& rng) : m_rng{rng} {}
44+
4245
[[nodiscard]] bool GetCoin(const COutPoint& outpoint, Coin& coin) const override
4346
{
4447
std::map<COutPoint, Coin>::const_iterator it = map_.find(outpoint);
@@ -283,7 +286,7 @@ void SimulationTest(CCoinsView* base, bool fake_best_block)
283286
// Run the above simulation for multiple base types.
284287
BOOST_FIXTURE_TEST_CASE(coins_cache_simulation_test, CacheTest)
285288
{
286-
CCoinsViewTest base;
289+
CCoinsViewTest base{m_rng};
287290
SimulationTest(&base, false);
288291

289292
CCoinsViewDB db_base{{.path = "test", .cache_bytes = 1 << 23, .memory_only = true}, {}};
@@ -322,7 +325,7 @@ BOOST_FIXTURE_TEST_CASE(updatecoins_simulation_test, UpdateTest)
322325
std::map<COutPoint, Coin> result;
323326

324327
// The cache stack.
325-
CCoinsViewTest base; // A CCoinsViewTest at the bottom.
328+
CCoinsViewTest base{m_rng}; // A CCoinsViewTest at the bottom.
326329
std::vector<std::unique_ptr<CCoinsViewCacheTest>> stack; // A stack of CCoinsViewCaches on top.
327330
stack.push_back(std::make_unique<CCoinsViewCacheTest>(&base)); // Start with one cache.
328331

src/test/cuckoocache_tests.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,15 +309,15 @@ void test_cache_generations()
309309
// immediately and never uses the other half.
310310
struct block_activity {
311311
std::vector<uint256> reads;
312-
block_activity(uint32_t n_insert, Cache& c) : reads()
312+
block_activity(uint32_t n_insert, FastRandomContext& rng, Cache& c)
313313
{
314314
std::vector<uint256> inserts;
315315
inserts.resize(n_insert);
316316
reads.reserve(n_insert / 2);
317317
for (uint32_t i = 0; i < n_insert; ++i) {
318318
uint32_t* ptr = (uint32_t*)inserts[i].begin();
319319
for (uint8_t j = 0; j < 8; ++j)
320-
*(ptr++) = InsecureRand32();
320+
*(ptr++) = rng.rand32();
321321
}
322322
for (uint32_t i = 0; i < n_insert / 4; ++i)
323323
reads.push_back(inserts[i]);
@@ -351,7 +351,7 @@ void test_cache_generations()
351351
for (uint32_t i = 0; i < total; ++i) {
352352
if (last_few.size() == WINDOW_SIZE)
353353
last_few.pop_front();
354-
last_few.emplace_back(BLOCK_SIZE, set);
354+
last_few.emplace_back(BLOCK_SIZE, m_rng, set);
355355
uint32_t count = 0;
356356
for (auto& act : last_few)
357357
for (uint32_t k = 0; k < POP_AMOUNT; ++k) {

src/test/net_tests.cpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,10 +1011,10 @@ BOOST_AUTO_TEST_CASE(advertise_local_address)
10111011

10121012
namespace {
10131013

1014-
CKey GenerateRandomTestKey() noexcept
1014+
CKey GenerateRandomTestKey(FastRandomContext& rng) noexcept
10151015
{
10161016
CKey key;
1017-
uint256 key_data = InsecureRand256();
1017+
uint256 key_data = rng.rand256();
10181018
key.Set(key_data.begin(), key_data.end(), true);
10191019
return key;
10201020
}
@@ -1029,6 +1029,7 @@ CKey GenerateRandomTestKey() noexcept
10291029
*/
10301030
class V2TransportTester
10311031
{
1032+
FastRandomContext& m_rng;
10321033
V2Transport m_transport; //!< V2Transport being tested
10331034
BIP324Cipher m_cipher; //!< Cipher to help with the other side
10341035
bool m_test_initiator; //!< Whether m_transport is the initiator (true) or responder (false)
@@ -1042,9 +1043,10 @@ class V2TransportTester
10421043

10431044
public:
10441045
/** Construct a tester object. test_initiator: whether the tested transport is initiator. */
1045-
explicit V2TransportTester(bool test_initiator)
1046-
: m_transport{0, test_initiator},
1047-
m_cipher{GenerateRandomTestKey(), MakeByteSpan(InsecureRand256())},
1046+
explicit V2TransportTester(FastRandomContext& rng, bool test_initiator)
1047+
: m_rng{rng},
1048+
m_transport{0, test_initiator},
1049+
m_cipher{GenerateRandomTestKey(m_rng), MakeByteSpan(m_rng.rand256())},
10481050
m_test_initiator(test_initiator) {}
10491051

10501052
/** Data type returned by Interact:
@@ -1345,7 +1347,7 @@ BOOST_AUTO_TEST_CASE(v2transport_test)
13451347
{
13461348
// A mostly normal scenario, testing a transport in initiator mode.
13471349
for (int i = 0; i < 10; ++i) {
1348-
V2TransportTester tester(true);
1350+
V2TransportTester tester(m_rng, true);
13491351
auto ret = tester.Interact();
13501352
BOOST_REQUIRE(ret && ret->empty());
13511353
tester.SendKey();
@@ -1386,7 +1388,7 @@ BOOST_AUTO_TEST_CASE(v2transport_test)
13861388

13871389
// Normal scenario, with a transport in responder node.
13881390
for (int i = 0; i < 10; ++i) {
1389-
V2TransportTester tester(false);
1391+
V2TransportTester tester(m_rng, false);
13901392
tester.SendKey();
13911393
tester.SendGarbage();
13921394
auto ret = tester.Interact();
@@ -1429,7 +1431,7 @@ BOOST_AUTO_TEST_CASE(v2transport_test)
14291431
bool send_immediately = !initiator || InsecureRandBool();
14301432
/** How many decoy packets to send before the first and second real message. */
14311433
unsigned num_decoys_1 = InsecureRandRange(1000), num_decoys_2 = InsecureRandRange(1000);
1432-
V2TransportTester tester(initiator);
1434+
V2TransportTester tester(m_rng, initiator);
14331435
if (send_immediately) {
14341436
tester.SendKey();
14351437
tester.SendGarbage(garb_len);
@@ -1480,7 +1482,7 @@ BOOST_AUTO_TEST_CASE(v2transport_test)
14801482

14811483
// Too long garbage (initiator).
14821484
{
1483-
V2TransportTester tester(true);
1485+
V2TransportTester tester(m_rng, true);
14841486
auto ret = tester.Interact();
14851487
BOOST_REQUIRE(ret && ret->empty());
14861488
tester.SendKey();
@@ -1493,7 +1495,7 @@ BOOST_AUTO_TEST_CASE(v2transport_test)
14931495

14941496
// Too long garbage (responder).
14951497
{
1496-
V2TransportTester tester(false);
1498+
V2TransportTester tester(m_rng, false);
14971499
tester.SendKey();
14981500
tester.SendGarbage(V2Transport::MAX_GARBAGE_LEN + 1);
14991501
auto ret = tester.Interact();
@@ -1506,7 +1508,7 @@ BOOST_AUTO_TEST_CASE(v2transport_test)
15061508

15071509
// Send garbage that includes the first 15 garbage terminator bytes somewhere.
15081510
{
1509-
V2TransportTester tester(true);
1511+
V2TransportTester tester(m_rng, true);
15101512
auto ret = tester.Interact();
15111513
BOOST_REQUIRE(ret && ret->empty());
15121514
tester.SendKey();
@@ -1545,15 +1547,15 @@ BOOST_AUTO_TEST_CASE(v2transport_test)
15451547

15461548
// Send correct network's V1 header
15471549
{
1548-
V2TransportTester tester(false);
1550+
V2TransportTester tester(m_rng, false);
15491551
tester.SendV1Version(Params().MessageStart());
15501552
auto ret = tester.Interact();
15511553
BOOST_CHECK(ret);
15521554
}
15531555

15541556
// Send wrong network's V1 header
15551557
{
1556-
V2TransportTester tester(false);
1558+
V2TransportTester tester(m_rng, false);
15571559
tester.SendV1Version(CChainParams::Main()->MessageStart());
15581560
auto ret = tester.Interact();
15591561
BOOST_CHECK(!ret);

src/test/orphanage_tests.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ BOOST_FIXTURE_TEST_SUITE(orphanage_tests, TestingSetup)
2121
class TxOrphanageTest : public TxOrphanage
2222
{
2323
public:
24+
TxOrphanageTest(FastRandomContext& rng) : m_rng{rng} {}
25+
2426
inline size_t CountOrphans() const
2527
{
2628
return m_orphans.size();
@@ -34,9 +36,11 @@ class TxOrphanageTest : public TxOrphanage
3436
it = m_orphans.begin();
3537
return it->second.tx;
3638
}
39+
40+
FastRandomContext& m_rng;
3741
};
3842

39-
static void MakeNewKeyWithFastRandomContext(CKey& key, FastRandomContext& rand_ctx = g_insecure_rand_ctx)
43+
static void MakeNewKeyWithFastRandomContext(CKey& key, FastRandomContext& rand_ctx)
4044
{
4145
std::vector<unsigned char> keydata;
4246
keydata = rand_ctx.randbytes(32);
@@ -106,9 +110,9 @@ BOOST_AUTO_TEST_CASE(DoS_mapOrphans)
106110
// signature's R and S values have leading zeros.
107111
g_insecure_rand_ctx.Reseed(uint256{33});
108112

109-
TxOrphanageTest orphanage;
113+
TxOrphanageTest orphanage{m_rng};
110114
CKey key;
111-
MakeNewKeyWithFastRandomContext(key);
115+
MakeNewKeyWithFastRandomContext(key, m_rng);
112116
FillableSigningProvider keystore;
113117
BOOST_CHECK(keystore.AddKey(key));
114118

src/test/pmt_tests.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@
1717
class CPartialMerkleTreeTester : public CPartialMerkleTree
1818
{
1919
public:
20+
CPartialMerkleTreeTester(FastRandomContext& rng) : m_rng{rng} {}
21+
2022
// flip one bit in one of the hashes - this should break the authentication
2123
void Damage() {
2224
unsigned int n = InsecureRandRange(vHash.size());
2325
int bit = InsecureRandBits(8);
2426
*(vHash[n].begin() + (bit>>3)) ^= 1<<(bit&7);
2527
}
28+
29+
FastRandomContext& m_rng;
2630
};
2731

2832
BOOST_FIXTURE_TEST_SUITE(pmt_tests, BasicTestingSetup)
@@ -77,7 +81,7 @@ BOOST_AUTO_TEST_CASE(pmt_test1)
7781
BOOST_CHECK(ss.size() <= 10 + (258*n+7)/8);
7882

7983
// deserialize into a tester copy
80-
CPartialMerkleTreeTester pmt2;
84+
CPartialMerkleTreeTester pmt2{m_rng};
8185
ss >> pmt2;
8286

8387
// extract merkle root and matched txids from copy

src/test/prevector_tests.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,16 +207,16 @@ class prevector_tester {
207207
BOOST_CHECK_MESSAGE(passed, "insecure_rand: " + rand_seed.ToString());
208208
}
209209

210-
prevector_tester() {
211-
rand_seed = InsecureRand256();
212-
g_insecure_rand_ctx.Reseed(rand_seed);
210+
prevector_tester(FastRandomContext& rng) {
211+
rand_seed = rng.rand256();
212+
rng.Reseed(rand_seed);
213213
}
214214
};
215215

216216
BOOST_AUTO_TEST_CASE(PrevectorTestInt)
217217
{
218218
for (int j = 0; j < 64; j++) {
219-
prevector_tester<8, int> test;
219+
prevector_tester<8, int> test{m_rng};
220220
for (int i = 0; i < 2048; i++) {
221221
if (InsecureRandBits(2) == 0) {
222222
test.insert(InsecureRandRange(test.size() + 1), int(InsecureRand32()));

src/test/txrequest_tests.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,13 @@ std::chrono::microseconds TxRequestTest::RandomTime1y() { return std::chrono::mi
7878
*/
7979
class Scenario
8080
{
81+
FastRandomContext& m_rng;
8182
Runner& m_runner;
8283
std::chrono::microseconds m_now;
8384
std::string m_testname;
8485

8586
public:
86-
Scenario(Runner& runner, std::chrono::microseconds starttime) : m_runner(runner), m_now(starttime) {}
87+
Scenario(FastRandomContext& rng, Runner& runner, std::chrono::microseconds starttime) : m_rng(rng), m_runner(runner), m_now(starttime) {}
8788

8889
/** Set a name for the current test, to give more clear error messages. */
8990
void SetTestName(std::string testname)
@@ -719,7 +720,7 @@ void TxRequestTest::TestInterleavedScenarios()
719720
// Introduce some variation in the start time of each scenario, so they don't all start off
720721
// concurrently, but get a more random interleaving.
721722
auto scenario_start = starttime + RandomTime8s() + RandomTime8s() + RandomTime8s();
722-
Scenario scenario(runner, scenario_start);
723+
Scenario scenario(m_rng, runner, scenario_start);
723724
for (int j = 0; builders.size() && j < 10; ++j) {
724725
builders.back()(scenario);
725726
builders.pop_back();

src/test/util/coins.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
#include <stdint.h>
1414
#include <utility>
1515

16-
COutPoint AddTestCoin(CCoinsViewCache& coins_view)
16+
COutPoint AddTestCoin(FastRandomContext& rng, CCoinsViewCache& coins_view)
1717
{
1818
Coin new_coin;
19-
COutPoint outpoint{Txid::FromUint256(InsecureRand256()), /*nIn=*/0};
19+
COutPoint outpoint{Txid::FromUint256(rng.rand256()), /*nIn=*/0};
2020
new_coin.nHeight = 1;
2121
new_coin.out.nValue = InsecureRandMoneyAmount();
2222
new_coin.out.scriptPubKey.assign(uint32_t{56}, 1);

src/test/util/coins.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
#include <primitives/transaction.h>
99

1010
class CCoinsViewCache;
11+
class FastRandomContext;
1112

1213
/**
1314
* Create a Coin with DynamicMemoryUsage of 80 bytes and add it to the given view.
1415
* @param[in,out] coins_view The coins view cache to add the new coin to.
1516
* @returns the COutPoint of the created coin.
1617
*/
17-
COutPoint AddTestCoin(CCoinsViewCache& coins_view);
18+
COutPoint AddTestCoin(FastRandomContext& rng, CCoinsViewCache& coins_view);
1819

1920
#endif // BITCOIN_TEST_UTIL_COINS_H

src/test/validation_chainstate_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ BOOST_AUTO_TEST_CASE(validation_chainstate_resize_caches)
3636
// Add a coin to the in-memory cache, upsize once, then downsize.
3737
{
3838
LOCK(::cs_main);
39-
const auto outpoint = AddTestCoin(c1.CoinsTip());
39+
const auto outpoint = AddTestCoin(m_rng, c1.CoinsTip());
4040

4141
// Set a meaningless bestblock value in the coinsview cache - otherwise we won't
4242
// flush during ResizecoinsCaches() and will subsequently hit an assertion.

0 commit comments

Comments
 (0)