index, refactor: deduplicate LookUpOne
What changed, and why it matters
This commit is a simple code cleanup: it removes two nearly identical helper functions from two separate files and puts one shared copy in a common header. The behavior of the function is unchanged; only its location and the way callers pass block information to it are adjusted. There is no security fix here.
No security action needed; this is a routine refactor.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change deduplicates LookUpOne/LookupOne between BlockFilterIndex and CoinStatsIndex. The logic remains identical: read by height, compare hash, fall back to hash index. Callers now construct an interfaces::BlockRef explicitly. No semantic changes to database reads, validation, or consensus code.
Changed components
src/index/blockfilterindex.cppsrc/index/coinstatsindex.cppsrc/index/db_key.hInspect captured patch +22 / −39
diff --git a/src/index/blockfilterindex.cpp b/src/index/blockfilterindex.cpp
index 62a78016..68c11e35 100644
--- a/src/index/blockfilterindex.cpp
+++ b/src/index/blockfilterindex.cpp
@@ -297,24 +297,6 @@ bool BlockFilterIndex::CustomRemove(const interfaces::BlockInfo& block)
return true;
}
-static bool LookupOne(const CDBWrapper& db, const CBlockIndex* block_index, DBVal& result)
-{
- // First check if the result is stored under the height index and the value there matches the
- // block hash. This should be the case if the block is on the active chain.
- std::pair<uint256, DBVal> read_out;
- if (!db.Read(DBHeightKey(block_index->nHeight), read_out)) {
- return false;
- }
- if (read_out.first == block_index->GetBlockHash()) {
- result = std::move(read_out.second);
- return true;
- }
-
- // If value at the height index corresponds to an different block, the result will be stored in
- // the hash index.
- return db.Read(DBHashKey(block_index->GetBlockHash()), result);
-}
-
static bool LookupRange(CDBWrapper& db, const std::string& index_name, int start_height,
const CBlockIndex* stop_index, std::vector<DBVal>& results)
{
@@ -377,7 +359,7 @@ static bool LookupRange(CDBWrapper& db, const std::string& index_name, int start
bool BlockFilterIndex::LookupFilter(const CBlockIndex* block_index, BlockFilter& filter_out) const
{
DBVal entry;
- if (!LookupOne(*m_db, block_index, entry)) {
+ if (!LookUpOne(*m_db, {block_index->GetBlockHash(), block_index->nHeight}, entry)) {
return false;
}
@@ -400,7 +382,7 @@ bool BlockFilterIndex::LookupFilterHeader(const CBlockIndex* block_index, uint25
}
DBVal entry;
- if (!LookupOne(*m_db, block_index, entry)) {
+ if (!LookUpOne(*m_db, {block_index->GetBlockHash(), block_index->nHeight}, entry)) {
return false;
}
diff --git a/src/index/coinstatsindex.cpp b/src/index/coinstatsindex.cpp
index a12c52d0..bb914c1e 100644
--- a/src/index/coinstatsindex.cpp
+++ b/src/index/coinstatsindex.cpp
@@ -234,25 +234,6 @@ bool CoinStatsIndex::CustomRemove(const interfaces::BlockInfo& block)
return true;
}
-static bool LookUpOne(const CDBWrapper& db, const interfaces::BlockRef& block, DBVal& result)
-{
- // First check if the result is stored under the height index and the value
- // there matches the block hash. This should be the case if the block is on
- // the active chain.
- std::pair<uint256, DBVal> read_out;
- if (!db.Read(DBHeightKey(block.height), read_out)) {
- return false;
- }
- if (read_out.first == block.hash) {
- result = std::move(read_out.second);
- return true;
- }
-
- // If value at the height index corresponds to an different block, the
- // result will be stored in the hash index.
- return db.Read(DBHashKey(block.hash), result);
-}
-
std::optional<CCoinsStats> CoinStatsIndex::LookUpStats(const CBlockIndex& block_index) const
{
CCoinsStats stats{block_index.nHeight, block_index.GetBlockHash()};
diff --git a/src/index/db_key.h b/src/index/db_key.h
index c5369675..329f5643 100644
--- a/src/index/db_key.h
+++ b/src/index/db_key.h
@@ -91,4 +91,24 @@ template <typename DBVal>
return true;
}
+template <typename DBVal>
+static bool LookUpOne(const CDBWrapper& db, const interfaces::BlockRef& block, DBVal& result)
+{
+ // First check if the result is stored under the height index and the value
+ // there matches the block hash. This should be the case if the block is on
+ // the active chain.
+ std::pair<uint256, DBVal> read_out;
+ if (!db.Read(DBHeightKey(block.height), read_out)) {
+ return false;
+ }
+ if (read_out.first == block.hash) {
+ result = std::move(read_out.second);
+ return true;
+ }
+
+ // If value at the height index corresponds to an different block, the
+ // result will be stored in the hash index.
+ return db.Read(DBHashKey(block.hash), result);
+}
+
#endif // BITCOIN_INDEX_DB_KEY_H
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.