diff --git a/README.md b/README.md index 38aac80..74ac2d6 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ [![Go Report Card](https://goreportcard.com/badge/github.com/vulcanize/eth-ipfs-state-validator)](https://goreportcard.com/report/github.com/vulcanize/eth-ipfs-state-validator) -> Uses [ipfs-ethdb](https://github.com/vulcanize/ipfs-ethdb/postgres) to validate completeness of IPFS Ethereum state data +> Uses [ipfs-ethdb](https://github.com/vulcanize/ipfs-ethdb/tree/master/postgres) to validate completeness of IPFS Ethereum state data ## Background diff --git a/cmd/root.go b/cmd/root.go index 591a702..fc41502 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -16,7 +16,9 @@ package cmd import ( + "fmt" "os" + "strings" "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -24,14 +26,9 @@ import ( ) var ( - subCommand string - logWithCommand logrus.Entry - stateRootStr string - storageRootStr string - validationType string - contractAddrStr string - cfgFile string - ipfsPath string + subCommand string + logWithCommand logrus.Entry + cfgFile string ) // rootCmd represents the base command when called without any subcommands @@ -82,6 +79,8 @@ func logLevel() error { } func init() { + cobra.OnInitialize(initConfig) + viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) viper.AutomaticEnv() rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file location") @@ -101,3 +100,16 @@ func init() { viper.BindPFlag("database.password", rootCmd.PersistentFlags().Lookup("database-password")) viper.BindPFlag("log.level", rootCmd.PersistentFlags().Lookup("log-level")) } + +func initConfig() { + if cfgFile != "" { + viper.SetConfigFile(cfgFile) + if err := viper.ReadInConfig(); err == nil { + logrus.Printf("Using config file: %s", viper.ConfigFileUsed()) + } else { + logrus.Fatal(fmt.Sprintf("Couldn't read config file: %s", err.Error())) + } + } else { + logrus.Warn("No config file passed with --config flag") + } +} diff --git a/cmd/validateTrie.go b/cmd/validateTrie.go index 104a321..279e948 100644 --- a/cmd/validateTrie.go +++ b/cmd/validateTrie.go @@ -22,6 +22,7 @@ import ( _ "github.com/lib/pq" //postgres driver "github.com/sirupsen/logrus" "github.com/spf13/cobra" + "github.com/spf13/viper" "github.com/vulcanize/eth-ipfs-state-validator/pkg" ) @@ -62,7 +63,10 @@ func validateTrie() { if err != nil { logWithCommand.Fatal(err) } - switch strings.ToLower(validationType) { + stateRootStr := viper.GetString("validator.stateRoot") + storageRootStr := viper.GetString("validator.storageRoot") + contractAddrStr := viper.GetString("validator.address") + switch strings.ToLower(viper.GetString("validator.type")) { case "f", "full": if stateRootStr == "" { logWithCommand.Fatal("must provide a state root for full state validation") @@ -98,6 +102,7 @@ func validateTrie() { } func newValidator() (*validator.Validator, error) { + ipfsPath := viper.GetString("ipfs.path") if ipfsPath == "" { db, err := validator.NewDB() if err != nil { @@ -114,9 +119,16 @@ func newValidator() (*validator.Validator, error) { func init() { rootCmd.AddCommand(validateTrieCmd) - validateTrieCmd.Flags().StringVarP(&stateRootStr, "state-root", "s", "", "Root of the state trie we wish to validate; for full or state validation") - validateTrieCmd.Flags().StringVarP(&validationType, "type", "t", "full", "Type of validations: full, state, storage") - validateTrieCmd.Flags().StringVarP(&storageRootStr, "storage-root", "o", "", "Root of the storage trie we wish to validate; for storage validation") - validateTrieCmd.Flags().StringVarP(&contractAddrStr, "address", "a", "", "Contract address for the storage trie we wish to validate; for storage validation") - validateTrieCmd.Flags().StringVarP(&ipfsPath, "ipfs-path", "i", "", "Path to IPFS repository") + + validateTrieCmd.PersistentFlags().String("state-root", "", "Root of the state trie we wish to validate; for full or state validation") + validateTrieCmd.PersistentFlags().String("type", "", "Type of validations: full, state, storage") + validateTrieCmd.PersistentFlags().String("storage-root", "", "Root of the storage trie we wish to validate; for storage validation") + validateTrieCmd.PersistentFlags().String("address", "", "Contract address for the storage trie we wish to validate; for storage validation") + validateTrieCmd.PersistentFlags().String("ipfs-path", "", "Path to IPFS repository; if provided operations move through the IPFS repo otherwise Postgres connection params are expected in the provided config") + + viper.BindPFlag("validator.stateRoot", validateTrieCmd.PersistentFlags().Lookup("state-root")) + viper.BindPFlag("validator.type", validateTrieCmd.PersistentFlags().Lookup("type")) + viper.BindPFlag("validator.storageRoot", validateTrieCmd.PersistentFlags().Lookup("storage-root")) + viper.BindPFlag("validator.address", validateTrieCmd.PersistentFlags().Lookup("address")) + viper.BindPFlag("ipfs.path", validateTrieCmd.PersistentFlags().Lookup("ipfs-path")) } diff --git a/environments/example.toml b/environments/example.toml index 63f6cfe..42bb251 100644 --- a/environments/example.toml +++ b/environments/example.toml @@ -1,6 +1,10 @@ [database] - name = "vulcanize_public" # $DATABASE_NAME - hostname = "localhost" # $DATABASE_HOSTNAME - port = 5432 # $DATABASE_PORT - user = "postgres" # $DATABASE_USER - password = "" # $DATABASE_PASSWORD \ No newline at end of file + name = "vulcanize_public" + hostname = "localhost" + port = 5432 + user = "postgres" + password = "" + +[validator] + type = "state" + stateRoot = "0xd7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544" \ No newline at end of file