Skip to content

Commit e104f0f

Browse files
committed
Move block writing out of AcceptBlock
1 parent 50701ba commit e104f0f

File tree

1 file changed

+29
-20
lines changed

1 file changed

+29
-20
lines changed

src/validation.cpp

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2759,7 +2759,7 @@ static bool ReceivedBlockTransactions(const CBlock &block, CValidationState& sta
27592759
return true;
27602760
}
27612761

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)
27632763
{
27642764
LOCK(cs_LastBlockFile);
27652765

@@ -2808,7 +2808,7 @@ static bool FindBlockPos(CValidationState &state, CDiskBlockPos &pos, unsigned i
28082808
}
28092809
}
28102810
else
2811-
return state.Error("out of disk space");
2811+
return error("out of disk space");
28122812
}
28132813
}
28142814

@@ -3196,6 +3196,25 @@ bool ProcessNewBlockHeaders(const std::vector<CBlockHeader>& headers, CValidatio
31963196
return true;
31973197
}
31983198

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+
31993218
/** Store block on disk. If dbp is non-nullptr, the file is known to already reside on disk */
32003219
static bool AcceptBlock(const std::shared_ptr<const CBlock>& pblock, CValidationState& state, const CChainParams& chainparams, CBlockIndex** ppindex, bool fRequested, const CDiskBlockPos* dbp, bool* fNewBlock)
32013220
{
@@ -3257,19 +3276,13 @@ static bool AcceptBlock(const std::shared_ptr<const CBlock>& pblock, CValidation
32573276
if (!IsInitialBlockDownload() && chainActive.Tip() == pindex->pprev)
32583277
GetMainSignals().NewPoWValidBlock(pindex, pblock);
32593278

3260-
int nHeight = pindex->nHeight;
3261-
32623279
// Write block to history file
32633280
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+
}
32733286
if (!ReceivedBlockTransactions(block, state, pindex, blockPos, chainparams.GetConsensus()))
32743287
return error("AcceptBlock(): ReceivedBlockTransactions failed");
32753288
} catch (const std::runtime_error& e) {
@@ -4037,15 +4050,11 @@ bool LoadGenesisBlock(const CChainParams& chainparams)
40374050

40384051
try {
40394052
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())
40474055
return error("%s: writing genesis block to disk failed", __func__);
40484056
CBlockIndex *pindex = AddToBlockIndex(block);
4057+
CValidationState state;
40494058
if (!ReceivedBlockTransactions(block, state, pindex, blockPos, chainparams.GetConsensus()))
40504059
return error("%s: genesis block not accepted", __func__);
40514060
} catch (const std::runtime_error& e) {

0 commit comments

Comments
 (0)