mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-06-08 06:00:33 +02:00
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.
This commit is contained in:
parent
68f77dd21e
commit
fa54cab473
@ -182,7 +182,7 @@ void SimulationTest(CCoinsView* base, bool fake_best_block)
|
|||||||
|
|
||||||
if (InsecureRandRange(5) == 0 || coin.IsSpent()) {
|
if (InsecureRandRange(5) == 0 || coin.IsSpent()) {
|
||||||
Coin newcoin;
|
Coin newcoin;
|
||||||
newcoin.out.nValue = InsecureRandMoneyAmount();
|
newcoin.out.nValue = RandMoney(m_rng);
|
||||||
newcoin.nHeight = 1;
|
newcoin.nHeight = 1;
|
||||||
|
|
||||||
// Infrequently test adding unspendable coins.
|
// Infrequently test adding unspendable coins.
|
||||||
|
@ -110,7 +110,7 @@ void RandomTransaction(CMutableTransaction& tx, bool fSingle)
|
|||||||
for (int out = 0; out < outs; out++) {
|
for (int out = 0; out < outs; out++) {
|
||||||
tx.vout.emplace_back();
|
tx.vout.emplace_back();
|
||||||
CTxOut &txout = tx.vout.back();
|
CTxOut &txout = tx.vout.back();
|
||||||
txout.nValue = InsecureRandMoneyAmount();
|
txout.nValue = RandMoney(m_rng);
|
||||||
RandomScript(txout.scriptPubKey);
|
RandomScript(txout.scriptPubKey);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,7 +18,7 @@ COutPoint AddTestCoin(FastRandomContext& rng, CCoinsViewCache& coins_view)
|
|||||||
Coin new_coin;
|
Coin new_coin;
|
||||||
COutPoint outpoint{Txid::FromUint256(rng.rand256()), /*nIn=*/0};
|
COutPoint outpoint{Txid::FromUint256(rng.rand256()), /*nIn=*/0};
|
||||||
new_coin.nHeight = 1;
|
new_coin.nHeight = 1;
|
||||||
new_coin.out.nValue = InsecureRandMoneyAmount();
|
new_coin.out.nValue = RandMoney(rng);
|
||||||
new_coin.out.scriptPubKey.assign(uint32_t{56}, 1);
|
new_coin.out.scriptPubKey.assign(uint32_t{56}, 1);
|
||||||
coins_view.AddCoin(outpoint, std::move(new_coin), /*possible_overwrite=*/false);
|
coins_view.AddCoin(outpoint, std::move(new_coin), /*possible_overwrite=*/false);
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
// Copyright (c) 2023 The Bitcoin Core developers
|
// Copyright (c) 2023-present The Bitcoin Core developers
|
||||||
// Distributed under the MIT software license, see the accompanying
|
// Distributed under the MIT software license, see the accompanying
|
||||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
|
|
||||||
@ -52,9 +52,10 @@ static inline bool InsecureRandBool()
|
|||||||
return g_insecure_rand_ctx.randbool();
|
return g_insecure_rand_ctx.randbool();
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline CAmount InsecureRandMoneyAmount()
|
template <RandomNumberGenerator Rng>
|
||||||
|
inline CAmount RandMoney(Rng&& rng)
|
||||||
{
|
{
|
||||||
return static_cast<CAmount>(InsecureRandRange(MAX_MONEY + 1));
|
return CAmount{rng.randrange(MAX_MONEY + 1)};
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // BITCOIN_TEST_UTIL_RANDOM_H
|
#endif // BITCOIN_TEST_UTIL_RANDOM_H
|
||||||
|
Loading…
x
Reference in New Issue
Block a user