Skip to content

Commit 01af278

Browse files
UdjinM6claude
andcommitted
refactor: return std::optional<uint256> from ReadBlockFromDisk(FlatFilePos)
Replace the uint256* hash_out output parameter with a std::optional return value. The optional is empty on failure, contains the block hash on success. This makes the hash lifetime explicit and eliminates the risk of dangling pointers. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 89d8c45 commit 01af278

3 files changed

Lines changed: 15 additions & 12 deletions

File tree

src/node/blockstorage.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -745,42 +745,44 @@ bool BlockManager::WriteUndoDataForBlock(const CBlockUndo& blockundo, BlockValid
745745
return true;
746746
}
747747

748-
bool ReadBlockFromDisk(CBlock& block, const FlatFilePos& pos, const Consensus::Params& consensusParams, uint256* hash_out)
748+
std::optional<uint256> ReadBlockFromDisk(CBlock& block, const FlatFilePos& pos, const Consensus::Params& consensusParams)
749749
{
750750
block.SetNull();
751751

752752
// Open history file to read
753753
CAutoFile filein(OpenBlockFile(pos, true), SER_DISK, CLIENT_VERSION);
754754
if (filein.IsNull()) {
755-
return error("ReadBlockFromDisk: OpenBlockFile failed for %s", pos.ToString());
755+
error("ReadBlockFromDisk: OpenBlockFile failed for %s", pos.ToString());
756+
return std::nullopt;
756757
}
757758

758759
// Read block
759760
try {
760761
filein >> block;
761762
} catch (const std::exception& e) {
762-
return error("%s: Deserialize or I/O error - %s at %s", __func__, e.what(), pos.ToString());
763+
error("%s: Deserialize or I/O error - %s at %s", __func__, e.what(), pos.ToString());
764+
return std::nullopt;
763765
}
764766

765767
// Check the header
766768
const uint256 hash{block.GetHash()};
767769
if (!CheckProofOfWork(hash, block.nBits, consensusParams)) {
768-
return error("ReadBlockFromDisk: Errors in block header at %s", pos.ToString());
770+
error("ReadBlockFromDisk: Errors in block header at %s", pos.ToString());
771+
return std::nullopt;
769772
}
770-
if (hash_out) *hash_out = hash;
771773

772-
return true;
774+
return hash;
773775
}
774776

775777
bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex, const Consensus::Params& consensusParams)
776778
{
777779
const FlatFilePos block_pos{WITH_LOCK(cs_main, return pindex->GetBlockPos())};
778780

779-
uint256 hash;
780-
if (!ReadBlockFromDisk(block, block_pos, consensusParams, &hash)) {
781+
const auto hash{ReadBlockFromDisk(block, block_pos, consensusParams)};
782+
if (!hash) {
781783
return false;
782784
}
783-
if (hash != pindex->GetBlockHash()) {
785+
if (*hash != pindex->GetBlockHash()) {
784786
return error("ReadBlockFromDisk(CBlock&, CBlockIndex*): GetHash() doesn't match index for %s at %s",
785787
pindex->ToString(), block_pos.ToString());
786788
}

src/node/blockstorage.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <txdb.h>
1414

1515
#include <cstdint>
16+
#include <optional>
1617
#include <unordered_map>
1718
#include <vector>
1819

@@ -221,7 +222,7 @@ fs::path GetBlockPosFilename(const FlatFilePos& pos);
221222
void UnlinkPrunedFiles(const std::set<int>& setFilesToPrune);
222223

223224
/** Functions for disk access for blocks */
224-
bool ReadBlockFromDisk(CBlock& block, const FlatFilePos& pos, const Consensus::Params& consensusParams, uint256* hash_out = nullptr);
225+
std::optional<uint256> ReadBlockFromDisk(CBlock& block, const FlatFilePos& pos, const Consensus::Params& consensusParams);
225226
bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex, const Consensus::Params& consensusParams);
226227

227228
bool UndoReadFromDisk(CBlockUndo& blockundo, const CBlockIndex* pindex);

src/validation.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5185,8 +5185,8 @@ void CChainState::LoadExternalBlockFile(
51855185
while (range.first != range.second) {
51865186
std::multimap<uint256, FlatFilePos>::iterator it = range.first;
51875187
std::shared_ptr<CBlock> pblockrecursive = std::make_shared<CBlock>();
5188-
uint256 blockhash;
5189-
if (ReadBlockFromDisk(*pblockrecursive, it->second, m_params.GetConsensus(), &blockhash)) {
5188+
if (auto opt_hash{ReadBlockFromDisk(*pblockrecursive, it->second, m_params.GetConsensus())}) {
5189+
const uint256& blockhash = *opt_hash;
51905190
LogPrint(BCLog::REINDEX, "%s: Processing out of order child %s of %s\n", __func__, blockhash.ToString(),
51915191
head.ToString());
51925192
LOCK(cs_main);

0 commit comments

Comments
 (0)