minor doc fixes; missing config init

This commit is contained in:
Ian Norden 2020-07-12 23:30:28 -05:00
parent ca51cd14d3
commit dd4034068f
4 changed files with 48 additions and 20 deletions

View File

@ -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) [![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 ## Background

View File

@ -16,7 +16,9 @@
package cmd package cmd
import ( import (
"fmt"
"os" "os"
"strings"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -26,12 +28,7 @@ import (
var ( var (
subCommand string subCommand string
logWithCommand logrus.Entry logWithCommand logrus.Entry
stateRootStr string
storageRootStr string
validationType string
contractAddrStr string
cfgFile string cfgFile string
ipfsPath string
) )
// rootCmd represents the base command when called without any subcommands // rootCmd represents the base command when called without any subcommands
@ -82,6 +79,8 @@ func logLevel() error {
} }
func init() { func init() {
cobra.OnInitialize(initConfig)
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AutomaticEnv() viper.AutomaticEnv()
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file location") 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("database.password", rootCmd.PersistentFlags().Lookup("database-password"))
viper.BindPFlag("log.level", rootCmd.PersistentFlags().Lookup("log-level")) 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")
}
}

View File

@ -22,6 +22,7 @@ import (
_ "github.com/lib/pq" //postgres driver _ "github.com/lib/pq" //postgres driver
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/vulcanize/eth-ipfs-state-validator/pkg" "github.com/vulcanize/eth-ipfs-state-validator/pkg"
) )
@ -62,7 +63,10 @@ func validateTrie() {
if err != nil { if err != nil {
logWithCommand.Fatal(err) 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": case "f", "full":
if stateRootStr == "" { if stateRootStr == "" {
logWithCommand.Fatal("must provide a state root for full state validation") logWithCommand.Fatal("must provide a state root for full state validation")
@ -98,6 +102,7 @@ func validateTrie() {
} }
func newValidator() (*validator.Validator, error) { func newValidator() (*validator.Validator, error) {
ipfsPath := viper.GetString("ipfs.path")
if ipfsPath == "" { if ipfsPath == "" {
db, err := validator.NewDB() db, err := validator.NewDB()
if err != nil { if err != nil {
@ -114,9 +119,16 @@ func newValidator() (*validator.Validator, error) {
func init() { func init() {
rootCmd.AddCommand(validateTrieCmd) 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.PersistentFlags().String("state-root", "", "Root of the state trie we wish to validate; for full or state validation")
validateTrieCmd.Flags().StringVarP(&storageRootStr, "storage-root", "o", "", "Root of the storage trie we wish to validate; for storage validation") validateTrieCmd.PersistentFlags().String("type", "", "Type of validations: full, state, storage")
validateTrieCmd.Flags().StringVarP(&contractAddrStr, "address", "a", "", "Contract address for the storage trie we wish to validate; for storage validation") validateTrieCmd.PersistentFlags().String("storage-root", "", "Root of the storage trie we wish to validate; for storage validation")
validateTrieCmd.Flags().StringVarP(&ipfsPath, "ipfs-path", "i", "", "Path to IPFS repository") 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"))
} }

View File

@ -1,6 +1,10 @@
[database] [database]
name = "vulcanize_public" # $DATABASE_NAME name = "vulcanize_public"
hostname = "localhost" # $DATABASE_HOSTNAME hostname = "localhost"
port = 5432 # $DATABASE_PORT port = 5432
user = "postgres" # $DATABASE_USER user = "postgres"
password = "" # $DATABASE_PASSWORD password = ""
[validator]
type = "state"
stateRoot = "0xd7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544"