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.

129 lines
3.9 KiB

7 years ago
7 years ago
7 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 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](../spec/abci/abci.md) 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: kv.Pairs{
  63. kv.Pair{Key: []byte("sender"), Value: []byte("Bob")},
  64. kv.Pair{Key: []byte("recipient"), Value: []byte("Alice")},
  65. kv.Pair{Key: []byte("balance"), Value: []byte("100")},
  66. },
  67. },
  68. }
  69. return types.ResponseDeliverTx{Code: code.CodeTypeOK, Events: events}
  70. }
  71. ```
  72. If you want Tendermint to only index transactions by "transfer.sender" event type,
  73. in the config set `tx_index.index_tags="transfer.sender"`. If you to index all events,
  74. set `index_all_tags=true`
  75. Note, there are a few predefined event types:
  76. - `tx.hash` (transaction's hash)
  77. - `tx.height` (height of the block transaction was committed in)
  78. Tendermint will throw a warning if you try to use any of the above keys.
  79. ## Querying Transactions
  80. You can query the transaction results by calling `/tx_search` RPC endpoint:
  81. ```shell
  82. curl "localhost:26657/tx_search?query=\"account.name='igor'\"&prove=true"
  83. ```
  84. Check out [API docs](https://docs.tendermint.com/master/rpc/#/Info/tx_search) for more information
  85. on query syntax and other options.
  86. ## Subscribing to Transactions
  87. Clients can subscribe to transactions with the given tags via WebSocket by providing
  88. a query to `/subscribe` RPC endpoint.
  89. ```json
  90. {
  91. "jsonrpc": "2.0",
  92. "method": "subscribe",
  93. "id": "0",
  94. "params": {
  95. "query": "account.name='igor'"
  96. }
  97. }
  98. ```
  99. Check out [API docs](https://docs.tendermint.com/master/rpc/#subscribe) for more information
  100. on query syntax and other options.