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.

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