24#include <validation.h>
56 throw std::ios_base::failure(
"Invalid format for spender index DB key");
64 :
BaseIndex(
std::move(chain),
"txospenderindex"), m_db{
std::make_unique<
DB>(
gArgs.GetDataDirNet() /
"indexes" /
"txospenderindex" /
"db", n_cache_size, f_memory, f_wipe)}
93 for (
const auto& [outpoint, pos] : items) {
98 m_db->WriteBatch(batch);
105 for (
const auto& [outpoint, pos] : items) {
108 m_db->WriteBatch(batch);
113 std::vector<std::pair<COutPoint, CDiskTxPos>> items;
114 items.reserve(block.
data->
vtx.size());
117 for (
const auto& tx : block.
data->
vtx) {
118 if (!tx->IsCoinBase()) {
119 for (
const auto& input : tx->vin) {
120 items.emplace_back(input.prevout, pos);
156 }
catch (
const std::exception& e) {
164 std::unique_ptr<CDBIterator> it(
m_db->NewIterator());
169 for (it->Seek(std::pair{DB_TXOSPENDERINDEX, prefix}); it->Valid() && it->GetKey(key) && key.
hash ==
prefix; it->Next()) {
171 for (
const auto& input : spender->tx->vin) {
172 if (input.prevout == txo) {
173 return std::optional{*spender};
177 LogError(
"Deserialize or I/O error - %s", spender.error());
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...
Base class for indices of blockchain data.
Chainstate * m_chainstate
std::vector< CTransactionRef > vtx
Batch of changes queued to be written to a CDBWrapper.
void Write(const K &key, const V &value)
An outpoint - a combination of a transaction hash and an index n into its vout.
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.
Optimized SipHash-2-4 implementation for uint256.
void EraseSpenderInfos(const std::vector< std::pair< COutPoint, CDiskTxPos > > &items)
std::pair< uint64_t, uint64_t > m_siphash_key
TxoSpenderIndex(std::unique_ptr< interfaces::Chain > chain, size_t n_cache_size, bool f_memory=false, bool f_wipe=false)
std::unique_ptr< BaseIndex::DB > m_db
bool CustomRemove(const interfaces::BlockInfo &block) override
Rewind index by one block during a chain reorg.
util::Expected< TxoSpender, std::string > ReadTransaction(const CDiskTxPos &pos) const
util::Expected< std::optional< TxoSpender >, std::string > FindSpender(const COutPoint &txo) const
interfaces::Chain::NotifyOptions CustomOptions() override
Return custom notification options for index.
void WriteSpenderInfos(const std::vector< std::pair< COutPoint, CDiskTxPos > > &items)
BaseIndex::DB & GetDB() const override
bool CustomAppend(const interfaces::BlockInfo &block) override
Write update index entries for a newly connected block.
bool IsBlockPruned(const CBlockIndex &block) const EXCLUSIVE_LOCKS_REQUIRED(void UpdatePruneLock(const std::string &name, const PruneLockInfo &lock_info) EXCLUSIVE_LOCKS_REQUIRED(AutoFile OpenBlockFile(const FlatFilePos &pos, bool fReadOnly) const
Check whether the block associated with this index entry is pruned or not.
const uint256 & ToUint256() const LIFETIMEBOUND
std::string GetHex() const
The util::Expected class provides a standard way for low-level functions to return either error value...
The util::Unexpected class represents an unexpected value stored in util::Expected.
static constexpr TransactionSerParams TX_WITH_WITNESS
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...
uint64_t GetSerializeSize(const T &t)
SERIALIZE_METHODS(DBKey, obj)
DBKey(const uint64_t &hash_in, const CDiskTxPos &pos_in)
Block data sent with blockConnected, blockDisconnected notifications.
Options specifying which chain notifications are required.
bool disconnect_data
Include block data with block disconnected notifications.
constexpr uint8_t DB_TXOSPENDERINDEX
static DBKey CreateKey(std::pair< uint64_t, uint64_t > siphash_key, const COutPoint &vout, const CDiskTxPos &pos)
std::unique_ptr< TxoSpenderIndex > g_txospenderindex
The global txo spender index. May be null.
static std::vector< std::pair< COutPoint, CDiskTxPos > > BuildSpenderPositions(const interfaces::BlockInfo &block)
static uint64_t CreateKeyPrefix(std::pair< uint64_t, uint64_t > siphash_key, const COutPoint &vout)