Skip to content

Commit facdb8b

Browse files
author
MarcoFalke
committed
Add BlockManagerOpts::chainparams reference
and use it in blockstorage.cpp
1 parent 6c7ebcc commit facdb8b

File tree

8 files changed

+31
-9
lines changed

8 files changed

+31
-9
lines changed

src/bitcoin-chainstate.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ int main(int argc, char* argv[])
8686
.datadir = gArgs.GetDataDirNet(),
8787
.adjusted_time_callback = NodeClock::now,
8888
};
89-
ChainstateManager chainman{chainman_opts, {}};
89+
const node::BlockManager::Options blockman_opts{
90+
.chainparams = chainman_opts.chainparams,
91+
};
92+
ChainstateManager chainman{chainman_opts, blockman_opts};
9093

9194
node::CacheSizes cache_sizes;
9295
cache_sizes.block_tree_db = 2 << 20;

src/init.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,7 +1036,9 @@ bool AppInitParameterInteraction(const ArgsManager& args, bool use_syscall_sandb
10361036
if (const auto error{ApplyArgsManOptions(args, chainman_opts_dummy)}) {
10371037
return InitError(*error);
10381038
}
1039-
node::BlockManager::Options blockman_opts_dummy{};
1039+
node::BlockManager::Options blockman_opts_dummy{
1040+
.chainparams = chainman_opts_dummy.chainparams,
1041+
};
10401042
if (const auto error{ApplyArgsManOptions(args, blockman_opts_dummy)}) {
10411043
return InitError(*error);
10421044
}
@@ -1439,7 +1441,9 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
14391441
};
14401442
Assert(!ApplyArgsManOptions(args, chainman_opts)); // no error can happen, already checked in AppInitParameterInteraction
14411443

1442-
node::BlockManager::Options blockman_opts{};
1444+
node::BlockManager::Options blockman_opts{
1445+
.chainparams = chainman_opts.chainparams,
1446+
};
14431447
Assert(!ApplyArgsManOptions(args, blockman_opts)); // no error can happen, already checked in AppInitParameterInteraction
14441448

14451449
// cache size calculations

src/kernel/blockmanager_opts.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
55
#ifndef BITCOIN_KERNEL_BLOCKMANAGER_OPTS_H
66
#define BITCOIN_KERNEL_BLOCKMANAGER_OPTS_H
77

