Skip to content

Commit ccd4db7

Browse files
committed
Merge bitcoin/bitcoin#27570: refactor: Remove need to pass chainparams to BlockManager methods
fa5d7c3 Remove unused chainparams from BlockManager methods (MarcoFalke) fa3f74a Replace pindex pointer with block reference (MarcoFalke) facdb8b Add BlockManagerOpts::chainparams reference (MarcoFalke) Pull request description: Seems confusing to pass chainparams to each method individually, when the params can't change anyway for the whole lifetime of the block manager, and also must be equal to the ones used by the chainstate manager. Fix this issue by removing them from the methods and instead storing a reference once in a member field. ACKs for top commit: dergoegge: Code review ACK fa5d7c3 TheCharlatan: ACK fa5d7c3 Tree-SHA512: b44e2466b70a2a39a46625d618ce3173897ef30418db4efb9ff73d0eb2c082633204a5586c34b95f227e6711e64f19f12d5ac0f9f34692d40cb372e98389324b
2 parents 5d1014d + fa5d7c3 commit ccd4db7

9 files changed

+53
-31
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: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,9 @@ CBlockIndex* BlockManager::InsertBlockIndex(const uint256& hash)
253253
return pindex;
254254
}
255255

256-
bool BlockManager::LoadBlockIndex(const Consensus::Params& consensus_params)
256+
bool BlockManager::LoadBlockIndex()
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

@@ -318,9 +318,9 @@ bool BlockManager::WriteBlockIndexDB()
318318
return true;
319319
}
320320

