15
15
class CBlockIndex ;
16
16
17
17
/* *
18
- * TxIndex is used to look up transactions included in the blockchain by hash.
19
- * The index is written to a LevelDB database and records the filesystem
20
- * location of each transaction by transaction hash .
18
+ * Base class for indices of blockchain data. This implements
19
+ * CValidationInterface and ensures blocks are indexed sequentially according
20
+ * to their position in the active chain .
21
21
*/
22
- class TxIndex final : public CValidationInterface
22
+ class BaseIndex : public CValidationInterface
23
23
{
24
24
private:
25
- const std::unique_ptr<TxIndexDB> m_db;
26
-
27
25
// / Whether the index is in sync with the main chain. The flag is flipped
28
26
// / from false to true once, after which point this starts processing
29
27
// / ValidationInterface notifications to stay in sync.
30
- std::atomic<bool > m_synced;
28
+ std::atomic<bool > m_synced{ false } ;
31
29
32
- // / The last block in the chain that the TxIndex is in sync with.
33
- std::atomic<const CBlockIndex*> m_best_block_index;
30
+ // / The last block in the chain that the index is in sync with.
31
+ std::atomic<const CBlockIndex*> m_best_block_index{ nullptr } ;
34
32
35
33
std::thread m_thread_sync;
36
34
CThreadInterrupt m_interrupt;
37
35
38
- // / Initialize internal state from the database and block index.
39
- bool Init ();
40
-
41
- // / Sync the tx index with the block index starting from the current best
42
- // / block. Intended to be run in its own thread, m_thread_sync, and can be
43
- // / interrupted with m_interrupt. Once the txindex gets in sync, the
44
- // / m_synced flag is set and the BlockConnected ValidationInterface callback
45
- // / takes over and the sync thread exits.
36
+ // / Sync the index with the block index starting from the current best block.
37
+ // / Intended to be run in its own thread, m_thread_sync, and can be
38
+ // / interrupted with m_interrupt. Once the index gets in sync, the m_synced
39
+ // / flag is set and the BlockConnected ValidationInterface callback takes
40
+ // / over and the sync thread exits.
46
41
void ThreadSync ();
47
42
48
- // / Write update index entries for a newly connected block.
49
- bool WriteBlock (const CBlock& block, const CBlockIndex* pindex);
50
-
51
43
// / Write the current chain block locator to the DB.
52
44
bool WriteBestBlock (const CBlockIndex* block_index);
53
45
@@ -57,27 +49,25 @@ class TxIndex final : public CValidationInterface
57
49
58
50
void ChainStateFlushed (const CBlockLocator& locator) override ;
59
51
60
- public:
61
- // / Constructs the TxIndex, which becomes available to be queried.
62
- explicit TxIndex (std::unique_ptr<TxIndexDB> db);
52
+ // / Initialize internal state from the database and block index.
53
+ virtual bool Init ();
63
54
55
+ // / Write update index entries for a newly connected block.
56
+ virtual bool WriteBlock (const CBlock& block, const CBlockIndex* pindex) { return true ; }
57
+
58
+ virtual BaseIndexDB& GetDB () const = 0;
59
+
60
+ public:
64
61
// / Destructor interrupts sync thread if running and blocks until it exits.
65
- ~TxIndex ();
62
+ virtual ~BaseIndex ();
66
63
67
- // / Blocks the current thread until the transaction index is caught up to
68
- // / the current state of the block chain. This only blocks if the index has gotten in sync once
69
- // / and only needs to process blocks in the ValidationInterface queue. If the index is catching
70
- // / up from far behind, this method does not block and immediately returns false.
64
+ // / Blocks the current thread until the index is caught up to the current
65
+ // / state of the block chain. This only blocks if the index has gotten in
66
+ // / sync once and only needs to process blocks in the ValidationInterface
67
+ // / queue. If the index is catching up from far behind, this method does
68
+ // / not block and immediately returns false.
71
69
bool BlockUntilSyncedToCurrentChain ();
72
70
73
- // / Look up a transaction by hash.
74
- // /
75
- // / @param[in] tx_hash The hash of the transaction to be returned.
76
- // / @param[out] block_hash The hash of the block the transaction is found in.
77
- // / @param[out] tx The transaction itself.
78
- // / @return true if transaction is found, false otherwise
79
- bool FindTx (const uint256& tx_hash, uint256& block_hash, CTransactionRef& tx) const ;
80
-
81
71
void Interrupt ();
82
72
83
73
// / Start initializes the sync state and registers the instance as a
@@ -88,6 +78,37 @@ class TxIndex final : public CValidationInterface
88
78
void Stop ();
89
79
};
90
80
81
+ /* *
82
+ * TxIndex is used to look up transactions included in the blockchain by hash.
83
+ * The index is written to a LevelDB database and records the filesystem
84
+ * location of each transaction by transaction hash.
85
+ */
86
+ class TxIndex final : public BaseIndex
87
+ {
88
+ private:
89
+ const std::unique_ptr<TxIndexDB> m_db;
90
+
91
+ protected:
92
+ // / Override base class init to migrate from old database.
93
+ bool Init () override ;
94
+
95
+ bool WriteBlock (const CBlock& block, const CBlockIndex* pindex) override ;
96
+
97
+ BaseIndexDB& GetDB () const override ;
98
+
99
+ public:
100
+ // / Constructs the index, which becomes available to be queried.
101
+ explicit TxIndex (std::unique_ptr<TxIndexDB> db);
102
+
103
+ // / Look up a transaction by hash.
104
+ // /
105
+ // / @param[in] tx_hash The hash of the transaction to be returned.
106
+ // / @param[out] block_hash The hash of the block the transaction is found in.
107
+ // / @param[out] tx The transaction itself.
108
+ // / @return true if transaction is found, false otherwise
109
+ bool FindTx (const uint256& tx_hash, uint256& block_hash, CTransactionRef& tx) const ;
110
+ };
111
+
91
112
// / The global transaction index, used in GetTransaction. May be null.
92
113
extern std::unique_ptr<TxIndex> g_txindex;
93
114
0 commit comments