8+
class CChainParams;
9+
810
namespace kernel {
911

1012
/**
1113
* An options struct for `BlockManager`, more ergonomically referred to as
1214
* `BlockManager::Options` due to the using-declaration in `BlockManager`.
1315
*/
1416
struct BlockManagerOpts {
17+
const CChainParams& chainparams;
1518
uint64_t prune_target{0};
1619
};
1720

src/node/blockstorage.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ CBlockIndex* BlockManager::InsertBlockIndex(const uint256& hash)
255255

256256
bool BlockManager::LoadBlockIndex(const Consensus::Params& consensus_params)
257257
{
258-
if (!m_block_tree_db->LoadBlockIndexGuts(consensus_params, [this](const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main) { return this->InsertBlockIndex(hash); })) {
258+
if (!m_block_tree_db->LoadBlockIndexGuts(GetConsensus(), [this](const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main) { return this->InsertBlockIndex(hash); })) {
259259
return false;
260260
}
261261

@@ -729,7 +729,7 @@ bool BlockManager::WriteUndoDataForBlock(const CBlockUndo& blockundo, BlockValid
729729
if (!FindUndoPos(state, pindex->nFile, _pos, ::GetSerializeSize(blockundo, CLIENT_VERSION) + 40)) {
730730
return error("ConnectBlock(): FindUndoPos failed");
731731
}
732-
if (!UndoWriteToDisk(blockundo, _pos, pindex->pprev->GetBlockHash(), chainparams.MessageStart())) {
732+
if (!UndoWriteToDisk(blockundo, _pos, pindex->pprev->GetBlockHash(), GetParams().MessageStart())) {
733733
return AbortNode(state, "Failed to write undo data");
734734
}
735735
// 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
847847
return FlatFilePos();
848848
}
849849
if (!position_known) {
850-
if (!WriteBlockToDisk(block, blockPos, chainparams.MessageStart())) {
850+
if (!WriteBlockToDisk(block, blockPos, GetParams().MessageStart())) {
851851
AbortNode("Failed to write block");
852852
return FlatFilePos();
853853
}

src/node/blockstorage.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <attributes.h>
99
#include <chain.h>
1010
#include <kernel/blockmanager_opts.h>
11+
#include <kernel/chainparams.h>
1112
#include <kernel/cs_main.h>
1213
#include <protocol.h>
1314
#include <sync.h>
@@ -81,6 +82,8 @@ class BlockManager
8182
friend ChainstateManager;
8283

8384
private:
85+
const CChainParams& GetParams() const { return m_opts.chainparams; }
86+
const Consensus::Params& GetConsensus() const { return m_opts.chainparams.GetConsensus(); }
8487
/**
8588
* Load the blocktree off disk and into memory. Populate certain metadata
8689
* per index entry (nStatus, nChainWork, nTimeMax, etc.) as well as peripheral

src/test/blockmanager_tests.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ BOOST_FIXTURE_TEST_SUITE(blockmanager_tests, BasicTestingSetup)
2121
BOOST_AUTO_TEST_CASE(blockmanager_find_block_pos)
2222
{
2323
const auto params {CreateChainParams(ArgsManager{}, CBaseChainParams::MAIN)};
24-
BlockManager blockman{{}};
24+
node::BlockManager::Options blockman_opts{
25+
.chainparams = *params,
26+
};
27+
BlockManager blockman{blockman_opts};
2528
CChain chain {};
2629
// simulate adding a genesis block normally
2730
BOOST_CHECK_EQUAL(blockman.SaveBlockToDisk(params->GenesisBlock(), 0, chain, *params, nullptr).nPos, BLOCK_SERIALIZATION_HEADER_SIZE);

src/test/util/setup_common.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,10 @@ ChainTestingSetup::ChainTestingSetup(const std::string& chainName, const std::ve
185185
.adjusted_time_callback = GetAdjustedTime,
186186
.check_block_index = true,
187187
};
188-
m_node.chainman = std::make_unique<ChainstateManager>(chainman_opts, node::BlockManager::Options{});
188+
node::BlockManager::Options blockman_opts{
189+
.chainparams = chainman_opts.chainparams,
190+
};
191+
m_node.chainman = std::make_unique<ChainstateManager>(chainman_opts, blockman_opts);
189192
m_node.chainman->m_blockman.m_block_tree_db = std::make_unique<CBlockTreeDB>(DBParams{
190193
.path = m_args.GetDataDirNet() / "blocks" / "index",
191194
.cache_bytes = static_cast<size_t>(m_cache_sizes.block_tree_db),

src/test/validation_chainstatemanager_tests.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,10 +381,13 @@ struct SnapshotTestSetup : TestChain100Setup {
381381
.datadir = m_args.GetDataDirNet(),
382382
.adjusted_time_callback = GetAdjustedTime,
383383
};
384+
node::BlockManager::Options blockman_opts{
385+
.chainparams = chainman_opts.chainparams,
386+
};
384387
// For robustness, ensure the old manager is destroyed before creating a
385388
// new one.
386389
m_node.chainman.reset();
387-
m_node.chainman = std::make_unique<ChainstateManager>(chainman_opts, node::BlockManager::Options{});
390+
m_node.chainman = std::make_unique<ChainstateManager>(chainman_opts, blockman_opts);
388391
}
389392
return *Assert(m_node.chainman);
390393
}

0 commit comments

Comments
 (0)