diff --git a/src/index/blockfilterindex.cpp b/src/index/blockfilterindex.cpp index 68c11e35ef..8bf5e4a7e4 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 BlockFilterIndex::ReadFilterHeader(int height, const uint256& expected_block_hash) { std::pair 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(*db_it, batch, m_name, block.height)) { + if (!index_util::CopyHeightIndexToHashIndex(*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(stop_index->nHeight - start_height + 1); std::vector> values(results_size); - DBHeightKey key(start_height); + index_util::DBHeightKey key(start_height); std::unique_ptr 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(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 bb914c1ef1..0917ac451c 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(*db_it, batch, m_name, block.height)) { + if (!index_util::CopyHeightIndexToHashIndex(*db_it, batch, m_name, block.height)) { return false; } @@ -240,7 +240,7 @@ std::optional 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& 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 329f564334..b7334c81e4 100644 --- a/src/index/db_key.h +++ b/src/index/db_key.h @@ -16,6 +16,7 @@ #include #include +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