Skip to content

Commit 9d92c3d

Browse files
committed
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.
1 parent 81f5ade commit 9d92c3d

File tree

6 files changed

+10
-5
lines changed

6 files changed

+10
-5
lines changed

src/test/coins_tests.cpp

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

174174
if (InsecureRandRange(5) == 0 || coin.IsSpent()) {
175175
Coin newcoin;
176-
newcoin.out.nValue = InsecureRand32();
176+
newcoin.out.nValue = InsecureRandMoneyAmount();
177177
newcoin.nHeight = 1;
178178

179179
// 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 static RandomTransaction(CMutableTransaction& tx, bool fSingle)
110110
for (int out = 0; out < outs; out++) {
111111
tx.vout.push_back(CTxOut());
112112
CTxOut &txout = tx.vout.back();
113-
txout.nValue = InsecureRandRange(100000000);
113+
txout.nValue = InsecureRandMoneyAmount();
114114
RandomScript(txout.scriptPubKey);
115115
}
116116
}

src/test/util/blockfilter.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,3 @@ bool ComputeFilter(BlockFilterType filter_type, const CBlockIndex* block_index,
2828
filter = BlockFilter(filter_type, block, block_undo);
2929
return true;
3030
}
31-

src/test/util/random.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#ifndef BITCOIN_TEST_UTIL_RANDOM_H
66
#define BITCOIN_TEST_UTIL_RANDOM_H
77

8+
#include <consensus/amount.h>
89
#include <random.h>
910
#include <test/util/setup_common.h>
1011
#include <uint256.h>
@@ -36,4 +37,9 @@ static inline bool InsecureRandBool()
3637
return g_insecure_rand_ctx.randbool();
3738
}
3839

40+
static inline CAmount InsecureRandMoneyAmount()
41+
{
42+
return static_cast<CAmount>(InsecureRandRange(MAX_MONEY + 1));
43+
}
44+
3945
#endif // BITCOIN_TEST_UTIL_RANDOM_H

src/test/validation_chainstate_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ BOOST_AUTO_TEST_CASE(validation_chainstate_resize_caches)
3232
uint256 txid = InsecureRand256();
3333
COutPoint outp{txid, 0};
3434
newcoin.nHeight = 1;
35-
newcoin.out.nValue = InsecureRand32();
35+
newcoin.out.nValue = InsecureRandMoneyAmount();
3636
newcoin.out.scriptPubKey.assign(uint32_t{56}, 1);
3737
coins_view.AddCoin(outp, std::move(newcoin), false);
3838

src/test/validation_flush_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
3131
uint256 txid = InsecureRand256();
3232
COutPoint outp{txid, 0};
3333
newcoin.nHeight = 1;
34-
newcoin.out.nValue = InsecureRand32();
34+
newcoin.out.nValue = InsecureRandMoneyAmount();
3535
newcoin.out.scriptPubKey.assign(uint32_t{56}, 1);
3636
coins_view.AddCoin(outp, std::move(newcoin), false);
3737

0 commit comments

Comments
 (0)