Skip to content

Commit

Permalink
Add superfluous key checks in a generalized way to all configs, use t…
Browse files Browse the repository at this point in the history
…hese in the prerun of each command
  • Loading branch information
pharr117 committed Oct 16, 2023
1 parent 1d107c2 commit 45f9882
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 1 deletion.
6 changes: 6 additions & 0 deletions cmd/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ func setupIndex(cmd *cobra.Command, args []string) error {
return err
}

ignoredKeys := config.CheckSuperfluousIndexKeys(viperConf.AllKeys())

if len(ignoredKeys) > 0 {
config.Log.Warnf("Warning, the following invalid keys will be ignored: %v", ignoredKeys)
}

setupLogger(indexer.cfg.Log.Level, indexer.cfg.Log.Path, indexer.cfg.Log.Pretty)

// 0 is an invalid starting block, set it to 1
Expand Down
6 changes: 6 additions & 0 deletions cmd/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ func setupQuery(cmd *cobra.Command, args []string) error {
return err
}

ignoredKeys := config.CheckSuperfluousQueryKeys(viperConf.AllKeys())

if len(ignoredKeys) > 0 {
config.Log.Warnf("Warning, the following invalid keys will be ignored: %v", ignoredKeys)
}

setupLogger(queryConfig.Log.Level, queryConfig.Log.Path, queryConfig.Log.Pretty)

db, err := connectToDBAndMigrate(queryConfig.Database)
Expand Down
6 changes: 6 additions & 0 deletions cmd/update_denoms.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ func setupUpdateDenoms(cmd *cobra.Command, args []string) error {
return err
}

ignoredKeys := config.CheckSuperfluousUpdateDenomsKeys(viperConf.AllKeys())

if len(ignoredKeys) > 0 {
config.Log.Warnf("Warning, the following invalid keys will be ignored: %v", ignoredKeys)
}

setupLogger(updateDenomsConfig.Log.Level, updateDenomsConfig.Log.Path, updateDenomsConfig.Log.Pretty)

db, err := connectToDBAndMigrate(updateDenomsConfig.Database)
Expand Down
6 changes: 6 additions & 0 deletions cmd/update_epochs.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ func setupUpdateEpochs(cmd *cobra.Command, args []string) error {
return err
}

ignoredKeys := config.CheckSuperfluousUpdateDenomsKeys(viperConf.AllKeys())

if len(ignoredKeys) > 0 {
config.Log.Warnf("Warning, the following invalid keys will be ignored: %v", ignoredKeys)
}

setupLogger(updateEpochsConfig.Log.Level, updateEpochsConfig.Log.Path, updateEpochsConfig.Log.Pretty)

db, err := connectToDBAndMigrate(updateEpochsConfig.Database)
Expand Down
47 changes: 46 additions & 1 deletion config/common_configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"errors"
"fmt"
"reflect"
"strings"

"github.com/DefiantLabs/cosmos-indexer/util"
Expand Down Expand Up @@ -33,7 +34,7 @@ type lens struct {
}

type throttlingBase struct {
Throttling float64
Throttling float64 `mapstructure:"throttling"`
}

type retryBase struct {
Expand Down Expand Up @@ -118,3 +119,47 @@ func validateThrottlingConf(throttlingConf throttlingBase) error {
}
return nil
}

// Reads the Viper mapstructure tag to get the valid keys for a given config struct
func getValidConfigKeys(section any, baseName string) (keys []string) {
v := reflect.ValueOf(section)
typeOfS := v.Type()

if baseName == "" {
baseName = strings.ToLower(typeOfS.Name())
}

for i := 0; i < v.NumField(); i++ {
field := typeOfS.Field(i)

// Hack to get around the fact that we have embedded struct inside a struct in some of our definitions
if !strings.HasPrefix(field.Type.String(), "config.") {
name := field.Tag.Get("mapstructure")
if name == "" {
name = field.Name
}

key := fmt.Sprintf("%v.%v", baseName, strings.ReplaceAll(strings.ToLower(name), " ", ""))
keys = append(keys, key)
}
}
return
}

