mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-04-29 14:59:39 -04:00
Add BlockManagerOpts::chainparams reference
and use it in blockstorage.cpp
This commit is contained in:
parent
6c7ebcc14b
commit
facdb8b331
8 changed files with 31 additions and 9 deletions
|
@ -86,7 +86,10 @@ int main(int argc, char* argv[])
|
||||||
.datadir = gArgs.GetDataDirNet(),
|
.datadir = gArgs.GetDataDirNet(),
|
||||||
.adjusted_time_callback = NodeClock::now,
|
.adjusted_time_callback = NodeClock::now,
|
||||||
};
|
};
|
||||||
ChainstateManager chainman{chainman_opts, {}};
|
const node::BlockManager::Options blockman_opts{
|
||||||
|
.chainparams = chainman_opts.chainparams,
|
||||||
|
};
|
||||||
|
ChainstateManager chainman{chainman_opts, blockman_opts};
|
||||||
|
|
||||||
node::CacheSizes cache_sizes;
|
node::CacheSizes cache_sizes;
|
||||||
cache_sizes.block_tree_db = 2 << 20;
|
cache_sizes.block_tree_db = 2 << 20;
|
||||||
|
|
|
@ -1036,7 +1036,9 @@ bool AppInitParameterInteraction(const ArgsManager& args, bool use_syscall_sandb
|
||||||
if (const auto error{ApplyArgsManOptions(args, chainman_opts_dummy)}) {
|
if (const auto error{ApplyArgsManOptions(args, chainman_opts_dummy)}) {
|
||||||
return InitError(*error);
|
return InitError(*error);
|
||||||
}
|
}
|
||||||
node::BlockManager::Options blockman_opts_dummy{};
|
node::BlockManager::Options blockman_opts_dummy{
|
||||||
|
.chainparams = chainman_opts_dummy.chainparams,
|
||||||
|
};
|
||||||
if (const auto error{ApplyArgsManOptions(args, blockman_opts_dummy)}) {
|
if (const auto error{ApplyArgsManOptions(args, blockman_opts_dummy)}) {
|
||||||
return InitError(*error);
|
return InitError(*error);
|
||||||
}
|
}
|
||||||
|
@ -1439,7 +1441,9 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
|
||||||
};
|
};
|
||||||
Assert(!ApplyArgsManOptions(args, chainman_opts)); // no error can happen, already checked in AppInitParameterInteraction
|
Assert(!ApplyArgsManOptions(args, chainman_opts)); // no error can happen, already checked in AppInitParameterInteraction
|
||||||
|
|
||||||
node::BlockManager::Options blockman_opts{};
|
node::BlockManager::Options blockman_opts{
|
||||||
|
.chainparams = chainman_opts.chainparams,
|
||||||
|
};
|
||||||
Assert(!ApplyArgsManOptions(args, blockman_opts)); // no error can happen, already checked in AppInitParameterInteraction
|
Assert(!ApplyArgsManOptions(args, blockman_opts)); // no error can happen, already checked in AppInitParameterInteraction
|
||||||
|
|
||||||
// cache size calculations
|
// cache size calculations
|
||||||
|
|
|
@ -5,6 +5,8 @@
|
||||||
#ifndef BITCOIN_KERNEL_BLOCKMANAGER_OPTS_H
|
#ifndef BITCOIN_KERNEL_BLOCKMANAGER_OPTS_H
|
||||||
#define BITCOIN_KERNEL_BLOCKMANAGER_OPTS_H
|
#define BITCOIN_KERNEL_BLOCKMANAGER_OPTS_H
|
||||||
|
|
||||||
|
class CChainParams;
|
||||||
|
|
||||||
namespace kernel {
|
namespace kernel {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -12,6 +14,7 @@ namespace kernel {
|
||||||
* `BlockManager::Options` due to the using-declaration in `BlockManager`.
|
* `BlockManager::Options` due to the using-declaration in `BlockManager`.
|
||||||
*/
|
*/
|
||||||
struct BlockManagerOpts {
|
struct BlockManagerOpts {
|
||||||
|
const CChainParams& chainparams;
|
||||||
uint64_t prune_target{0};
|
uint64_t prune_target{0};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -255,7 +255,7 @@ CBlockIndex* BlockManager::InsertBlockIndex(const uint256& hash)
|
||||||
|
|
||||||
bool BlockManager::LoadBlockIndex(const Consensus::Params& consensus_params)
|
bool BlockManager::LoadBlockIndex(const Consensus::Params& consensus_params)
|
||||||
{
|
{
|
||||||
if (!m_block_tree_db->LoadBlockIndexGuts(consensus_params, [this](const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main) { return this->InsertBlockIndex(hash); })) {
|
if (!m_block_tree_db->LoadBlockIndexGuts(GetConsensus(), [this](const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main) { return this->InsertBlockIndex(hash); })) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -729,7 +729,7 @@ bool BlockManager::WriteUndoDataForBlock(const CBlockUndo& blockundo, BlockValid
|
||||||
if (!FindUndoPos(state, pindex->nFile, _pos, ::GetSerializeSize(blockundo, CLIENT_VERSION) + 40)) {
|
if (!FindUndoPos(state, pindex->nFile, _pos, ::GetSerializeSize(blockundo, CLIENT_VERSION) + 40)) {
|
||||||
return error("ConnectBlock(): FindUndoPos failed");
|
return error("ConnectBlock(): FindUndoPos failed");
|
||||||
}
|
}
|
||||||
if (!UndoWriteToDisk(blockundo, _pos, pindex->pprev->GetBlockHash(), chainparams.MessageStart())) {
|
if (!UndoWriteToDisk(blockundo, _pos, pindex->pprev->GetBlockHash(), GetParams().MessageStart())) {
|
||||||
return AbortNode(state, "Failed to write undo data");
|
return AbortNode(state, "Failed to write undo data");
|
||||||
}
|
}
|
||||||
// rev files are written in block height order, whereas blk files are written as blocks come in (often out of order)
|
// rev files are written in block height order, whereas blk files are written as blocks come in (often out of order)
|
||||||
|
@ -847,7 +847,7 @@ FlatFilePos BlockManager::SaveBlockToDisk(const CBlock& block, int nHeight, CCha
|
||||||
return FlatFilePos();
|
return FlatFilePos();
|
||||||
}
|
}
|
||||||
if (!position_known) {
|
if (!position_known) {
|
||||||
if (!WriteBlockToDisk(block, blockPos, chainparams.MessageStart())) {
|
if (!WriteBlockToDisk(block, blockPos, GetParams().MessageStart())) {
|
||||||
AbortNode("Failed to write block");
|
AbortNode("Failed to write block");
|
||||||
return FlatFilePos();
|
return FlatFilePos();
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
#include <attributes.h>
|
#include <attributes.h>
|
||||||
#include <chain.h>
|
#include <chain.h>
|
||||||
#include <kernel/blockmanager_opts.h>
|
#include <kernel/blockmanager_opts.h>
|
||||||
|
#include <kernel/chainparams.h>
|
||||||
#include <kernel/cs_main.h>
|
#include <kernel/cs_main.h>
|
||||||
#include <protocol.h>
|
#include <protocol.h>
|
||||||
#include <sync.h>
|
#include <sync.h>
|
||||||
|
@ -81,6 +82,8 @@ class BlockManager
|
||||||
friend ChainstateManager;
|
friend ChainstateManager;
|
||||||
|
|
||||||
private:
|
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
|
* Load the blocktree off disk and into memory. Populate certain metadata
|
||||||
* per index entry (nStatus, nChainWork, nTimeMax, etc.) as well as peripheral
|
* per index entry (nStatus, nChainWork, nTimeMax, etc.) as well as peripheral
|
||||||
|
|
|
@ -21,7 +21,10 @@ BOOST_FIXTURE_TEST_SUITE(blockmanager_tests, BasicTestingSetup)
|
||||||
BOOST_AUTO_TEST_CASE(blockmanager_find_block_pos)
|
BOOST_AUTO_TEST_CASE(blockmanager_find_block_pos)
|
||||||
{
|
{
|
||||||
const auto params {CreateChainParams(ArgsManager{}, CBaseChainParams::MAIN)};
|
const auto params {CreateChainParams(ArgsManager{}, CBaseChainParams::MAIN)};
|
||||||
BlockManager blockman{{}};
|
node::BlockManager::Options blockman_opts{
|
||||||
|
.chainparams = *params,
|
||||||
|
};
|
||||||
|
BlockManager blockman{blockman_opts};
|
||||||
CChain chain {};
|
CChain chain {};
|
||||||
// simulate adding a genesis block normally
|
// simulate adding a genesis block normally
|
||||||
BOOST_CHECK_EQUAL(blockman.SaveBlockToDisk(params->GenesisBlock(), 0, chain, *params, nullptr).nPos, BLOCK_SERIALIZATION_HEADER_SIZE);
|
BOOST_CHECK_EQUAL(blockman.SaveBlockToDisk(params->GenesisBlock(), 0, chain, *params, nullptr).nPos, BLOCK_SERIALIZATION_HEADER_SIZE);
|
||||||
|
|
|
@ -185,7 +185,10 @@ ChainTestingSetup::ChainTestingSetup(const std::string& chainName, const std::ve
|
||||||
.adjusted_time_callback = GetAdjustedTime,
|
.adjusted_time_callback = GetAdjustedTime,
|
||||||
.check_block_index = true,
|
.check_block_index = true,
|
||||||
};
|
};
|
||||||
m_node.chainman = std::make_unique<ChainstateManager>(chainman_opts, node::BlockManager::Options{});
|
node::BlockManager::Options blockman_opts{
|
||||||
|
.chainparams = chainman_opts.chainparams,
|
||||||
|
};
|
||||||
|
m_node.chainman = std::make_unique<ChainstateManager>(chainman_opts, blockman_opts);
|
||||||
m_node.chainman->m_blockman.m_block_tree_db = std::make_unique<CBlockTreeDB>(DBParams{
|
m_node.chainman->m_blockman.m_block_tree_db = std::make_unique<CBlockTreeDB>(DBParams{
|
||||||
.path = m_args.GetDataDirNet() / "blocks" / "index",
|
.path = m_args.GetDataDirNet() / "blocks" / "index",
|
||||||
.cache_bytes = static_cast<size_t>(m_cache_sizes.block_tree_db),
|
.cache_bytes = static_cast<size_t>(m_cache_sizes.block_tree_db),
|
||||||
|
|
|
@ -381,10 +381,13 @@ struct SnapshotTestSetup : TestChain100Setup {
|
||||||
.datadir = m_args.GetDataDirNet(),
|
.datadir = m_args.GetDataDirNet(),
|
||||||
.adjusted_time_callback = GetAdjustedTime,
|
.adjusted_time_callback = GetAdjustedTime,
|
||||||
};
|
};
|
||||||
|
node::BlockManager::Options blockman_opts{
|
||||||
|
.chainparams = chainman_opts.chainparams,
|
||||||
|
};
|
||||||
// For robustness, ensure the old manager is destroyed before creating a
|
// For robustness, ensure the old manager is destroyed before creating a
|
||||||
// new one.
|
// new one.
|
||||||
m_node.chainman.reset();
|
m_node.chainman.reset();
|
||||||
m_node.chainman = std::make_unique<ChainstateManager>(chainman_opts, node::BlockManager::Options{});
|
m_node.chainman = std::make_unique<ChainstateManager>(chainman_opts, blockman_opts);
|
||||||
}
|
}
|
||||||
return *Assert(m_node.chainman);
|
return *Assert(m_node.chainman);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue