Repository: bitcoin/bitcoin
File: src/node/chainstate.cpp (C++)

Code before:
#include <arith_uint256.h>
#include <chain.h>
#include <coins.h>
#include <consensus/params.h>
#include <kernel/caches.h>
#include <logging.h>
#include <node/blockstorage.h>
#include <sync.h>
#include <threadsafety.h>
#include <tinyformat.h>
#include <txdb.h>
#include <uint256.h>
#include <util/fs.h>
#include <util/signalinterrupt.h>
#include <util/time.h>
#include <util/translation.h>
#include <validation.h>

#include <algorithm>
#include <atomic>
#include <cassert>
#include <limits>
#include <memory>
#include <vector>

using kernel::CacheSizes;

namespace node {
// Complete initialization of chainstates after the initial call has been made
// to ChainstateManager::InitializeChainstate().
static ChainstateLoadResult CompleteChainstateInitialization(
    ChainstateManager& chainman,
    const CacheSizes& cache_sizes,
    const ChainstateLoadOptions& options) EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
{
    auto& pblocktree{chainman.m_blockman.m_block_tree_db};
    // new BlockTreeDB tries to delete the existing file, which
    // fails if it's still open from the previous loop. Close it first:
    pblocktree.reset();
    try {
        pblocktree = std::make_unique<BlockTreeDB>(DBParams{
            .path = chainman.m_options.datadir / "blocks" / "index",
            .cache_bytes = static_cast<size_t>(cache_sizes.block_tree_db),
            .memory_only = options.block_tree_db_in_memory,
            .wipe_data = options.wipe_block_tree_db,
            .options = chainman.m_options.block_tree_db});
    } catch (dbwrapper_error& err) {
        LogError("%s\n", err.what());
        return {ChainstateLoadStatus::FAILURE, _("Error opening block database")};
    }


Review comment (performance):
in ccf8caa4430f57b8f148e96ac2a241221977bcab

nit: I've raised this before, but since we now have a `kernel::CacheSizes` and `node::CacheSizes`, I think `using` a `kernel::CacheSizes` in a `node` file is unnecessarily confusing, so I would prefer making this explicit (as in the `chainstate.h` file):

<details>
<summary>git diff on ccf8caa443</summary>

```diff
diff --git a/src/node/chainstate.cpp b/src/node/chainstate.cpp
index 660d2c3289..d4d8828f73 100644
--- a/src/node/chainstate.cpp
+++ b/src/node/chainstate.cpp
@@ -29,14 +29,12 @@
 #include <memory>
 #include <vector>
 
-using kernel::CacheSizes;
-
 namespace node {
 // Complete initialization of chainstates after the initial call has been made
 // to ChainstateManager::InitializeChainstate().
 static ChainstateLoadResult CompleteChainstateInitialization(
     ChainstateManager& chainman,
-    const CacheSizes& cache_sizes,
+    const kernel::CacheSizes& cache_sizes,
     const ChainstateLoadOptions& options) EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
 {
     auto& pblocktree{chainman.m_blockman.m_block_tree_db};
@@ -172,7 +170,7 @@ static ChainstateLoadResult CompleteChainstateInitialization(
     return {ChainstateLoadStatus::SUCCESS, {}};
 }
 
-ChainstateLoadResult LoadChainstate(ChainstateManager& chainman, const CacheSizes& cache_sizes,
+ChainstateLoadResult LoadChainstate(ChainstateManager& chainman, const kernel::CacheSizes& cache_sizes,
                                     const ChainstateLoadOptions& options)
 {
     if (!chainman.AssumedValidBlock().IsNull()) {

```
</details>


Code after:
#include <arith_uint256.h>
#include <chain.h>
#include <coins.h>
#include <consensus/params.h>
#include <kernel/caches.h>
#include <logging.h>
#include <node/blockstorage.h>
#include <sync.h>
#include <threadsafety.h>
#include <tinyformat.h>
#include <txdb.h>
#include <uint256.h>
#include <util/fs.h>
#include <util/signalinterrupt.h>
#include <util/time.h>
#include <util/translation.h>
#include <validation.h>

#include <algorithm>
#include <atomic>
#include <cassert>
#include <limits>
#include <memory>
#include <vector>

using kernel::CacheSizes;

namespace node {
// Complete initialization of chainstates after the initial call has been made
// to ChainstateManager::InitializeChainstate().
static ChainstateLoadResult CompleteChainstateInitialization(
    ChainstateManager& chainman,
    const CacheSizes& cache_sizes,
    const ChainstateLoadOptions& options) EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
{
    auto& pblocktree{chainman.m_blockman.m_block_tree_db};
    // new BlockTreeDB tries to delete the existing file, which
    // fails if it's still open from the previous loop. Close it first:
    pblocktree.reset();
    try {
        pblocktree = std::make_unique<BlockTreeDB>(DBParams{
            .path = chainman.m_options.datadir / "blocks" / "index",
            .cache_bytes = cache_sizes.block_tree_db,
            .memory_only = options.block_tree_db_in_memory,
            .wipe_data = options.wipe_block_tree_db,
            .options = chainman.m_options.block_tree_db});
    } catch (dbwrapper_error& err) {
        LogError("%s\n", err.what());
        return {ChainstateLoadStatus::FAILURE, _("Error opening block database")};
    }