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.

111 lines
3.4 KiB

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