# Indexing Transactions
Tendermint allows you to index transactions and blocks and later query or
subscribe to their results. Transactions are indexed by TxResult.Events
and
blocks are indexed by Response(Begin|End)Block.Events
. However, transactions
are also indexed by a primary key which includes the transaction hash and maps
to and stores the corresponding TxResult
. Blocks are indexed by a primary key
which includes the block height and maps to and stores the block height, i.e.
the block itself is never stored.
Each event contains a type and a list of attributes, which are key-value pairs
denoting something about what happened during the method's execution. For more
details on Events
, see the
ABCI (opens new window)
documentation.
An Event
has a composite key associated with it. A compositeKey
is
constructed by its type and key separated by a dot.
For example:
would be equal to the composite key of jack.account.number
.
By default, Tendermint will index all transactions by their respective hashes and height and blocks by their height.
Tendermint allows for different events within the same height to have equal attributes.
# Configuration
Operators can configure indexing via the [tx_index]
section. The indexer
field takes a series of supported indexers. If null
is included, indexing will
be turned off regardless of other values provided.
# Supported Indexers
# KV
The kv
indexer type is an embedded key-value store supported by the main
underlying Tendermint database. Using the kv
indexer type allows you to query
for block and transaction events directly against Tendermint's RPC. However, the
query syntax is limited and so this indexer type might be deprecated or removed
entirely in the future.
Implementation and data layout
The kv indexer stores each attribute of an event individually, by creating a composite key of the event type, attribute key, attribute value, height and event sequence.
For example the following events:
will be represented as follows in the store:
The key is thus formed of the event type, the attribute key and value, the event the attribute belongs to (EndBlock
or BeginBlock
),
the height and the event number. The event number is a local variable kept by the indexer and incremented when a new event is processed.
It is an int64
variable and has no other semantics besides being used to associate attributes belonging to the same events within a height.
This variable is not atomically incremented as event indexing is deterministic. Should this ever change, the event id generation
will be broken.
# PostgreSQL
The psql
indexer type allows an operator to enable block and transaction event
indexing by proxying it to an external PostgreSQL instance allowing for the events
to be stored in relational models. Since the events are stored in a RDBMS, operators
can leverage SQL to perform a series of rich and complex queries that are not
supported by the kv
indexer type. Since operators can leverage SQL directly,
searching is not enabled for the psql
indexer type via Tendermint's RPC -- any
such query will fail.
Note, the SQL schema is stored in state/indexer/sink/psql/schema.sql
and operators
must explicitly create the relations prior to starting Tendermint and enabling
the psql
indexer type.
Example:
# Default Indexes
The Tendermint tx and block event indexer indexes a few select reserved events by default.
# Transactions
The following indexes are indexed by default:
tx.height
tx.hash
# Blocks
The following indexes are indexed by default:
block.height
# Adding Events
Applications are free to define which events to index. Tendermint does not
expose functionality to define which events to index and which to ignore. In
your application's DeliverTx
method, add the Events
field with pairs of
UTF-8 encoded strings (e.g. "transfer.sender": "Bob", "transfer.recipient":
"Alice", "transfer.balance": "100").
Example:
If the indexer is not null
, the transaction will be indexed. Each event is
indexed using a composite key in the form of {eventType}.{eventAttribute}={eventValue}
,
e.g. transfer.sender=bob
.
# Querying Transactions Events
You can query for a paginated set of transaction by their events by calling the
/tx_search
RPC endpoint:
If the conditions are related to transaction events and the user wants to make sure the
conditions are true within the same events, the match.event
keyword should be used,
as described below
Check out API docs (opens new window) for more information on query syntax and other options.
# Subscribing to Transactions
Clients can subscribe to transactions with the given tags via WebSocket by providing
a query to /subscribe
RPC endpoint.
Check out API docs (opens new window) for more information on query syntax and other options.
# Querying Block Events
You can query for a paginated set of blocks by their events by calling the
/block_search
RPC endpoint:
# match_events
keyword
The query results in the height number(s) (or transaction hashes when querying transactions) which contain events whose attributes match the query conditions. However, there are two options to query the indexers. To demonstrate the two modes, we reuse the two events where Bob and Tom send money to Alice and query the block indexer. We issue the following query:
The result will return height 1 even though the attributes matching the conditions in the query occurred in different events.
If we wish to retrieve only heights where the attributes occurred within the same event, the query syntax is as follows:
Currently the default behaviour is if match_events
is set to false.
Check out API docs (opens new window) for more information on query syntax and other options.
Backwards compatibility
Up until Tendermint 0.34.25, the event sequence was not stored in the kvstore and the match_events
keyword in the RPC query is not ignored by older versions. Thus, in a network running mixed Tendermint versions, nodes running older versions will still return blocks (or transactions) whose attributes match within different events on the same height.