mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-11 06:28:31 +01:00
De-duplicate add_coin methods to a test util helper
This commit is contained in:
@@ -60,6 +60,7 @@ if [ "${RUN_TIDY}" = "true" ]; then
|
|||||||
" src/rpc/signmessage.cpp"\
|
" src/rpc/signmessage.cpp"\
|
||||||
" src/test/fuzz/txorphan.cpp"\
|
" src/test/fuzz/txorphan.cpp"\
|
||||||
" src/test/fuzz/util/"\
|
" src/test/fuzz/util/"\
|
||||||
|
" src/test/util/coins.cpp"\
|
||||||
" src/uint256.cpp"\
|
" src/uint256.cpp"\
|
||||||
" src/util/bip32.cpp"\
|
" src/util/bip32.cpp"\
|
||||||
" src/util/bytevectorhash.cpp"\
|
" src/util/bytevectorhash.cpp"\
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ EXTRA_LIBRARIES += \
|
|||||||
TEST_UTIL_H = \
|
TEST_UTIL_H = \
|
||||||
test/util/blockfilter.h \
|
test/util/blockfilter.h \
|
||||||
test/util/chainstate.h \
|
test/util/chainstate.h \
|
||||||
|
test/util/coins.h \
|
||||||
test/util/json.h \
|
test/util/json.h \
|
||||||
test/util/logging.h \
|
test/util/logging.h \
|
||||||
test/util/mining.h \
|
test/util/mining.h \
|
||||||
@@ -30,6 +31,7 @@ libtest_util_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(BOOST_CPPFLAGS)
|
|||||||
libtest_util_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
|
libtest_util_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
|
||||||
libtest_util_a_SOURCES = \
|
libtest_util_a_SOURCES = \
|
||||||
test/util/blockfilter.cpp \
|
test/util/blockfilter.cpp \
|
||||||
|
test/util/coins.cpp \
|
||||||
test/util/json.cpp \
|
test/util/json.cpp \
|
||||||
test/util/logging.cpp \
|
test/util/logging.cpp \
|
||||||
test/util/mining.cpp \
|
test/util/mining.cpp \
|
||||||
|
|||||||
27
src/test/util/coins.cpp
Normal file
27
src/test/util/coins.cpp
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
// Copyright (c) 2023 The Bitcoin Core developers
|
||||||
|
// Distributed under the MIT software license, see the accompanying
|
||||||
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
|
|
||||||
|
#include <test/util/coins.h>
|
||||||
|
|
||||||
|
#include <coins.h>
|
||||||
|
#include <primitives/transaction.h>
|
||||||
|
#include <script/script.h>
|
||||||
|
#include <test/util/random.h>
|
||||||
|
#include <uint256.h>
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
COutPoint AddTestCoin(CCoinsViewCache& coins_view)
|
||||||
|
{
|
||||||
|
Coin new_coin;
|
||||||
|
const uint256 txid{InsecureRand256()};
|
||||||
|
COutPoint outpoint{txid, /*nIn=*/0};
|
||||||
|
new_coin.nHeight = 1;
|
||||||
|
new_coin.out.nValue = InsecureRandMoneyAmount();
|
||||||
|
new_coin.out.scriptPubKey.assign(uint32_t{56}, 1);
|
||||||
|
coins_view.AddCoin(outpoint, std::move(new_coin), /*possible_overwrite=*/false);
|
||||||
|
|
||||||
|
return outpoint;
|
||||||
|
};
|
||||||
19
src/test/util/coins.h
Normal file
19
src/test/util/coins.h
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
// Copyright (c) 2023 The Bitcoin Core developers
|
||||||
|
// Distributed under the MIT software license, see the accompanying
|
||||||
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
|
|
||||||
|
#ifndef BITCOIN_TEST_UTIL_COINS_H
|
||||||
|
#define BITCOIN_TEST_UTIL_COINS_H
|
||||||
|
|
||||||
|
#include <primitives/transaction.h>
|
||||||
|
|
||||||
|
class CCoinsViewCache;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a Coin with DynamicMemoryUsage of 80 bytes and add it to the given view.
|
||||||
|
* @param[in,out] coins_view The coins view cache to add the new coin to.
|
||||||
|
* @returns the COutPoint of the created coin.
|
||||||
|
*/
|
||||||
|
COutPoint AddTestCoin(CCoinsViewCache& coins_view);
|
||||||
|
|
||||||
|
#endif // BITCOIN_TEST_UTIL_COINS_H
|
||||||
@@ -8,6 +8,7 @@
|
|||||||
#include <rpc/blockchain.h>
|
#include <rpc/blockchain.h>
|
||||||
#include <sync.h>
|
#include <sync.h>
|
||||||
#include <test/util/chainstate.h>
|
#include <test/util/chainstate.h>
|
||||||
|
#include <test/util/coins.h>
|
||||||
#include <test/util/random.h>
|
#include <test/util/random.h>
|
||||||
#include <test/util/setup_common.h>
|
#include <test/util/setup_common.h>
|
||||||
#include <uint256.h>
|
#include <uint256.h>
|
||||||
@@ -25,20 +26,6 @@ BOOST_AUTO_TEST_CASE(validation_chainstate_resize_caches)
|
|||||||
{
|
{
|
||||||
ChainstateManager& manager = *Assert(m_node.chainman);
|
ChainstateManager& manager = *Assert(m_node.chainman);
|
||||||
CTxMemPool& mempool = *Assert(m_node.mempool);
|
CTxMemPool& mempool = *Assert(m_node.mempool);
|
||||||
|
|
||||||
//! Create and add a Coin with DynamicMemoryUsage of 80 bytes to the given view.
|
|
||||||
auto add_coin = [](CCoinsViewCache& coins_view) -> COutPoint {
|
|
||||||
Coin newcoin;
|
|
||||||
uint256 txid = InsecureRand256();
|
|
||||||
COutPoint outp{txid, 0};
|
|
||||||
newcoin.nHeight = 1;
|
|
||||||
newcoin.out.nValue = InsecureRandMoneyAmount();
|
|
||||||
newcoin.out.scriptPubKey.assign(uint32_t{56}, 1);
|
|
||||||
coins_view.AddCoin(outp, std::move(newcoin), false);
|
|
||||||
|
|
||||||
return outp;
|
|
||||||
};
|
|
||||||
|
|
||||||
Chainstate& c1 = WITH_LOCK(cs_main, return manager.InitializeChainstate(&mempool));
|
Chainstate& c1 = WITH_LOCK(cs_main, return manager.InitializeChainstate(&mempool));
|
||||||
c1.InitCoinsDB(
|
c1.InitCoinsDB(
|
||||||
/*cache_size_bytes=*/1 << 23, /*in_memory=*/true, /*should_wipe=*/false);
|
/*cache_size_bytes=*/1 << 23, /*in_memory=*/true, /*should_wipe=*/false);
|
||||||
@@ -48,7 +35,7 @@ BOOST_AUTO_TEST_CASE(validation_chainstate_resize_caches)
|
|||||||
// Add a coin to the in-memory cache, upsize once, then downsize.
|
// Add a coin to the in-memory cache, upsize once, then downsize.
|
||||||
{
|
{
|
||||||
LOCK(::cs_main);
|
LOCK(::cs_main);
|
||||||
auto outpoint = add_coin(c1.CoinsTip());
|
const auto outpoint = AddTestCoin(c1.CoinsTip());
|
||||||
|
|
||||||
// Set a meaningless bestblock value in the coinsview cache - otherwise we won't
|
// Set a meaningless bestblock value in the coinsview cache - otherwise we won't
|
||||||
// flush during ResizecoinsCaches() and will subsequently hit an assertion.
|
// flush during ResizecoinsCaches() and will subsequently hit an assertion.
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
//
|
//
|
||||||
#include <sync.h>
|
#include <sync.h>
|
||||||
|
#include <test/util/coins.h>
|
||||||
#include <test/util/random.h>
|
#include <test/util/random.h>
|
||||||
#include <test/util/setup_common.h>
|
#include <test/util/setup_common.h>
|
||||||
#include <validation.h>
|
#include <validation.h>
|
||||||
@@ -25,19 +26,6 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
|
|||||||
LOCK(::cs_main);
|
LOCK(::cs_main);
|
||||||
auto& view = chainstate.CoinsTip();
|
auto& view = chainstate.CoinsTip();
|
||||||
|
|
||||||
//! Create and add a Coin with DynamicMemoryUsage of 80 bytes to the given view.
|
|
||||||
auto add_coin = [](CCoinsViewCache& coins_view) -> COutPoint {
|
|
||||||
Coin newcoin;
|
|
||||||
uint256 txid = InsecureRand256();
|
|
||||||
COutPoint outp{txid, 0};
|
|
||||||
newcoin.nHeight = 1;
|
|
||||||
newcoin.out.nValue = InsecureRandMoneyAmount();
|
|
||||||
newcoin.out.scriptPubKey.assign(uint32_t{56}, 1);
|
|
||||||
coins_view.AddCoin(outp, std::move(newcoin), false);
|
|
||||||
|
|
||||||
return outp;
|
|
||||||
};
|
|
||||||
|
|
||||||
// The number of bytes consumed by coin's heap data, i.e. CScript
|
// The number of bytes consumed by coin's heap data, i.e. CScript
|
||||||
// (prevector<28, unsigned char>) when assigned 56 bytes of data per above.
|
// (prevector<28, unsigned char>) when assigned 56 bytes of data per above.
|
||||||
//
|
//
|
||||||
@@ -62,7 +50,7 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
|
|||||||
// Add a bunch of coins to see that we at least flip over to CRITICAL.
|
// Add a bunch of coins to see that we at least flip over to CRITICAL.
|
||||||
|
|
||||||
for (int i{0}; i < 1000; ++i) {
|
for (int i{0}; i < 1000; ++i) {
|
||||||
COutPoint res = add_coin(view);
|
const COutPoint res = AddTestCoin(view);
|
||||||
BOOST_CHECK_EQUAL(view.AccessCoin(res).DynamicMemoryUsage(), COIN_SIZE);
|
BOOST_CHECK_EQUAL(view.AccessCoin(res).DynamicMemoryUsage(), COIN_SIZE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,7 +72,7 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
|
|||||||
constexpr int COINS_UNTIL_CRITICAL{3};
|
constexpr int COINS_UNTIL_CRITICAL{3};
|
||||||
|
|
||||||
for (int i{0}; i < COINS_UNTIL_CRITICAL; ++i) {
|
for (int i{0}; i < COINS_UNTIL_CRITICAL; ++i) {
|
||||||
COutPoint res = add_coin(view);
|
const COutPoint res = AddTestCoin(view);
|
||||||
print_view_mem_usage(view);
|
print_view_mem_usage(view);
|
||||||
BOOST_CHECK_EQUAL(view.AccessCoin(res).DynamicMemoryUsage(), COIN_SIZE);
|
BOOST_CHECK_EQUAL(view.AccessCoin(res).DynamicMemoryUsage(), COIN_SIZE);
|
||||||
BOOST_CHECK_EQUAL(
|
BOOST_CHECK_EQUAL(
|
||||||
@@ -94,7 +82,7 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
|
|||||||
|
|
||||||
// Adding some additional coins will push us over the edge to CRITICAL.
|
// Adding some additional coins will push us over the edge to CRITICAL.
|
||||||
for (int i{0}; i < 4; ++i) {
|
for (int i{0}; i < 4; ++i) {
|
||||||
add_coin(view);
|
AddTestCoin(view);
|
||||||
print_view_mem_usage(view);
|
print_view_mem_usage(view);
|
||||||
if (chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes=*/0) ==
|
if (chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes=*/0) ==
|
||||||
CoinsCacheSizeState::CRITICAL) {
|
CoinsCacheSizeState::CRITICAL) {
|
||||||
@@ -112,7 +100,7 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
|
|||||||
CoinsCacheSizeState::OK);
|
CoinsCacheSizeState::OK);
|
||||||
|
|
||||||
for (int i{0}; i < 3; ++i) {
|
for (int i{0}; i < 3; ++i) {
|
||||||
add_coin(view);
|
AddTestCoin(view);
|
||||||
print_view_mem_usage(view);
|
print_view_mem_usage(view);
|
||||||
BOOST_CHECK_EQUAL(
|
BOOST_CHECK_EQUAL(
|
||||||
chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes=*/1 << 10),
|
chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes=*/1 << 10),
|
||||||
@@ -121,7 +109,7 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
|
|||||||
|
|
||||||
// Adding another coin with the additional mempool room will put us >90%
|
// Adding another coin with the additional mempool room will put us >90%
|
||||||
// but not yet critical.
|
// but not yet critical.
|
||||||
add_coin(view);
|
AddTestCoin(view);
|
||||||
print_view_mem_usage(view);
|
print_view_mem_usage(view);
|
||||||
|
|
||||||
// Only perform these checks on 64 bit hosts; I haven't done the math for 32.
|
// 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)
|
|||||||
|
|
||||||
// Using the default max_* values permits way more coins to be added.
|
// Using the default max_* values permits way more coins to be added.
|
||||||
for (int i{0}; i < 1000; ++i) {
|
for (int i{0}; i < 1000; ++i) {
|
||||||
add_coin(view);
|
AddTestCoin(view);
|
||||||
BOOST_CHECK_EQUAL(
|
BOOST_CHECK_EQUAL(
|
||||||
chainstate.GetCoinsCacheSizeState(),
|
chainstate.GetCoinsCacheSizeState(),
|
||||||
CoinsCacheSizeState::OK);
|
CoinsCacheSizeState::OK);
|
||||||
|
|||||||
Reference in New Issue
Block a user