mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-25 02:33:24 -03: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
4 changed files with 7 additions and 6 deletions
|
@ -182,7 +182,7 @@ void SimulationTest(CCoinsView* base, bool fake_best_block)
|
|||
|
||||
if (InsecureRandRange(5) == 0 || coin.IsSpent()) {
|
||||
Coin newcoin;
|
||||
newcoin.out.nValue = InsecureRandMoneyAmount();
|
||||
newcoin.out.nValue = RandMoney(m_rng);
|
||||
newcoin.nHeight = 1;
|
||||
|
||||
// Infrequently test adding unspendable coins.
|
||||
|
|
|
@ -110,7 +110,7 @@ void RandomTransaction(CMutableTransaction& tx, bool fSingle)
|
|||
for (int out = 0; out < outs; out++) {
|
||||
tx.vout.emplace_back();
|
||||
CTxOut &txout = tx.vout.back();
|
||||
txout.nValue = InsecureRandMoneyAmount();
|
||||
txout.nValue = RandMoney(m_rng);
|
||||
RandomScript(txout.scriptPubKey);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,7 +18,7 @@ COutPoint AddTestCoin(FastRandomContext& rng, CCoinsViewCache& coins_view)
|
|||
Coin new_coin;
|
||||
COutPoint outpoint{Txid::FromUint256(rng.rand256()), /*nIn=*/0};
|
||||
new_coin.nHeight = 1;
|
||||
new_coin.out.nValue = InsecureRandMoneyAmount();
|
||||
new_coin.out.nValue = RandMoney(rng);
|
||||
new_coin.out.scriptPubKey.assign(uint32_t{56}, 1);
|
||||
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
|
||||
// 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();
|
||||
}
|
||||
|
||||
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
|
||||
|
|
Loading…
Add table
Reference in a new issue