You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

181 lines
5.7 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. ---
  2. order: 6
  3. ---
  4. # Indexing Transactions
  5. Tendermint allows you to index transactions and blocks and later query or
  6. subscribe to their results. Transactions are indexed by `TxResult.Events` and
  7. blocks are indexed by `Response(Begin|End)Block.Events`. However, transactions
  8. are also indexed by a primary key which includes the transaction hash and maps
  9. to and stores the corresponding `TxResult`. Blocks are indexed by a primary key
  10. which includes the block height and maps to and stores the block height, i.e.
  11. the block itself is never stored.
  12. Each event contains a type and a list of attributes, which are key-value pairs
  13. denoting something about what happened during the method's execution. For more
  14. details on `Events`, see the
  15. [ABCI](https://github.com/tendermint/spec/blob/master/spec/abci/abci.md#events)
  16. documentation.
  17. An `Event` has a composite key associated with it. A `compositeKey` is
  18. constructed by its type and key separated by a dot.
  19. For example:
  20. ```json
  21. "jack": [
  22. "account.number": 100
  23. ]
  24. ```
  25. would be equal to the composite key of `jack.account.number`.
  26. By default, Tendermint will index all transactions by their respective hashes
  27. and height and blocks by their height.
  28. ## Configuration
  29. Operators can configure indexing via the `[tx_index]` section. The `indexer`
  30. field takes a series of supported indexers. If `null` is included, indexing will
  31. be turned off regardless of other values provided.
  32. ```toml
  33. [tx-index]
  34. # The backend database list to back the indexer.
  35. # If list contains null, meaning no indexer service will be used.
  36. #
  37. # The application will set which txs to index. In some cases a node operator will be able
  38. # to decide which txs to index based on configuration set in the application.
  39. #
  40. # Options:
  41. # 1) "null"
  42. # 2) "kv" (default) - the simplest possible indexer, backed by key-value storage (defaults to levelDB; see DBBackend).
  43. # - When "kv" is chosen "tx.height" and "tx.hash" will always be indexed.
  44. # 3) "psql" - the indexer services backed by PostgreSQL.
  45. # indexer = []
  46. ```
  47. ### Supported Indexers
  48. #### KV
  49. The `kv` indexer type is an embedded key-value store supported by the main
  50. underlying Tendermint database. Using the `kv` indexer type allows you to query
  51. for block and transaction events directly against Tendermint's RPC. However, the
  52. query syntax is limited and so this indexer type might be deprecated or removed
  53. entirely in the future.
  54. #### PostgreSQL
  55. The `psql` indexer type allows an operator to enable block and transaction event
  56. indexing by proxying it to an external PostgreSQL instance allowing for the events
  57. to be stored in relational models. Since the events are stored in a RDBMS, operators
  58. can leverage SQL to perform a series of rich and complex queries that are not
  59. supported by the `kv` indexer type. Since operators can leverage SQL directly,
  60. searching is not enabled for the `psql` indexer type via Tendermint's RPC -- any
  61. such query will fail.
  62. Note, the SQL schema is stored in `state/indexer/sink/psql/schema.sql` and operators
  63. must explicitly create the relations prior to starting Tendermint and enabling
  64. the `psql` indexer type.
  65. Example:
  66. ```shell
  67. $ psql ... -f state/indexer/sink/psql/schema.sql
  68. ```
  69. ## Default Indexes
  70. The Tendermint tx and block event indexer indexes a few select reserved events
  71. by default.
  72. ### Transactions
  73. The following indexes are indexed by default:
  74. - `tx.height`
  75. - `tx.hash`
  76. ### Blocks
  77. The following indexes are indexed by default:
  78. - `block.height`
  79. ## Adding Events
  80. Applications are free to define which events to index. Tendermint does not
  81. expose functionality to define which events to index and which to ignore. In
  82. your application's `DeliverTx` method, add the `Events` field with pairs of
  83. UTF-8 encoded strings (e.g. "transfer.sender": "Bob", "transfer.recipient":
  84. "Alice", "transfer.balance": "100").
  85. Example:
  86. ```go
  87. func (app *KVStoreApplication) DeliverTx(req types.RequestDeliverTx) types.Result {
  88. //...
  89. events := []abci.Event{
  90. {
  91. Type: "transfer",
  92. Attributes: []abci.EventAttribute{
  93. {Key: []byte("sender"), Value: []byte("Bob"), Index: true},
  94. {Key: []byte("recipient"), Value: []byte("Alice"), Index: true},
  95. {Key: []byte("balance"), Value: []byte("100"), Index: true},
  96. {Key: []byte("note"), Value: []byte("nothing"), Index: true},
  97. },
  98. },
  99. }
  100. return types.ResponseDeliverTx{Code: code.CodeTypeOK, Events: events}
  101. }
  102. ```
  103. If the indexer is not `null`, the transaction will be indexed. Each event is
  104. indexed using a composite key in the form of `{eventType}.{eventAttribute}={eventValue}`,
  105. e.g. `transfer.sender=bob`.
  106. ## Querying Transactions Events
  107. You can query for a paginated set of transaction by their events by calling the
  108. `/tx_search` RPC endpoint:
  109. ```bash
  110. curl "localhost:26657/tx_search?query=\"message.sender='cosmos1...'\"&prove=true"
  111. ```
  112. Check out [API docs](https://docs.tendermint.com/master/rpc/#/Info/tx_search)
  113. for more information on query syntax and other options.
  114. ## Subscribing to Transactions
  115. Clients can subscribe to transactions with the given tags via WebSocket by providing
  116. a query to `/subscribe` RPC endpoint.
  117. ```json
  118. {
  119. "jsonrpc": "2.0",
  120. "method": "subscribe",
  121. "id": "0",
  122. "params": {
  123. "query": "message.sender='cosmos1...'"
  124. }
  125. }
  126. ```
  127. Check out [API docs](https://docs.tendermint.com/master/rpc/#subscribe) for more information
  128. on query syntax and other options.
  129. ## Querying Blocks Events
  130. You can query for a paginated set of blocks by their events by calling the
  131. `/block_search` RPC endpoint:
  132. ```bash
  133. curl "localhost:26657/block_search?query=\"block.height > 10 AND val_set.num_changed > 0\""
  134. ```
  135. Check out [API docs](https://docs.tendermint.com/master/rpc/#/Info/block_search)
  136. for more information on query syntax and other options.