Skip to content

Commit

Permalink
Add validation require on all Config. Check if Cosmos.GenesisValidato…
Browse files Browse the repository at this point in the history
…rTx is valid. Remove some test as there is require value without default value
  • Loading branch information
NicolasMahe committed Sep 30, 2019
1 parent bcf3aaf commit 3254d45
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 31 deletions.
32 changes: 15 additions & 17 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,23 @@ type Config struct {
}

Tendermint struct {
*tmconfig.Config
Path string
*tmconfig.Config `validate:"required"`
Path string `validate:"required"`
}

Cosmos CosmosConfig
}

// CosmosConfig is the struct to hold cosmos related configs.
type CosmosConfig struct {
Path string
ChainID string
GenesisTime time.Time

GenesisValidatorTx StdTx

ValidatorPubKey PubKeyEd25519
Path string `validate:"required"`
ChainID string `validate:"required"`
GenesisTime time.Time `validate:"required"`
GenesisValidatorTx StdTx `validate:"required"`
ValidatorPubKey PubKeyEd25519 `validate:"required"`
}

// New creates a new config with default values.
// Default creates a new config with default values.
func Default() (*Config, error) {
home, err := homedir.Dir()
if err != nil {
Expand Down Expand Up @@ -149,10 +147,13 @@ func (c *Config) Prepare() error {
// Validate checks values and return an error if any validation failed.
func (c *Config) Validate() error {
if !xstrings.SliceContains([]string{"text", "json"}, c.Log.Format) {
return fmt.Errorf("value %q is not an allowed", c.Log.Format)
return fmt.Errorf("config.Log.Format value %q is not an allowed", c.Log.Format)
}
if _, err := logrus.ParseLevel(c.Log.Level); err != nil {
return err
return fmt.Errorf("config.Log.Level error: %w", err)
}
if err := authtypes.StdTx(c.Cosmos.GenesisValidatorTx).ValidateBasic(); err != nil {
return fmt.Errorf("config.Cosmos.GenesisValidatorTx error: %w", err.Stacktrace())
}
return validator.New().Struct(c)
}
Expand Down Expand Up @@ -187,18 +188,15 @@ func (tx *StdTx) Decode(value string) error {
if value == "" {
return nil
}

cdc := codec.New()
codec.RegisterCrypto(cdc)
sdktypes.RegisterCodec(cdc)
stakingtypes.RegisterCodec(cdc)

if err := cdc.UnmarshalJSON([]byte(value), tx); err != nil {
return fmt.Errorf("unmarshal genesis validator error: %s", err)
}
signers := authtypes.StdTx(*tx).GetSigners()
if l := len(signers); l == 0 || signers[l-1] == nil {
return fmt.Errorf("genesis validator error: no signer address")
if err := authtypes.StdTx(*tx).ValidateBasic(); err != nil {
return err.Stacktrace()
}
return nil
}
15 changes: 1 addition & 14 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@ func TestDefaultValue(t *testing.T) {
require.Equal(t, "engine", c.Name)
}

func TestNew(t *testing.T) {
c, err := New()
require.NoError(t, err)
require.NotNil(t, c)
}

func TestLoad(t *testing.T) {
snapsnot := map[string]string{
"MESG_SERVER_ADDRESS": "",
Expand Down Expand Up @@ -71,14 +65,7 @@ func TestLoad(t *testing.T) {

func TestValidate(t *testing.T) {
c, _ := Default()
require.NoError(t, c.Validate())

c, _ = Default()
c.Log.Format = "wrongValue"
require.Error(t, c.Validate())

c, _ = Default()
c.Log.Level = "wrongValue"
c.Load()
require.Error(t, c.Validate())
}

Expand Down

0 comments on commit 3254d45

Please sign in to comment.