From 4f8bcb1cce50b533fa27deaefa77ac971c4e0fab Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Tue, 6 Jul 2021 08:48:05 -0400 Subject: [PATCH] docs: update events (#6658) * docs: update events * lint++ * lint++ --- docs/app-dev/indexing-transactions.md | 53 +++++++++++++++++++++++---- node/setup.go | 11 +++++- state/indexer/sink/psql/schema.sql | 4 +- 3 files changed, 55 insertions(+), 13 deletions(-) diff --git a/docs/app-dev/indexing-transactions.md b/docs/app-dev/indexing-transactions.md index 78161a040..15108cb05 100644 --- a/docs/app-dev/indexing-transactions.md +++ b/docs/app-dev/indexing-transactions.md @@ -31,24 +31,61 @@ For example: would be equal to the composite key of `jack.account.number`. -Let's take a look at the `[tx_index]` config section: +By default, Tendermint will index all transactions by their respective hashes +and height and blocks by their height. + +## Configuration + +Operators can configure indexing via the `[tx_index]` section. The `indexer` +field takes a series of supported indexers. If `null` is included, indexing will +be turned off regardless of other values provided. ```toml -##### transactions indexer configuration options ##### -[tx_index] +[tx-index] -# What indexer to use for transactions +# The backend database list to back the indexer. +# If list contains null, meaning no indexer service will be used. +# +# The application will set which txs to index. In some cases a node operator will be able +# to decide which txs to index based on configuration set in the application. # # Options: # 1) "null" # 2) "kv" (default) - the simplest possible indexer, backed by key-value storage (defaults to levelDB; see DBBackend). -indexer = "kv" +# - When "kv" is chosen "tx.height" and "tx.hash" will always be indexed. +# 3) "psql" - the indexer services backed by PostgreSQL. +# indexer = [] ``` -By default, Tendermint will index all transactions by their respective hashes -and height and blocks by their height. +### Supported Indexers + +#### KV -You can turn off indexing completely by setting `tx_index` to `null`. +The `kv` indexer type is an embedded key-value store supported by the main +underling Tendermint database. Using the `kv` indexer type allows you to query +for block and transaction events directly against Tendermint's RPC. However, the +query syntax is limited and so this indexer type might be deprecated or removed +entirely in the future. + +#### PostgreSQL + +The `psql` indexer type allows an operator to enable block and transaction event +indexing by proxying it to an external PostgreSQL instance allowing for the events +to be stored in relational models. Since the events are stored in a RDBMS, operators +can leverage SQL to perform a series of rich and complex queries that are not +supported by the `kv` indexer type. Since operators can leverage SQL directly, +searching is not enabled for the `psql` indexer type via Tendermint's RPC -- any +such query will fail. + +Note, the SQL schema is stored in `state/indexer/sink/psql/schema.sql` and operators +must explicitly create the relations prior to starting Tendermint and enabling +the `psql` indexer type. + +Example: + +```shell +$ psql ... -f state/indexer/sink/psql/schema.sql +``` ## Default Indexes diff --git a/node/setup.go b/node/setup.go index a3d88267e..0e5fc0b3b 100644 --- a/node/setup.go +++ b/node/setup.go @@ -81,13 +81,14 @@ func createAndStartIndexerService( eventSinks := []indexer.EventSink{} - // Check duplicated sinks. + // check for duplicated sinks sinks := map[string]bool{} for _, s := range config.TxIndex.Indexer { sl := strings.ToLower(s) if sinks[sl] { return nil, nil, errors.New("found duplicated sinks, please check the tx-index section in the config.toml") } + sinks[sl] = true } @@ -95,25 +96,31 @@ loop: for k := range sinks { switch k { case string(indexer.NULL): - // when we see null in the config, the eventsinks will be reset with the nullEventSink. + // When we see null in the config, the eventsinks will be reset with the + // nullEventSink. eventSinks = []indexer.EventSink{null.NewEventSink()} break loop + case string(indexer.KV): store, err := dbProvider(&cfg.DBContext{ID: "tx_index", Config: config}) if err != nil { return nil, nil, err } + eventSinks = append(eventSinks, kv.NewEventSink(store)) + case string(indexer.PSQL): conn := config.TxIndex.PsqlConn if conn == "" { return nil, nil, errors.New("the psql connection settings cannot be empty") } + es, _, err := psql.NewEventSink(conn, chainID) if err != nil { return nil, nil, err } eventSinks = append(eventSinks, es) + default: return nil, nil, errors.New("unsupported event sink type") } diff --git a/state/indexer/sink/psql/schema.sql b/state/indexer/sink/psql/schema.sql index 0be9bdfa8..0563136e2 100644 --- a/state/indexer/sink/psql/schema.sql +++ b/state/indexer/sink/psql/schema.sql @@ -25,9 +25,7 @@ CREATE TABLE tx_events ( created_at TIMESTAMPTZ NOT NULL, chain_id VARCHAR NOT NULL, UNIQUE (hash, key), - FOREIGN KEY (tx_result_id) - REFERENCES tx_results(id) - ON DELETE CASCADE + FOREIGN KEY (tx_result_id) REFERENCES tx_results(id) ON DELETE CASCADE ); CREATE INDEX idx_block_events_key_value ON block_events(key, value); CREATE INDEX idx_tx_events_key_value ON tx_events(key, value);