321-
bool BlockManager::LoadBlockIndexDB(const Consensus::Params& consensus_params)
321+
bool BlockManager::LoadBlockIndexDB()
322322
{
323-
if (!LoadBlockIndex(consensus_params)) {
323+
if (!LoadBlockIndex()) {
324324
return false;
325325
}
326326

@@ -720,31 +720,31 @@ static bool WriteBlockToDisk(const CBlock& block, FlatFilePos& pos, const CMessa
720720
return true;
721721
}
722722

723-
bool BlockManager::WriteUndoDataForBlock(const CBlockUndo& blockundo, BlockValidationState& state, CBlockIndex* pindex, const CChainParams& chainparams)
723+
bool BlockManager::WriteUndoDataForBlock(const CBlockUndo& blockundo, BlockValidationState& state, CBlockIndex& block)
724724
{
725725
AssertLockHeld(::cs_main);
726726
// Write undo information to disk
727-
if (pindex->GetUndoPos().IsNull()) {
727+
if (block.GetUndoPos().IsNull()) {
728728
FlatFilePos _pos;
729-
if (!FindUndoPos(state, pindex->nFile, _pos, ::GetSerializeSize(blockundo, CLIENT_VERSION) + 40)) {
729+
if (!FindUndoPos(state, block.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, block.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)
736736
// we want to flush the rev (undo) file once we've written the last block, which is indicated by the last height
737737
// in the block file info as below; note that this does not catch the case where the undo writes are keeping up
738738
// with the block writes (usually when a synced up node is getting newly mined blocks) -- this case is caught in
739739
// the FindBlockPos function
740-
if (_pos.nFile < m_last_blockfile && static_cast<uint32_t>(pindex->nHeight) == m_blockfile_info[_pos.nFile].nHeightLast) {
740+
if (_pos.nFile < m_last_blockfile && static_cast<uint32_t>(block.nHeight) == m_blockfile_info[_pos.nFile].nHeightLast) {
741741
FlushUndoFile(_pos.nFile, true);
742742
}
743743

744744
// update nUndoPos in block index
745-
pindex->nUndoPos = _pos.nPos;
746-
pindex->nStatus |= BLOCK_HAVE_UNDO;
747-
m_dirty_blockindex.insert(pindex);
745+
block.nUndoPos = _pos.nPos;
746+
block.nStatus |= BLOCK_HAVE_UNDO;
747+
m_dirty_blockindex.insert(&block);
748748
}
749749

750750
return true;
@@ -829,7 +829,7 @@ bool ReadRawBlockFromDisk(std::vector<uint8_t>& block, const FlatFilePos& pos, c
829829
return true;
830830
}
831831

832-
FlatFilePos BlockManager::SaveBlockToDisk(const CBlock& block, int nHeight, CChain& active_chain, const CChainParams& chainparams, const FlatFilePos* dbp)
832+
FlatFilePos BlockManager::SaveBlockToDisk(const CBlock& block, int nHeight, CChain& active_chain, const FlatFilePos* dbp)
833833
{
834834
unsigned int nBlockSize = ::GetSerializeSize(block, CLIENT_VERSION);
835835
FlatFilePos blockPos;
@@ -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: 7 additions & 4 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,12 +82,14 @@ 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
8790
* collections like m_dirty_blockindex.
8891
*/
89-
bool LoadBlockIndex(const Consensus::Params& consensus_params)
92+
bool LoadBlockIndex()
9093
EXCLUSIVE_LOCKS_REQUIRED(cs_main);
9194
void FlushBlockFile(bool fFinalize = false, bool finalize_undo = false);
9295
void FlushUndoFile(int block_file, bool finalize = false);
@@ -162,7 +165,7 @@ class BlockManager
162165
std::unique_ptr<CBlockTreeDB> m_block_tree_db GUARDED_BY(::cs_main);
163166

164167
bool WriteBlockIndexDB() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
165-
bool LoadBlockIndexDB(const Consensus::Params& consensus_params) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
168+
bool LoadBlockIndexDB() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
166169

167170
/**
168171
* Remove any pruned block & undo files that are still on disk.
@@ -184,11 +187,11 @@ class BlockManager
184187
/** Get block file info entry for one block file */
185188
CBlockFileInfo* GetBlockFileInfo(size_t n);
186189

187-
bool WriteUndoDataForBlock(const CBlockUndo& blockundo, BlockValidationState& state, CBlockIndex* pindex, const CChainParams& chainparams)
190+
bool WriteUndoDataForBlock(const CBlockUndo& blockundo, BlockValidationState& state, CBlockIndex& block)
188191
EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
189192

190193
/** Store block on disk. If dbp is not nullptr, then it provides the known position of the block within a block file on disk. */
191-
FlatFilePos SaveBlockToDisk(const CBlock& block, int nHeight, CChain& active_chain, const CChainParams& chainparams, const FlatFilePos* dbp);
194+
FlatFilePos SaveBlockToDisk(const CBlock& block, int nHeight, CChain& active_chain, const FlatFilePos* dbp);
192195

193196
/** Whether running in -prune mode. */
194197
[[nodiscard]] bool IsPruneMode() const { return m_prune_mode; }

src/test/blockmanager_tests.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,26 @@ 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
27-
BOOST_CHECK_EQUAL(blockman.SaveBlockToDisk(params->GenesisBlock(), 0, chain, *params, nullptr).nPos, BLOCK_SERIALIZATION_HEADER_SIZE);
30+
BOOST_CHECK_EQUAL(blockman.SaveBlockToDisk(params->GenesisBlock(), 0, chain, nullptr).nPos, BLOCK_SERIALIZATION_HEADER_SIZE);
2831
// simulate what happens during reindex
2932
// simulate a well-formed genesis block being found at offset 8 in the blk00000.dat file
3033
// the block is found at offset 8 because there is an 8 byte serialization header
3134
// consisting of 4 magic bytes + 4 length bytes before each block in a well-formed blk file.
3235
FlatFilePos pos{0, BLOCK_SERIALIZATION_HEADER_SIZE};
33-
BOOST_CHECK_EQUAL(blockman.SaveBlockToDisk(params->GenesisBlock(), 0, chain, *params, &pos).nPos, BLOCK_SERIALIZATION_HEADER_SIZE);
36+
BOOST_CHECK_EQUAL(blockman.SaveBlockToDisk(params->GenesisBlock(), 0, chain, &pos).nPos, BLOCK_SERIALIZATION_HEADER_SIZE);
3437
// now simulate what happens after reindex for the first new block processed
3538
// the actual block contents don't matter, just that it's a block.
3639
// verify that the write position is at offset 0x12d.
3740
// this is a check to make sure that https://github.com/bitcoin/bitcoin/issues/21379 does not recur
3841
// 8 bytes (for serialization header) + 285 (for serialized genesis block) = 293
3942
// add another 8 bytes for the second block's serialization header and we get 293 + 8 = 301
40-
FlatFilePos actual{blockman.SaveBlockToDisk(params->GenesisBlock(), 1, chain, *params, nullptr)};
43+
FlatFilePos actual{blockman.SaveBlockToDisk(params->GenesisBlock(), 1, chain, nullptr)};
4144
BOOST_CHECK_EQUAL(actual.nPos, BLOCK_SERIALIZATION_HEADER_SIZE + ::GetSerializeSize(params->GenesisBlock(), CLIENT_VERSION) + BLOCK_SERIALIZATION_HEADER_SIZE);
4245
}
4346

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
}

src/validation.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2374,7 +2374,7 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
23742374
if (fJustCheck)
23752375
return true;
23762376

2377-
if (!m_blockman.WriteUndoDataForBlock(blockundo, state, pindex, params)) {
2377+
if (!m_blockman.WriteUndoDataForBlock(blockundo, state, *pindex)) {
23782378
return false;
23792379
}
23802380

@@ -4000,7 +4000,7 @@ bool Chainstate::AcceptBlock(const std::shared_ptr<const CBlock>& pblock, BlockV
40004000
// Write block to history file
40014001
if (fNewBlock) *fNewBlock = true;
40024002
try {
4003-
FlatFilePos blockPos{m_blockman.SaveBlockToDisk(block, pindex->nHeight, m_chain, params, dbp)};
4003+
FlatFilePos blockPos{m_blockman.SaveBlockToDisk(block, pindex->nHeight, m_chain, dbp)};
40044004
if (blockPos.IsNull()) {
40054005
state.Error(strprintf("%s: Failed to find position to write new block to disk", __func__));
40064006
return false;
@@ -4418,7 +4418,7 @@ bool ChainstateManager::LoadBlockIndex()
44184418
// Load block index from databases
44194419
bool needs_init = fReindex;
44204420
if (!fReindex) {
4421-
bool ret = m_blockman.LoadBlockIndexDB(GetConsensus());
4421+
bool ret{m_blockman.LoadBlockIndexDB()};
44224422
if (!ret) return false;
44234423

44244424
m_blockman.ScanAndUnlinkAlreadyPrunedFiles();
@@ -4522,7 +4522,7 @@ bool Chainstate::LoadGenesisBlock()
45224522

45234523
try {
45244524
const CBlock& block = params.GenesisBlock();
4525-
FlatFilePos blockPos{m_blockman.SaveBlockToDisk(block, 0, m_chain, params, nullptr)};
4525+
FlatFilePos blockPos{m_blockman.SaveBlockToDisk(block, 0, m_chain, nullptr)};
45264526
if (blockPos.IsNull()) {
45274527
return error("%s: writing genesis block to disk failed", __func__);
45284528
}

0 commit comments

Comments
 (0)