diff --git a/src/bitcoin-chainstate.cpp b/src/bitcoin-chainstate.cpp --- a/src/bitcoin-chainstate.cpp +++ b/src/bitcoin-chainstate.cpp @@ -89,7 +89,10 @@ .config = config, .adjusted_time_callback = NodeClock::now, }; - ChainstateManager chainman{chainman_opts, {}}; + const node::BlockManager::Options blockman_opts{ + .chainparams = chainman_opts.config.GetChainParams(), + }; + ChainstateManager chainman{chainman_opts, blockman_opts}; node::CacheSizes cache_sizes; cache_sizes.block_tree_db = 2 << 20; diff --git a/src/init.cpp b/src/init.cpp --- a/src/init.cpp +++ b/src/init.cpp @@ -1984,7 +1984,9 @@ if (const auto error{ApplyArgsManOptions(args, chainman_opts_dummy)}) { return InitError(*error); } - node::BlockManager::Options blockman_opts_dummy{}; + node::BlockManager::Options blockman_opts_dummy{ + .chainparams = chainman_opts_dummy.config.GetChainParams(), + }; if (const auto error{ApplyArgsManOptions(args, blockman_opts_dummy)}) { return InitError(*error); } @@ -2372,7 +2374,9 @@ LogPrintf("Skipping checkpoint verification.\n"); } - node::BlockManager::Options blockman_opts{}; + node::BlockManager::Options blockman_opts{ + .chainparams = chainman_opts.config.GetChainParams(), + }; // no error can happen, already checked in AppInitParameterInteraction Assert(!ApplyArgsManOptions(args, blockman_opts)); diff --git a/src/kernel/blockmanager_opts.h b/src/kernel/blockmanager_opts.h --- a/src/kernel/blockmanager_opts.h +++ b/src/kernel/blockmanager_opts.h @@ -5,6 +5,8 @@ #ifndef BITCOIN_KERNEL_BLOCKMANAGER_OPTS_H #define BITCOIN_KERNEL_BLOCKMANAGER_OPTS_H +class CChainParams; + namespace kernel { /** @@ -12,6 +14,7 @@ * `BlockManager::Options` due to the using-declaration in `BlockManager`. */ struct BlockManagerOpts { + const CChainParams &chainparams; uint64_t prune_target{0}; }; diff --git a/src/node/blockstorage.h b/src/node/blockstorage.h --- a/src/node/blockstorage.h +++ b/src/node/blockstorage.h @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -68,13 +69,16 @@ friend ChainstateManager; private: + const CChainParams &GetParams() const { return m_opts.chainparams; } + const Consensus::Params &GetConsensus() const { + return m_opts.chainparams.GetConsensus(); + } /** * Load the blocktree off disk and into memory. Populate certain metadata * per index entry (nStatus, nChainWork, nTimeMax, etc.) as well as * peripheral collections like m_dirty_blockindex. */ - bool LoadBlockIndex(const Consensus::Params &consensus_params) - EXCLUSIVE_LOCKS_REQUIRED(cs_main); + bool LoadBlockIndex() EXCLUSIVE_LOCKS_REQUIRED(cs_main); void FlushBlockFile(bool fFinalize = false, bool finalize_undo = false); void FlushUndoFile(int block_file, bool finalize = false); bool FindBlockPos(FlatFilePos &pos, unsigned int nAddSize, @@ -158,8 +162,7 @@ std::unique_ptr m_block_tree_db GUARDED_BY(::cs_main); bool WriteBlockIndexDB() EXCLUSIVE_LOCKS_REQUIRED(::cs_main); - bool LoadBlockIndexDB(const Consensus::Params &consensus_params) - EXCLUSIVE_LOCKS_REQUIRED(::cs_main); + bool LoadBlockIndexDB() EXCLUSIVE_LOCKS_REQUIRED(::cs_main); CBlockIndex *AddToBlockIndex(const CBlockHeader &block, CBlockIndex *&best_header) @@ -181,8 +184,7 @@ CBlockFileInfo *GetBlockFileInfo(size_t n); bool WriteUndoDataForBlock(const CBlockUndo &blockundo, - BlockValidationState &state, CBlockIndex *pindex, - const CChainParams &chainparams) + BlockValidationState &state, CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(::cs_main); /** @@ -190,9 +192,7 @@ * position of the block within a block file on disk. */ FlatFilePos SaveBlockToDisk(const CBlock &block, int nHeight, - CChain &active_chain, - const CChainParams &chainparams, - const FlatFilePos *dbp); + CChain &active_chain, const FlatFilePos *dbp); /** Whether running in -prune mode. */ [[nodiscard]] bool IsPruneMode() const { return m_prune_mode; } diff --git a/src/node/blockstorage.cpp b/src/node/blockstorage.cpp --- a/src/node/blockstorage.cpp +++ b/src/node/blockstorage.cpp @@ -239,11 +239,13 @@ return pindex; } -bool BlockManager::LoadBlockIndex(const Consensus::Params ¶ms) { +bool BlockManager::LoadBlockIndex() { AssertLockHeld(cs_main); if (!m_block_tree_db->LoadBlockIndexGuts( - params, [this](const BlockHash &hash) EXCLUSIVE_LOCKS_REQUIRED( - cs_main) { return this->InsertBlockIndex(hash); })) { + GetConsensus(), + [this](const BlockHash &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main) { + return this->InsertBlockIndex(hash); + })) { return false; } @@ -309,8 +311,8 @@ return true; } -bool BlockManager::LoadBlockIndexDB(const Consensus::Params &consensus_params) { - if (!LoadBlockIndex(consensus_params)) { +bool BlockManager::LoadBlockIndexDB() { + if (!LoadBlockIndex()) { return false; } @@ -701,8 +703,7 @@ bool BlockManager::WriteUndoDataForBlock(const CBlockUndo &blockundo, BlockValidationState &state, - CBlockIndex *pindex, - const CChainParams &chainparams) { + CBlockIndex *pindex) { AssertLockHeld(::cs_main); // Write undo information to disk if (pindex->GetUndoPos().IsNull()) { @@ -712,7 +713,7 @@ return error("ConnectBlock(): FindUndoPos failed"); } if (!UndoWriteToDisk(blockundo, _pos, pindex->pprev->GetBlockHash(), - chainparams.DiskMagic())) { + GetParams().DiskMagic())) { return AbortNode(state, "Failed to write undo data"); } // rev files are written in block height order, whereas blk files are @@ -823,7 +824,6 @@ FlatFilePos BlockManager::SaveBlockToDisk(const CBlock &block, int nHeight, CChain &active_chain, - const CChainParams &chainparams, const FlatFilePos *dbp) { unsigned int nBlockSize = ::GetSerializeSize(block, CLIENT_VERSION); FlatFilePos blockPos; @@ -846,7 +846,7 @@ return FlatFilePos(); } if (!position_known) { - if (!WriteBlockToDisk(block, blockPos, chainparams.DiskMagic())) { + if (!WriteBlockToDisk(block, blockPos, GetParams().DiskMagic())) { AbortNode("Failed to write block"); return FlatFilePos(); } diff --git a/src/test/blockmanager_tests.cpp b/src/test/blockmanager_tests.cpp --- a/src/test/blockmanager_tests.cpp +++ b/src/test/blockmanager_tests.cpp @@ -19,12 +19,14 @@ BOOST_AUTO_TEST_CASE(blockmanager_find_block_pos) { const auto params{CreateChainParams(CBaseChainParams::MAIN)}; - BlockManager blockman{{}}; + node::BlockManager::Options blockman_opts{ + .chainparams = *params, + }; + BlockManager blockman{blockman_opts}; CChain chain{}; // simulate adding a genesis block normally BOOST_CHECK_EQUAL( - blockman - .SaveBlockToDisk(params->GenesisBlock(), 0, chain, *params, nullptr) + blockman.SaveBlockToDisk(params->GenesisBlock(), 0, chain, nullptr) .nPos, BLOCK_SERIALIZATION_HEADER_SIZE); // simulate what happens during reindex @@ -34,9 +36,7 @@ // before each block in a well-formed blk file. FlatFilePos pos{0, BLOCK_SERIALIZATION_HEADER_SIZE}; BOOST_CHECK_EQUAL( - blockman - .SaveBlockToDisk(params->GenesisBlock(), 0, chain, *params, &pos) - .nPos, + blockman.SaveBlockToDisk(params->GenesisBlock(), 0, chain, &pos).nPos, BLOCK_SERIALIZATION_HEADER_SIZE); // now simulate what happens after reindex for the first new block processed // the actual block contents don't matter, just that it's a block. @@ -46,8 +46,8 @@ // (for serialization header) + 285 (for serialized genesis block) = 293 add // another 8 bytes for the second block's serialization header and we get // 293 + 8 = 301 - FlatFilePos actual{blockman.SaveBlockToDisk(params->GenesisBlock(), 1, - chain, *params, nullptr)}; + FlatFilePos actual{ + blockman.SaveBlockToDisk(params->GenesisBlock(), 1, chain, nullptr)}; BOOST_CHECK_EQUAL( actual.nPos, BLOCK_SERIALIZATION_HEADER_SIZE + diff --git a/src/test/util/setup_common.cpp b/src/test/util/setup_common.cpp --- a/src/test/util/setup_common.cpp +++ b/src/test/util/setup_common.cpp @@ -216,8 +216,11 @@ .check_block_index = true, }; ApplyArgsManOptions(*m_node.args, chainman_opts); - m_node.chainman = std::make_unique( - chainman_opts, node::BlockManager::Options{}); + node::BlockManager::Options blockman_opts{ + .chainparams = chainman_opts.config.GetChainParams(), + }; + m_node.chainman = + std::make_unique(chainman_opts, blockman_opts); m_node.chainman->m_blockman.m_block_tree_db = std::make_unique(m_cache_sizes.block_tree_db, true); // Call Upgrade on the block database so that the version field is set, diff --git a/src/test/validation_chainstatemanager_tests.cpp b/src/test/validation_chainstatemanager_tests.cpp --- a/src/test/validation_chainstatemanager_tests.cpp +++ b/src/test/validation_chainstatemanager_tests.cpp @@ -400,11 +400,14 @@ .config = ::GetConfig(), .adjusted_time_callback = GetAdjustedTime, }; + node::BlockManager::Options blockman_opts{ + .chainparams = chainman_opts.config.GetChainParams(), + }; // For robustness, ensure the old manager is destroyed before // creating a new one. m_node.chainman.reset(); m_node.chainman = std::make_unique( - chainman_opts, node::BlockManager::Options{}); + chainman_opts, blockman_opts); } return *Assert(m_node.chainman); } diff --git a/src/validation.cpp b/src/validation.cpp --- a/src/validation.cpp +++ b/src/validation.cpp @@ -2094,7 +2094,7 @@ return true; } - if (!m_blockman.WriteUndoDataForBlock(blockundo, state, pindex, params)) { + if (!m_blockman.WriteUndoDataForBlock(blockundo, state, pindex)) { return false; } @@ -4437,8 +4437,8 @@ *fNewBlock = true; } try { - FlatFilePos blockPos{m_blockman.SaveBlockToDisk(block, pindex->nHeight, - m_chain, params, dbp)}; + FlatFilePos blockPos{ + m_blockman.SaveBlockToDisk(block, pindex->nHeight, m_chain, dbp)}; if (blockPos.IsNull()) { state.Error(strprintf( "%s: Failed to find position to write new block to disk", @@ -4969,7 +4969,7 @@ // Load block index from databases bool needs_init = fReindex; if (!fReindex) { - bool ret = m_blockman.LoadBlockIndexDB(GetConsensus()); + bool ret = m_blockman.LoadBlockIndexDB(); if (!ret) { return false; } @@ -5099,7 +5099,7 @@ try { const CBlock &block = params.GenesisBlock(); FlatFilePos blockPos{ - m_blockman.SaveBlockToDisk(block, 0, m_chain, params, nullptr)}; + m_blockman.SaveBlockToDisk(block, 0, m_chain, nullptr)}; if (blockPos.IsNull()) { return error("%s: writing genesis block to disk failed", __func__); }