index: restrict index helper function to namespace
What changed, and why it matters
This change is a simple code cleanup: it wraps some shared helper functions and constants in a namespace called index_util and updates the two files that use them to reference the new namespace. There is no change to what the code does, only to how it is organized. It does not fix a security bug or introduce a vulnerability.
No security action needed. Treat as a normal refactoring commit.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit places DBHeightKey, DBHashKey, DB_BLOCK_HEIGHT, DB_BLOCK_HASH, and LookUpOne/CopyHeightIndexToHashIndex helpers inside a new index_util namespace in src/index/db_key.h, then updates all call sites in blockfilterindex.cpp and coinstatsindex.cpp to qualify them with index_util::. The logic, serialization, and database behavior are unchanged; this is a pure refactoring for symbol scoping.
Changed components
src/index/db_key.hsrc/index/blockfilterindex.cppsrc/index/coinstatsindex.cppInspect captured patch +18 / −16
diff --git a/src/index/blockfilterindex.cpp b/src/index/blockfilterindex.cpp
index 68c11e35..8bf5e4a7 100644
--- a/src/index/blockfilterindex.cpp
+++ b/src/index/blockfilterindex.cpp
@@ -235,7 +235,7 @@ size_t BlockFilterIndex::WriteFilterToDisk(FlatFilePos& pos, const BlockFilter&
std::optional<uint256> BlockFilterIndex::ReadFilterHeader(int height, const uint256& expected_block_hash)
{
std::pair<uint256, DBVal> read_out;
- if (!m_db->Read(DBHeightKey(height), read_out)) {
+ if (!m_db->Read(index_util::DBHeightKey(height), read_out)) {
return std::nullopt;
}
@@ -268,7 +268,7 @@ bool BlockFilterIndex::Write(const BlockFilter& filter, uint32_t block_height, c
value.second.header = filter_header;
value.second.pos = m_next_filter_pos;
- m_db->Write(DBHeightKey(block_height), value);
+ m_db->Write(index_util::DBHeightKey(block_height), value);
m_next_filter_pos.nPos += bytes_written;
return true;
@@ -282,7 +282,7 @@ bool BlockFilterIndex::CustomRemove(const interfaces::BlockInfo& block)
// During a reorg, we need to copy block filter that is getting disconnected from the
// height index to the hash index so we can still find it when the height index entry
// is overwritten.
- if (!CopyHeightIndexToHashIndex<DBVal>(*db_it, batch, m_name, block.height)) {
+ if (!index_util::CopyHeightIndexToHashIndex<DBVal>(*db_it, batch, m_name, block.height)) {
return false;
}
@@ -313,9 +313,9 @@ static bool LookupRange(CDBWrapper& db, const std::string& index_name, int start
size_t results_size = static_cast<size_t>(stop_index->nHeight - start_height + 1);
std::vector<std::pair<uint256, DBVal>> values(results_size);
- DBHeightKey key(start_height);
+ index_util::DBHeightKey key(start_height);
std::unique_ptr<CDBIterator> db_it(db.NewIterator());
- db_it->Seek(DBHeightKey(start_height));
+ db_it->Seek(index_util::DBHeightKey(start_height));
for (int height = start_height; height <= stop_index->nHeight; ++height) {
if (!db_it->Valid() || !db_it->GetKey(key) || key.height != height) {
return false;
@@ -324,7 +324,7 @@ static bool LookupRange(CDBWrapper& db, const std::string& index_name, int start
size_t i = static_cast<size_t>(height - start_height);
if (!db_it->GetValue(values[i])) {
LogError("unable to read value in %s at key (%c, %d)",
- index_name, DB_BLOCK_HEIGHT, height);
+ index_name, index_util::DB_BLOCK_HEIGHT, height);
return false;
}
@@ -346,9 +346,9 @@ static bool LookupRange(CDBWrapper& db, const std::string& index_name, int start
continue;
}
- if (!db.Read(DBHashKey(block_hash), results[i])) {
+ if (!db.Read(index_util::DBHashKey(block_hash), results[i])) {
LogError("unable to read value in %s at key (%c, %s)",
- index_name, DB_BLOCK_HASH, block_hash.ToString());
+ index_name, index_util::DB_BLOCK_HASH, block_hash.ToString());
return false;
}
}
@@ -359,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->GetBlockHash(), block_index->nHeight}, entry)) {
+ if (!index_util::LookUpOne(*m_db, {block_index->GetBlockHash(), block_index->nHeight}, entry)) {
return false;
}
@@ -382,7 +382,7 @@ bool BlockFilterIndex::LookupFilterHeader(const CBlockIndex* block_index, uint25
}
DBVal entry;
- if (!LookUpOne(*m_db, {block_index->GetBlockHash(), block_index->nHeight}, entry)) {
+ if (!index_util::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 bb914c1e..0917ac45 100644
--- a/src/index/coinstatsindex.cpp
+++ b/src/index/coinstatsindex.cpp
@@ -210,7 +210,7 @@ bool CoinStatsIndex::CustomAppend(const interfaces::BlockInfo& block)
// Intentionally do not update DB_MUHASH here so it stays in sync with
// DB_BEST_BLOCK, and the index is not corrupted if there is an unclean shutdown.
- m_db->Write(DBHeightKey(block.height), value);
+ m_db->Write(index_util::DBHeightKey(block.height), value);
return true;
}
@@ -221,7 +221,7 @@ bool CoinStatsIndex::CustomRemove(const interfaces::BlockInfo& block)
// During a reorg, copy the block's hash digest from the height index to the hash index,
// ensuring it's still accessible after the height index entry is overwritten.
- if (!CopyHeightIndexToHashIndex<DBVal>(*db_it, batch, m_name, block.height)) {
+ if (!index_util::CopyHeightIndexToHashIndex<DBVal>(*db_it, batch, m_name, block.height)) {
return false;
}
@@ -240,7 +240,7 @@ std::optional<CCoinsStats> CoinStatsIndex::LookUpStats(const CBlockIndex& block_
stats.index_used = true;
DBVal entry;
- if (!LookUpOne(*m_db, {block_index.GetBlockHash(), block_index.nHeight}, entry)) {
+ if (!index_util::LookUpOne(*m_db, {block_index.GetBlockHash(), block_index.nHeight}, entry)) {
return std::nullopt;
}
@@ -275,7 +275,7 @@ bool CoinStatsIndex::CustomInit(const std::optional<interfaces::BlockRef>& block
if (block) {
DBVal entry;
- if (!LookUpOne(*m_db, *block, entry)) {
+ if (!index_util::LookUpOne(*m_db, *block, entry)) {
LogError("Cannot read current %s state; index may be corrupted",
GetName());
return false;
@@ -330,7 +330,7 @@ bool CoinStatsIndex::RevertBlock(const interfaces::BlockInfo& block)
// Ignore genesis block
if (block.height > 0) {
- if (!m_db->Read(DBHeightKey(block.height - 1), read_out)) {
+ if (!m_db->Read(index_util::DBHeightKey(block.height - 1), read_out)) {
return false;
}
@@ -339,7 +339,7 @@ bool CoinStatsIndex::RevertBlock(const interfaces::BlockInfo& block)
LogWarning("previous block header belongs to unexpected block %s; expected %s",
read_out.first.ToString(), expected_block_hash.ToString());
- if (!m_db->Read(DBHashKey(expected_block_hash), read_out)) {
+ if (!m_db->Read(index_util::DBHashKey(expected_block_hash), read_out)) {
LogError("previous block header not found; expected %s",
expected_block_hash.ToString());
return false;
diff --git a/src/index/db_key.h b/src/index/db_key.h
index 329f5643..b7334c81 100644
--- a/src/index/db_key.h
+++ b/src/index/db_key.h
@@ -16,6 +16,7 @@
#include <string>
#include <utility>
+namespace index_util {
/*
* This file includes the logic for the db keys used by blockfilterindex and coinstatsindex.
* Index data is usually indexed by height, but in case of a reorg, entries of blocks no
@@ -110,5 +111,6 @@ static bool LookUpOne(const CDBWrapper& db, const interfaces::BlockRef& block, D
// result will be stored in the hash index.
return db.Read(DBHashKey(block.hash), result);
}
+} // namespace index_util
#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.