Skip to content

Commit fa54cab

Browse files
author
MarcoFalke
committed
test: refactor: Accept any RandomNumberGenerator in RandMoney
Accepting any Rng in RandMoney makes tests more flexible to use a different Rng. Also, passing in the Rng clarifies the call sites, so that they all use g_rand_ctx explicitly and consistently.
1 parent 68f77dd commit fa54cab

File tree

4 files changed

+7
-6
lines changed

4 files changed

+7
-6
lines changed

src/test/coins_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ void SimulationTest(CCoinsView* base, bool fake_best_block)
182182

183183
if (InsecureRandRange(5) == 0 || coin.IsSpent()) {
184184
Coin newcoin;
185-
newcoin.out.nValue = InsecureRandMoneyAmount();
185+
newcoin.out.nValue = RandMoney(m_rng);
186186
newcoin.nHeight = 1;
187187

188188
// Infrequently test adding unspendable coins.

src/test/sighash_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ void RandomTransaction(CMutableTransaction& tx, bool fSingle)
110110
for (int out = 0; out < outs; out++) {
111111
tx.vout.emplace_back();
112112
CTxOut &txout = tx.vout.back();
113-
txout.nValue = InsecureRandMoneyAmount();
113+
txout.nValue = RandMoney(m_rng);
114114
RandomScript(txout.scriptPubKey);
115115
}
116116
}

src/test/util/coins.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ COutPoint AddTestCoin(FastRandomContext& rng, CCoinsViewCache& coins_view)
1818
Coin new_coin;
1919
COutPoint outpoint{Txid::FromUint256(rng.rand256()), /*nIn=*/0};
2020
new_coin.nHeight = 1;
21-
new_coin.out.nValue = InsecureRandMoneyAmount();
21+
new_coin.out.nValue = RandMoney(rng);
2222
new_coin.out.scriptPubKey.assign(uint32_t{56}, 1);
2323
coins_view.AddCoin(outpoint, std::move(new_coin), /*possible_overwrite=*/false);
2424

src/test/util/random.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2023 The Bitcoin Core developers
1+
// Copyright (c) 2023-present The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

@@ -52,9 +52,10 @@ static inline bool InsecureRandBool()
5252
return g_insecure_rand_ctx.randbool();
5353
}
5454

55-
static inline CAmount InsecureRandMoneyAmount()
55+
template <RandomNumberGenerator Rng>
56+
inline CAmount RandMoney(Rng&& rng)
5657
{
57-
return static_cast<CAmount>(InsecureRandRange(MAX_MONEY + 1));
58+
return CAmount{rng.randrange(MAX_MONEY + 1)};
5859
}
5960

6061
#endif // BITCOIN_TEST_UTIL_RANDOM_H

0 commit comments

Comments
 (0)