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.

106 lines
3.3 KiB

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