Browse Source

Update indexing-transactions.md (#4045)

* Update indexing-transactions.md

* Toml instead of Yaml
pull/4049/head
Alexander Bezobchuk 5 years ago
committed by Marko
parent
commit
9ad9c5a786
1 changed files with 42 additions and 29 deletions
  1. +42
    -29
      docs/app-dev/indexing-transactions.md

+ 42
- 29
docs/app-dev/indexing-transactions.md View File

@ -3,9 +3,18 @@
Tendermint allows you to index transactions and later query or subscribe
to their results.
Events can be used to index transactions and blocks according to what happened
during their execution. Note that the set of events returned for a block from
`BeginBlock` and `EndBlock` are merged. In case both methods return the same
tag, only the value defined in `EndBlock` is used.
Each event contains a type and a list of attributes, which are key-value pairs
denoting something about what happened during the method's execution. For more
details on `Events`, see the [ABCI](../spec/abci/abci.md) documentation.
Let's take a look at the `[tx_index]` config section:
```
```toml
##### transactions indexer configuration options #####
[tx_index]
@ -36,57 +45,61 @@ index_all_tags = false
By default, Tendermint will index all transactions by their respective
hashes using an embedded simple indexer. Note, we are planning to add
more options in the future (e.g., Postgresql indexer).
more options in the future (e.g., PostgreSQL indexer).
## Adding tags
## Adding Events
In your application's `DeliverTx` method, add the `Tags` field with the
pairs of UTF-8 encoded strings (e.g. "account.owner": "Bob", "balance":
"100.0", "date": "2018-01-02").
In your application's `DeliverTx` method, add the `Events` field with pairs of
UTF-8 encoded strings (e.g. "transfer.sender": "Bob", "transfer.recipient": "Alice",
"transfer.balance": "100").
Example:
```
```go
func (app *KVStoreApplication) DeliverTx(req types.RequestDeliverTx) types.Result {
...
tags := []cmn.KVPair{
{[]byte("account.name"), []byte("igor")},
{[]byte("account.address"), []byte("0xdeadbeef")},
{[]byte("tx.amount"), []byte("7")},
//...
events := []abci.Event{
{
Type: "transfer",
Attributes: cmn.KVPairs{
cmn.KVPair{Key: []byte("sender"), Value: []byte("Bob")},
cmn.KVPair{Key: []byte("recipient"), Value: []byte("Alice")},
cmn.KVPair{Key: []byte("balance"), Value: []byte("100")},
},
},
}
return types.ResponseDeliverTx{Code: code.CodeTypeOK, Tags: tags}
return types.ResponseDeliverTx{Code: code.CodeTypeOK, Events: events}
}
```
If you want Tendermint to only index transactions by "account.name" tag,
in the config set `tx_index.index_tags="account.name"`. If you to index
all tags, set `index_all_tags=true`
If you want Tendermint to only index transactions by "transfer.sender" event type,
in the config set `tx_index.index_tags="transfer.sender"`. If you to index all events,
set `index_all_tags=true`
Note, there are a few predefined tags:
Note, there are a few predefined event types:
- `tx.hash` (transaction's hash)
- `tx.height` (height of the block transaction was committed in)
Tendermint will throw a warning if you try to use any of the above keys.
## Querying transactions
## Querying Transactions
You can query the transaction results by calling `/tx_search` RPC
endpoint:
You can query the transaction results by calling `/tx_search` RPC endpoint:
```
```shell
curl "localhost:26657/tx_search?query=\"account.name='igor'\"&prove=true"
```
Check out [API docs](https://tendermint.com/rpc/#txsearch)
for more information on query syntax and other options.
Check out [API docs](https://tendermint.com/rpc/#txsearch) for more information
on query syntax and other options.
## Subscribing to transactions
## Subscribing to Transactions
Clients can subscribe to transactions with the given tags via Websocket
by providing a query to `/subscribe` RPC endpoint.
Clients can subscribe to transactions with the given tags via Websocket by providing
a query to `/subscribe` RPC endpoint.
```
```json
{
"jsonrpc": "2.0",
"method": "subscribe",
@ -97,5 +110,5 @@ by providing a query to `/subscribe` RPC endpoint.
}
```
Check out [API docs](https://tendermint.com/rpc/#subscribe) for
more information on query syntax and other options.
Check out [API docs](https://tendermint.com/rpc/#subscribe) for more information
on query syntax and other options.

Loading…
Cancel
Save