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.

63 lines
1.8 KiB

  1. package sink
  2. import (
  3. "errors"
  4. "strings"
  5. "github.com/tendermint/tendermint/config"
  6. "github.com/tendermint/tendermint/internal/state/indexer"
  7. "github.com/tendermint/tendermint/internal/state/indexer/sink/kv"
  8. "github.com/tendermint/tendermint/internal/state/indexer/sink/null"
  9. "github.com/tendermint/tendermint/internal/state/indexer/sink/psql"
  10. )
  11. // EventSinksFromConfig constructs a slice of indexer.EventSink using the provided
  12. // configuration.
  13. func EventSinksFromConfig(cfg *config.Config, dbProvider config.DBProvider, chainID string) ([]indexer.EventSink, error) {
  14. if len(cfg.TxIndex.Indexer) == 0 {
  15. return []indexer.EventSink{null.NewEventSink()}, nil
  16. }
  17. // check for duplicated sinks
  18. sinks := map[string]struct{}{}
  19. for _, s := range cfg.TxIndex.Indexer {
  20. sl := strings.ToLower(s)
  21. if _, ok := sinks[sl]; ok {
  22. return nil, errors.New("found duplicated sinks, please check the tx-index section in the config.toml")
  23. }
  24. sinks[sl] = struct{}{}
  25. }
  26. eventSinks := []indexer.EventSink{}
  27. for k := range sinks {
  28. switch indexer.EventSinkType(k) {
  29. case indexer.NULL:
  30. // When we see null in the config, the eventsinks will be reset with the
  31. // nullEventSink.
  32. return []indexer.EventSink{null.NewEventSink()}, nil
  33. case indexer.KV:
  34. store, err := dbProvider(&config.DBContext{ID: "tx_index", Config: cfg})
  35. if err != nil {
  36. return nil, err
  37. }
  38. eventSinks = append(eventSinks, kv.NewEventSink(store))
  39. case indexer.PSQL:
  40. conn := cfg.TxIndex.PsqlConn
  41. if conn == "" {
  42. return nil, errors.New("the psql connection settings cannot be empty")
  43. }
  44. es, err := psql.NewEventSink(conn, chainID)
  45. if err != nil {
  46. return nil, err
  47. }
  48. eventSinks = append(eventSinks, es)
  49. default:
  50. return nil, errors.New("unsupported event sink type")
  51. }
  52. }
  53. return eventSinks, nil
  54. }