From facd01e6ffbbd019312f370a3807de0b95bbd745 Mon Sep 17 00:00:00 2001 From: Hao Xu Date: Tue, 5 Aug 2025 13:24:48 +0800 Subject: [PATCH] refactor: remove redundant locator cleanup in BaseIndex::Init() Leverage locator.IsNull() to simplify ReadBestBlock() and remove unnecessary SetNull(). Co-authored-by: furszy Co-authored-by: l0rinc Signed-off-by: Hao Xu --- src/index/base.cpp | 12 ++++++------ src/index/base.h | 3 ++- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/index/base.cpp b/src/index/base.cpp index 82259ac046a..2403989eddb 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 4131b06cad9..5a8ad8bf1f8 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);