Skip to content

Commit f0bb102

Browse files
committed
refactor: Move functions to BlockManager methods
This is a commit in preparation for the next few commits. The functions are moved to methods to avoid their re-declaration for the purpose of passing in BlockManager options. The functions that were now moved into the BlockManager should no longer use the params as an argument, but instead use the member variable. In the moved ReadBlockFromDisk and UndoReadFromDisk, change the function signature to accept a reference to a CBlockIndex instead of a raw pointer. The pointer is expected to be non-null, so reflect that in the type. To allow for the move of functions to BlockManager methods all call sites require an instantiated BlockManager, or a callback to one.
1 parent cfbb212 commit f0bb102

22 files changed

+117
-134
lines changed

src/index/base.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
#include <string>
2424
#include <utility>
2525

26-
using node::ReadBlockFromDisk;
27-
2826
constexpr uint8_t DB_BEST_BLOCK{'B'};
2927

3028
constexpr auto SYNC_LOG_INTERVAL{30s};
@@ -159,8 +157,6 @@ void BaseIndex::ThreadSync()
159157
SetSyscallSandboxPolicy(SyscallSandboxPolicy::TX_INDEX);
160158
const CBlockIndex* pindex = m_best_block_index.load();
161159
if (!m_synced) {
162-
auto& consensus_params = Params().GetConsensus();
163-
164160
std::chrono::steady_clock::time_point last_log_time{0s};
165161
std::chrono::steady_clock::time_point last_locator_write_time{0s};
166162
while (true) {
@@ -207,7 +203,7 @@ void BaseIndex::ThreadSync()
207203

208204
CBlock block;
209205
interfaces::BlockInfo block_info = kernel::MakeBlockInfo(pindex);
210-
if (!ReadBlockFromDisk(block, pindex, consensus_params)) {
206+
if (!m_chainstate->m_blockman.ReadBlockFromDisk(block, *pindex)) {
211207
FatalError("%s: Failed to read block %s from disk",
212208
__func__, pindex->GetBlockHash().ToString());
213209
return;

src/index/blockfilterindex.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
#include <util/fs_helpers.h>
1313
#include <validation.h>
1414

15-
using node::UndoReadFromDisk;
16-
1715
/* The index database stores three items for each block: the disk location of the encoded filter,
1816
* its dSHA256 hash, and the header. Those belonging to blocks on the active chain are indexed by
1917
* height, and those belonging to blocks that have been reorganized out of the active chain are
@@ -223,7 +221,7 @@ bool BlockFilterIndex::CustomAppend(const interfaces::BlockInfo& block)
223221
// pindex variable gives indexing code access to node internals. It
224222
// will be removed in upcoming commit
225223
const CBlockIndex* pindex = WITH_LOCK(cs_main, return m_chainstate->m_blockman.LookupBlockIndex(block.hash));
226-
if (!UndoReadFromDisk(block_undo, pindex)) {
224+
if (!m_chainstate->m_blockman.UndoReadFromDisk(block_undo, *pindex)) {
227225
return false;
228226
}
229227

src/index/coinstatsindex.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ using kernel::CCoinsStats;
1919
using kernel::GetBogoSize;
2020
using kernel::TxOutSer;
2121

22-
using node::ReadBlockFromDisk;
23-
using node::UndoReadFromDisk;
24-
2522
static constexpr uint8_t DB_BLOCK_HASH{'s'};
2623
static constexpr uint8_t DB_BLOCK_HEIGHT{'t'};
2724
static constexpr uint8_t DB_MUHASH{'M'};
@@ -125,7 +122,7 @@ bool CoinStatsIndex::CustomAppend(const interfaces::BlockInfo& block)
125122
// pindex variable gives indexing code access to node internals. It
126123
// will be removed in upcoming commit
127124
const CBlockIndex* pindex = WITH_LOCK(cs_main, return m_chainstate->m_blockman.LookupBlockIndex(block.hash));
128-
if (!UndoReadFromDisk(block_undo, pindex)) {
125+
if (!m_chainstate->m_blockman.UndoReadFromDisk(block_undo, *pindex)) {
129126
return false;
130127
}
131128

@@ -282,12 +279,11 @@ bool CoinStatsIndex::CustomRewind(const interfaces::BlockKey& current_tip, const
282279
LOCK(cs_main);
283280
const CBlockIndex* iter_tip{m_chainstate->m_blockman.LookupBlockIndex(current_tip.hash)};
284281
const CBlockIndex* new_tip_index{m_chainstate->m_blockman.LookupBlockIndex(new_tip.hash)};
285-
const auto& consensus_params{Params().GetConsensus()};
286282

287283
do {
288284
CBlock block;
289285

290-
if (!ReadBlockFromDisk(block, iter_tip, consensus_params)) {
286+
if (!m_chainstate->m_blockman.ReadBlockFromDisk(block, *iter_tip)) {
291287
return error("%s: Failed to read block %s from disk",
292288
__func__, iter_tip->GetBlockHash().ToString());
293289
}
@@ -409,7 +405,7 @@ bool CoinStatsIndex::ReverseBlock(const CBlock& block, const CBlockIndex* pindex
409405

410406
// Ignore genesis block
411407
if (pindex->nHeight > 0) {
412-
if (!UndoReadFromDisk(block_undo, pindex)) {
408+
if (!m_chainstate->m_blockman.UndoReadFromDisk(block_undo, *pindex)) {
413409
return false;
414410
}
415411

src/index/txindex.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
#include <node/blockstorage.h>
1111
#include <validation.h>
1212

13-
using node::OpenBlockFile;
14-
1513
constexpr uint8_t DB_TXINDEX{'t'};
1614

1715
std::unique_ptr<TxIndex> g_txindex;
@@ -80,7 +78,7 @@ bool TxIndex::FindTx(const uint256& tx_hash, uint256& block_hash, CTransactionRe
8078
return false;
8179
}
8280

83-
CAutoFile file(OpenBlockFile(postx, true), SER_DISK, CLIENT_VERSION);
81+
CAutoFile file(m_chainstate->m_blockman.OpenBlockFile(postx, true), SER_DISK, CLIENT_VERSION);
8482
if (file.IsNull()) {
8583
return error("%s: OpenBlockFile failed", __func__);
8684
}

src/init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1427,7 +1427,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
14271427
g_zmq_notification_interface = CZMQNotificationInterface::Create(
14281428
[&chainman = node.chainman](CBlock& block, const CBlockIndex& index) {
14291429
assert(chainman);
1430-
return node::ReadBlockFromDisk(block, &index, chainman->GetConsensus());
1430+
return chainman->m_blockman.ReadBlockFromDisk(block, index);
14311431
});
14321432

14331433
if (g_zmq_notification_interface) {

src/net_processing.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,6 @@
5151
#include <optional>
5252
#include <typeinfo>
5353

54-
using node::ReadBlockFromDisk;
55-
using node::ReadRawBlockFromDisk;
56-
5754
/** How long to cache transactions in mapRelay for normal relay */
5855
static constexpr auto RELAY_TX_CACHE_TIME = 15min;
5956
/** How long a transaction has to be in the mempool before it can unconditionally be relayed (even when not in mapRelay). */
@@ -2189,15 +2186,15 @@ void PeerManagerImpl::ProcessGetBlockData(CNode& pfrom, Peer& peer, const CInv&
21892186
// Fast-path: in this case it is possible to serve the block directly from disk,
21902187
// as the network format matches the format on disk
21912188
std::vector<uint8_t> block_data;
2192-
if (!ReadRawBlockFromDisk(block_data, pindex->GetBlockPos(), m_chainparams.MessageStart())) {
2189+
if (!m_chainman.m_blockman.ReadRawBlockFromDisk(block_data, pindex->GetBlockPos(), m_chainparams.MessageStart())) {
21932190
assert(!"cannot load block from disk");
21942191
}
21952192
m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::BLOCK, Span{block_data}));
21962193
// Don't set pblock as we've sent the block
21972194
} else {
21982195
// Send block from disk
21992196
std::shared_ptr<CBlock> pblockRead = std::make_shared<CBlock>();
2200-
if (!ReadBlockFromDisk(*pblockRead, pindex, m_chainparams.GetConsensus())) {
2197+
if (!m_chainman.m_blockman.ReadBlockFromDisk(*pblockRead, *pindex)) {
22012198
assert(!"cannot load block from disk");
22022199
}
22032200
pblock = pblockRead;
@@ -3889,7 +3886,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
38893886

38903887
if (pindex->nHeight >= m_chainman.ActiveChain().Height() - MAX_BLOCKTXN_DEPTH) {
38913888
CBlock block;
3892-
bool ret = ReadBlockFromDisk(block, pindex, m_chainparams.GetConsensus());
3889+
const bool ret{m_chainman.m_blockman.ReadBlockFromDisk(block, *pindex)};
38933890
assert(ret);
38943891

38953892
SendBlockTransactions(pfrom, *peer, block, req);
@@ -5546,7 +5543,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
55465543
m_connman.PushMessage(pto, std::move(cached_cmpctblock_msg.value()));
55475544
} else {
55485545
CBlock block;
5549-
bool ret = ReadBlockFromDisk(block, pBestIndex, consensusParams);
5546+
const bool ret{m_chainman.m_blockman.ReadBlockFromDisk(block, *pBestIndex)};
55505547
assert(ret);
55515548
CBlockHeaderAndShortTxIDs cmpctblock{block};
55525549
m_connman.PushMessage(pto, msgMaker.Make(NetMsgType::CMPCTBLOCK, cmpctblock));

src/node/blockstorage.cpp

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,6 @@ bool CBlockIndexHeightOnlyComparator::operator()(const CBlockIndex* pa, const CB
5353
return pa->nHeight < pb->nHeight;
5454
}
5555

56-
static FILE* OpenUndoFile(const FlatFilePos& pos, bool fReadOnly = false);
57-
static FlatFileSeq BlockFileSeq();
58-
static FlatFileSeq UndoFileSeq();
59-
6056
std::vector<CBlockIndex*> BlockManager::GetAllBlockIndices()
6157
{
6258
AssertLockHeld(cs_main);
@@ -423,7 +419,7 @@ const CBlockIndex* BlockManager::GetFirstStoredBlock(const CBlockIndex& start_bl
423419
// rev files since they'll be rewritten by the reindex anyway. This ensures that m_blockfile_info
424420
// is in sync with what's actually on disk by the time we start downloading, so that pruning
425421
// works correctly.
426-
void CleanupBlockRevFiles()
422+
void BlockManager::CleanupBlockRevFiles() const
427423
{
428424
std::map<std::string, fs::path> mapBlockFiles;
429425

@@ -467,7 +463,7 @@ CBlockFileInfo* BlockManager::GetBlockFileInfo(size_t n)
467463
return &m_blockfile_info.at(n);
468464
}
469465

470-
static bool UndoWriteToDisk(const CBlockUndo& blockundo, FlatFilePos& pos, const uint256& hashBlock, const CMessageHeader::MessageStartChars& messageStart)
466+
bool BlockManager::UndoWriteToDisk(const CBlockUndo& blockundo, FlatFilePos& pos, const uint256& hashBlock, const CMessageHeader::MessageStartChars& messageStart) const
471467
{
472468
// Open history file to append
473469
AutoFile fileout{OpenUndoFile(pos)};
@@ -496,9 +492,9 @@ static bool UndoWriteToDisk(const CBlockUndo& blockundo, FlatFilePos& pos, const
496492
return true;
497493
}
498494

499-
bool UndoReadFromDisk(CBlockUndo& blockundo, const CBlockIndex* pindex)
495+
bool BlockManager::UndoReadFromDisk(CBlockUndo& blockundo, const CBlockIndex& index) const
500496
{
501-
const FlatFilePos pos{WITH_LOCK(::cs_main, return pindex->GetUndoPos())};
497+
const FlatFilePos pos{WITH_LOCK(::cs_main, return index.GetUndoPos())};
502498

503499
if (pos.IsNull()) {
504500
return error("%s: no undo data available", __func__);
@@ -514,7 +510,7 @@ bool UndoReadFromDisk(CBlockUndo& blockundo, const CBlockIndex* pindex)
514510
uint256 hashChecksum;
515511
HashVerifier verifier{filein}; // Use HashVerifier as reserializing may lose data, c.f. commit d342424301013ec47dc146a4beb49d5c9319d80a
516512
try {
517-
verifier << pindex->pprev->GetBlockHash();
513+
verifier << index.pprev->GetBlockHash();
518514
verifier >> blockundo;
519515
filein >> hashChecksum;
520516
} catch (const std::exception& e) {
@@ -570,7 +566,7 @@ uint64_t BlockManager::CalculateCurrentUsage()
570566
return retval;
571567
}
572568

573-
void UnlinkPrunedFiles(const std::set<int>& setFilesToPrune)
569+
void BlockManager::UnlinkPrunedFiles(const std::set<int>& setFilesToPrune) const
574570
{
575571
std::error_code ec;
576572
for (std::set<int>::iterator it = setFilesToPrune.begin(); it != setFilesToPrune.end(); ++it) {
@@ -583,28 +579,28 @@ void UnlinkPrunedFiles(const std::set<int>& setFilesToPrune)
583579
}
584580
}
585581

586-
static FlatFileSeq BlockFileSeq()
582+
FlatFileSeq BlockManager::BlockFileSeq() const
587583
{
588584
return FlatFileSeq(gArgs.GetBlocksDirPath(), "blk", gArgs.GetBoolArg("-fastprune", false) ? 0x4000 /* 16kb */ : BLOCKFILE_CHUNK_SIZE);
589585
}
590586

591-
static FlatFileSeq UndoFileSeq()
587+
FlatFileSeq BlockManager::UndoFileSeq() const
592588
{
593589
return FlatFileSeq(gArgs.GetBlocksDirPath(), "rev", UNDOFILE_CHUNK_SIZE);
594590
}
595591

596-
FILE* OpenBlockFile(const FlatFilePos& pos, bool fReadOnly)
592+
FILE* BlockManager::OpenBlockFile(const FlatFilePos& pos, bool fReadOnly) const
597593
{
598594
return BlockFileSeq().Open(pos, fReadOnly);
599595
}
600596

601597
/** Open an undo file (rev?????.dat) */
602-
static FILE* OpenUndoFile(const FlatFilePos& pos, bool fReadOnly)
598+
FILE* BlockManager::OpenUndoFile(const FlatFilePos& pos, bool fReadOnly) const
603599
{
604600
return UndoFileSeq().Open(pos, fReadOnly);
605601
}
606602

607-
fs::path GetBlockPosFilename(const FlatFilePos& pos)
603+
fs::path BlockManager::GetBlockPosFilename(const FlatFilePos& pos) const
608604
{
609605
return BlockFileSeq().FileName(pos);
610606
}
@@ -697,7 +693,7 @@ bool BlockManager::FindUndoPos(BlockValidationState& state, int nFile, FlatFileP
697693
return true;
698694
}
699695

700-
static bool WriteBlockToDisk(const CBlock& block, FlatFilePos& pos, const CMessageHeader::MessageStartChars& messageStart)
696+
bool BlockManager::WriteBlockToDisk(const CBlock& block, FlatFilePos& pos, const CMessageHeader::MessageStartChars& messageStart) const
701697
{
702698
// Open history file to append
703699
CAutoFile fileout(OpenBlockFile(pos), SER_DISK, CLIENT_VERSION);
@@ -750,7 +746,7 @@ bool BlockManager::WriteUndoDataForBlock(const CBlockUndo& blockundo, BlockValid
750746
return true;
751747
}
752748

753-
bool ReadBlockFromDisk(CBlock& block, const FlatFilePos& pos, const Consensus::Params& consensusParams)
749+
bool BlockManager::ReadBlockFromDisk(CBlock& block, const FlatFilePos& pos) const
754750
{
755751
block.SetNull();
756752

@@ -768,33 +764,33 @@ bool ReadBlockFromDisk(CBlock& block, const FlatFilePos& pos, const Consensus::P
768764
}
769765

770766
// Check the header
771-
if (!CheckProofOfWork(block.GetHash(), block.nBits, consensusParams)) {
767+
if (!CheckProofOfWork(block.GetHash(), block.nBits, GetConsensus())) {
772768
return error("ReadBlockFromDisk: Errors in block header at %s", pos.ToString());
773769
}
774770

775771
// Signet only: check block solution
776-
if (consensusParams.signet_blocks && !CheckSignetBlockSolution(block, consensusParams)) {
772+
if (GetConsensus().signet_blocks && !CheckSignetBlockSolution(block, GetConsensus())) {
777773
return error("ReadBlockFromDisk: Errors in block solution at %s", pos.ToString());
778774
}
779775

780776
return true;
781777
}
782778

783-
bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex, const Consensus::Params& consensusParams)
779+
bool BlockManager::ReadBlockFromDisk(CBlock& block, const CBlockIndex& index) const
784780
{
785-
const FlatFilePos block_pos{WITH_LOCK(cs_main, return pindex->GetBlockPos())};
781+
const FlatFilePos block_pos{WITH_LOCK(cs_main, return index.GetBlockPos())};
786782

787-
if (!ReadBlockFromDisk(block, block_pos, consensusParams)) {
783+
if (!ReadBlockFromDisk(block, block_pos)) {
788784
return false;
789785
}
790-
if (block.GetHash() != pindex->GetBlockHash()) {
786+
if (block.GetHash() != index.GetBlockHash()) {
791787
return error("ReadBlockFromDisk(CBlock&, CBlockIndex*): GetHash() doesn't match index for %s at %s",
792-
pindex->ToString(), block_pos.ToString());
788+
index.ToString(), block_pos.ToString());
793789
}
794790
return true;
795791
}
796792

797-
bool ReadRawBlockFromDisk(std::vector<uint8_t>& block, const FlatFilePos& pos, const CMessageHeader::MessageStartChars& message_start)
793+
bool BlockManager::ReadRawBlockFromDisk(std::vector<uint8_t>& block, const FlatFilePos& pos, const CMessageHeader::MessageStartChars& message_start) const
798794
{
799795
FlatFilePos hpos = pos;
800796
hpos.nPos -= 8; // Seek back 8 bytes for meta header
@@ -888,10 +884,10 @@ void ThreadImport(ChainstateManager& chainman, std::vector<fs::path> vImportFile
888884
std::multimap<uint256, FlatFilePos> blocks_with_unknown_parent;
889885
while (true) {
890886
FlatFilePos pos(nFile, 0);
891-
if (!fs::exists(GetBlockPosFilename(pos))) {
887+
if (!fs::exists(chainman.m_blockman.GetBlockPosFilename(pos))) {
892888
break; // No block files left to reindex
893889
}
894-
FILE* file = OpenBlockFile(pos, true);
890+
FILE* file = chainman.m_blockman.OpenBlockFile(pos, true);
895891
if (!file) {
896892
break; // This error is logged in OpenBlockFile
897893
}

src/node/blockstorage.h

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,14 @@ class BlockManager
9696
bool FindBlockPos(FlatFilePos& pos, unsigned int nAddSize, unsigned int nHeight, CChain& active_chain, uint64_t nTime, bool fKnown);
9797
bool FindUndoPos(BlockValidationState& state, int nFile, FlatFilePos& pos, unsigned int nAddSize);
9898

99+
FlatFileSeq BlockFileSeq() const;
100+
FlatFileSeq UndoFileSeq() const;
101+
102+
FILE* OpenUndoFile(const FlatFilePos& pos, bool fReadOnly = false) const;
103+
104+
bool WriteBlockToDisk(const CBlock& block, FlatFilePos& pos, const CMessageHeader::MessageStartChars& messageStart) const;
105+
bool UndoWriteToDisk(const CBlockUndo& blockundo, FlatFilePos& pos, const uint256& hashBlock, const CMessageHeader::MessageStartChars& messageStart) const;
106+
99107
/* Calculate the block/rev files to delete based on height specified by user with RPC command pruneblockchain */
100108
void FindFilesToPruneManual(std::set<int>& setFilesToPrune, int nManualPruneHeight, int chain_tip_height);
101109

@@ -219,26 +227,27 @@ class BlockManager
219227

220228
//! Create or update a prune lock identified by its name
221229
void UpdatePruneLock(const std::string& name, const PruneLockInfo& lock_info) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
222-
};
223230

224-
void CleanupBlockRevFiles();
231+
/** Open a block file (blk?????.dat) */
232+
FILE* OpenBlockFile(const FlatFilePos& pos, bool fReadOnly = false) const;
225233

226-
/** Open a block file (blk?????.dat) */
227-
FILE* OpenBlockFile(const FlatFilePos& pos, bool fReadOnly = false);
228-
/** Translation to a filesystem path */
229-
fs::path GetBlockPosFilename(const FlatFilePos& pos);
234+
/** Translation to a filesystem path */
235+
fs::path GetBlockPosFilename(const FlatFilePos& pos) const;
230236

231-
/**
232-
* Actually unlink the specified files
233-
*/
234-
void UnlinkPrunedFiles(const std::set<int>& setFilesToPrune);
237+
/**
238+
* Actually unlink the specified files
239+
*/
240+
void UnlinkPrunedFiles(const std::set<int>& setFilesToPrune) const;
235241

236-
/** Functions for disk access for blocks */
237-
bool ReadBlockFromDisk(CBlock& block, const FlatFilePos& pos, const Consensus::Params& consensusParams);
238-
bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex, const Consensus::Params& consensusParams);
239-
bool ReadRawBlockFromDisk(std::vector<uint8_t>& block, const FlatFilePos& pos, const CMessageHeader::MessageStartChars& message_start);
242+
/** Functions for disk access for blocks */
243+
bool ReadBlockFromDisk(CBlock& block, const FlatFilePos& pos) const;
244+
bool ReadBlockFromDisk(CBlock& block, const CBlockIndex& index) const;
245+
bool ReadRawBlockFromDisk(std::vector<uint8_t>& block, const FlatFilePos& pos, const CMessageHeader::MessageStartChars& message_start) const;
240246

241-
bool UndoReadFromDisk(CBlockUndo& blockundo, const CBlockIndex* pindex);
247+
bool UndoReadFromDisk(CBlockUndo& blockundo, const CBlockIndex& index) const;
248+
249+
void CleanupBlockRevFiles() const;
250+
};
242251

243252
void ThreadImport(ChainstateManager& chainman, std::vector<fs::path> vImportFiles, const ArgsManager& args, const fs::path& mempool_path);
244253
} // namespace node

src/node/chainstate.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ static ChainstateLoadResult CompleteChainstateInitialization(
5151
pblocktree->WriteReindexing(true);
5252
//If we're reindexing in prune mode, wipe away unusable block files and all undo data files
5353
if (options.prune) {
54-
CleanupBlockRevFiles();
54+
chainman.m_blockman.CleanupBlockRevFiles();
5555
}
5656
}
5757

0 commit comments

Comments
 (0)