Skip to content

Commit 6d79e83

Browse files
authored
Merge pull request #13 from thepastaclaw/bp-ccd4db7d62
backport: bitcoin#27570 — refactor: Remove need to pass chainparams to BlockManager methods
2 parents 6a29bc5 + bb6c2cf commit 6d79e83

7 files changed

Lines changed: 61 additions & 25 deletions

File tree

src/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ BITCOIN_CORE_H = \
270270
instantsend/lock.h \
271271
instantsend/net_instantsend.h \
272272
instantsend/signing.h \
273+
kernel/blockmanager_opts.h \
273274
kernel/coinstats.h \
274275
key.h \
275276
key_io.h \

src/kernel/blockmanager_opts.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) 2022 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_KERNEL_BLOCKMANAGER_OPTS_H
6+
#define BITCOIN_KERNEL_BLOCKMANAGER_OPTS_H
7+
8+
class CChainParams;
9+
10+
namespace kernel {
11+
12+
/**
13+
* An options struct for `BlockManager`, more ergonomically referred to as
14+
* `BlockManager::Options` due to the using-declaration in `BlockManager`.
15+
*/
16+
struct BlockManagerOpts {
17+
const CChainParams& chainparams;
18+
};
19+
20+
} // namespace kernel
21+
22+
#endif // BITCOIN_KERNEL_BLOCKMANAGER_OPTS_H

src/node/blockstorage.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,9 @@ CBlockIndex* BlockManager::InsertBlockIndex(const uint256& hash)
269269
return pindex;
270270
}
271271

