aboutsummaryrefslogtreecommitdiff
path: root/src/index/txindex.h
diff options
context:
space:
mode:
authorJim Posen <jimpo@coinbase.com>2017-12-08 10:19:57 -0800
committerJim Posen <jimpo@coinbase.com>2018-04-25 11:25:07 -0700
commit34d68bf3a3db2b78c07180416949bbc58bd0b682 (patch)
treeb283962424bb5097ba4f4218dad2f9a6bb1ef3a9 /src/index/txindex.h
parentc88bcec93fa8b969e65b1fe7716bda429276bbd4 (diff)
downloadbitcoin-34d68bf3a3db2b78c07180416949bbc58bd0b682.tar.xz
[index] Create new TxIndex class.
The TxIndex will be responsible for building the transaction index concurrently with the main validation thread by implementing ValidationInterface. This does not process blocks concurrently yet.
Diffstat (limited to 'src/index/txindex.h')
-rw-r--r--src/index/txindex.h60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/index/txindex.h b/src/index/txindex.h
new file mode 100644
index 0000000000..3d42a89635
--- /dev/null
+++ b/src/index/txindex.h
@@ -0,0 +1,60 @@
+// 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>
+#include <txdb.h>
+#include <uint256.h>
+#include <validationinterface.h>
+
+class CBlockIndex;
+
+/**
+ * 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 CValidationInterface
+{
+private:
+ const std::unique_ptr<TxIndexDB> m_db;
+
+ /// 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.
+ std::atomic<bool> m_synced;
+
+ /// The last block in the chain that the TxIndex is in sync with.
+ std::atomic<const CBlockIndex*> m_best_block_index;
+
+ /// Initialize internal state from the database and block index.
+ bool Init();
+
+ /// Write update index entries for a newly connected block.
+ bool WriteBlock(const CBlock& block, const CBlockIndex* pindex);
+
+protected:
+ void BlockConnected(const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex,
+ const std::vector<CTransactionRef>& txn_conflicted) override;
+
+ void SetBestChain(const CBlockLocator& locator) override;
+
+public:
+ /// Constructs the TxIndex, which becomes available to be queried.
+ explicit TxIndex(std::unique_ptr<TxIndexDB> db);
+
+ /// Look up the on-disk location of a transaction by hash.
+ bool FindTx(const uint256& txid, CDiskTxPos& pos) const;
+
+ /// 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();
+};
+
+#endif // BITCOIN_INDEX_TXINDEX_H