2017-12-08 19:19:57 +01:00
|
|
|
// Copyright (c) 2017-2018 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_H
|
|
|
|
#define BITCOIN_INDEX_TXINDEX_H
|
|
|
|
|
|
|
|
#include <primitives/block.h>
|
2018-03-30 09:39:08 +02:00
|
|
|
#include <primitives/transaction.h>
|
2017-12-08 19:52:42 +01:00
|
|
|
#include <threadinterrupt.h>
|
2017-12-08 19:19:57 +01:00
|
|
|
#include <txdb.h>
|
|
|
|
#include <uint256.h>
|
|
|
|
#include <validationinterface.h>
|
|
|
|
|
|
|
|
class CBlockIndex;
|
|
|
|
|
|
|
|
/**
|
2018-05-15 23:47:37 +02:00
|
|
|
* Base class for indices of blockchain data. This implements
|
|
|
|
* CValidationInterface and ensures blocks are indexed sequentially according
|
|
|
|
* to their position in the active chain.
|
2017-12-08 19:19:57 +01:00
|
|
|
*/
|
2018-05-15 23:47:37 +02:00
|
|
|
class BaseIndex : public CValidationInterface
|
2017-12-08 19:19:57 +01:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
/// Whether the index is in sync with the main chain. The flag is flipped
|
|
|
|
/// from false to true once, after which point this starts processing
|
|
|
|
/// ValidationInterface notifications to stay in sync.
|
2018-05-15 23:47:37 +02:00
|
|
|
std::atomic<bool> m_synced{false};
|
2017-12-08 19:19:57 +01:00
|
|
|
|
2018-05-15 23:47:37 +02:00
|
|
|
/// The last block in the chain that the index is in sync with.
|
|
|
|
std::atomic<const CBlockIndex*> m_best_block_index{nullptr};
|
2017-12-08 19:19:57 +01:00
|
|
|
|
2017-12-08 19:42:31 +01:00
|
|
|
std::thread m_thread_sync;
|
2017-12-08 19:52:42 +01:00
|
|
|
CThreadInterrupt m_interrupt;
|
2017-12-08 19:42:31 +01:00
|
|
|
|
2018-05-15 23:47:37 +02:00
|
|
|
/// Sync the index with the block index starting from the current best block.
|
|
|
|
/// Intended to be run in its own thread, m_thread_sync, and can be
|
|
|
|
/// interrupted with m_interrupt. Once the index gets in sync, the m_synced
|
|
|
|
/// flag is set and the BlockConnected ValidationInterface callback takes
|
|
|
|
/// over and the sync thread exits.
|
2017-12-08 19:42:31 +01:00
|
|
|
void ThreadSync();
|
|
|
|
|
|
|
|
/// Write the current chain block locator to the DB.
|
|
|
|
bool WriteBestBlock(const CBlockIndex* block_index);
|
|
|
|
|
2017-12-08 19:19:57 +01:00
|
|
|
protected:
|
|
|
|
void BlockConnected(const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex,
|
|
|
|
const std::vector<CTransactionRef>& txn_conflicted) override;
|
|
|
|
|
2018-04-27 20:01:02 +02:00
|
|
|
void ChainStateFlushed(const CBlockLocator& locator) override;
|
2017-12-08 19:19:57 +01:00
|
|
|
|
2018-05-15 23:47:37 +02:00
|
|
|
/// Initialize internal state from the database and block index.
|
|
|
|
virtual bool Init();
|
2017-12-08 19:19:57 +01:00
|
|
|
|
2018-05-15 23:47:37 +02:00
|
|
|
/// Write update index entries for a newly connected block.
|
|
|
|
virtual bool WriteBlock(const CBlock& block, const CBlockIndex* pindex) { return true; }
|
|
|
|
|
|
|
|
virtual BaseIndexDB& GetDB() const = 0;
|
|
|
|
|
2018-05-16 00:45:20 +02:00
|
|
|
/// Get the name of the index for display in logs.
|
|
|
|
virtual const char* GetName() const = 0;
|
|
|
|
|
2018-05-15 23:47:37 +02:00
|
|
|
public:
|
2017-12-08 19:52:42 +01:00
|
|
|
/// Destructor interrupts sync thread if running and blocks until it exits.
|
2018-05-15 23:47:37 +02:00
|
|
|
virtual ~BaseIndex();
|
2017-12-08 19:52:42 +01:00
|
|
|
|
2018-05-15 23:47:37 +02:00
|
|
|
/// Blocks the current thread until the index is caught up to the current
|
|
|
|
/// state of the block chain. This only blocks if the index has gotten in
|
|
|
|
/// sync once and only needs to process blocks in the ValidationInterface
|
|
|
|
/// queue. If the index is catching up from far behind, this method does
|
|
|
|
/// not block and immediately returns false.
|
2017-12-08 20:20:10 +01:00
|
|
|
bool BlockUntilSyncedToCurrentChain();
|
|
|
|
|
2017-12-08 19:52:42 +01:00
|
|
|
void Interrupt();
|
|
|
|
|
2017-12-08 19:19:57 +01:00
|
|
|
/// Start initializes the sync state and registers the instance as a
|
|
|
|
/// ValidationInterface so that it stays in sync with blockchain updates.
|
|
|
|
void Start();
|
|
|
|
|
|
|
|
/// Stops the instance from staying in sync with blockchain updates.
|
|
|
|
void Stop();
|
|
|
|
};
|
|
|
|
|
2018-05-15 23:47:37 +02:00
|
|
|
/**
|
|
|
|
* TxIndex is used to look up transactions included in the blockchain by hash.
|
|
|
|
* The index is written to a LevelDB database and records the filesystem
|
|
|
|
* location of each transaction by transaction hash.
|
|
|
|
*/
|
|
|
|
class TxIndex final : public BaseIndex
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
const std::unique_ptr<TxIndexDB> m_db;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
/// Override base class init to migrate from old database.
|
|
|
|
bool Init() override;
|
|
|
|
|
|
|
|
bool WriteBlock(const CBlock& block, const CBlockIndex* pindex) override;
|
|
|
|
|
|
|
|
BaseIndexDB& GetDB() const override;
|
|
|
|
|
2018-05-16 00:45:20 +02:00
|
|
|
const char* GetName() const override { return "txindex"; }
|
|
|
|
|
2018-05-15 23:47:37 +02:00
|
|
|
public:
|
|
|
|
/// Constructs the index, which becomes available to be queried.
|
|
|
|
explicit TxIndex(std::unique_ptr<TxIndexDB> db);
|
|
|
|
|
|
|
|
/// Look up a transaction by hash.
|
|
|
|
///
|
|
|
|
/// @param[in] tx_hash The hash of the transaction to be returned.
|
|
|
|
/// @param[out] block_hash The hash of the block the transaction is found in.
|
|
|
|
/// @param[out] tx The transaction itself.
|
|
|
|
/// @return true if transaction is found, false otherwise
|
|
|
|
bool FindTx(const uint256& tx_hash, uint256& block_hash, CTransactionRef& tx) const;
|
|
|
|
};
|
|
|
|
|
2017-12-08 20:29:59 +01:00
|
|
|
/// The global transaction index, used in GetTransaction. May be null.
|
|
|
|
extern std::unique_ptr<TxIndex> g_txindex;
|
|
|
|
|
2017-12-08 19:19:57 +01:00
|
|
|
#endif // BITCOIN_INDEX_TXINDEX_H
|