blockstorage: [refactor] Use chainman reference where possible

Also, add missing { } for style.

Can be reviewed with `--word-diff-regex=.`
This commit is contained in:
MarcoFalke 2021-04-02 19:27:59 +02:00
parent fa0c7d9ad2
commit fa121b628d
No known key found for this signature in database
GPG Key ID: CE2B75697E69A548

View File

@ -22,8 +22,9 @@ static bool WriteBlockToDisk(const CBlock& block, FlatFilePos& pos, const CMessa
{ {
// Open history file to append // Open history file to append
CAutoFile fileout(OpenBlockFile(pos), SER_DISK, CLIENT_VERSION); CAutoFile fileout(OpenBlockFile(pos), SER_DISK, CLIENT_VERSION);
if (fileout.IsNull()) if (fileout.IsNull()) {
return error("WriteBlockToDisk: OpenBlockFile failed"); return error("WriteBlockToDisk: OpenBlockFile failed");
}
// Write index header // Write index header
unsigned int nSize = GetSerializeSize(block, fileout.GetVersion()); unsigned int nSize = GetSerializeSize(block, fileout.GetVersion());
@ -31,8 +32,9 @@ static bool WriteBlockToDisk(const CBlock& block, FlatFilePos& pos, const CMessa
// Write block // Write block
long fileOutPos = ftell(fileout.Get()); long fileOutPos = ftell(fileout.Get());
if (fileOutPos < 0) if (fileOutPos < 0) {
return error("WriteBlockToDisk: ftell failed"); return error("WriteBlockToDisk: ftell failed");
}
pos.nPos = (unsigned int)fileOutPos; pos.nPos = (unsigned int)fileOutPos;
fileout << block; fileout << block;
@ -45,20 +47,21 @@ bool ReadBlockFromDisk(CBlock& block, const FlatFilePos& pos, const Consensus::P
// Open history file to read // Open history file to read
CAutoFile filein(OpenBlockFile(pos, true), SER_DISK, CLIENT_VERSION); CAutoFile filein(OpenBlockFile(pos, true), SER_DISK, CLIENT_VERSION);
if (filein.IsNull()) if (filein.IsNull()) {
return error("ReadBlockFromDisk: OpenBlockFile failed for %s", pos.ToString()); return error("ReadBlockFromDisk: OpenBlockFile failed for %s", pos.ToString());
}
// Read block // Read block
try { try {
filein >> block; filein >> block;
} } catch (const std::exception& e) {
catch (const std::exception& e) {
return error("%s: Deserialize or I/O error - %s at %s", __func__, e.what(), pos.ToString()); return error("%s: Deserialize or I/O error - %s at %s", __func__, e.what(), pos.ToString());
} }
// Check the header // Check the header
if (!CheckProofOfWork(block.GetHash(), block.nBits, consensusParams)) if (!CheckProofOfWork(block.GetHash(), block.nBits, consensusParams)) {
return error("ReadBlockFromDisk: Errors in block header at %s", pos.ToString()); return error("ReadBlockFromDisk: Errors in block header at %s", pos.ToString());
}
// Signet only: check block solution // Signet only: check block solution
if (consensusParams.signet_blocks && !CheckSignetBlockSolution(block, consensusParams)) { if (consensusParams.signet_blocks && !CheckSignetBlockSolution(block, consensusParams)) {
@ -76,11 +79,13 @@ bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex, const Consensus
blockPos = pindex->GetBlockPos(); blockPos = pindex->GetBlockPos();
} }
if (!ReadBlockFromDisk(block, blockPos, consensusParams)) if (!ReadBlockFromDisk(block, blockPos, consensusParams)) {
return false; return false;
if (block.GetHash() != pindex->GetBlockHash()) }
if (block.GetHash() != pindex->GetBlockHash()) {
return error("ReadBlockFromDisk(CBlock&, CBlockIndex*): GetHash() doesn't match index for %s at %s", return error("ReadBlockFromDisk(CBlock&, CBlockIndex*): GetHash() doesn't match index for %s at %s",
pindex->ToString(), pindex->GetBlockPos().ToString()); pindex->ToString(), pindex->GetBlockPos().ToString());
}
return true; return true;
} }
@ -135,8 +140,9 @@ FlatFilePos SaveBlockToDisk(const CBlock& block, int nHeight, CChain& active_cha
{ {
unsigned int nBlockSize = ::GetSerializeSize(block, CLIENT_VERSION); unsigned int nBlockSize = ::GetSerializeSize(block, CLIENT_VERSION);
FlatFilePos blockPos; FlatFilePos blockPos;
if (dbp != nullptr) if (dbp != nullptr) {
blockPos = *dbp; blockPos = *dbp;
}
if (!FindBlockPos(blockPos, nBlockSize + 8, nHeight, active_chain, block.GetBlockTime(), dbp != nullptr)) { if (!FindBlockPos(blockPos, nBlockSize + 8, nHeight, active_chain, block.GetBlockTime(), dbp != nullptr)) {
error("%s: FindBlockPos failed", __func__); error("%s: FindBlockPos failed", __func__);
return FlatFilePos(); return FlatFilePos();
@ -177,13 +183,15 @@ void ThreadImport(ChainstateManager& chainman, std::vector<fs::path> vImportFile
int nFile = 0; int nFile = 0;
while (true) { while (true) {
FlatFilePos pos(nFile, 0); FlatFilePos pos(nFile, 0);
if (!fs::exists(GetBlockPosFilename(pos))) if (!fs::exists(GetBlockPosFilename(pos))) {
break; // No block files left to reindex break; // No block files left to reindex
}
FILE* file = OpenBlockFile(pos, true); FILE* file = OpenBlockFile(pos, true);
if (!file) if (!file) {
break; // This error is logged in OpenBlockFile break; // This error is logged in OpenBlockFile
}
LogPrintf("Reindexing block file blk%05u.dat...\n", (unsigned int)nFile); LogPrintf("Reindexing block file blk%05u.dat...\n", (unsigned int)nFile);
::ChainstateActive().LoadExternalBlockFile(chainparams, file, &pos); chainman.ActiveChainstate().LoadExternalBlockFile(chainparams, file, &pos);
if (ShutdownRequested()) { if (ShutdownRequested()) {
LogPrintf("Shutdown requested. Exit %s\n", __func__); LogPrintf("Shutdown requested. Exit %s\n", __func__);
return; return;
@ -194,7 +202,7 @@ void ThreadImport(ChainstateManager& chainman, std::vector<fs::path> vImportFile
fReindex = false; fReindex = false;
LogPrintf("Reindexing finished\n"); LogPrintf("Reindexing finished\n");
// To avoid ending up in a situation without genesis block, re-try initializing (no-op if reindexing worked): // To avoid ending up in a situation without genesis block, re-try initializing (no-op if reindexing worked):
::ChainstateActive().LoadGenesisBlock(chainparams); chainman.ActiveChainstate().LoadGenesisBlock(chainparams);
} }
// -loadblock= // -loadblock=
@ -202,7 +210,7 @@ void ThreadImport(ChainstateManager& chainman, std::vector<fs::path> vImportFile
FILE* file = fsbridge::fopen(path, "rb"); FILE* file = fsbridge::fopen(path, "rb");
if (file) { if (file) {
LogPrintf("Importing blocks file %s...\n", path.string()); LogPrintf("Importing blocks file %s...\n", path.string());
::ChainstateActive().LoadExternalBlockFile(chainparams, file); chainman.ActiveChainstate().LoadExternalBlockFile(chainparams, file);
if (ShutdownRequested()) { if (ShutdownRequested()) {
LogPrintf("Shutdown requested. Exit %s\n", __func__); LogPrintf("Shutdown requested. Exit %s\n", __func__);
return; return;