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.

114 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
7 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 using an embedded simple indexer. Note, we are planning to add
  38. more options in the future (e.g., PostgreSQL indexer).
  39. You can turn off indexing completely by setting `tx_index` to `null`.
  40. ## Adding Events
  41. Applications are free to define which events to index. Tendermint does not
  42. expose functionality to define which events to index and which to ignore. In
  43. your application's `DeliverTx` method, add the `Events` field with pairs of
  44. UTF-8 encoded strings (e.g. "transfer.sender": "Bob", "transfer.recipient":
  45. "Alice", "transfer.balance": "100").
  46. Example:
  47. ```go
  48. func (app *KVStoreApplication) DeliverTx(req types.RequestDeliverTx) types.Result {
  49. //...
  50. events := []abci.Event{
  51. {
  52. Type: "transfer",
  53. Attributes: []abci.EventAttribute{
  54. {Key: []byte("sender"), Value: []byte("Bob"), Index: true},
  55. {Key: []byte("recipient"), Value: []byte("Alice"), Index: true},
  56. {Key: []byte("balance"), Value: []byte("100"), Index: true},
  57. {Key: []byte("note"), Value: []byte("nothing"), Index: true},
  58. },
  59. },
  60. }
  61. return types.ResponseDeliverTx{Code: code.CodeTypeOK, Events: events}
  62. }
  63. ```
  64. The transaction will be indexed (if the indexer is not `null`) with a certain
  65. attribute if the attribute's `Index` field is set to `true`. In the above
  66. example, all attributes will be used.
  67. ## Querying Transactions
  68. You can query the transaction results by calling `/tx_search` RPC endpoint:
  69. ```bash
  70. curl "localhost:26657/tx_search?query=\"account.name='igor'\"&prove=true"
  71. ```
  72. Check out [API docs](https://docs.tendermint.com/master/rpc/#/Info/tx_search) for more information
  73. on query syntax and other options.
  74. ## Subscribing to Transactions
  75. Clients can subscribe to transactions with the given tags via WebSocket by providing
  76. a query to `/subscribe` RPC endpoint.
  77. ```json
  78. {
  79. "jsonrpc": "2.0",
  80. "method": "subscribe",
  81. "id": "0",
  82. "params": {
  83. "query": "account.name='igor'"
  84. }
  85. }
  86. ```
  87. Check out [API docs](https://docs.tendermint.com/master/rpc/#subscribe) for more information
  88. on query syntax and other options.