test: cover unused mempool space in coins cache
What changed, and why it matters
This is a routine test-only change. It adjusts an existing unit test to better exercise how Bitcoin Core's coin cache behaves when memory reserved for the mempool is not actually being used by the mempool. No production code was changed, so there is no direct security risk from this commit itself.
No security action needed. Review as normal test maintenance.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit modifies src/test/validation_flush_tests.cpp. It adds a call to ResizeCoinsCaches at the start of the test, changes several size_t constants to uint64_t, and updates comments/assertions to clarify that unused mempool headroom can be reclaimed by the coins cache. The test verifies state transitions (OK → LARGE → CRITICAL) and that flushing returns the cache to OK. No consensus, networking, wallet, or mempool logic is altered.
Changed components
src/test/validation_flush_tests.cppInspect captured patch +9 / −8
diff --git a/src/test/validation_flush_tests.cpp b/src/test/validation_flush_tests.cpp
index c35cef7f..263edd8b 100644
--- a/src/test/validation_flush_tests.cpp
+++ b/src/test/validation_flush_tests.cpp
@@ -18,33 +18,33 @@ BOOST_FIXTURE_TEST_SUITE(validation_flush_tests, TestingSetup)
//! then with additional mempool head-room.
BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
{
+ constexpr uint64_t MAX_COINS_BYTES{8_MiB};
+ constexpr uint64_t MAX_MEMPOOL_BYTES{4_MiB};
+ constexpr uint64_t MAX_ATTEMPTS{50'000};
Chainstate& chainstate{m_node.chainman->ActiveChainstate()};
LOCK(::cs_main);
+ BOOST_REQUIRE(chainstate.ResizeCoinsCaches(MAX_COINS_BYTES, /*coinsdb_size=*/1_MiB));
CCoinsViewCache& view{chainstate.CoinsTip()};
// Sanity: an empty cache should be ≲ 1 chunk (~ 256 KiB).
BOOST_CHECK_LT(view.DynamicMemoryUsage() / (256 * 1024.0), 1.1);
- constexpr size_t MAX_COINS_BYTES{8_MiB};
- constexpr size_t MAX_MEMPOOL_BYTES{4_MiB};
- constexpr size_t MAX_ATTEMPTS{50'000};
-
// Run the same growth-path twice: first with 0 head-room, then with extra head-room
- for (size_t max_mempool_size_bytes : {size_t{0}, MAX_MEMPOOL_BYTES}) {
+ for (uint64_t max_mempool_size_bytes : {uint64_t{0}, MAX_MEMPOOL_BYTES}) {
const int64_t full_cap{int64_t(MAX_COINS_BYTES + max_mempool_size_bytes)};
const int64_t large_cap{LargeCoinsCacheThreshold(full_cap)};
// OK → LARGE
auto state{chainstate.GetCoinsCacheSizeState(MAX_COINS_BYTES, max_mempool_size_bytes)};
- for (size_t i{0}; i < MAX_ATTEMPTS && int64_t(view.DynamicMemoryUsage()) <= large_cap; ++i) {
+ for (uint64_t i{0}; i < MAX_ATTEMPTS && int64_t(view.DynamicMemoryUsage()) <= large_cap; ++i) {
BOOST_CHECK_EQUAL(state, CoinsCacheSizeState::OK);
AddTestCoin(m_rng, view);
state = chainstate.GetCoinsCacheSizeState(MAX_COINS_BYTES, max_mempool_size_bytes);
}
// LARGE → CRITICAL
- for (size_t i{0}; i < MAX_ATTEMPTS && int64_t(view.DynamicMemoryUsage()) <= full_cap; ++i) {
+ for (uint64_t i{0}; i < MAX_ATTEMPTS && int64_t(view.DynamicMemoryUsage()) <= full_cap; ++i) {
BOOST_CHECK_EQUAL(state, CoinsCacheSizeState::LARGE);
AddTestCoin(m_rng, view);
state = chainstate.GetCoinsCacheSizeState(MAX_COINS_BYTES, max_mempool_size_bytes);
@@ -52,7 +52,7 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
BOOST_CHECK_EQUAL(state, CoinsCacheSizeState::CRITICAL);
}
- // Default thresholds (no explicit limits) permit many more coins.
+ // Unused mempool space permits many more coins.
for (int i{0}; i < 1'000; ++i) {
AddTestCoin(m_rng, view);
BOOST_CHECK_EQUAL(chainstate.GetCoinsCacheSizeState(), CoinsCacheSizeState::OK);
@@ -60,6 +60,7 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
// CRITICAL → OK via Flush
BOOST_CHECK_EQUAL(chainstate.GetCoinsCacheSizeState(MAX_COINS_BYTES, /*max_mempool_size_bytes=*/0), CoinsCacheSizeState::CRITICAL);
+ BOOST_CHECK_EQUAL(chainstate.GetCoinsCacheSizeState(), CoinsCacheSizeState::OK);
view.SetBestBlock(m_rng.rand256());
view.Flush();
BOOST_CHECK_EQUAL(chainstate.GetCoinsCacheSizeState(MAX_COINS_BYTES, /*max_mempool_size_bytes=*/0), CoinsCacheSizeState::OK);
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.