Skip to content

Commit

Permalink
CBlockTreeDB: Add ::Options, init with BlockManager
Browse files Browse the repository at this point in the history
  • Loading branch information
dongcarl committed Jul 18, 2022
1 parent 0e625d4 commit ed7ae6a
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 23 deletions.
4 changes: 4 additions & 0 deletions src/bitcoin-chainstate.cpp
Expand Up @@ -80,6 +80,10 @@ int main(int argc, char* argv[])
const ChainstateManager::Options chainman_opts{
.chainparams = chainparams,
.adjusted_time_callback = static_cast<int64_t (*)()>(GetTime),
.block_tree_db_opts = {
.db_path = gArgs.GetDataDirNet() / "blocks" / "index",
.cache_size = 2 << 20,
},
};
ChainstateManager chainman{chainman_opts};

Expand Down
22 changes: 15 additions & 7 deletions src/init.cpp
Expand Up @@ -1439,22 +1439,26 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
for (bool fLoaded = false; !fLoaded && !ShutdownRequested();) {
node.mempool = std::make_unique<CTxMemPool>(mempool_opts);

const bool fReset = fReindex;

const ChainstateManager::Options chainman_opts{
.chainparams = chainparams,
.adjusted_time_callback = GetAdjustedTime,
.block_tree_db_opts = {
.db_path = args.GetDataDirNet() / "blocks" / "index",
.cache_size = static_cast<size_t>(cache_sizes.block_tree_db),
.wipe_existing = fReset,
.do_compact = args.GetBoolArg("-forcecompactdb", false),
},
};
node.chainman = std::make_unique<ChainstateManager>(chainman_opts);
ChainstateManager& chainman = *node.chainman;

const bool fReset = fReindex;
bilingual_str strLoadError;

uiInterface.InitMessage(_("Loading block index…").translated);
const int64_t load_block_index_start_time = GetTimeMillis();
std::optional<ChainstateLoadingError> maybe_load_error;
try {
node.chainman = std::make_unique<ChainstateManager>(chainman_opts);
maybe_load_error = LoadChainstate(fReset,
chainman,
*Assert(node.chainman),
Assert(node.mempool.get()),
fPruneMode,
fReindexChainState,
Expand All @@ -1473,6 +1477,9 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
LogPrintf("%s\n", e.what());
maybe_load_error = ChainstateLoadingError::ERROR_GENERIC_BLOCKDB_OPEN_FAILED;
}

bilingual_str strLoadError;

if (maybe_load_error.has_value()) {
switch (maybe_load_error.value()) {
case ChainstateLoadingError::ERROR_LOADING_BLOCK_DB:
Expand Down Expand Up @@ -1503,12 +1510,13 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
break;
case ChainstateLoadingError::ERROR_BLOCKS_WITNESS_INSUFFICIENTLY_VALIDATED:
strLoadError = strprintf(_("Witness data for blocks after height %d requires validation. Please restart with -reindex."),
chainman.GetConsensus().SegwitHeight);
chainman_opts.chainparams.GetConsensus().SegwitHeight);
break;
case ChainstateLoadingError::SHUTDOWN_PROBED:
break;
}
} else {
ChainstateManager& chainman = *Assert(node.chainman);
std::optional<ChainstateLoadVerifyError> maybe_verify_error;
try {
uiInterface.InitMessage(_("Verifying blocks…").translated);
Expand Down
3 changes: 3 additions & 0 deletions src/kernel/chainstatemanager_opts.h
Expand Up @@ -5,6 +5,8 @@
#ifndef BITCOIN_KERNEL_CHAINSTATEMANAGER_OPTS_H
#define BITCOIN_KERNEL_CHAINSTATEMANAGER_OPTS_H

#include <txdb.h>

#include <cstdint>
#include <functional>

Expand All @@ -20,6 +22,7 @@ namespace kernel {
struct ChainstateManagerOpts {
const CChainParams& chainparams;
const std::function<int64_t()> adjusted_time_callback{nullptr};
CBlockTreeDB::Options block_tree_db_opts;
};

} // namespace kernel
Expand Down
3 changes: 3 additions & 0 deletions src/node/blockstorage.cpp
Expand Up @@ -57,6 +57,9 @@ static FILE* OpenUndoFile(const FlatFilePos& pos, bool fReadOnly = false);
static FlatFileSeq BlockFileSeq();
static FlatFileSeq UndoFileSeq();

BlockManager::BlockManager(const CBlockTreeDB::Options& block_tree_db_opts)
: m_block_tree_db{std::make_unique<CBlockTreeDB>(block_tree_db_opts)} {}

std::vector<CBlockIndex*> BlockManager::GetAllBlockIndices()
{
AssertLockHeld(cs_main);
Expand Down
2 changes: 2 additions & 0 deletions src/node/blockstorage.h
Expand Up @@ -140,6 +140,8 @@ class BlockManager
std::unordered_map<std::string, PruneLockInfo> m_prune_locks GUARDED_BY(::cs_main);

public:
explicit BlockManager(const CBlockTreeDB::Options& block_tree_db_opts);

BlockMap m_block_index GUARDED_BY(cs_main);

std::vector<CBlockIndex*> GetAllBlockIndices() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
Expand Down
4 changes: 0 additions & 4 deletions src/node/chainstate.cpp
Expand Up @@ -33,10 +33,6 @@ std::optional<ChainstateLoadingError> LoadChainstate(bool fReset,
chainman.m_total_coinsdb_cache = nCoinDBCache;

auto& pblocktree{chainman.m_blockman.m_block_tree_db};
// new CBlockTreeDB tries to delete the existing file, which
// fails if it's still open from the previous loop. Close it first:
pblocktree.reset();
pblocktree.reset(new CBlockTreeDB(nBlockTreeDBCache, block_tree_db_in_memory, fReset));

if (fReset) {
pblocktree->WriteReindexing(true);
Expand Down
7 changes: 6 additions & 1 deletion src/test/util/setup_common.cpp
Expand Up @@ -185,9 +185,14 @@ ChainTestingSetup::ChainTestingSetup(const std::string& chainName, const std::ve
const ChainstateManager::Options chainman_opts{
.chainparams = chainparams,
.adjusted_time_callback = GetAdjustedTime,
.block_tree_db_opts = {
.db_path = gArgs.GetDataDirNet() / "blocks" / "index",
.cache_size = static_cast<size_t>(m_cache_sizes.block_tree_db),
.in_memory = true,
.do_compact = gArgs.GetBoolArg("-forcecompactdb", false),
},
};
m_node.chainman = std::make_unique<ChainstateManager>(chainman_opts);
m_node.chainman->m_blockman.m_block_tree_db = std::make_unique<CBlockTreeDB>(m_cache_sizes.block_tree_db, true);

// Start script-checking threads. Set g_parallel_script_checks to true so they are used.
constexpr int script_check_threads = 2;
Expand Down
11 changes: 2 additions & 9 deletions src/txdb.cpp
Expand Up @@ -193,15 +193,8 @@ size_t CCoinsViewDB::EstimateSize() const
return m_db->EstimateSize(DB_COIN, uint8_t(DB_COIN + 1));
}

CBlockTreeDB::CBlockTreeDB(size_t nCacheSize, bool fMemory, bool fWipe)
: CDBWrapper{{
.db_path = gArgs.GetDataDirNet() / "blocks" / "index",
.cache_size = nCacheSize,
.in_memory = fMemory,
.wipe_existing = fWipe,
.obfuscate_data = false,
.do_compact = gArgs.GetBoolArg("-forcecompactdb", false),
}} {}
CBlockTreeDB::CBlockTreeDB(const Options& opts)
: CDBWrapper{opts.ToDBWrapperOptions()} {}

bool CBlockTreeDB::ReadBlockFileInfo(int nFile, CBlockFileInfo &info) {
return Read(std::make_pair(DB_BLOCK_FILES, nFile), info);
Expand Down
23 changes: 22 additions & 1 deletion src/txdb.h
Expand Up @@ -79,7 +79,28 @@ class CCoinsViewDB final : public CCoinsView
class CBlockTreeDB : public CDBWrapper
{
public:
explicit CBlockTreeDB(size_t nCacheSize, bool fMemory = false, bool fWipe = false);
struct Options
{
fs::path db_path;
size_t cache_size;
bool in_memory = false;
bool wipe_existing = false;
bool do_compact = false;

CDBWrapper::Options ToDBWrapperOptions() const
{
return CDBWrapper::Options{
.db_path = db_path,
.cache_size = cache_size,
.in_memory = in_memory,
.wipe_existing = wipe_existing,
.obfuscate_data = false,
.do_compact = do_compact,
};
}
};

explicit CBlockTreeDB(const Options& opts);

bool WriteBatchSync(const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo, int nLastFile, const std::vector<const CBlockIndex*>& blockinfo);
bool ReadBlockFileInfo(int nFile, CBlockFileInfo &info);
Expand Down
3 changes: 2 additions & 1 deletion src/validation.h
Expand Up @@ -838,7 +838,8 @@ class ChainstateManager

explicit ChainstateManager(const Options& opts)
: m_chainparams{opts.chainparams},
m_adjusted_time_callback{Assert(opts.adjusted_time_callback)} {};
m_adjusted_time_callback{Assert(opts.adjusted_time_callback)},
m_blockman{opts.block_tree_db_opts} {};

const CChainParams& GetParams() const { return m_chainparams; }
const Consensus::Params& GetConsensus() const { return m_chainparams.GetConsensus(); }
Expand Down

0 comments on commit ed7ae6a

Please sign in to comment.