272-
bool BlockManager::LoadBlockIndex(const Consensus::Params& consensus_params)
272+
bool BlockManager::LoadBlockIndex()
273273
{
274-
if (!m_block_tree_db->LoadBlockIndexGuts(consensus_params, [this](const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main) { return this->InsertBlockIndex(hash); })) {
274+
if (!m_block_tree_db->LoadBlockIndexGuts(GetConsensus(), [this](const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main) { return this->InsertBlockIndex(hash); })) {
275275
return false;
276276
}
277277

@@ -348,7 +348,7 @@ bool BlockManager::WriteBlockIndexDB()
348348

349349
bool BlockManager::LoadBlockIndexDB()
350350
{
351-
if (!LoadBlockIndex(::Params().GetConsensus())) {
351+
if (!LoadBlockIndex()) {
352352
return false;
353353
}
354354

@@ -726,31 +726,31 @@ static bool WriteBlockToDisk(const CBlock& block, FlatFilePos& pos, const CMessa
726726
return true;
727727
}
728728

729-
bool BlockManager::WriteUndoDataForBlock(const CBlockUndo& blockundo, BlockValidationState& state, CBlockIndex* pindex, const CChainParams& chainparams)
729+
bool BlockManager::WriteUndoDataForBlock(const CBlockUndo& blockundo, BlockValidationState& state, CBlockIndex& block)
730730
{
731731
AssertLockHeld(::cs_main);
732732
// Write undo information to disk
733-
if (pindex->GetUndoPos().IsNull()) {
733+
if (block.GetUndoPos().IsNull()) {
734734
FlatFilePos _pos;
735-
if (!FindUndoPos(state, pindex->nFile, _pos, ::GetSerializeSize(blockundo, CLIENT_VERSION) + 40)) {
735+
if (!FindUndoPos(state, block.nFile, _pos, ::GetSerializeSize(blockundo, CLIENT_VERSION) + 40)) {
736736
return error("ConnectBlock(): FindUndoPos failed");
737737
}
738-
if (!UndoWriteToDisk(blockundo, _pos, pindex->pprev->GetBlockHash(), chainparams.MessageStart())) {
738+
if (!UndoWriteToDisk(blockundo, _pos, block.pprev->GetBlockHash(), GetParams().MessageStart())) {
739739
return AbortNode(state, "Failed to write undo data");
740740
}
741741
// rev files are written in block height order, whereas blk files are written as blocks come in (often out of order)
742742
// we want to flush the rev (undo) file once we've written the last block, which is indicated by the last height
743743
// in the block file info as below; note that this does not catch the case where the undo writes are keeping up
744744
// with the block writes (usually when a synced up node is getting newly mined blocks) -- this case is caught in
745745
// the FindBlockPos function
746-
if (_pos.nFile < m_last_blockfile && static_cast<uint32_t>(pindex->nHeight) == m_blockfile_info[_pos.nFile].nHeightLast) {
746+
if (_pos.nFile < m_last_blockfile && static_cast<uint32_t>(block.nHeight) == m_blockfile_info[_pos.nFile].nHeightLast) {
747747
FlushUndoFile(_pos.nFile, true);
748748
}
749749

750750
// update nUndoPos in block index
751-
pindex->nUndoPos = _pos.nPos;
752-
pindex->nStatus |= BLOCK_HAVE_UNDO;
753-
m_dirty_blockindex.insert(pindex);
751+
block.nUndoPos = _pos.nPos;
752+
block.nStatus |= BLOCK_HAVE_UNDO;
753+
m_dirty_blockindex.insert(&block);
754754
}
755755

756756
return true;
@@ -795,7 +795,7 @@ bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex, const Consensus
795795
return true;
796796
}
797797

798-
FlatFilePos BlockManager::SaveBlockToDisk(const CBlock& block, int nHeight, CChain& active_chain, const CChainParams& chainparams, const FlatFilePos* dbp)
798+
FlatFilePos BlockManager::SaveBlockToDisk(const CBlock& block, int nHeight, CChain& active_chain, const FlatFilePos* dbp)
799799
{
800800
unsigned int nBlockSize = ::GetSerializeSize(block, CLIENT_VERSION);
801801
FlatFilePos blockPos;
@@ -813,7 +813,7 @@ FlatFilePos BlockManager::SaveBlockToDisk(const CBlock& block, int nHeight, CCha
813813
return FlatFilePos();
814814
}
815815
if (!position_known) {
816-
if (!WriteBlockToDisk(block, blockPos, chainparams.MessageStart())) {
816+
if (!WriteBlockToDisk(block, blockPos, GetParams().MessageStart())) {
817817
AbortNode("Failed to write block");
818818
return FlatFilePos();
819819
}

src/node/blockstorage.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
#include <attributes.h>
99
#include <chain.h>
10+
#include <chainparams.h>
1011
#include <fs.h>
12+
#include <kernel/blockmanager_opts.h>
1113
#include <protocol.h>
1214
#include <sync.h>
1315
#include <txdb.h>
@@ -96,12 +98,16 @@ class BlockManager
9698
friend ChainstateManager;
9799

98100
private:
101+
const kernel::BlockManagerOpts m_opts;
102+
103+
const CChainParams& GetParams() const { return m_opts.chainparams; }
104+
const Consensus::Params& GetConsensus() const { return m_opts.chainparams.GetConsensus(); }
99105
/**
100106
* Load the blocktree off disk and into memory. Populate certain metadata
101107
* per index entry (nStatus, nChainWork, nTimeMax, etc.) as well as peripheral
102108
* collections like m_dirty_blockindex.
103109
*/
104-
bool LoadBlockIndex(const Consensus::Params& consensus_params)
110+
bool LoadBlockIndex()
105111
EXCLUSIVE_LOCKS_REQUIRED(cs_main);
106112
void FlushBlockFile(bool fFinalize = false, bool finalize_undo = false);
107113
void FlushUndoFile(int block_file, bool finalize = false);
@@ -152,6 +158,10 @@ class BlockManager
152158
std::unordered_map<std::string, PruneLockInfo> m_prune_locks GUARDED_BY(::cs_main);
153159

154160
public:
161+
using Options = kernel::BlockManagerOpts;
162+
163+
explicit BlockManager(Options opts) : m_opts{opts} {}
164+
155165
BlockMap m_block_index GUARDED_BY(cs_main);
156166
PrevBlockMap m_prev_block_index GUARDED_BY(cs_main);
157167

@@ -183,11 +193,11 @@ class BlockManager
183193
/** Get block file info entry for one block file */
184194
CBlockFileInfo* GetBlockFileInfo(size_t n);
185195

186-
bool WriteUndoDataForBlock(const CBlockUndo& blockundo, BlockValidationState& state, CBlockIndex* pindex, const CChainParams& chainparams)
196+
bool WriteUndoDataForBlock(const CBlockUndo& blockundo, BlockValidationState& state, CBlockIndex& block)
187197
EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
188198

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

192202
/** Calculate the amount of disk space the block & undo files currently use */
193203
uint64_t CalculateCurrentUsage();

src/test/blockmanager_tests.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,26 @@ BOOST_FIXTURE_TEST_SUITE(blockmanager_tests, BasicTestingSetup)
1919
BOOST_AUTO_TEST_CASE(blockmanager_find_block_pos)
2020
{
2121
const auto params {CreateChainParams(ArgsManager{}, CBaseChainParams::MAIN)};
22-
BlockManager blockman {};
22+
node::BlockManager::Options blockman_opts{
23+
.chainparams = *params,
24+
};
25+
BlockManager blockman{blockman_opts};
2326
CChain chain {};
2427
// simulate adding a genesis block normally
25-
BOOST_CHECK_EQUAL(blockman.SaveBlockToDisk(params->GenesisBlock(), 0, chain, *params, nullptr).nPos, BLOCK_SERIALIZATION_HEADER_SIZE);
28+
BOOST_CHECK_EQUAL(blockman.SaveBlockToDisk(params->GenesisBlock(), 0, chain, nullptr).nPos, BLOCK_SERIALIZATION_HEADER_SIZE);
2629
// simulate what happens during reindex
2730
// simulate a well-formed genesis block being found at offset 8 in the blk00000.dat file
2831
// the block is found at offset 8 because there is an 8 byte serialization header
2932
// consisting of 4 magic bytes + 4 length bytes before each block in a well-formed blk file.
3033
FlatFilePos pos{0, BLOCK_SERIALIZATION_HEADER_SIZE};
31-
BOOST_CHECK_EQUAL(blockman.SaveBlockToDisk(params->GenesisBlock(), 0, chain, *params, &pos).nPos, BLOCK_SERIALIZATION_HEADER_SIZE);
34+
BOOST_CHECK_EQUAL(blockman.SaveBlockToDisk(params->GenesisBlock(), 0, chain, &pos).nPos, BLOCK_SERIALIZATION_HEADER_SIZE);
3235
// now simulate what happens after reindex for the first new block processed
3336
// the actual block contents don't matter, just that it's a block.
3437
// verify that the write position is at offset 0x12d.
3538
// this is a check to make sure that https://github.com/bitcoin/bitcoin/issues/21379 does not recur
3639
// 8 bytes (for serialization header) + 285 (for serialized genesis block) = 293
3740
// add another 8 bytes for the second block's serialization header and we get 293 + 8 = 301
38-
FlatFilePos actual{blockman.SaveBlockToDisk(params->GenesisBlock(), 1, chain, *params, nullptr)};
41+
FlatFilePos actual{blockman.SaveBlockToDisk(params->GenesisBlock(), 1, chain, nullptr)};
3942
BOOST_CHECK_EQUAL(actual.nPos, BLOCK_SERIALIZATION_HEADER_SIZE + ::GetSerializeSize(params->GenesisBlock(), CLIENT_VERSION) + BLOCK_SERIALIZATION_HEADER_SIZE);
4043
}
4144

src/validation.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2677,7 +2677,7 @@ bool CChainState::ConnectBlock(const CBlock& block, BlockValidationState& state,
26772677

26782678
int64_t nTime6 = GetTimeMicros();
26792679

2680-
if (!m_blockman.WriteUndoDataForBlock(blockundo, state, pindex, m_params)) {
2680+
if (!m_blockman.WriteUndoDataForBlock(blockundo, state, *pindex)) {
26812681
return false;
26822682
}
26832683

@@ -4421,7 +4421,7 @@ bool CChainState::AcceptBlock(const std::shared_ptr<const CBlock>& pblock, Block
44214421
// Write block to history file
44224422
if (fNewBlock) *fNewBlock = true;
44234423
try {
4424-
FlatFilePos blockPos{m_blockman.SaveBlockToDisk(block, pindex->nHeight, m_chain, m_params, dbp)};
4424+
FlatFilePos blockPos{m_blockman.SaveBlockToDisk(block, pindex->nHeight, m_chain, dbp)};
44254425
if (blockPos.IsNull()) {
44264426
state.Error(strprintf("%s: Failed to find position to write new block to disk", __func__));
44274427
return false;
@@ -4930,7 +4930,7 @@ bool ChainstateManager::LoadBlockIndex()
49304930
// Load block index from databases
49314931
bool needs_init = fReindex;
49324932
if (!fReindex) {
4933-
bool ret = m_blockman.LoadBlockIndexDB();
4933+
bool ret{m_blockman.LoadBlockIndexDB()};
49344934
if (!ret) return false;
49354935

49364936
std::vector<CBlockIndex*> vSortedByHeight{m_blockman.GetAllBlockIndices()};
@@ -5034,7 +5034,7 @@ void ChainstateManager::InitAdditionalIndexes()
50345034

50355035
bool CChainState::AddGenesisBlock(const CBlock& block, BlockValidationState& state)
50365036
{
5037-
FlatFilePos blockPos{m_blockman.SaveBlockToDisk(block, 0, m_chain, m_params, nullptr)};
5037+
FlatFilePos blockPos{m_blockman.SaveBlockToDisk(block, 0, m_chain, nullptr)};
50385038
if (blockPos.IsNull()) {
50395039
return error("%s: writing genesis block to disk failed (%s)", __func__, state.ToString());
50405040
}

src/validation.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,7 @@ class ChainstateManager
919919
friend CChainState;
920920

921921
public:
922-
explicit ChainstateManager(const CChainParams& chainparams) : m_chainparams{chainparams} { }
922+
explicit ChainstateManager(const CChainParams& chainparams) : m_chainparams{chainparams}, m_blockman{{chainparams}} { }
923923

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

0 commit comments

Comments
 (0)