refactor: remove redundant locator cleanup in BaseIndex::Init()

Leverage locator.IsNull() to simplify ReadBestBlock() and remove
unnecessary SetNull().

Co-authored-by: furszy <matiasfurszyfer@protonmail.com>
Co-authored-by: l0rinc <pap.lorinc@gmail.com>
Signed-off-by: Hao Xu <hao.xu@linux.dev>
This commit is contained in:
Hao Xu
2025-08-05 13:24:48 +08:00
parent 1444ed855f
commit facd01e6ff
2 changed files with 8 additions and 7 deletions

View File

@@ -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;

View File

@@ -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);