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.

144 lines
4.2 KiB

7 years ago
7 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. Let's take a look at the `[tx_index]` config section:
  27. ```toml
  28. ##### transactions indexer configuration options #####
  29. [tx_index]
  30. # What indexer to use for transactions
  31. #
  32. # Options:
  33. # 1) "null"
  34. # 2) "kv" (default) - the simplest possible indexer, backed by key-value storage (defaults to levelDB; see DBBackend).
  35. indexer = "kv"
  36. ```
  37. By default, Tendermint will index all transactions by their respective hashes
  38. and height and blocks by their height.
  39. You can turn off indexing completely by setting `tx_index` to `null`.
  40. ## Default Indexes
  41. The Tendermint tx and block event indexer indexes a few select reserved events
  42. by default.
  43. ### Transactions
  44. The following indexes are indexed by default:
  45. - `tx.height`
  46. - `tx.hash`
  47. ### Blocks
  48. The following indexes are indexed by default:
  49. - `block.height`
  50. ## Adding Events
  51. Applications are free to define which events to index. Tendermint does not
  52. expose functionality to define which events to index and which to ignore. In
  53. your application's `DeliverTx` method, add the `Events` field with pairs of
  54. UTF-8 encoded strings (e.g. "transfer.sender": "Bob", "transfer.recipient":
  55. "Alice", "transfer.balance": "100").
  56. Example:
  57. ```go
  58. func (app *KVStoreApplication) DeliverTx(req types.RequestDeliverTx) types.Result {
  59. //...
  60. events := []abci.Event{
  61. {
  62. Type: "transfer",
  63. Attributes: []abci.EventAttribute{
  64. {Key: []byte("sender"), Value: []byte("Bob"), Index: true},
  65. {Key: []byte("recipient"), Value: []byte("Alice"), Index: true},
  66. {Key: []byte("balance"), Value: []byte("100"), Index: true},
  67. {Key: []byte("note"), Value: []byte("nothing"), Index: true},
  68. },
  69. },
  70. }
  71. return types.ResponseDeliverTx{Code: code.CodeTypeOK, Events: events}
  72. }
  73. ```
  74. If the indexer is not `null`, the transaction will be indexed. Each event is
  75. indexed using a composite key in the form of `{eventType}.{eventAttribute}={eventValue}`,
  76. e.g. `transfer.sender=bob`.
  77. ## Querying Transactions Events
  78. You can query for a paginated set of transaction by their events by calling the
  79. `/tx_search` RPC endpoint:
  80. ```bash
  81. curl "localhost:26657/tx_search?query=\"message.sender='cosmos1...'\"&prove=true"
  82. ```
  83. Check out [API docs](https://docs.tendermint.com/master/rpc/#/Info/tx_search)
  84. for more information on query syntax and other options.
  85. ## Subscribing to Transactions
  86. Clients can subscribe to transactions with the given tags via WebSocket by providing
  87. a query to `/subscribe` RPC endpoint.
  88. ```json
  89. {
  90. "jsonrpc": "2.0",
  91. "method": "subscribe",
  92. "id": "0",
  93. "params": {
  94. "query": "message.sender='cosmos1...'"
  95. }
  96. }
  97. ```
  98. Check out [API docs](https://docs.tendermint.com/master/rpc/#subscribe) for more information
  99. on query syntax and other options.
  100. ## Querying Blocks Events
  101. You can query for a paginated set of blocks by their events by calling the
  102. `/block_search` RPC endpoint:
  103. ```bash
  104. curl "localhost:26657/block_search?query=\"block.height > 10 AND val_set.num_changed > 0\""
  105. ```
  106. Check out [API docs](https://docs.tendermint.com/master/rpc/#/Info/block_search)
  107. for more information on query syntax and other options.