func addDatabaseConfigKeys(validKeys map[string]struct{}) {
for _, key := range getValidConfigKeys(Database{}, "") {
validKeys[key] = struct{}{}
}
}

func addLogConfigKeys(validKeys map[string]struct{}) {
for _, key := range getValidConfigKeys(log{}, "") {
validKeys[key] = struct{}{}
}
}

func addLensConfigKeys(validKeys map[string]struct{}) {
for _, key := range getValidConfigKeys(lens{}, "") {
validKeys[key] = struct{}{}
}
}
31 changes: 31 additions & 0 deletions config/index_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,34 @@ func (conf *IndexConfig) Validate() error {

return nil
}

func CheckSuperfluousIndexKeys(keys []string) []string {
validKeys := make(map[string]struct{})

addDatabaseConfigKeys(validKeys)
addLogConfigKeys(validKeys)
addLensConfigKeys(validKeys)

// add base keys
for _, key := range getValidConfigKeys(indexBase{}, "base") {
validKeys[key] = struct{}{}
}

for _, key := range getValidConfigKeys(throttlingBase{}, "base") {
validKeys[key] = struct{}{}
}

for _, key := range getValidConfigKeys(retryBase{}, "base") {
validKeys[key] = struct{}{}
}

// Check keys
ignoredKeys := make([]string, 0)
for _, key := range keys {
if _, ok := validKeys[key]; !ok {
ignoredKeys = append(ignoredKeys, key)
}
}

return ignoredKeys
}
22 changes: 22 additions & 0 deletions config/query_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,25 @@ func (conf *QueryConfig) Validate(validCsvParsers []string) error {

return nil
}

func CheckSuperfluousQueryKeys(keys []string) []string {
validKeys := make(map[string]struct{})

addDatabaseConfigKeys(validKeys)
addLogConfigKeys(validKeys)

// add base keys
for _, key := range getValidConfigKeys(queryBase{}, "base") {
validKeys[key] = struct{}{}
}

// Check keys
ignoredKeys := make([]string, 0)
for _, key := range keys {
if _, ok := validKeys[key]; !ok {
ignoredKeys = append(ignoredKeys, key)
}
}

return ignoredKeys
}
27 changes: 27 additions & 0 deletions config/update_denoms_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,30 @@ func (conf *UpdateDenomsConfig) Validate() error {

return nil
}

func CheckSuperfluousUpdateDenomsKeys(keys []string) []string {
validKeys := make(map[string]struct{})

addDatabaseConfigKeys(validKeys)
addLogConfigKeys(validKeys)
addLensConfigKeys(validKeys)

// add base keys
for _, key := range getValidConfigKeys(updateDenomsBase{}, "base") {
validKeys[key] = struct{}{}
}

for _, key := range getValidConfigKeys(retryBase{}, "base") {
validKeys[key] = struct{}{}
}

// Check keys
ignoredKeys := make([]string, 0)
for _, key := range keys {
if _, ok := validKeys[key]; !ok {
ignoredKeys = append(ignoredKeys, key)
}
}

return ignoredKeys
}
27 changes: 27 additions & 0 deletions config/update_epochs_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,30 @@ func (conf *UpdateEpochsConfig) Validate() error {

return nil
}

func CheckSuperfluousUpdateEpochsKeys(keys []string) []string {
validKeys := make(map[string]struct{})

addDatabaseConfigKeys(validKeys)
addLogConfigKeys(validKeys)
addLensConfigKeys(validKeys)

// add base keys
for _, key := range getValidConfigKeys(updateEpochsBase{}, "base") {
validKeys[key] = struct{}{}
}

for _, key := range getValidConfigKeys(throttlingBase{}, "base") {
validKeys[key] = struct{}{}
}

// Check keys
ignoredKeys := make([]string, 0)
for _, key := range keys {
if _, ok := validKeys[key]; !ok {
ignoredKeys = append(ignoredKeys, key)
}
}

return ignoredKeys
}

0 comments on commit 45f9882

Please sign in to comment.