refactor: remove redundant locator cleanup in BaseIndex::Init()
What changed, and why it matters
This is a small internal code cleanup in Bitcoin Core's index subsystem. It changes how the program reads a saved 'block locator' (a bookmark of where an index thinks it is in the blockchain) so the code is slightly simpler. The behavior is unchanged: if no bookmark exists, the result is still empty. There is no security issue visible in the change.
No security action needed. Treat as normal code-review/merge for a refactor.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit refactors BaseIndex::DB::ReadBestBlock() from an out-parameter returning bool to a value-returning CBlockLocator. The caller in BaseIndex::Init() no longer needs to declare a CBlockLocator and then call SetNull() on failure; ReadBestBlock() now returns an empty/null locator when the database record is absent. The header comment explicitly notes the returned locator will be empty if no record exists. This is a pure refactor with no functional change to index initialization logic.
Changed components
src/index/base.cppsrc/index/base.hInspect captured patch +8 / −7
diff --git a/src/index/base.cpp b/src/index/base.cpp
index 82259ac0..2403989e 100644
--- a/src/index/base.cpp
+++ b/src/index/base.cpp
@@ -59,13 +59,16 @@ BaseIndex::DB::DB(const fs::path& path, size_t n_cache_size, bool f_memory, bool
.options = [] { DBOptions options; node::ReadDatabaseArgs(gArgs, options); return options; }()}}
{}
-bool BaseIndex::DB::ReadBestBlock(CBlockLocator& locator) const
+CBlockLocator BaseIndex::DB::ReadBestBlock() const
{
+ CBlockLocator locator;
+
bool success = Read(DB_BEST_BLOCK, locator);
if (!success) {
locator.SetNull();
}
- return success;
+
+ return locator;
}
void BaseIndex::DB::WriteBestBlock(CDBBatch& batch, const CBlockLocator& locator)
@@ -97,10 +100,7 @@ bool BaseIndex::Init()
// callbacks are not missed once m_synced is true.
m_chain->context()->validation_signals->RegisterValidationInterface(this);
- CBlockLocator locator;
- if (!GetDB().ReadBestBlock(locator)) {
- locator.SetNull();
- }
+ const auto locator{GetDB().ReadBestBlock()};
LOCK(cs_main);
CChain& index_chain = m_chainstate->m_chain;
diff --git a/src/index/base.h b/src/index/base.h
index 4131b06c..5a8ad8bf 100644
--- a/src/index/base.h
+++ b/src/index/base.h
@@ -56,7 +56,8 @@ protected:
bool f_memory = false, bool f_wipe = false, bool f_obfuscate = false);
/// Read block locator of the chain that the index is in sync with.
- bool ReadBestBlock(CBlockLocator& locator) const;
+ /// Note, the returned locator will be empty if no record exists.
+ CBlockLocator ReadBestBlock() const;
/// Write block locator of the chain that the index is in sync with.
void WriteBestBlock(CDBBatch& batch, const CBlockLocator& locator);
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.