Skip to content

Commit fa121b6

Browse files
author
MarcoFalke
committed
blockstorage: [refactor] Use chainman reference where possible
Also, add missing { } for style. Can be reviewed with `--word-diff-regex=.`
1 parent fa0c7d9 commit fa121b6

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

src/node/blockstorage.cpp

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,19 @@ static bool WriteBlockToDisk(const CBlock& block, FlatFilePos& pos, const CMessa
2222
{
2323
// Open history file to append
2424
CAutoFile fileout(OpenBlockFile(pos), SER_DISK, CLIENT_VERSION);
25-
if (fileout.IsNull())
25+
if (fileout.IsNull()) {
2626
return error("WriteBlockToDisk: OpenBlockFile failed");
27+
}
2728

2829
// Write index header
2930
unsigned int nSize = GetSerializeSize(block, fileout.GetVersion());
3031
fileout << messageStart << nSize;
3132

3233
// Write block
3334
long fileOutPos = ftell(fileout.Get());
34-
if (fileOutPos < 0)
35+
if (fileOutPos < 0) {
3536
return error("WriteBlockToDisk: ftell failed");
37+
}
3638
pos.nPos = (unsigned int)fileOutPos;
3739
fileout << block;
3840

@@ -45,20 +47,21 @@ bool ReadBlockFromDisk(CBlock& block, const FlatFilePos& pos, const Consensus::P
4547

4648
// Open history file to read
4749
CAutoFile filein(OpenBlockFile(pos, true), SER_DISK, CLIENT_VERSION);
48-
if (filein.IsNull())
50+
if (filein.IsNull()) {
4951
return error("ReadBlockFromDisk: OpenBlockFile failed for %s", pos.ToString());
52+
}
5053

5154
// Read block
5255
try {
5356
filein >> block;
54-
}
55-
catch (const std::exception& e) {
57+
} catch (const std::exception& e) {
5658
return error("%s: Deserialize or I/O error - %s at %s", __func__, e.what(), pos.ToString());
5759
}
5860

5961
// Check the header
60-
if (!CheckProofOfWork(block.GetHash(), block.nBits, consensusParams))
62+
if (!CheckProofOfWork(block.GetHash(), block.nBits, consensusParams)) {
6163
return error("ReadBlockFromDisk: Errors in block header at %s", pos.ToString());
64+
}
6265

6366
// Signet only: check block solution
6467
if (consensusParams.signet_blocks && !CheckSignetBlockSolution(block, consensusParams)) {
@@ -76,11 +79,13 @@ bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex, const Consensus
7679
blockPos = pindex->GetBlockPos();
7780
}
7881

79-
if (!ReadBlockFromDisk(block, blockPos, consensusParams))
82+
if (!ReadBlockFromDisk(block, blockPos, consensusParams)) {
8083
return false;
81-
if (block.GetHash() != pindex->GetBlockHash())
84+
}
85+
if (block.GetHash() != pindex->GetBlockHash()) {
8286
return error("ReadBlockFromDisk(CBlock&, CBlockIndex*): GetHash() doesn't match index for %s at %s",
8387
pindex->ToString(), pindex->GetBlockPos().ToString());
88+
}
8489
return true;
8590
}
8691

@@ -135,8 +140,9 @@ FlatFilePos SaveBlockToDisk(const CBlock& block, int nHeight, CChain& active_cha
135140
{
136141
unsigned int nBlockSize = ::GetSerializeSize(block, CLIENT_VERSION);
137142
FlatFilePos blockPos;
138-
if (dbp != nullptr)
143+
if (dbp != nullptr) {
139144
blockPos = *dbp;
145+
}
140146
if (!FindBlockPos(blockPos, nBlockSize + 8, nHeight, active_chain, block.GetBlockTime(), dbp != nullptr)) {
141147
error("%s: FindBlockPos failed", __func__);
142148
return FlatFilePos();
@@ -177,13 +183,15 @@ void ThreadImport(ChainstateManager& chainman, std::vector<fs::path> vImportFile
177183
int nFile = 0;
178184
while (true) {
179185
FlatFilePos pos(nFile, 0);
180-
if (!fs::exists(GetBlockPosFilename(pos)))
186+
if (!fs::exists(GetBlockPosFilename(pos))) {
181187
break; // No block files left to reindex
188+
}
182189
FILE* file = OpenBlockFile(pos, true);
183-
if (!file)
190+
if (!file) {
184191
break; // This error is logged in OpenBlockFile
192+
}
185193
LogPrintf("Reindexing block file blk%05u.dat...\n", (unsigned int)nFile);
186-
::ChainstateActive().LoadExternalBlockFile(chainparams, file, &pos);
194+
chainman.ActiveChainstate().LoadExternalBlockFile(chainparams, file, &pos);
187195
if (ShutdownRequested()) {
188196
LogPrintf("Shutdown requested. Exit %s\n", __func__);
189197
return;
@@ -194,15 +202,15 @@ void ThreadImport(ChainstateManager& chainman, std::vector<fs::path> vImportFile
194202
fReindex = false;
195203
LogPrintf("Reindexing finished\n");
196204
// To avoid ending up in a situation without genesis block, re-try initializing (no-op if reindexing worked):
197-
::ChainstateActive().LoadGenesisBlock(chainparams);
205+
chainman.ActiveChainstate().LoadGenesisBlock(chainparams);
198206
}
199207

200208
// -loadblock=
201209
for (const fs::path& path : vImportFiles) {
202210
FILE* file = fsbridge::fopen(path, "rb");
203211
if (file) {
204212
LogPrintf("Importing blocks file %s...\n", path.string());
205-
::ChainstateActive().LoadExternalBlockFile(chainparams, file);
213+
chainman.ActiveChainstate().LoadExternalBlockFile(chainparams, file);
206214
if (ShutdownRequested()) {
207215
LogPrintf("Shutdown requested. Exit %s\n", __func__);
208216
return;

0 commit comments

Comments
 (0)