26#include <validation.h>
46 std::pair<uint64_t, uint64_t> salt;
60 explicit DB(
size_t n_cache_size,
bool f_memory =
false,
bool f_wipe =
false);
75 DB(
size_t n_cache_size,
bool f_memory,
bool f_wipe,
bool has_legacy);
86 DB(n_cache_size, f_memory, f_wipe,
90TxIndex::DB::DB(
size_t n_cache_size,
bool f_memory,
bool f_wipe,
bool has_legacy) :
92 m_hasher{ReadOrCreateTxidHasher(*this)},
93 m_has_legacy{has_legacy}
118 uint32_t block_seq{0};
126 for (
const auto& tx : block.
data->
vtx) {
130 tx_offset_in_block += tx->ComputeTotalSize();
135TxIndex::TxIndex(std::unique_ptr<interfaces::Chain> chain,
size_t n_cache_size,
bool f_memory,
bool f_wipe)
138 if (
m_db->m_has_legacy) {
139 LogInfo(
"txindex contains entries in the legacy format, which uses excessive disk space. "
140 "To reclaim disk space, stop the node, delete %s and restart to rebuild the index.",
150 if (block.
height == 0)
return true;
153 m_db->WriteTxs(block);
168 bool in_active_chain;
170 std::vector<Candidate> candidates;
172 std::unique_ptr<CDBIterator> it{
m_db->NewIterator()};
175 for (it->Seek(key); it->Valid() && it->GetKey(key) && key.hash_prefix ==
prefix; it->Next()) {
178 LogWarning(
"Block sequence %u not found for txid %s", key.pos.block_seq, tx_hash.
ToString());
188 const FlatFilePos tx_position{block_index->
nFile, block_index->nDataPos + key.pos.tx_offset_in_block};
194 std::ranges::sort(candidates, std::greater{}, [](
const Candidate& c) {
195 return std::pair{c.in_active_chain, c.block_seq};
198 for (
const auto& candidate : candidates) {
207 }
catch (
const std::exception& e) {
208 LogWarning(
"Deserialize or I/O error - %s", e.what());
211 if (tx->GetHash() == tx_hash) {
238 }
catch (
const std::exception& e) {
239 LogError(
"Deserialize or I/O error - %s", e.what());
242 if (tx->GetHash() != tx_hash) {
@ BLOCK_HAVE_DATA
full block available in blk*.dat
fs::path GetDataDirNet() const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Get data directory path with appended network identifier.
Non-refcounted RAII wrapper for FILE*.
The database stores a block locator of the chain the database is synced to so that the index can effi...
virtual CBlockLocator ReadBestBlock() const
Read block locator of the chain that the index is in sync with.
Base class for indices of blockchain data.
Chainstate * m_chainstate
std::vector< CTransactionRef > vtx
The block chain is a tree shaped structure starting with the genesis block at the root,...
bool Contains(const CBlockIndex &index) const
Efficiently check whether a block is present in this chain.
Batch of changes queued to be written to a CDBWrapper.
void Write(const K &key, const V &value)
bool Read(const K &key, V &value) const
void Write(const K &key, const V &value, bool fSync=false)
CChain m_chain
The current chain of blockheaders we consult and build on.
node::BlockManager & m_blockman
Reference to a BlockManager instance which itself is shared across all Chainstate instances.
uint64_t rand64() noexcept
Generate a random 64-bit integer.
A custom weaker variant of SipHash-1-3 without padding, and supporting "jumbo" inputs.
Access to the txindex database (indexes/txindex/)
const bool m_has_legacy
Whether the database contains any legacy ('t' + txid) entries.
DB(size_t n_cache_size, bool f_memory=false, bool f_wipe=false)
void WriteBestBlock(CDBBatch &batch, const CBlockLocator &locator) override
Write block locator of the chain that the index is in sync with.
const SipHasher13UJ m_hasher
Used to hash the txid to compute the prefix.
CBlockLocator ReadBestBlock() const override
Read block locator of the chain that the index is in sync with.
void WriteTxs(const interfaces::BlockInfo &block)
Write a block of transaction positions to the DB.
TxIndex is used to look up transactions included in the blockchain by hash.
BaseIndex::DB & GetDB() const override
std::optional< TxIndexResult > FindTx(const Txid &tx_hash) const
Look up a transaction by hash.
bool CustomAppend(const interfaces::BlockInfo &block) override
Write update index entries for a newly connected block.
TxIndex(std::unique_ptr< interfaces::Chain > chain, size_t n_cache_size, bool f_memory=false, bool f_wipe=false)
Constructs the index, which becomes available to be queried.
virtual ~TxIndex() override
const std::unique_ptr< DB > m_db
std::optional< TxIndexResult > FindLegacyTx(const Txid &tx_hash) const
Look up a transaction among the legacy (full-txid) entries.
std::string ToString() const
CBlockIndex * LookupBlockIndex(const uint256 &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
bool IsBlockPruned(const CBlockIndex &block) const EXCLUSIVE_LOCKS_REQUIRED(void UpdatePruneLock(const std::string &name, const PruneLockInfo &lock_info) EXCLUSIVE_LOCKS_REQUIRED(bool DeletePruneLock(const std::string &name) EXCLUSIVE_LOCKS_REQUIRED(AutoFile OpenBlockFile(const FlatFilePos &pos, bool fReadOnly) const
Check whether the block associated with this index entry is pruned or not.
std::string ToString() const
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
static std::string PathToString(const path &path)
Convert path object to a byte string.
const std::string DB_TXID_HASH_SALT
constexpr std::array< std::byte, 0 > EMPTY_VALUE
Empty value of a hashed txindex row, whose position is encoded in its key.
constexpr uint8_t DB_TXINDEX
Prefix of a legacy (pre-hashing) txindex row.
TxHashKeyPrefix CreateKeyPrefix(const SipHasher13UJ &hasher, const Txid &txid)
constexpr uint32_t BLOCK_HEADER_SIZE
Serialized size of a block header, the offset of the first byte after it.
const std::string DB_NEXT_BLOCK_SEQ
std::pair< uint8_t, uint256 > LegacyTxKey(const Txid &txid)
Key of a legacy (pre-hashing) txindex row: the full txid under the 't' prefix.
const std::string DB_BEST_BLOCK_V2
constexpr TransactionSerParams TX_WITH_WITNESS
std::shared_ptr< const CTransaction > CTransactionRef
constexpr unsigned int GetSizeOfCompactSize(uint64_t nSize)
Compact Size size < 253 – 1 byte size <= USHRT_MAX – 3 bytes (253 + 2 bytes) size <= UINT_MAX – 5 byt...
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
A found transaction and the hash of the block that contains it.
Block data sent with blockConnected, blockDisconnected notifications.
Key for looking up the sequence number assigned to the block with the given hash.
Key for looking up the hash of the block with the given sequence number.
The location of a transaction: the sequence number of the block that contains it and the transaction'...
static fs::path TxIndexDBPath()
std::unique_ptr< TxIndex > g_txindex
The global transaction index, used in GetTransaction. May be null.