Indexing Transactions
CometBFT allows you to index transactions and blocks and later query or subscribe to their results. Transactions are indexed byResponseFinalizeBlock.tx_results.events and
blocks are indexed by ResponseFinalizeBlock.events. However, transactions
are also indexed by a primary key which includes the transaction hash and maps
to and stores the corresponding transaction results. 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 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:
jack.account.number.
By default, CometBFT will index all transactions by their respective hashes
and height and blocks by their height.
CometBFT 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
Thekv indexer type is an embedded key-value store supported by the main
underlying CometBFT database. Using the kv indexer type allows you to query
for block and transaction events directly against CometBFT’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
with
- event type,
- attribute key,
- attribute value,
- event generator (e.g.
FinalizeBlock) - the height, and
- event counter. For example the following events:
FinalizeBlock call for height 1:
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
Thepsql 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 CometBFT’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 CometBFT and enabling
the psql indexer type.
Example:
Default Indexes
The CometBFT tx and block event indexer indexes a few select reserved events by default.Transactions
The following indexes are indexed by default:tx.heighttx.hash
Blocks
The following indexes are indexed by default:block.height
Adding Events
Applications are free to define which events to index. CometBFT does not expose functionality to define which events to index and which to ignore. In your application’sFinalizeBlock method, add the Events field with pairs of
UTF-8 encoded strings (e.g. “transfer.sender”: “Bob”, “transfer.recipient”:
“Alice”, “transfer.balance”: “100”).
Example:
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:
Subscribing to Transactions
Clients can subscribe to transactions with the given tags via WebSocket by providing a query to/subscribe RPC endpoint.
Querying Block Events
You can query for a paginated set of blocks by their events by calling the/block_search RPC endpoint:
Event attribute value types
Users can use anything as an event value. However, if the event attribute value is a number, the following needs to be taken into account:- Negative numbers will not be properly retrieved when querying the indexer.
- Event values are converted to big floats (from the
big/mathpackage). The precision of the floating point number is set to the bit length of the integer it is supposed to represent, so that there is no loss of information due to insufficient precision. This was not present before CometBFT v0.38.x and all float values were ignored. - As of CometBFT v0.38.x, queries can contain floating point numbers as well.
- Note that comparing to floats can be imprecise with a high number of decimals.
Event type and attribute key format
An event type/attribute key is a string that can contain any Unicode letter or digit, as well as the following characters:. (dot), - (dash), _
(underscore). The event type/attribute key must not start with - (dash) or
. (dot).