Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: move config validators check to validate only when required #3199

Merged
merged 15 commits into from
Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
- [#3084](https://github.com/ignite/cli/pull/3084) Add Ignite Chain documentation.
- [#3109](https://github.com/ignite/cli/pull/3109) Refactor scaffolding for proto files to not rely on placeholders.
- [#3106](https://github.com/ignite/cli/pull/3106) Add zoom image plugin.
- [#3194](https://github.com/ignite/cli/issues/3194) Move config validators check to validate only when required.
- [#3183](https://github.com/ignite/cli/pull/3183/) Make config optional for init phase.
- [#3224](https://github.com/ignite/cli/pull/3224) Remove grpc_* prefix from query files in scaffolded chains
- [#3229](https://github.com/ignite/cli/pull/3229) Rename `campaign` to `project` in ignite network set of commands
Expand Down
20 changes: 20 additions & 0 deletions ignite/config/chain/base/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,26 @@ import (
xyaml "github.com/ignite/cli/ignite/pkg/yaml"
)

var (
// DefaultGRPCAddress is the default GRPC address.
DefaultGRPCAddress = "0.0.0.0:9090"

// DefaultGRPCWebAddress is the default GRPC-Web address.
DefaultGRPCWebAddress = "0.0.0.0:9091"

// DefaultAPIAddress is the default API address.
DefaultAPIAddress = "0.0.0.0:1317"

// DefaultRPCAddress is the default RPC address.
DefaultRPCAddress = "0.0.0.0:26657"

// DefaultP2PAddress is the default P2P address.
DefaultP2PAddress = "0.0.0.0:26656"

// DefaultPProfAddress is the default Prof address.
DefaultPProfAddress = "0.0.0.0:6060"
)

// Account holds the options related to setting up Cosmos wallets.
type Account struct {
Name string `yaml:"name"`
Expand Down
19 changes: 17 additions & 2 deletions ignite/config/chain/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,13 @@ var (
}
)

// Config defines the latest chain config.
type Config = v1.Config
type (
// Config defines the latest chain config.
Config = v1.Config

// Validator defines the latest validator settings.
Validator = v1.Validator
)

// DefaultChainConfig returns a config for the latest version initialized with default values.
func DefaultChainConfig() *Config {
Expand Down Expand Up @@ -159,3 +164,13 @@ func Save(c Config, path string) error {

return yaml.NewEncoder(file).Encode(c)
}

// FirstValidator returns the first validator from the validators list.
// An error is returned when there are no validators defined in the config.
func FirstValidator(conf *Config) (Validator, error) {
if len(conf.Validators) == 0 {
return Validator{}, &ValidationError{"at least one validator is required"}
}

return conf.Validators[0], nil
}
4 changes: 0 additions & 4 deletions ignite/config/chain/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,6 @@ func validateConfig(c *Config) error {
return &ValidationError{"at least one account is required"}
}

if len(c.Validators) == 0 {
return &ValidationError{"at least one validator is required"}
}

for _, validator := range c.Validators {
if validator.Name == "" {
return &ValidationError{"validator 'name' is required"}
Expand Down
24 changes: 12 additions & 12 deletions ignite/config/chain/v1/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,43 +87,43 @@ func (c *Config) updateValidatorAddresses() (err error) {
func incrementDefaultServerPortsBy(s Servers, inc uint64) (Servers, error) {
var err error

if s.GRPC.Address == DefaultGRPCAddress {
s.GRPC.Address, err = xnet.IncreasePortBy(DefaultGRPCAddress, inc)
if s.GRPC.Address == base.DefaultGRPCAddress {
s.GRPC.Address, err = xnet.IncreasePortBy(base.DefaultGRPCAddress, inc)
if err != nil {
return Servers{}, err
}
}

if s.GRPCWeb.Address == DefaultGRPCWebAddress {
s.GRPCWeb.Address, err = xnet.IncreasePortBy(DefaultGRPCWebAddress, inc)
if s.GRPCWeb.Address == base.DefaultGRPCWebAddress {
s.GRPCWeb.Address, err = xnet.IncreasePortBy(base.DefaultGRPCWebAddress, inc)
if err != nil {
return Servers{}, err
}
}

if s.API.Address == DefaultAPIAddress {
s.API.Address, err = xnet.IncreasePortBy(DefaultAPIAddress, inc)
if s.API.Address == base.DefaultAPIAddress {
s.API.Address, err = xnet.IncreasePortBy(base.DefaultAPIAddress, inc)
if err != nil {
return Servers{}, err
}
}

if s.P2P.Address == DefaultP2PAddress {
s.P2P.Address, err = xnet.IncreasePortBy(DefaultP2PAddress, inc)
if s.P2P.Address == base.DefaultP2PAddress {
s.P2P.Address, err = xnet.IncreasePortBy(base.DefaultP2PAddress, inc)
if err != nil {
return Servers{}, err
}
}

if s.RPC.Address == DefaultRPCAddress {
s.RPC.Address, err = xnet.IncreasePortBy(DefaultRPCAddress, inc)
if s.RPC.Address == base.DefaultRPCAddress {
s.RPC.Address, err = xnet.IncreasePortBy(base.DefaultRPCAddress, inc)
if err != nil {
return Servers{}, err
}
}

if s.RPC.PProfAddress == DefaultPProfAddress {
s.RPC.PProfAddress, err = xnet.IncreasePortBy(DefaultPProfAddress, inc)
if s.RPC.PProfAddress == base.DefaultPProfAddress {
s.RPC.PProfAddress, err = xnet.IncreasePortBy(base.DefaultPProfAddress, inc)
if err != nil {
return Servers{}, err
}
Expand Down
40 changes: 20 additions & 20 deletions ignite/config/chain/v1/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,12 @@ func TestConfigValidatorDefaultServers(t *testing.T) {
require.NoError(t, err)

// Assert
require.Equal(t, v1.DefaultGRPCAddress, servers.GRPC.Address)
require.Equal(t, v1.DefaultGRPCWebAddress, servers.GRPCWeb.Address)
require.Equal(t, v1.DefaultAPIAddress, servers.API.Address)
require.Equal(t, v1.DefaultRPCAddress, servers.RPC.Address)
require.Equal(t, v1.DefaultP2PAddress, servers.P2P.Address)
require.Equal(t, v1.DefaultPProfAddress, servers.RPC.PProfAddress)
require.Equal(t, base.DefaultGRPCAddress, servers.GRPC.Address)
require.Equal(t, base.DefaultGRPCWebAddress, servers.GRPCWeb.Address)
require.Equal(t, base.DefaultAPIAddress, servers.API.Address)
require.Equal(t, base.DefaultRPCAddress, servers.RPC.Address)
require.Equal(t, base.DefaultP2PAddress, servers.P2P.Address)
require.Equal(t, base.DefaultPProfAddress, servers.RPC.PProfAddress)
}

func TestConfigValidatorWithExistingServers(t *testing.T) {
Expand Down Expand Up @@ -141,10 +141,10 @@ func TestConfigValidatorWithExistingServers(t *testing.T) {
// Assert
require.Equal(t, rpcAddr, servers.RPC.Address)
require.Equal(t, apiAddr, servers.API.Address)
require.Equal(t, v1.DefaultGRPCAddress, servers.GRPC.Address)
require.Equal(t, v1.DefaultGRPCWebAddress, servers.GRPCWeb.Address)
require.Equal(t, v1.DefaultP2PAddress, servers.P2P.Address)
require.Equal(t, v1.DefaultPProfAddress, servers.RPC.PProfAddress)
require.Equal(t, base.DefaultGRPCAddress, servers.GRPC.Address)
require.Equal(t, base.DefaultGRPCWebAddress, servers.GRPCWeb.Address)
require.Equal(t, base.DefaultP2PAddress, servers.P2P.Address)
require.Equal(t, base.DefaultPProfAddress, servers.RPC.PProfAddress)
}

func TestConfigValidatorsWithExistingServers(t *testing.T) {
Expand Down Expand Up @@ -188,10 +188,10 @@ func TestConfigValidatorsWithExistingServers(t *testing.T) {
require.Equal(t, apiAddr, servers.API.Address)

// Assert: The second validator should have the ports incremented by 10
require.Equal(t, xnet.MustIncreasePortBy(v1.DefaultGRPCAddress, inc), servers.GRPC.Address)
require.Equal(t, xnet.MustIncreasePortBy(v1.DefaultGRPCWebAddress, inc), servers.GRPCWeb.Address)
require.Equal(t, xnet.MustIncreasePortBy(v1.DefaultP2PAddress, inc), servers.P2P.Address)
require.Equal(t, xnet.MustIncreasePortBy(v1.DefaultPProfAddress, inc), servers.RPC.PProfAddress)
require.Equal(t, xnet.MustIncreasePortBy(base.DefaultGRPCAddress, inc), servers.GRPC.Address)
require.Equal(t, xnet.MustIncreasePortBy(base.DefaultGRPCWebAddress, inc), servers.GRPCWeb.Address)
require.Equal(t, xnet.MustIncreasePortBy(base.DefaultP2PAddress, inc), servers.P2P.Address)
require.Equal(t, xnet.MustIncreasePortBy(base.DefaultPProfAddress, inc), servers.RPC.PProfAddress)
}

func TestConfigValidatorsDefaultServers(t *testing.T) {
Expand Down Expand Up @@ -221,12 +221,12 @@ func TestConfigValidatorsDefaultServers(t *testing.T) {
require.NoError(t, err)

// Assert: The second validator should have the ports incremented by 10
require.Equal(t, xnet.MustIncreasePortBy(v1.DefaultGRPCAddress, inc), servers.GRPC.Address)
require.Equal(t, xnet.MustIncreasePortBy(v1.DefaultGRPCWebAddress, inc), servers.GRPCWeb.Address)
require.Equal(t, xnet.MustIncreasePortBy(v1.DefaultAPIAddress, inc), servers.API.Address)
require.Equal(t, xnet.MustIncreasePortBy(v1.DefaultRPCAddress, inc), servers.RPC.Address)
require.Equal(t, xnet.MustIncreasePortBy(v1.DefaultP2PAddress, inc), servers.P2P.Address)
require.Equal(t, xnet.MustIncreasePortBy(v1.DefaultPProfAddress, inc), servers.RPC.PProfAddress)
require.Equal(t, xnet.MustIncreasePortBy(base.DefaultGRPCAddress, inc), servers.GRPC.Address)
require.Equal(t, xnet.MustIncreasePortBy(base.DefaultGRPCWebAddress, inc), servers.GRPCWeb.Address)
require.Equal(t, xnet.MustIncreasePortBy(base.DefaultAPIAddress, inc), servers.API.Address)
require.Equal(t, xnet.MustIncreasePortBy(base.DefaultRPCAddress, inc), servers.RPC.Address)
require.Equal(t, xnet.MustIncreasePortBy(base.DefaultP2PAddress, inc), servers.P2P.Address)
require.Equal(t, xnet.MustIncreasePortBy(base.DefaultPProfAddress, inc), servers.RPC.PProfAddress)
}

func TestClone(t *testing.T) {
Expand Down
32 changes: 7 additions & 25 deletions ignite/config/chain/v1/validator_servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,18 @@ import (
"fmt"

"github.com/mitchellh/mapstructure"
)

var (
// DefaultGRPCAddress is the default GRPC address.
DefaultGRPCAddress = "0.0.0.0:9090"

// DefaultGRPCWebAddress is the default GRPC-Web address.
DefaultGRPCWebAddress = "0.0.0.0:9091"

// DefaultAPIAddress is the default API address.
DefaultAPIAddress = "0.0.0.0:1317"

// DefaultRPCAddress is the default RPC address.
DefaultRPCAddress = "0.0.0.0:26657"

// DefaultP2PAddress is the default P2P address.
DefaultP2PAddress = "0.0.0.0:26656"

// DefaultPProfAddress is the default Prof address.
DefaultPProfAddress = "0.0.0.0:6060"
baseconfig "github.com/ignite/cli/ignite/config/chain/base"
)

func DefaultServers() Servers {
s := Servers{}
s.GRPC.Address = DefaultGRPCAddress
s.GRPCWeb.Address = DefaultGRPCWebAddress
s.API.Address = DefaultAPIAddress
s.P2P.Address = DefaultP2PAddress
s.RPC.Address = DefaultRPCAddress
s.RPC.PProfAddress = DefaultPProfAddress
s.GRPC.Address = baseconfig.DefaultGRPCAddress
s.GRPCWeb.Address = baseconfig.DefaultGRPCWebAddress
s.API.Address = baseconfig.DefaultAPIAddress
s.P2P.Address = baseconfig.DefaultP2PAddress
s.RPC.Address = baseconfig.DefaultRPCAddress
s.RPC.PProfAddress = baseconfig.DefaultPProfAddress

return s
}
Expand Down
40 changes: 18 additions & 22 deletions ignite/services/chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,15 +304,13 @@ func (c *Chain) Home() (string, error) {
// DefaultHome returns the blockchain node's default home dir when not specified in the app.
func (c *Chain) DefaultHome() (string, error) {
// check if home is defined in config
config, err := c.Config()
cfg, err := c.Config()
if err != nil {
return "", err
}
if len(config.Validators) > 0 {
validator := config.Validators[0]
if validator.Home != "" {
return validator.Home, nil
}
validator, _ := chainconfig.FirstValidator(cfg)
if validator.Home != "" {
return validator.Home, nil
}

return c.appHome(), nil
Expand Down Expand Up @@ -374,30 +372,28 @@ func (c *Chain) ClientTOMLPath() (string, error) {

// KeyringBackend returns the keyring backend chosen for the chain.
func (c *Chain) KeyringBackend() (chaincmd.KeyringBackend, error) {
// 1st.
// When keyring backend is initialized as a chain
// option it overrides any configured backends.
if c.options.keyringBackend != "" {
return c.options.keyringBackend, nil
}

config, err := c.Config()
// Try to get keyring backend from the first configured validator
cfg, err := c.Config()
if err != nil {
return "", err
}

// 2nd.
if len(config.Validators) > 0 {
validator := config.Validators[0]

if validator.Client != nil {
if backend, ok := validator.Client["keyring-backend"]; ok {
if backendStr, ok := backend.(string); ok {
return chaincmd.KeyringBackendFromString(backendStr)
}
validator, _ := chainconfig.FirstValidator(cfg)
if validator.Client != nil {
if v, ok := validator.Client["keyring-backend"]; ok {
if backend, ok := v.(string); ok {
return chaincmd.KeyringBackendFromString(backend)
}
}
}

// 3rd.
// Try to get keyring backend from client.toml config file
configTOMLPath, err := c.ClientTOMLPath()
if err != nil {
return "", err
Expand All @@ -413,7 +409,7 @@ func (c *Chain) KeyringBackend() (chaincmd.KeyringBackend, error) {
return chaincmd.KeyringBackendFromString(conf.KeyringBackend)
}

// 4th.
// Use test backend as default when none is configured
return chaincmd.KeyringBackendTest, nil
}

Expand Down Expand Up @@ -444,14 +440,14 @@ func (c *Chain) Commands(ctx context.Context) (chaincmdrunner.Runner, error) {
return chaincmdrunner.Runner{}, err
}

config, err := c.Config()
cfg, err := c.Config()
if err != nil {
return chaincmdrunner.Runner{}, err
}

servers := chainconfigv1.DefaultServers()
if len(config.Validators) > 0 {
validator := config.Validators[0]
if len(cfg.Validators) > 0 {
validator, _ := chainconfig.FirstValidator(cfg)
jeronimoalbi marked this conversation as resolved.
Show resolved Hide resolved
servers, err = validator.GetServers()
if err != nil {
return chaincmdrunner.Runner{}, err
Expand Down
7 changes: 6 additions & 1 deletion ignite/services/chain/faucet.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/pkg/errors"

chainconfig "github.com/ignite/cli/ignite/config/chain"
chaincmdrunner "github.com/ignite/cli/ignite/pkg/chaincmd/runner"
"github.com/ignite/cli/ignite/pkg/cosmosfaucet"
"github.com/ignite/cli/ignite/pkg/xurl"
Expand Down Expand Up @@ -55,7 +56,11 @@ func (c *Chain) Faucet(ctx context.Context) (cosmosfaucet.Faucet, error) {
}

// construct faucet options.
validator := conf.Validators[0]
validator, err := chainconfig.FirstValidator(conf)
if err != nil {
return cosmosfaucet.Faucet{}, err
}

servers, err := validator.GetServers()
if err != nil {
return cosmosfaucet.Faucet{}, err
Expand Down
Loading