Skip to content

Commit fa88cfd

Browse files
author
MarcoFalke
committed
Move functions to BlockManager
Needed for a later commit
1 parent e31cdb0 commit fa88cfd

File tree

5 files changed

+32
-25
lines changed

5 files changed

+32
-25
lines changed

src/node/blockstorage.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ std::string CBlockFileInfo::ToString() const
470470
return strprintf("CBlockFileInfo(blocks=%u, size=%u, heights=%u...%u, time=%s...%s)", nBlocks, nSize, nHeightFirst, nHeightLast, FormatISO8601Date(nTimeFirst), FormatISO8601Date(nTimeLast));
471471
}
472472

473-
CBlockFileInfo* GetBlockFileInfo(size_t n)
473+
CBlockFileInfo* BlockManager::GetBlockFileInfo(size_t n)
474474
{
475475
LOCK(cs_LastBlockFile);
476476

@@ -538,15 +538,15 @@ bool UndoReadFromDisk(CBlockUndo& blockundo, const CBlockIndex* pindex)
538538
return true;
539539
}
540540

541-
static void FlushUndoFile(int block_file, bool finalize = false)
541+
void BlockManager::FlushUndoFile(int block_file, bool finalize)
542542
{
543543
FlatFilePos undo_pos_old(block_file, vinfoBlockFile[block_file].nUndoSize);
544544
if (!UndoFileSeq().Flush(undo_pos_old, finalize)) {
545545
AbortNode("Flushing undo file to disk failed. This is likely the result of an I/O error.");
546546
}
547547
}
548548

549-
void FlushBlockFile(bool fFinalize = false, bool finalize_undo = false)
549+
void BlockManager::FlushBlockFile(bool fFinalize, bool finalize_undo)
550550
{
551551
LOCK(cs_LastBlockFile);
552552
FlatFilePos block_pos_old(nLastBlockFile, vinfoBlockFile[nLastBlockFile].nSize);
@@ -558,7 +558,7 @@ void FlushBlockFile(bool fFinalize = false, bool finalize_undo = false)
558558
if (!fFinalize || finalize_undo) FlushUndoFile(nLastBlockFile, finalize_undo);
559559
}
560560

561-
uint64_t CalculateCurrentUsage()
561+
uint64_t BlockManager::CalculateCurrentUsage()
562562
{
563563
LOCK(cs_LastBlockFile);
564564

@@ -605,7 +605,7 @@ fs::path GetBlockPosFilename(const FlatFilePos& pos)
605605
return BlockFileSeq().FileName(pos);
606606
}
607607

