[#TxIndex-01]
= TxIndex
:mrdocs:

TxIndex is used to look up transactions included in the blockchain by hash&period; The index is written to a LevelDB database and records the block sequence number and serialized block offset of each transaction by transaction hash&period;

== Synopsis

Declared in `&lt;index&sol;txindex&period;h&gt;`

[source,cpp,subs="verbatim,replacements,macros,-callouts"]
----
class TxIndex final
    : public xref:BaseIndex.adoc[BaseIndex]
----

== Base Classes

[cols="1,4"]
|===
| Name| Description
| `xref:BaseIndex.adoc[BaseIndex]`
| Base class for the node&apos;s optional block&hyphen;chain indexes&period;
|===

== Member Functions

[cols="1,4"]
|===
| Name| Description
| xref:TxIndex-01/2constructor.adoc[`TxIndex`]         [.small]#[constructor]#
| Constructs the index, which becomes available to be queried&period;
| xref:TxIndex-01/2destructor.adoc[`&#126;TxIndex`] [.small]#[destructor]# [.small]#[virtual]#
| Destructor is declared because this class contains a unique&lowbar;ptr to an incomplete type&period;
| xref:BaseIndex/BlockUntilSyncedToCurrentChain.adoc[`BlockUntilSyncedToCurrentChain`] 
| Blocks the current thread until the index is caught up to the current state of the block chain&period; This only blocks if the index has gotten in sync once and only needs to process blocks in the ValidationInterface queue&period; If the index is catching up from far behind, this method does not block and immediately returns false&period;
| xref:BaseIndex/CustomOptions.adoc[`CustomOptions`]  [.small]#[virtual]#
| Return custom notification options for index&period;
| xref:TxIndex-01/FindTx.adoc[`FindTx`] 
| Look up a transaction by hash&period;
| xref:BaseIndex/GetName.adoc[`GetName`] 
| Get the name of the index for display in logs&period;
| xref:BaseIndex/GetSummary.adoc[`GetSummary`] 
| Get a summary of the index and its state&period;
| xref:BaseIndex/Init.adoc[`Init`] 
| Initializes the sync state and registers the instance to the validation interface so that it stays in sync with blockchain updates&period;
| xref:BaseIndex/Interrupt.adoc[`Interrupt`] 
| Signal the background sync thread to stop as soon as possible&period;
| xref:BaseIndex/StartBackgroundSync.adoc[`StartBackgroundSync`] 
| Starts the initial sync process on a background thread&period;
| xref:BaseIndex/Stop.adoc[`Stop`] 
| Stops the instance from staying in sync with blockchain updates&period;
| xref:BaseIndex/Sync.adoc[`Sync`] 
| index&lowbar;sync Sync the index with the block index starting from the current best block&period; Intended to be run in its own thread, m&lowbar;thread&lowbar;sync, and can be interrupted with m&lowbar;interrupt&period; Once the index gets in sync, the m&lowbar;synced flag is set and the BlockConnected ValidationInterface callback takes over and the sync thread exits&period;
|===


== Protected Types

[cols="1,4"]
|===
| Name| Description
| xref:TxIndex-01/DB.adoc[`DB`] 
| Database access layer for the transaction index&period;
|===


== Protected Member Functions

[cols="1,4"]
|===
| Name| Description
| xref:CValidationInterface/ActiveTipChange.adoc[`ActiveTipChange`]  [.small]#[virtual]#
| Notifies listeners any time the block chain tip changes, synchronously&period;
| xref:CValidationInterface/BlockChecked.adoc[`BlockChecked`]  [.small]#[virtual]#
| Notifies listeners of a block validation result&period; If the provided BlockValidationState IsValid, the provided block is guaranteed to be the current best block at the time the callback was generated (not necessarily now)&period;
| xref:BaseIndex/BlockConnected.adoc[`BlockConnected`]  [.small]#[virtual]#
| Handle a newly connected block by appending it to the index&period;
| xref:CValidationInterface/BlockDisconnected.adoc[`BlockDisconnected`]  [.small]#[virtual]#
| Notifies listeners of a block being disconnected Provides the block that was disconnected&period;
| xref:BaseIndex/ChainStateFlushed.adoc[`ChainStateFlushed`]  [.small]#[virtual]#
| Handle a chainstate flush by committing the index up to the flushed point&period;
| xref:TxIndex-01/CustomAppend.adoc[`CustomAppend`]  [.small]#[virtual]#
| Record the transactions of a newly connected block in the index&period;
| xref:BaseIndex/CustomCommit.adoc[`CustomCommit`]  [.small]#[virtual]#
| Virtual method called internally by Commit that can be overridden to atomically commit more index state&period;
| xref:BaseIndex/CustomInit.adoc[`CustomInit`]  [.small]#[virtual]#
| Initialize internal state from the database and block index&period;
| xref:BaseIndex/CustomRemove.adoc[`CustomRemove`]  [.small]#[virtual]#
| Rewind index by one block during a chain reorg&period;
| xref:TxIndex-01/GetDB.adoc[`GetDB`]  [.small]#[virtual]#
| Access the database backing this index&period;
| xref:CValidationInterface/MempoolTransactionsRemovedForBlock.adoc[`MempoolTransactionsRemovedForBlock`]  [.small]#[virtual]#
| Notifies listeners of transactions removed from the mempool as as a result of new block being connected&period; MempoolTransactionsRemovedForBlock will be fired before BlockConnected&period;
| xref:CValidationInterface/NewPoWValidBlock.adoc[`NewPoWValidBlock`]  [.small]#[virtual]#
| Notifies listeners that a block which builds directly on our current tip has been received and connected to the headers tree, though not validated yet&period;
| xref:BaseIndex/SetBestBlockIndex.adoc[`SetBestBlockIndex`] 
| Update the internal best block index as well as the prune lock&period;
| xref:CValidationInterface/TransactionAddedToMempool.adoc[`TransactionAddedToMempool`]  [.small]#[virtual]#
| Notifies listeners of a transaction having been added to mempool&period;
| xref:CValidationInterface/TransactionRemovedFromMempool.adoc[`TransactionRemovedFromMempool`]  [.small]#[virtual]#
| Notifies listeners of a transaction leaving mempool&period;
| xref:CValidationInterface/UpdatedBlockTip.adoc[`UpdatedBlockTip`]  [.small]#[virtual]#
| Notifies listeners when the block chain tip advances&period;
|===


== Protected Data Members

[cols="1,4"]
|===
| Name| Description
| xref:BaseIndex/m_chain.adoc[`m&lowbar;chain`] 
| Interface used to query the blockchain the index tracks&period;
| xref:BaseIndex/m_chainstate.adoc[`m&lowbar;chainstate`] 
| Chainstate the index reads blocks from (not owned)&period;
| xref:BaseIndex/m_name.adoc[`m&lowbar;name`] 
| Human&hyphen;readable name of the index&period;
| xref:BaseIndex/m_thread_name.adoc[`m&lowbar;thread&lowbar;name`] 
| Name given to the background sync thread&period;
|===


== Friends

[cols="1,4"]
|===
| Name| Description
| `xref:txindex_tests/TxIndexTest.adoc[txindex&lowbar;tests::TxIndexTest]`
| Test fixture granting access to TxIndex private members&period;
|===


[.small]#Created with https://www.mrdocs.com[MrDocs]#
