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.

133 lines
4.2 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
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 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 [ABCI]https://github.com/tendermint/spec/blob/master/spec/abci/abci.md#events) documentation.
  14. An Event has a composite key associated with it. A `compositeKey` is constructed by its type and key separated by a dot.
  15. For example:
  16. ```json
  17. "jack": [
  18. "account.number": 100
  19. ]
  20. ```
  21. would be equal to the composite key of `jack.account.number`.
  22. Let's take a look at the `[tx_index]` config section:
  23. ```toml
  24. ##### transactions indexer configuration options #####
  25. [tx_index]
  26. # What indexer to use for transactions
  27. #
  28. # Options:
  29. # 1) "null"
  30. # 2) "kv" (default) - the simplest possible indexer, backed by key-value storage (defaults to levelDB; see DBBackend).
  31. indexer = "kv"
  32. # Comma-separated list of composite keys to index (by default the only key is "tx.hash")
  33. #
  34. # You can also index transactions by height by adding "tx.height" key here.
  35. #
  36. # It's recommended to index only a subset of keys due to possible memory
  37. # bloat. This is, of course, depends on the indexer's DB and the volume of
  38. # transactions.
  39. index_keys = ""
  40. # When set to true, tells indexer to index all compositeKeys (predefined keys:
  41. # "tx.hash", "tx.height" and all keys from DeliverTx responses).
  42. #
  43. # Note this may be not desirable (see the comment above). Indexkeys has a
  44. # precedence over IndexAllKeys (i.e. when given both, IndexKeys will be
  45. # indexed).
  46. index_all_keys = false
  47. ```
  48. By default, Tendermint will index all transactions by their respective
  49. hashes using an embedded simple indexer. Note, we are planning to add
  50. more options in the future (e.g., PostgreSQL indexer).
  51. ## Adding Events
  52. In your application's `DeliverTx` method, add the `Events` field with pairs of
  53. UTF-8 encoded strings (e.g. "transfer.sender": "Bob", "transfer.recipient": "Alice",
  54. "transfer.balance": "100").
  55. Example:
  56. ```go
  57. func (app *KVStoreApplication) DeliverTx(req types.RequestDeliverTx) types.Result {
  58. //...
  59. events := []abci.Event{
  60. {
  61. Type: "transfer",
  62. Attributes: []abci.EventAttribute{
  63. {Key: []byte("sender"), Value: []byte("Bob")},
  64. {Key: []byte("recipient"), Value: []byte("Alice")},
  65. {Key: []byte("balance"), Value: []byte("100")},
  66. {Key: []byte("note"), Value: []byte("nothing"), Index: true},
  67. },
  68. },
  69. }
  70. return types.ResponseDeliverTx{Code: code.CodeTypeOK, Events: events}
  71. }
  72. ```
  73. If you want Tendermint to only index transactions by "transfer.sender" event type,
  74. in the config set `tx_index.index_tags="transfer.sender"`. If you to index all events,
  75. set `index_all_tags=true`
  76. Note, there are a few predefined event types:
  77. - `tx.hash` (transaction's hash)
  78. - `tx.height` (height of the block transaction was committed in)
  79. Tendermint will throw a warning if you try to use any of the above keys.
  80. The index will be added if the `Index` field of attribute is set to true. In above example, querying
  81. using `transfer.note` will work.
  82. ## Querying Transactions
  83. You can query the transaction results by calling `/tx_search` RPC endpoint:
  84. ```shell
  85. curl "localhost:26657/tx_search?query=\"account.name='igor'\"&prove=true"
  86. ```
  87. Check out [API docs](https://docs.tendermint.com/master/rpc/#/Info/tx_search) for more information
  88. on query syntax and other options.
  89. ## Subscribing to Transactions
  90. Clients can subscribe to transactions with the given tags via WebSocket by providing
  91. a query to `/subscribe` RPC endpoint.
  92. ```json
  93. {
  94. "jsonrpc": "2.0",
  95. "method": "subscribe",
  96. "id": "0",
  97. "params": {
  98. "query": "account.name='igor'"
  99. }
  100. }
  101. ```
  102. Check out [API docs](https://docs.tendermint.com/master/rpc/#subscribe) for more information
  103. on query syntax and other options.