index, refactor: deduplicate LookUpOne

LookUpOne is used by both coinstatsindex and blockfilterindex,
the two implementations had already started to deviate slightly
for no apparent reason.
This commit is contained in:
Martin Zumsande
2025-07-16 15:19:54 -04:00
parent a67d3eb91d
commit 032f3503e3
3 changed files with 22 additions and 39 deletions

View File

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

View File

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

View File

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