Skip to content

Commit 4275195

Browse files
committed
De-duplicate add_coin methods to a test util helper
1 parent 9d92c3d commit 4275195

File tree

6 files changed

+58
-34
lines changed

6 files changed

+58
-34
lines changed

ci/test/06_script_b.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ if [ "${RUN_TIDY}" = "true" ]; then
6060
" src/rpc/signmessage.cpp"\
6161
" src/test/fuzz/txorphan.cpp"\
6262
" src/test/fuzz/util/"\
63+
" src/test/util/coins.cpp"\
6364
" src/uint256.cpp"\
6465
" src/util/bip32.cpp"\
6566
" src/util/bytevectorhash.cpp"\

src/Makefile.test_util.include

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ EXTRA_LIBRARIES += \
1010
TEST_UTIL_H = \
1111
test/util/blockfilter.h \
1212
test/util/chainstate.h \
13+
test/util/coins.h \
1314
test/util/json.h \
1415
test/util/logging.h \
1516
test/util/mining.h \
@@ -30,6 +31,7 @@ libtest_util_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(BOOST_CPPFLAGS)
3031
libtest_util_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
3132
libtest_util_a_SOURCES = \
3233
test/util/blockfilter.cpp \
34+
test/util/coins.cpp \
3335
test/util/json.cpp \
3436
test/util/logging.cpp \
3537
test/util/mining.cpp \

src/test/util/coins.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (c) 2023 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <test/util/coins.h>
6+
7+
#include <coins.h>
8+
#include <primitives/transaction.h>
9+
#include <script/script.h>
10+
#include <test/util/random.h>
11+
#include <uint256.h>
12+
13+
#include <stdint.h>
14+
#include <utility>
15+
16+
COutPoint AddTestCoin(CCoinsViewCache& coins_view)
17+
{
18+
Coin new_coin;
19+
const uint256 txid{InsecureRand256()};
20+
COutPoint outpoint{txid, /*nIn=*/0};
21+
new_coin.nHeight = 1;
22+
new_coin.out.nValue = InsecureRandMoneyAmount();
23+
new_coin.out.scriptPubKey.assign(uint32_t{56}, 1);
24+
coins_view.AddCoin(outpoint, std::move(new_coin), /*possible_overwrite=*/false);
25+
26+
return outpoint;
27+
};

src/test/util/coins.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) 2023 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_TEST_UTIL_COINS_H
6+
#define BITCOIN_TEST_UTIL_COINS_H
7+
8+
#include <primitives/transaction.h>
9+
10+
class CCoinsViewCache;
11+
12+
/**
13+
* Create a Coin with DynamicMemoryUsage of 80 bytes and add it to the given view.
14+
* @param[in,out] coins_view The coins view cache to add the new coin to.
15+
* @returns the COutPoint of the created coin.
16+
*/
17+
COutPoint AddTestCoin(CCoinsViewCache& coins_view);
18+
19+
#endif // BITCOIN_TEST_UTIL_COINS_H

