Skip to content

Commit

Permalink
Add more validation to config
Browse files Browse the repository at this point in the history
  • Loading branch information
krhubert committed Sep 30, 2019
1 parent 42ebfc4 commit b4dbd65
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
29 changes: 19 additions & 10 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/sirupsen/logrus"
tmconfig "github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/crypto/ed25519"
"gopkg.in/go-playground/validator.v9"
)

const (
Expand All @@ -30,24 +31,24 @@ const (

// Config contains all the configuration needed.
type Config struct {
Name string
Path string
Name string `validate:"required"`
Path string `validate:"required"`

Server struct {
Address string
Address string `validate:"required"`
}

Log struct {
Format string
Format string `validate:"required"`
ForceColors bool
Level string
Level string `validate:"required"`
}

Database struct {
ServiceRelativePath string
InstanceRelativePath string
ExecutionRelativePath string
ProcessRelativePath string
ServiceRelativePath string `validate:"required"`
InstanceRelativePath string `validate:"required"`
ExecutionRelativePath string `validate:"required"`
ProcessRelativePath string `validate:"required"`
}

Tendermint struct {
Expand Down Expand Up @@ -153,7 +154,7 @@ func (c *Config) Validate() error {
if _, err := logrus.ParseLevel(c.Log.Level); err != nil {
return err
}
return nil
return validator.New().Struct(c)
}

// PubKeyEd25519 is type used to parse value provided by envconfig.
Expand Down Expand Up @@ -183,6 +184,10 @@ type StdTx authtypes.StdTx

// Decode parses string value as hex ed25519 key.
func (tx *StdTx) Decode(value string) error {
if value == "" {
return nil
}

cdc := codec.New()
codec.RegisterCrypto(cdc)
sdktypes.RegisterCodec(cdc)
Expand All @@ -191,5 +196,9 @@ func (tx *StdTx) Decode(value string) error {
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")
}
return nil
}
8 changes: 7 additions & 1 deletion config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ func TestLoad(t *testing.T) {
"MESG_LOG_LEVEL": "",
"MESG_LOG_FORCECOLORS": "",
"MESG_TENDERMINT_P2P_PERSISTENTPEERS": "",
"MESG_COSMOS_VALIDATOR_PUB_KEY": "",
"MESG_COSMOS_VALIDATORPUBKEY": "",
"MESG_COSMOS_GENESISVALIDATORTX": "",
}
for key := range snapsnot {
snapsnot[key] = os.Getenv(key)
Expand Down Expand Up @@ -80,3 +81,8 @@ func TestValidate(t *testing.T) {
c.Log.Level = "wrongValue"
require.Error(t, c.Validate())
}

func TestStdTXDecodeNoSignersError(t *testing.T) {
var tx StdTx
require.Error(t, tx.Decode(`{"msg":[{"type":"cosmos-sdk/MsgCreateValidator","value":{"description":{"identity":"","website":"","details":"create-first-validator"},"pubkey":"cosmosvalconspub1zcjduepqq0a87y3pur6vvzyp99t92me2zyxywz46kyq5vt7x2n4g987acmxszqey9p","value":{"denom":"stake","amount":"100000000"}}}],"fee":{"amount":[],"gas":"200000"}}`))
}

0 comments on commit b4dbd65

Please sign in to comment.