608-
bool FindBlockPos(FlatFilePos& pos, unsigned int nAddSize, unsigned int nHeight, CChain& active_chain, uint64_t nTime, bool fKnown = false)
608+
bool BlockManager::FindBlockPos(FlatFilePos& pos, unsigned int nAddSize, unsigned int nHeight, CChain& active_chain, uint64_t nTime, bool fKnown)
609609
{
610610
LOCK(cs_LastBlockFile);
611611

@@ -660,7 +660,7 @@ bool FindBlockPos(FlatFilePos& pos, unsigned int nAddSize, unsigned int nHeight,
660660
return true;
661661
}
662662

663-
static bool FindUndoPos(BlockValidationState& state, int nFile, FlatFilePos& pos, unsigned int nAddSize)
663+
bool BlockManager::FindUndoPos(BlockValidationState& state, int nFile, FlatFilePos& pos, unsigned int nAddSize)
664664
{
665665
pos.nFile = nFile;
666666

@@ -705,7 +705,7 @@ static bool WriteBlockToDisk(const CBlock& block, FlatFilePos& pos, const CMessa
705705
return true;
706706
}
707707

708-
bool WriteUndoDataForBlock(const CBlockUndo& blockundo, BlockValidationState& state, CBlockIndex* pindex, const CChainParams& chainparams)
708+
bool BlockManager::WriteUndoDataForBlock(const CBlockUndo& blockundo, BlockValidationState& state, CBlockIndex* pindex, const CChainParams& chainparams)
709709
{
710710
// Write undo information to disk
711711
if (pindex->GetUndoPos().IsNull()) {
@@ -825,7 +825,7 @@ bool ReadRawBlockFromDisk(std::vector<uint8_t>& block, const CBlockIndex* pindex
825825
}
826826

827827
/** Store block on disk. If dbp is non-nullptr, the file is known to already reside on disk */
828-
FlatFilePos SaveBlockToDisk(const CBlock& block, int nHeight, CChain& active_chain, const CChainParams& chainparams, const FlatFilePos* dbp)
828+
FlatFilePos BlockManager::SaveBlockToDisk(const CBlock& block, int nHeight, CChain& active_chain, const CChainParams& chainparams, const FlatFilePos* dbp)
829829
{
830830
unsigned int nBlockSize = ::GetSerializeSize(block, CLIENT_VERSION);
831831
FlatFilePos blockPos;

src/node/blockstorage.h

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ class BlockManager
6666
friend CChainState;
6767

6868
private:
69+
void FlushBlockFile(bool fFinalize = false, bool finalize_undo = false);
70+
void FlushUndoFile(int block_file, bool finalize = false);
71+
bool FindBlockPos(FlatFilePos& pos, unsigned int nAddSize, unsigned int nHeight, CChain& active_chain, uint64_t nTime, bool fKnown);
72+
bool FindUndoPos(BlockValidationState& state, int nFile, FlatFilePos& pos, unsigned int nAddSize);
73+
6974
/* Calculate the block/rev files to delete based on height specified by user with RPC command pruneblockchain */
7075
void FindFilesToPruneManual(std::set<int>& setFilesToPrune, int nManualPruneHeight, int chain_tip_height);
7176

@@ -120,6 +125,16 @@ class BlockManager
120125

121126
CBlockIndex* LookupBlockIndex(const uint256& hash) const EXCLUSIVE_LOCKS_REQUIRED(cs_main);
122127

128+
/** Get block file info entry for one block file */
129+
CBlockFileInfo* GetBlockFileInfo(size_t n);
130+
131+
bool WriteUndoDataForBlock(const CBlockUndo& blockundo, BlockValidationState& state, CBlockIndex* pindex, const CChainParams& chainparams);
132+
133+
FlatFilePos SaveBlockToDisk(const CBlock& block, int nHeight, CChain& active_chain, const CChainParams& chainparams, const FlatFilePos* dbp);
134+
135+
/** Calculate the amount of disk space the block & undo files currently use */
136+
uint64_t CalculateCurrentUsage();
137+
123138
//! Returns last CBlockIndex* that is a checkpoint
124139
CBlockIndex* GetLastCheckpoint(const CCheckpointData& data) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
125140

@@ -139,12 +154,6 @@ FILE* OpenBlockFile(const FlatFilePos& pos, bool fReadOnly = false);
139154
/** Translation to a filesystem path */
140155
fs::path GetBlockPosFilename(const FlatFilePos& pos);
141156

142-
/** Get block file info entry for one block file */
143-
CBlockFileInfo* GetBlockFileInfo(size_t n);
144-
145-
/** Calculate the amount of disk space the block & undo files currently use */
146-
uint64_t CalculateCurrentUsage();
147-
148157
/**
149158
* Actually unlink the specified files
150159
*/
@@ -157,9 +166,6 @@ bool ReadRawBlockFromDisk(std::vector<uint8_t>& block, const FlatFilePos& pos, c
157166
bool ReadRawBlockFromDisk(std::vector<uint8_t>& block, const CBlockIndex* pindex, const CMessageHeader::MessageStartChars& message_start);
158167

159168
bool UndoReadFromDisk(CBlockUndo& blockundo, const CBlockIndex* pindex);
160-
bool WriteUndoDataForBlock(const CBlockUndo& blockundo, BlockValidationState& state, CBlockIndex* pindex, const CChainParams& chainparams);
161-
162-
FlatFilePos SaveBlockToDisk(const CBlock& block, int nHeight, CChain& active_chain, const CChainParams& chainparams, const FlatFilePos* dbp);
163169

164170
void ThreadImport(ChainstateManager& chainman, std::vector<fs::path> vImportFiles, const ArgsManager& args);
165171

src/rpc/blockchain.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1564,7 +1564,7 @@ RPCHelpMan getblockchaininfo()
15641564
obj.pushKV("verificationprogress", GuessVerificationProgress(Params().TxData(), tip));
15651565
obj.pushKV("initialblockdownload", active_chainstate.IsInitialBlockDownload());
15661566
obj.pushKV("chainwork", tip->nChainWork.GetHex());
1567-
obj.pushKV("size_on_disk", CalculateCurrentUsage());
1567+
obj.pushKV("size_on_disk", chainman.m_blockman.CalculateCurrentUsage());
15681568
obj.pushKV("pruned", fPruneMode);
15691569
if (fPruneMode) {
15701570
const CBlockIndex* block = tip;

src/validation.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2135,7 +2135,7 @@ bool CChainState::ConnectBlock(const CBlock& block, BlockValidationState& state,
21352135
if (fJustCheck)
21362136
return true;
21372137

2138-
if (!WriteUndoDataForBlock(blockundo, state, pindex, m_params)) {
2138+
if (!m_blockman.WriteUndoDataForBlock(blockundo, state, pindex, m_params)) {
21392139
return false;
21402140
}
21412141

@@ -2269,7 +2269,7 @@ bool CChainState::FlushStateToDisk(
22692269
LOG_TIME_MILLIS_WITH_CATEGORY("write block and undo data to disk", BCLog::BENCH);
22702270

22712271
// First make sure all block and undo data is flushed to disk.
2272-
FlushBlockFile();
2272+
m_blockman.FlushBlockFile();
22732273
}
22742274

22752275
// Then update all block file information (which may refer to block and undo files).
@@ -3604,7 +3604,7 @@ bool CChainState::AcceptBlock(const std::shared_ptr<const CBlock>& pblock, Block
36043604
// Write block to history file
36053605
if (fNewBlock) *fNewBlock = true;
36063606
try {
3607-
FlatFilePos blockPos = SaveBlockToDisk(block, pindex->nHeight, m_chain, m_params, dbp);
3607+
FlatFilePos blockPos{m_blockman.SaveBlockToDisk(block, pindex->nHeight, m_chain, m_params, dbp)};
36083608
if (blockPos.IsNull()) {
36093609
state.Error(strprintf("%s: Failed to find position to write new block to disk", __func__));
36103610
return false;
@@ -4039,9 +4039,10 @@ bool CChainState::LoadGenesisBlock()
40394039

40404040
try {
40414041
const CBlock& block = m_params.GenesisBlock();
4042-
FlatFilePos blockPos = SaveBlockToDisk(block, 0, m_chain, m_params, nullptr);
4043-
if (blockPos.IsNull())
4042+
FlatFilePos blockPos{m_blockman.SaveBlockToDisk(block, 0, m_chain, m_params, nullptr)};
4043+
if (blockPos.IsNull()) {
40444044
return error("%s: writing genesis block to disk failed", __func__);
4045+
}
40454046
CBlockIndex *pindex = m_blockman.AddToBlockIndex(block);
40464047
ReceivedBlockTransactions(block, pindex, blockPos);
40474048
} catch (const std::runtime_error& e) {

src/wallet/test/wallet_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions, TestChain100Setup)
9292
{
9393
// Cap last block file size, and mine new block in a new block file.
9494
CBlockIndex* oldTip = m_node.chainman->ActiveChain().Tip();
95-
GetBlockFileInfo(oldTip->GetBlockPos().nFile)->nSize = MAX_BLOCKFILE_SIZE;
95+
WITH_LOCK(::cs_main, m_node.chainman->m_blockman.GetBlockFileInfo(oldTip->GetBlockPos().nFile)->nSize = MAX_BLOCKFILE_SIZE);
9696
CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
9797
CBlockIndex* newTip = m_node.chainman->ActiveChain().Tip();
9898

@@ -193,7 +193,7 @@ BOOST_FIXTURE_TEST_CASE(importmulti_rescan, TestChain100Setup)
193193
{
194194
// Cap last block file size, and mine new block in a new block file.
195195
CBlockIndex* oldTip = m_node.chainman->ActiveChain().Tip();
196-
GetBlockFileInfo(oldTip->GetBlockPos().nFile)->nSize = MAX_BLOCKFILE_SIZE;
196+
WITH_LOCK(::cs_main, m_node.chainman->m_blockman.GetBlockFileInfo(oldTip->GetBlockPos().nFile)->nSize = MAX_BLOCKFILE_SIZE);
197197
CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
198198
CBlockIndex* newTip = m_node.chainman->ActiveChain().Tip();
199199

0 commit comments

Comments
 (0)