src/test/validation_chainstate_tests.cpp

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <rpc/blockchain.h>
99
#include <sync.h>
1010
#include <test/util/chainstate.h>
11+
#include <test/util/coins.h>
1112
#include <test/util/random.h>
1213
#include <test/util/setup_common.h>
1314
#include <uint256.h>
@@ -25,20 +26,6 @@ BOOST_AUTO_TEST_CASE(validation_chainstate_resize_caches)
2526
{
2627
ChainstateManager& manager = *Assert(m_node.chainman);
2728
CTxMemPool& mempool = *Assert(m_node.mempool);
28-
29-
//! Create and add a Coin with DynamicMemoryUsage of 80 bytes to the given view.
30-
auto add_coin = [](CCoinsViewCache& coins_view) -> COutPoint {
31-
Coin newcoin;
32-
uint256 txid = InsecureRand256();
33-
COutPoint outp{txid, 0};
34-
newcoin.nHeight = 1;
35-
newcoin.out.nValue = InsecureRandMoneyAmount();
36-
newcoin.out.scriptPubKey.assign(uint32_t{56}, 1);
37-
coins_view.AddCoin(outp, std::move(newcoin), false);
38-
39-
return outp;
40-
};
41-
4229
Chainstate& c1 = WITH_LOCK(cs_main, return manager.InitializeChainstate(&mempool));
4330
c1.InitCoinsDB(
4431
/*cache_size_bytes=*/1 << 23, /*in_memory=*/true, /*should_wipe=*/false);
@@ -48,7 +35,7 @@ BOOST_AUTO_TEST_CASE(validation_chainstate_resize_caches)
4835
// Add a coin to the in-memory cache, upsize once, then downsize.
4936
{
5037
LOCK(::cs_main);
51-
auto outpoint = add_coin(c1.CoinsTip());
38+
const auto outpoint = AddTestCoin(c1.CoinsTip());
5239

5340
// Set a meaningless bestblock value in the coinsview cache - otherwise we won't
5441
// flush during ResizecoinsCaches() and will subsequently hit an assertion.

src/test/validation_flush_tests.cpp

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44
//
55
#include <sync.h>
6+
#include <test/util/coins.h>
67
#include <test/util/random.h>
78
#include <test/util/setup_common.h>
89
#include <validation.h>
@@ -25,19 +26,6 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
2526
LOCK(::cs_main);
2627
auto& view = chainstate.CoinsTip();
2728

28-
//! Create and add a Coin with DynamicMemoryUsage of 80 bytes to the given view.
29-
auto add_coin = [](CCoinsViewCache& coins_view) -> COutPoint {
30-
Coin newcoin;
31-
uint256 txid = InsecureRand256();
32-
COutPoint outp{txid, 0};
33-
newcoin.nHeight = 1;
34-
newcoin.out.nValue = InsecureRandMoneyAmount();
35-
newcoin.out.scriptPubKey.assign(uint32_t{56}, 1);
36-
coins_view.AddCoin(outp, std::move(newcoin), false);
37-
38-
return outp;
39-
};
40-
4129
// The number of bytes consumed by coin's heap data, i.e. CScript
4230
// (prevector<28, unsigned char>) when assigned 56 bytes of data per above.
4331
//
@@ -62,7 +50,7 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
6250
// Add a bunch of coins to see that we at least flip over to CRITICAL.
6351

6452
for (int i{0}; i < 1000; ++i) {
65-
COutPoint res = add_coin(view);
53+
const COutPoint res = AddTestCoin(view);
6654
BOOST_CHECK_EQUAL(view.AccessCoin(res).DynamicMemoryUsage(), COIN_SIZE);
6755
}
6856

@@ -84,7 +72,7 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
8472
constexpr int COINS_UNTIL_CRITICAL{3};
8573

8674
for (int i{0}; i < COINS_UNTIL_CRITICAL; ++i) {
87-
COutPoint res = add_coin(view);
75+
const COutPoint res = AddTestCoin(view);
8876
print_view_mem_usage(view);
8977
BOOST_CHECK_EQUAL(view.AccessCoin(res).DynamicMemoryUsage(), COIN_SIZE);
9078
BOOST_CHECK_EQUAL(
@@ -94,7 +82,7 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
9482

9583
// Adding some additional coins will push us over the edge to CRITICAL.
9684
for (int i{0}; i < 4; ++i) {
97-
add_coin(view);
85+
AddTestCoin(view);
9886
print_view_mem_usage(view);
9987
if (chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes=*/0) ==
10088
CoinsCacheSizeState::CRITICAL) {
@@ -112,7 +100,7 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
112100
CoinsCacheSizeState::OK);
113101

114102
for (int i{0}; i < 3; ++i) {
115-
add_coin(view);
103+
AddTestCoin(view);
116104
print_view_mem_usage(view);
117105
BOOST_CHECK_EQUAL(
118106
chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes=*/1 << 10),
@@ -121,7 +109,7 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
121109

122110
// Adding another coin with the additional mempool room will put us >90%
123111
// but not yet critical.
124-
add_coin(view);
112+
AddTestCoin(view);
125113
print_view_mem_usage(view);
126114

127115
// Only perform these checks on 64 bit hosts; I haven't done the math for 32.
@@ -137,7 +125,7 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
137125

138126
// Using the default max_* values permits way more coins to be added.
139127
for (int i{0}; i < 1000; ++i) {
140-
add_coin(view);
128+
AddTestCoin(view);
141129
BOOST_CHECK_EQUAL(
142130
chainstate.GetCoinsCacheSizeState(),
143131
CoinsCacheSizeState::OK);

0 commit comments

Comments
 (0)