0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-02 09:46:52 -05:00

Create InsecureRandMoneyAmount() test util helper

to generate semi-random CAmounts up to MAX_MONEY rather
than only uint32, and use it in the unit tests.
This commit is contained in:
Jon Atack 2023-01-20 11:05:09 -08:00
parent 81f5ade2a3
commit 9d92c3d7f4
6 changed files with 10 additions and 5 deletions

View file

@ -173,7 +173,7 @@ void SimulationTest(CCoinsView* base, bool fake_best_block)
if (InsecureRandRange(5) == 0 || coin.IsSpent()) {
Coin newcoin;
newcoin.out.nValue = InsecureRand32();
newcoin.out.nValue = InsecureRandMoneyAmount();
newcoin.nHeight = 1;
// Infrequently test adding unspendable coins.

View file

@ -110,7 +110,7 @@ void static RandomTransaction(CMutableTransaction& tx, bool fSingle)
for (int out = 0; out < outs; out++) {
tx.vout.push_back(CTxOut());
CTxOut &txout = tx.vout.back();
txout.nValue = InsecureRandRange(100000000);
txout.nValue = InsecureRandMoneyAmount();
RandomScript(txout.scriptPubKey);
}
}

View file

@ -28,4 +28,3 @@ bool ComputeFilter(BlockFilterType filter_type, const CBlockIndex* block_index,
filter = BlockFilter(filter_type, block, block_undo);
return true;
}

View file

@ -5,6 +5,7 @@
#ifndef BITCOIN_TEST_UTIL_RANDOM_H
#define BITCOIN_TEST_UTIL_RANDOM_H
#include <consensus/amount.h>
#include <random.h>
#include <test/util/setup_common.h>
#include <uint256.h>
@ -36,4 +37,9 @@ static inline bool InsecureRandBool()
return g_insecure_rand_ctx.randbool();
}
static inline CAmount InsecureRandMoneyAmount()
{
return static_cast<CAmount>(InsecureRandRange(MAX_MONEY + 1));
}
#endif // BITCOIN_TEST_UTIL_RANDOM_H

View file

@ -32,7 +32,7 @@ BOOST_AUTO_TEST_CASE(validation_chainstate_resize_caches)
uint256 txid = InsecureRand256();
COutPoint outp{txid, 0};
newcoin.nHeight = 1;
newcoin.out.nValue = InsecureRand32();
newcoin.out.nValue = InsecureRandMoneyAmount();
newcoin.out.scriptPubKey.assign(uint32_t{56}, 1);
coins_view.AddCoin(outp, std::move(newcoin), false);

View file

@ -31,7 +31,7 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
uint256 txid = InsecureRand256();
COutPoint outp{txid, 0};
newcoin.nHeight = 1;
newcoin.out.nValue = InsecureRand32();
newcoin.out.nValue = InsecureRandMoneyAmount();
newcoin.out.scriptPubKey.assign(uint32_t{56}, 1);
coins_view.AddCoin(outp, std::move(newcoin), false);