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.

113 lines
3.4 KiB

7 years ago
7 years ago
7 years ago
6 years ago
6 years ago
7 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 later query or subscribe
  6. to their results.
  7. Events can be used to index transactions and blocks according to what happened
  8. during their execution. Note that the set of events returned for a block from
  9. `BeginBlock` and `EndBlock` are merged. In case both methods return the same
  10. type, only the key-value pairs defined in `EndBlock` are used.
  11. Each event contains a type and a list of attributes, which are key-value pairs
  12. denoting something about what happened during the method's execution. For more
  13. details on `Events`, see the
  14. [ABCI](https://github.com/tendermint/spec/blob/master/spec/abci/abci.md#events)
  15. documentation.
  16. An Event has a composite key associated with it. A `compositeKey` is
  17. constructed by its type and key separated by a dot.
  18. For example:
  19. ```json
  20. "jack": [
  21. "account.number": 100
  22. ]
  23. ```
  24. would be equal to the composite key of `jack.account.number`.
  25. Let's take a look at the `[tx_index]` config section:
  26. ```toml
  27. ##### transactions indexer configuration options #####
  28. [tx_index]
  29. # What indexer to use for transactions
  30. #
  31. # Options:
  32. # 1) "null"
  33. # 2) "kv" (default) - the simplest possible indexer, backed by key-value storage (defaults to levelDB; see DBBackend).
  34. indexer = "kv"
  35. ```
  36. By default, Tendermint will index all transactions by their respective
  37. hashes and height using an embedded simple indexer.
  38. You can turn off indexing completely by setting `tx_index` to `null`.
  39. ## Adding Events
  40. Applications are free to define which events to index. Tendermint does not
  41. expose functionality to define which events to index and which to ignore. In
  42. your application's `DeliverTx` method, add the `Events` field with pairs of
  43. UTF-8 encoded strings (e.g. "transfer.sender": "Bob", "transfer.recipient":
  44. "Alice", "transfer.balance": "100").
  45. Example:
  46. ```go
  47. func (app *KVStoreApplication) DeliverTx(req types.RequestDeliverTx) types.Result {
  48. //...
  49. events := []abci.Event{
  50. {
  51. Type: "transfer",
  52. Attributes: []abci.EventAttribute{
  53. {Key: []byte("sender"), Value: []byte("Bob"), Index: true},
  54. {Key: []byte("recipient"), Value: []byte("Alice"), Index: true},
  55. {Key: []byte("balance"), Value: []byte("100"), Index: true},
  56. {Key: []byte("note"), Value: []byte("nothing"), Index: true},
  57. },
  58. },
  59. }
  60. return types.ResponseDeliverTx{Code: code.CodeTypeOK, Events: events}
  61. }
  62. ```
  63. The transaction will be indexed (if the indexer is not `null`) with a certain
  64. attribute if the attribute's `Index` field is set to `true`. In the above
  65. example, all attributes will be indexed.
  66. ## Querying Transactions
  67. You can query the transaction results by calling `/tx_search` RPC endpoint:
  68. ```bash
  69. curl "localhost:26657/tx_search?query=\"account.name='igor'\"&prove=true"
  70. ```
  71. Check out [API docs](https://docs.tendermint.com/master/rpc/#/Info/tx_search) for more information
  72. on query syntax and other options.
  73. ## Subscribing to Transactions
  74. Clients can subscribe to transactions with the given tags via WebSocket by providing
  75. a query to `/subscribe` RPC endpoint.
  76. ```json
  77. {
  78. "jsonrpc": "2.0",
  79. "method": "subscribe",
  80. "id": "0",
  81. "params": {
  82. "query": "account.name='igor'"
  83. }
  84. }
  85. ```
  86. Check out [API docs](https://docs.tendermint.com/master/rpc/#subscribe) for more information
  87. on query syntax and other options.