@@ -22,17 +22,19 @@ static bool WriteBlockToDisk(const CBlock& block, FlatFilePos& pos, const CMessa
22
22
{
23
23
// Open history file to append
24
24
CAutoFile fileout (OpenBlockFile (pos), SER_DISK, CLIENT_VERSION);
25
- if (fileout.IsNull ())
25
+ if (fileout.IsNull ()) {
26
26
return error (" WriteBlockToDisk: OpenBlockFile failed" );
27
+ }
27
28
28
29
// Write index header
29
30
unsigned int nSize = GetSerializeSize (block, fileout.GetVersion ());
30
31
fileout << messageStart << nSize;
31
32
32
33
// Write block
33
34
long fileOutPos = ftell (fileout.Get ());
34
- if (fileOutPos < 0 )
35
+ if (fileOutPos < 0 ) {
35
36
return error (" WriteBlockToDisk: ftell failed" );
37
+ }
36
38
pos.nPos = (unsigned int )fileOutPos;
37
39
fileout << block;
38
40
@@ -45,20 +47,21 @@ bool ReadBlockFromDisk(CBlock& block, const FlatFilePos& pos, const Consensus::P
45
47
46
48
// Open history file to read
47
49
CAutoFile filein (OpenBlockFile (pos, true ), SER_DISK, CLIENT_VERSION);
48
- if (filein.IsNull ())
50
+ if (filein.IsNull ()) {
49
51
return error (" ReadBlockFromDisk: OpenBlockFile failed for %s" , pos.ToString ());
52
+ }
50
53
51
54
// Read block
52
55
try {
53
56
filein >> block;
54
- }
55
- catch (const std::exception& e) {
57
+ } catch (const std::exception& e) {
56
58
return error (" %s: Deserialize or I/O error - %s at %s" , __func__, e.what (), pos.ToString ());
57
59
}
58
60
59
61
// Check the header
60
- if (!CheckProofOfWork (block.GetHash (), block.nBits , consensusParams))
62
+ if (!CheckProofOfWork (block.GetHash (), block.nBits , consensusParams)) {
61
63
return error (" ReadBlockFromDisk: Errors in block header at %s" , pos.ToString ());
64
+ }
62
65
63
66
// Signet only: check block solution
64
67
if (consensusParams.signet_blocks && !CheckSignetBlockSolution (block, consensusParams)) {
@@ -76,11 +79,13 @@ bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex, const Consensus
76
79
blockPos = pindex->GetBlockPos ();
77
80
}
78
81
79
- if (!ReadBlockFromDisk (block, blockPos, consensusParams))
82
+ if (!ReadBlockFromDisk (block, blockPos, consensusParams)) {
80
83
return false ;
81
- if (block.GetHash () != pindex->GetBlockHash ())
84
+ }
85
+ if (block.GetHash () != pindex->GetBlockHash ()) {
82
86
return error (" ReadBlockFromDisk(CBlock&, CBlockIndex*): GetHash() doesn't match index for %s at %s" ,
83
87
pindex->ToString (), pindex->GetBlockPos ().ToString ());
88
+ }
84
89
return true ;
85
90
}
86
91
@@ -135,8 +140,9 @@ FlatFilePos SaveBlockToDisk(const CBlock& block, int nHeight, CChain& active_cha
135
140
{
136
141
unsigned int nBlockSize = ::GetSerializeSize (block, CLIENT_VERSION);
137
142
FlatFilePos blockPos;
138
- if (dbp != nullptr )
143
+ if (dbp != nullptr ) {
139
144
blockPos = *dbp;
145
+ }
140
146
if (!FindBlockPos (blockPos, nBlockSize + 8 , nHeight, active_chain, block.GetBlockTime (), dbp != nullptr )) {
141
147
error (" %s: FindBlockPos failed" , __func__);
142
148
return FlatFilePos ();
@@ -177,13 +183,15 @@ void ThreadImport(ChainstateManager& chainman, std::vector<fs::path> vImportFile
177
183
int nFile = 0 ;
178
184
while (true ) {
179
185
FlatFilePos pos (nFile, 0 );
180
- if (!fs::exists (GetBlockPosFilename (pos)))
186
+ if (!fs::exists (GetBlockPosFilename (pos))) {
181
187
break ; // No block files left to reindex
188
+ }
182
189
FILE* file = OpenBlockFile (pos, true );
183
- if (!file)
190
+ if (!file) {
184
191
break ; // This error is logged in OpenBlockFile
192
+ }
185
193
LogPrintf (" Reindexing block file blk%05u.dat...\n " , (unsigned int )nFile);
186
- ::ChainstateActive ().LoadExternalBlockFile(chainparams, file, &pos);
194
+ chainman. ActiveChainstate ().LoadExternalBlockFile (chainparams, file, &pos);
187
195
if (ShutdownRequested ()) {
188
196
LogPrintf (" Shutdown requested. Exit %s\n " , __func__);
189
197
return ;
@@ -194,15 +202,15 @@ void ThreadImport(ChainstateManager& chainman, std::vector<fs::path> vImportFile
194
202
fReindex = false ;
195
203
LogPrintf (" Reindexing finished\n " );
196
204
// 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);
198
206
}
199
207
200
208
// -loadblock=
201
209
for (const fs::path& path : vImportFiles) {
202
210
FILE* file = fsbridge::fopen (path, " rb" );
203
211
if (file) {
204
212
LogPrintf (" Importing blocks file %s...\n " , path.string ());
205
- ::ChainstateActive ().LoadExternalBlockFile(chainparams, file);
213
+ chainman. ActiveChainstate ().LoadExternalBlockFile (chainparams, file);
206
214
if (ShutdownRequested ()) {
207
215
LogPrintf (" Shutdown requested. Exit %s\n " , __func__);
208
216
return ;
0 commit comments