Load chain config from a file

This commit is contained in:
Prathamesh Musale 2022-05-17 09:59:23 +05:30
parent e55b2b8ab3
commit c468b494dc
3 changed files with 46 additions and 4 deletions

View File

@ -25,3 +25,7 @@ integrationtest_local: | $(GINKGO) $(GOOSE)
go vet ./... go vet ./...
go fmt ./... go fmt ./...
./scripts/run_integration_test.sh ./scripts/run_integration_test.sh
build:
go fmt ./...
GO111MODULE=on go build

View File

@ -2,8 +2,11 @@ package cmd
import ( import (
"context" "context"
"encoding/json"
"fmt" "fmt"
"os"
"github.com/ethereum/go-ethereum/params"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
@ -34,10 +37,16 @@ func stateValidator() {
if height < 1 { if height < 1 {
logWithCommand.Fatalf("block height cannot be less the 1") logWithCommand.Fatalf("block height cannot be less the 1")
} }
trail := viper.GetUint64("validate.trail") trail := viper.GetUint64("validate.trail")
// TODO: add chain config logic here.
srvc := validator.NewService(cfg.DB, height, trail, nil) chainConfigPath := viper.GetString("ethereum.chainConfig")
chainCfg, err := LoadConfig(chainConfigPath)
fmt.Println("chainCfg", chainCfg)
if err != nil {
logWithCommand.Fatal(err)
}
srvc := validator.NewService(cfg.DB, height, trail, chainCfg)
_, err = srvc.Start(context.Background()) _, err = srvc.Start(context.Background())
if err != nil { if err != nil {
@ -53,8 +62,12 @@ func init() {
stateValidatorCmd.PersistentFlags().String("block-height", "1", "block height to initiate state validation") stateValidatorCmd.PersistentFlags().String("block-height", "1", "block height to initiate state validation")
stateValidatorCmd.PersistentFlags().String("trail", "0", "trail of block height to validate") stateValidatorCmd.PersistentFlags().String("trail", "0", "trail of block height to validate")
stateValidatorCmd.PersistentFlags().String("chainConfig", "", "path to chain config")
_ = viper.BindPFlag("validate.block-height", stateValidatorCmd.PersistentFlags().Lookup("block-height")) _ = viper.BindPFlag("validate.block-height", stateValidatorCmd.PersistentFlags().Lookup("block-height"))
_ = viper.BindPFlag("validate.trail", stateValidatorCmd.PersistentFlags().Lookup("trail")) _ = viper.BindPFlag("validate.trail", stateValidatorCmd.PersistentFlags().Lookup("trail"))
_ = viper.BindPFlag("ethereum.chainConfig", stateValidatorCmd.PersistentFlags().Lookup("chainConfig"))
} }
func initConfig() { func initConfig() {
@ -69,3 +82,25 @@ func initConfig() {
log.Warn("No config file passed with --config flag") log.Warn("No config file passed with --config flag")
} }
} }
// LoadConfig loads chain config from json file
func LoadConfig(chainConfigPath string) (*params.ChainConfig, error) {
file, err := os.Open(chainConfigPath)
if err != nil {
log.Error(fmt.Sprintf("Failed to read chain config file: %v", err))
return nil, err
}
defer file.Close()
chainConfig := new(params.ChainConfig)
if err := json.NewDecoder(file).Decode(chainConfig); err != nil {
log.Error(fmt.Sprintf("invalid chain config file: %v", err))
return nil, err
}
log.Info(fmt.Sprintf("Using chain config from %s file. Content %+v", chainConfigPath, chainConfig))
return chainConfig, nil
}

View File

@ -7,4 +7,7 @@
[validate] [validate]
block-height = 1 block-height = 1
trail = 10 trail = 10
[ethereum]
chainConfig = "./chain.json"