mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-12 06:58:57 +01:00
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.
This commit is contained in:
@@ -53,10 +53,6 @@ bool CBlockIndexHeightOnlyComparator::operator()(const CBlockIndex* pa, const CB
|
||||
return pa->nHeight < pb->nHeight;
|
||||
}
|
||||
|
||||
static FILE* OpenUndoFile(const FlatFilePos& pos, bool fReadOnly = false);
|
||||
static FlatFileSeq BlockFileSeq();
|
||||
static FlatFileSeq UndoFileSeq();
|
||||
|
||||
std::vector<CBlockIndex*> BlockManager::GetAllBlockIndices()
|
||||
{
|
||||
AssertLockHeld(cs_main);
|
||||
@@ -423,7 +419,7 @@ const CBlockIndex* BlockManager::GetFirstStoredBlock(const CBlockIndex& start_bl
|
||||
// rev files since they'll be rewritten by the reindex anyway. This ensures that m_blockfile_info
|
||||
// is in sync with what's actually on disk by the time we start downloading, so that pruning
|
||||
// works correctly.
|
||||
void CleanupBlockRevFiles()
|
||||
void BlockManager::CleanupBlockRevFiles() const
|
||||
{
|
||||
std::map<std::string, fs::path> mapBlockFiles;
|
||||
|
||||
@@ -467,7 +463,7 @@ CBlockFileInfo* BlockManager::GetBlockFileInfo(size_t n)
|
||||
return &m_blockfile_info.at(n);
|
||||
}
|
||||
|
||||
static bool UndoWriteToDisk(const CBlockUndo& blockundo, FlatFilePos& pos, const uint256& hashBlock, const CMessageHeader::MessageStartChars& messageStart)
|
||||
bool BlockManager::UndoWriteToDisk(const CBlockUndo& blockundo, FlatFilePos& pos, const uint256& hashBlock, const CMessageHeader::MessageStartChars& messageStart) const
|
||||
{
|
||||
// Open history file to append
|
||||
AutoFile fileout{OpenUndoFile(pos)};
|
||||
@@ -496,9 +492,9 @@ static bool UndoWriteToDisk(const CBlockUndo& blockundo, FlatFilePos& pos, const
|
||||
return true;
|
||||
}
|
||||
|
||||
bool UndoReadFromDisk(CBlockUndo& blockundo, const CBlockIndex* pindex)
|
||||
bool BlockManager::UndoReadFromDisk(CBlockUndo& blockundo, const CBlockIndex& index) const
|
||||
{
|
||||
const FlatFilePos pos{WITH_LOCK(::cs_main, return pindex->GetUndoPos())};
|
||||
const FlatFilePos pos{WITH_LOCK(::cs_main, return index.GetUndoPos())};
|
||||
|
||||
if (pos.IsNull()) {
|
||||
return error("%s: no undo data available", __func__);
|
||||
@@ -514,7 +510,7 @@ bool UndoReadFromDisk(CBlockUndo& blockundo, const CBlockIndex* pindex)
|
||||
uint256 hashChecksum;
|
||||
HashVerifier verifier{filein}; // Use HashVerifier as reserializing may lose data, c.f. commit d342424301013ec47dc146a4beb49d5c9319d80a
|
||||
try {
|
||||
verifier << pindex->pprev->GetBlockHash();
|
||||
verifier << index.pprev->GetBlockHash();
|
||||
verifier >> blockundo;
|
||||
filein >> hashChecksum;
|
||||
} catch (const std::exception& e) {
|
||||
@@ -570,7 +566,7 @@ uint64_t BlockManager::CalculateCurrentUsage()
|
||||
return retval;
|
||||
}
|
||||
|
||||
void UnlinkPrunedFiles(const std::set<int>& setFilesToPrune)
|
||||
void BlockManager::UnlinkPrunedFiles(const std::set<int>& setFilesToPrune) const
|
||||
{
|
||||
std::error_code ec;
|
||||
for (std::set<int>::iterator it = setFilesToPrune.begin(); it != setFilesToPrune.end(); ++it) {
|
||||
@@ -583,28 +579,28 @@ void UnlinkPrunedFiles(const std::set<int>& setFilesToPrune)
|
||||
}
|
||||
}
|
||||
|
||||
static FlatFileSeq BlockFileSeq()
|
||||
FlatFileSeq BlockManager::BlockFileSeq() const
|
||||
{
|
||||
return FlatFileSeq(gArgs.GetBlocksDirPath(), "blk", gArgs.GetBoolArg("-fastprune", false) ? 0x4000 /* 16kb */ : BLOCKFILE_CHUNK_SIZE);
|
||||
}
|
||||
|
||||
static FlatFileSeq UndoFileSeq()
|
||||
FlatFileSeq BlockManager::UndoFileSeq() const
|
||||
{
|
||||
return FlatFileSeq(gArgs.GetBlocksDirPath(), "rev", UNDOFILE_CHUNK_SIZE);
|
||||
}
|
||||
|
||||
FILE* OpenBlockFile(const FlatFilePos& pos, bool fReadOnly)
|
||||
FILE* BlockManager::OpenBlockFile(const FlatFilePos& pos, bool fReadOnly) const
|
||||
{
|
||||
return BlockFileSeq().Open(pos, fReadOnly);
|
||||
}
|
||||
|
||||
/** Open an undo file (rev?????.dat) */
|
||||
static FILE* OpenUndoFile(const FlatFilePos& pos, bool fReadOnly)
|
||||
FILE* BlockManager::OpenUndoFile(const FlatFilePos& pos, bool fReadOnly) const
|
||||
{
|
||||
return UndoFileSeq().Open(pos, fReadOnly);
|
||||
}
|
||||
|
||||
fs::path GetBlockPosFilename(const FlatFilePos& pos)
|
||||
fs::path BlockManager::GetBlockPosFilename(const FlatFilePos& pos) const
|
||||
{
|
||||
return BlockFileSeq().FileName(pos);
|
||||
}
|
||||
@@ -697,7 +693,7 @@ bool BlockManager::FindUndoPos(BlockValidationState& state, int nFile, FlatFileP
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool WriteBlockToDisk(const CBlock& block, FlatFilePos& pos, const CMessageHeader::MessageStartChars& messageStart)
|
||||
bool BlockManager::WriteBlockToDisk(const CBlock& block, FlatFilePos& pos, const CMessageHeader::MessageStartChars& messageStart) const
|
||||
{
|
||||
// Open history file to append
|
||||
CAutoFile fileout(OpenBlockFile(pos), SER_DISK, CLIENT_VERSION);
|
||||
@@ -750,7 +746,7 @@ bool BlockManager::WriteUndoDataForBlock(const CBlockUndo& blockundo, BlockValid
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ReadBlockFromDisk(CBlock& block, const FlatFilePos& pos, const Consensus::Params& consensusParams)
|
||||
bool BlockManager::ReadBlockFromDisk(CBlock& block, const FlatFilePos& pos) const
|
||||
{
|
||||
block.SetNull();
|
||||
|
||||
@@ -768,33 +764,33 @@ bool ReadBlockFromDisk(CBlock& block, const FlatFilePos& pos, const Consensus::P
|
||||
}
|
||||
|
||||
// Check the header
|
||||
if (!CheckProofOfWork(block.GetHash(), block.nBits, consensusParams)) {
|
||||
if (!CheckProofOfWork(block.GetHash(), block.nBits, GetConsensus())) {
|
||||
return error("ReadBlockFromDisk: Errors in block header at %s", pos.ToString());
|
||||
}
|
||||
|
||||
// Signet only: check block solution
|
||||
if (consensusParams.signet_blocks && !CheckSignetBlockSolution(block, consensusParams)) {
|
||||
if (GetConsensus().signet_blocks && !CheckSignetBlockSolution(block, GetConsensus())) {
|
||||
return error("ReadBlockFromDisk: Errors in block solution at %s", pos.ToString());
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex, const Consensus::Params& consensusParams)
|
||||
bool BlockManager::ReadBlockFromDisk(CBlock& block, const CBlockIndex& index) const
|
||||
{
|
||||
const FlatFilePos block_pos{WITH_LOCK(cs_main, return pindex->GetBlockPos())};
|
||||
const FlatFilePos block_pos{WITH_LOCK(cs_main, return index.GetBlockPos())};
|
||||
|
||||
if (!ReadBlockFromDisk(block, block_pos, consensusParams)) {
|
||||
if (!ReadBlockFromDisk(block, block_pos)) {
|
||||
return false;
|
||||
}
|
||||
if (block.GetHash() != pindex->GetBlockHash()) {
|
||||
if (block.GetHash() != index.GetBlockHash()) {
|
||||
return error("ReadBlockFromDisk(CBlock&, CBlockIndex*): GetHash() doesn't match index for %s at %s",
|
||||
pindex->ToString(), block_pos.ToString());
|
||||
index.ToString(), block_pos.ToString());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ReadRawBlockFromDisk(std::vector<uint8_t>& block, const FlatFilePos& pos, const CMessageHeader::MessageStartChars& message_start)
|
||||
bool BlockManager::ReadRawBlockFromDisk(std::vector<uint8_t>& block, const FlatFilePos& pos, const CMessageHeader::MessageStartChars& message_start) const
|
||||
{
|
||||
FlatFilePos hpos = pos;
|
||||
hpos.nPos -= 8; // Seek back 8 bytes for meta header
|
||||
@@ -888,10 +884,10 @@ void ThreadImport(ChainstateManager& chainman, std::vector<fs::path> vImportFile
|
||||
std::multimap<uint256, FlatFilePos> blocks_with_unknown_parent;
|
||||
while (true) {
|
||||
FlatFilePos pos(nFile, 0);
|
||||
if (!fs::exists(GetBlockPosFilename(pos))) {
|
||||
if (!fs::exists(chainman.m_blockman.GetBlockPosFilename(pos))) {
|
||||
break; // No block files left to reindex
|
||||
}
|
||||
FILE* file = OpenBlockFile(pos, true);
|
||||
FILE* file = chainman.m_blockman.OpenBlockFile(pos, true);
|
||||
if (!file) {
|
||||
break; // This error is logged in OpenBlockFile
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user