@@ -2759,7 +2759,7 @@ static bool ReceivedBlockTransactions(const CBlock &block, CValidationState& sta
2759
2759
return true ;
2760
2760
}
2761
2761
2762
- static bool FindBlockPos (CValidationState &state, CDiskBlockPos &pos, unsigned int nAddSize, unsigned int nHeight, uint64_t nTime, bool fKnown = false )
2762
+ static bool FindBlockPos (CDiskBlockPos &pos, unsigned int nAddSize, unsigned int nHeight, uint64_t nTime, bool fKnown = false )
2763
2763
{
2764
2764
LOCK (cs_LastBlockFile);
2765
2765
@@ -2808,7 +2808,7 @@ static bool FindBlockPos(CValidationState &state, CDiskBlockPos &pos, unsigned i
2808
2808
}
2809
2809
}
2810
2810
else
2811
- return state. Error (" out of disk space" );
2811
+ return error (" out of disk space" );
2812
2812
}
2813
2813
}
2814
2814
@@ -3196,6 +3196,25 @@ bool ProcessNewBlockHeaders(const std::vector<CBlockHeader>& headers, CValidatio
3196
3196
return true ;
3197
3197
}
3198
3198
3199
+ /* * Store block on disk. If dbp is non-nullptr, the file is known to already reside on disk */
3200
+ static CDiskBlockPos SaveBlockToDisk (const CBlock& block, int nHeight, const CChainParams& chainparams, const CDiskBlockPos* dbp) {
3201
+ unsigned int nBlockSize = ::GetSerializeSize (block, SER_DISK, CLIENT_VERSION);
3202
+ CDiskBlockPos blockPos;
3203
+ if (dbp != nullptr )
3204
+ blockPos = *dbp;
3205
+ if (!FindBlockPos (blockPos, nBlockSize+8 , nHeight, block.GetBlockTime (), dbp != nullptr )) {
3206
+ error (" %s: FindBlockPos failed" , __func__);
3207
+ return CDiskBlockPos ();
3208
+ }
3209
+ if (dbp == nullptr ) {
3210
+ if (!WriteBlockToDisk (block, blockPos, chainparams.MessageStart ())) {
3211
+ AbortNode (" Failed to write block" );
3212
+ return CDiskBlockPos ();
3213
+ }
3214
+ }
3215
+ return blockPos;
3216
+ }
3217
+
3199
3218
/* * Store block on disk. If dbp is non-nullptr, the file is known to already reside on disk */
3200
3219
static bool AcceptBlock (const std::shared_ptr<const CBlock>& pblock, CValidationState& state, const CChainParams& chainparams, CBlockIndex** ppindex, bool fRequested , const CDiskBlockPos* dbp, bool * fNewBlock )
3201
3220
{
@@ -3257,19 +3276,13 @@ static bool AcceptBlock(const std::shared_ptr<const CBlock>& pblock, CValidation
3257
3276
if (!IsInitialBlockDownload () && chainActive.Tip () == pindex->pprev )
3258
3277
GetMainSignals ().NewPoWValidBlock (pindex, pblock);
3259
3278
3260
- int nHeight = pindex->nHeight ;
3261
-
3262
3279
// Write block to history file
3263
3280
try {
3264
- unsigned int nBlockSize = ::GetSerializeSize (block, SER_DISK, CLIENT_VERSION);
3265
- CDiskBlockPos blockPos;
3266
- if (dbp != nullptr )
3267
- blockPos = *dbp;
3268
- if (!FindBlockPos (state, blockPos, nBlockSize+8 , nHeight, block.GetBlockTime (), dbp != nullptr ))
3269
- return error (" AcceptBlock(): FindBlockPos failed" );
3270
- if (dbp == nullptr )
3271
- if (!WriteBlockToDisk (block, blockPos, chainparams.MessageStart ()))
3272
- AbortNode (state, " Failed to write block" );
3281
+ CDiskBlockPos blockPos = SaveBlockToDisk (block, pindex->nHeight , chainparams, dbp);
3282
+ if (blockPos.IsNull ()) {
3283
+ state.Error (strprintf (" %s: Failed to find position to write new block to disk" , __func__));
3284
+ return false ;
3285
+ }
3273
3286
if (!ReceivedBlockTransactions (block, state, pindex, blockPos, chainparams.GetConsensus ()))
3274
3287
return error (" AcceptBlock(): ReceivedBlockTransactions failed" );
3275
3288
} catch (const std::runtime_error& e) {
@@ -4037,15 +4050,11 @@ bool LoadGenesisBlock(const CChainParams& chainparams)
4037
4050
4038
4051
try {
4039
4052
CBlock &block = const_cast <CBlock&>(chainparams.GenesisBlock ());
4040
- // Start new block file
4041
- unsigned int nBlockSize = ::GetSerializeSize (block, SER_DISK, CLIENT_VERSION);
4042
- CDiskBlockPos blockPos;
4043
- CValidationState state;
4044
- if (!FindBlockPos (state, blockPos, nBlockSize+8 , 0 , block.GetBlockTime ()))
4045
- return error (" %s: FindBlockPos failed" , __func__);
4046
- if (!WriteBlockToDisk (block, blockPos, chainparams.MessageStart ()))
4053
+ CDiskBlockPos blockPos = SaveBlockToDisk (block, 0 , chainparams, nullptr );
4054
+ if (blockPos.IsNull ())
4047
4055
return error (" %s: writing genesis block to disk failed" , __func__);
4048
4056
CBlockIndex *pindex = AddToBlockIndex (block);
4057
+ CValidationState state;
4049
4058
if (!ReceivedBlockTransactions (block, state, pindex, blockPos, chainparams.GetConsensus ()))
4050
4059
return error (" %s: genesis block not accepted" , __func__);
4051
4060
} catch (const std::runtime_error& e) {
0 commit comments