From 5a255970fd1b02052f669abe064ffa4d4776e40f Mon Sep 17 00:00:00 2001 From: Andrew Toth Date: Sat, 8 Aug 2026 11:58:30 -0400 Subject: [PATCH] refactor: move txindex db constants and legacy key to txindex_key.h No behavior change. Moving constants to a new file to make the next commit easier to review. --- src/index/txindex.cpp | 21 +++++---------------- src/index/txindex_key.h | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 16 deletions(-) create mode 100644 src/index/txindex_key.h diff --git a/src/index/txindex.cpp b/src/index/txindex.cpp index 6ca5f85fdff..b02f502fc38 100644 --- a/src/index/txindex.cpp +++ b/src/index/txindex.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -29,9 +30,6 @@ #include #include -constexpr uint8_t DB_TXINDEX{'t'}; -const std::string DB_BEST_BLOCK_V2{"best_block_v2"}; - std::unique_ptr g_txindex; @@ -41,10 +39,6 @@ class TxIndex::DB : public BaseIndex::DB public: explicit DB(size_t n_cache_size, bool f_memory = false, bool f_wipe = false); - /// Read the disk location of the transaction data with the given hash. Returns false if the - /// transaction hash is not indexed. - bool ReadTxPos(const Txid& txid, CDiskTxPos& pos) const; - /// Write a block of transaction positions to the DB. void WriteTxs(const interfaces::BlockInfo& block); @@ -56,15 +50,10 @@ TxIndex::DB::DB(size_t n_cache_size, bool f_memory, bool f_wipe) : BaseIndex::DB(gArgs.GetDataDirNet() / "indexes" / "txindex", n_cache_size, f_memory, f_wipe) {} -bool TxIndex::DB::ReadTxPos(const Txid& txid, CDiskTxPos& pos) const -{ - return Read(std::make_pair(DB_TXINDEX, txid.ToUint256()), pos); -} - CBlockLocator TxIndex::DB::ReadBestBlock() const { CBlockLocator locator; - if (Read(DB_BEST_BLOCK_V2, locator)) { + if (Read(txindex::DB_BEST_BLOCK_V2, locator)) { return locator; } // If we don't have a locator yet, start from the legacy best block. @@ -73,7 +62,7 @@ CBlockLocator TxIndex::DB::ReadBestBlock() const void TxIndex::DB::WriteBestBlock(CDBBatch& batch, const CBlockLocator& locator) { - batch.Write(DB_BEST_BLOCK_V2, locator); + batch.Write(txindex::DB_BEST_BLOCK_V2, locator); } void TxIndex::DB::WriteTxs(const interfaces::BlockInfo& block) @@ -81,7 +70,7 @@ void TxIndex::DB::WriteTxs(const interfaces::BlockInfo& block) CDBBatch batch(*this); CDiskTxPos pos({block.file_number, block.data_pos}, GetSizeOfCompactSize(block.data->vtx.size())); for (const auto& tx : block.data->vtx) { - batch.Write(std::make_pair(DB_TXINDEX, tx->GetHash().ToUint256()), pos); + batch.Write(std::make_pair(txindex::DB_TXINDEX, tx->GetHash().ToUint256()), pos); pos.nTxOffset += ::GetSerializeSize(TX_WITH_WITNESS(*tx)); } WriteBatch(batch); @@ -108,7 +97,7 @@ BaseIndex::DB& TxIndex::GetDB() const { return *m_db; } std::optional TxIndex::FindTx(const Txid& tx_hash) const { CDiskTxPos postx; - if (!m_db->ReadTxPos(tx_hash, postx)) { + if (!m_db->Read(txindex::LegacyTxKey(tx_hash), postx)) { return std::nullopt; } diff --git a/src/index/txindex_key.h b/src/index/txindex_key.h new file mode 100644 index 00000000000..395092e49ad --- /dev/null +++ b/src/index/txindex_key.h @@ -0,0 +1,36 @@ +// Copyright (c) The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#ifndef BITCOIN_INDEX_TXINDEX_KEY_H +#define BITCOIN_INDEX_TXINDEX_KEY_H + +#include +#include + +#include +#include +#include + +namespace txindex { +/* + * Database layout: + * + * ["best_block_v2"] -> current sync locator + * ['t', txid] -> legacy CDiskTxPos + * ['B'] -> legacy sync locator + */ + +inline const std::string DB_BEST_BLOCK_V2{"best_block_v2"}; +//! Prefix of a legacy (pre-hashing) txindex row. +constexpr uint8_t DB_TXINDEX{'t'}; + +//! Key of a legacy (pre-hashing) txindex row: the full txid under the 't' prefix. +inline std::pair LegacyTxKey(const Txid& txid) +{ + return {DB_TXINDEX, txid.ToUint256()}; +} + +} // namespace txindex + +#endif // BITCOIN_INDEX_TXINDEX_KEY_H