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 2 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 @@ -36,6 +36,7 @@
- [#3025](https://github.com/ignite/cli/issues/3025) Improve config version error handling.
- [#3084](https://github.com/ignite/cli/pull/3084) Add Ignite Chain documentation.
- [#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.

### Breaking Changes

Expand Down
19 changes: 17 additions & 2 deletions ignite/config/chainconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,13 @@ var (
}
)

// ChainConfig defines the latest chain config.
type ChainConfig = v1.Config
type (
// ChainConfig defines the latest chain config.
ChainConfig = 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() *ChainConfig {
Expand Down Expand Up @@ -172,3 +177,13 @@ func Save(c ChainConfig, 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 not validators defined in the config.
func FirstValidator(conf *ChainConfig) (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/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,6 @@ func validateConfig(c *ChainConfig) 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
30 changes: 22 additions & 8 deletions ignite/services/chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,12 @@ func (c *Chain) RPCPublicAddress() (string, error) {
if err != nil {
return "", err
}
validator := conf.Validators[0]

validator, err := config.FirstValidator(conf)
jeronimoalbi marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return "", err
}

servers, err := validator.GetServers()
if err != nil {
return "", err
Expand Down Expand Up @@ -291,11 +296,16 @@ 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
}

validator, err := config.FirstValidator(cfg)
if err != nil {
return "", err
}
validator := config.Validators[0]

if validator.Home != "" {
return validator.Home, nil
}
Expand Down Expand Up @@ -364,13 +374,13 @@ func (c *Chain) KeyringBackend() (chaincmd.KeyringBackend, error) {
return c.options.keyringBackend, nil
}

config, err := c.Config()
// 2nd.
jeronimoalbi marked this conversation as resolved.
Show resolved Hide resolved
cfg, err := c.Config()
if err != nil {
return "", err
}

// 2nd.
validator := config.Validators[0]
validator, _ := config.FirstValidator(cfg)
if validator.KeyringBackend != "" {
return chaincmd.KeyringBackendFromString(validator.KeyringBackend)
}
Expand Down Expand Up @@ -431,12 +441,16 @@ 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
}

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

validator := config.Validators[0]
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"

"github.com/ignite/cli/ignite/config"
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 := config.FirstValidator(conf)
if err != nil {
return cosmosfaucet.Faucet{}, err
}

servers, err := validator.GetServers()
if err != nil {
return cosmosfaucet.Faucet{}, err
Expand Down
67 changes: 42 additions & 25 deletions ignite/services/chain/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ func (c Chain) Gentx(ctx context.Context, runner chaincmdrunner.Runner, v Valida

// Start wraps the "appd start" command to begin running a chain from the daemon
func (c Chain) Start(ctx context.Context, runner chaincmdrunner.Runner, cfg *config.ChainConfig) error {
validator := cfg.Validators[0]
validator, err := config.FirstValidator(cfg)
if err != nil {
return err
}

servers, err := validator.GetServers()
if err != nil {
return err
Expand All @@ -60,14 +64,18 @@ func (c Chain) Configure(homePath string, cfg *config.ChainConfig) error {
}

func (c Chain) appTOML(homePath string, cfg *config.ChainConfig) error {
validator, err := config.FirstValidator(cfg)
if err != nil {
return err
}

// TODO find a better way in order to not delete comments in the toml.yml
path := filepath.Join(homePath, "config/app.toml")
config, err := toml.LoadFile(path)
appConfig, err := toml.LoadFile(path)
if err != nil {
return err
}

validator := cfg.Validators[0]
servers, err := validator.GetServers()
if err != nil {
return err
Expand All @@ -79,42 +87,46 @@ func (c Chain) appTOML(homePath string, cfg *config.ChainConfig) error {
}

// Set default config values
config.Set("api.enable", true)
config.Set("api.enabled-unsafe-cors", true)
config.Set("rpc.cors_allowed_origins", []string{"*"})
appConfig.Set("api.enable", true)
appConfig.Set("api.enabled-unsafe-cors", true)
appConfig.Set("rpc.cors_allowed_origins", []string{"*"})

// Update config values with the validator's Cosmos SDK app config
updateTomlTreeValues(config, validator.App)
updateTomlTreeValues(appConfig, validator.App)

// Make sure the API address have the protocol prefix
config.Set("api.address", apiAddr)
appConfig.Set("api.address", apiAddr)

staked, err := sdktypes.ParseCoinNormalized(validator.Bonded)
if err != nil {
return err
}
gas := sdktypes.NewInt64Coin(staked.Denom, 0)
config.Set("minimum-gas-prices", gas.String())
appConfig.Set("minimum-gas-prices", gas.String())

file, err := os.OpenFile(path, os.O_RDWR|os.O_TRUNC, 0o644)
if err != nil {
return err
}
defer file.Close()

_, err = config.WriteTo(file)
_, err = appConfig.WriteTo(file)
return err
}

func (c Chain) configTOML(homePath string, cfg *config.ChainConfig) error {
validator, err := config.FirstValidator(cfg)
if err != nil {
return err
}

// TODO find a better way in order to not delete comments in the toml.yml
path := filepath.Join(homePath, "config/config.toml")
config, err := toml.LoadFile(path)
tmConfig, err := toml.LoadFile(path)
if err != nil {
return err
}

validator := cfg.Validators[0]
servers, err := validator.GetServers()
if err != nil {
return err
Expand All @@ -131,31 +143,36 @@ func (c Chain) configTOML(homePath string, cfg *config.ChainConfig) error {
}

// Set default config values
config.Set("mode", "validator")
config.Set("rpc.cors_allowed_origins", []string{"*"})
config.Set("consensus.timeout_commit", "1s")
config.Set("consensus.timeout_propose", "1s")
tmConfig.Set("mode", "validator")
tmConfig.Set("rpc.cors_allowed_origins", []string{"*"})
tmConfig.Set("consensus.timeout_commit", "1s")
tmConfig.Set("consensus.timeout_propose", "1s")

// Update config values with the validator's Tendermint config
updateTomlTreeValues(config, validator.Config)
updateTomlTreeValues(tmConfig, validator.Config)

// Make sure the addresses have the protocol prefix
config.Set("rpc.laddr", rpcAddr)
config.Set("p2p.laddr", p2pAddr)
tmConfig.Set("rpc.laddr", rpcAddr)
tmConfig.Set("p2p.laddr", p2pAddr)

file, err := os.OpenFile(path, os.O_RDWR|os.O_TRUNC, 0o644)
if err != nil {
return err
}
defer file.Close()

_, err = config.WriteTo(file)
_, err = tmConfig.WriteTo(file)
return err
}

func (c Chain) clientTOML(homePath string, cfg *config.ChainConfig) error {
validator, err := config.FirstValidator(cfg)
if err != nil {
return err
}

path := filepath.Join(homePath, "config/client.toml")
config, err := toml.LoadFile(path)
tmConfig, err := toml.LoadFile(path)
if os.IsNotExist(err) {
return nil
}
Expand All @@ -165,19 +182,19 @@ func (c Chain) clientTOML(homePath string, cfg *config.ChainConfig) error {
}

// Set default config values
config.Set("keyring-backend", "test")
config.Set("broadcast-mode", "block")
tmConfig.Set("keyring-backend", "test")
tmConfig.Set("broadcast-mode", "block")

// Update config values with the validator's client config
updateTomlTreeValues(config, cfg.Validators[0].Client)
updateTomlTreeValues(tmConfig, validator.Client)

file, err := os.OpenFile(path, os.O_RDWR|os.O_TRUNC, 0o644)
if err != nil {
return err
}
defer file.Close()

_, err = config.WriteTo(file)
_, err = tmConfig.WriteTo(file)
return err
}

Expand Down
7 changes: 5 additions & 2 deletions ignite/services/chain/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,8 +461,11 @@ func (c *Chain) start(ctx context.Context, cfg *config.ChainConfig) error {
// set the app as being served
c.served = true

// Get the first validator
validator := cfg.Validators[0]
validator, err := config.FirstValidator(cfg)
if err != nil {
return err
}

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