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

refactor(chain): make config optional for init phase #3183

Merged
merged 26 commits into from
Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from 10 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 @@ -13,6 +13,7 @@
- [#3105](https://github.com/ignite/cli/pull/3105) Addition of `ignite plugin describe <path>` command
- [#2995](https://github.com/ignite/cli/pull/2995/) Add `ignite network request remove-validator` command.
- [#2999](https://github.com/ignite/cli/pull/2999/) Add `ignite network request remove-account` command.
- [#3183](https://github.com/ignite/cli/pull/3183/) Make config optional for init phase.

### Changes

Expand Down
2 changes: 1 addition & 1 deletion ignite/cmd/chain_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func chainInitHandler(cmd *cobra.Command, _ []string) error {
return err
}

if err := c.Init(cmd.Context(), true); err != nil {
if err := c.Init(cmd.Context(), true, true, true); err != nil {
return err
}

Expand Down
5 changes: 4 additions & 1 deletion ignite/cmd/network_chain_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"

"github.com/ignite/cli/ignite/pkg/chaincmd"
"github.com/ignite/cli/ignite/pkg/cliui"
"github.com/ignite/cli/ignite/pkg/cliui/cliquiz"
"github.com/ignite/cli/ignite/pkg/cliui/icons"
Expand Down Expand Up @@ -141,7 +142,9 @@ func networkChainInitHandler(cmd *cobra.Command, args []string) error {
return err
}

var networkOptions []networkchain.Option
networkOptions := []networkchain.Option{
networkchain.WithKeyringBackend(chaincmd.KeyringBackendTest),
}

if flagGetCheckDependencies(cmd) {
networkOptions = append(networkOptions, networkchain.CheckDependencies())
Expand Down
5 changes: 4 additions & 1 deletion ignite/cmd/network_chain_prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/spf13/cobra"

"github.com/ignite/cli/ignite/pkg/cache"
"github.com/ignite/cli/ignite/pkg/chaincmd"
"github.com/ignite/cli/ignite/pkg/cliui"
"github.com/ignite/cli/ignite/pkg/cliui/colors"
"github.com/ignite/cli/ignite/pkg/cliui/icons"
Expand Down Expand Up @@ -98,7 +99,9 @@ func networkChainPrepareHandler(cmd *cobra.Command, args []string) error {
return fmt.Errorf("chain %d launch has not been triggered yet. use --force to prepare anyway", launchID)
}

var networkOptions []networkchain.Option
networkOptions := []networkchain.Option{
networkchain.WithKeyringBackend(chaincmd.KeyringBackendTest),
}

if flagGetCheckDependencies(cmd) {
networkOptions = append(networkOptions, networkchain.CheckDependencies())
Expand Down
61 changes: 40 additions & 21 deletions ignite/services/chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/tendermint/spn/pkg/chainid"

"github.com/ignite/cli/ignite/config"
chainconfigv1 "github.com/ignite/cli/ignite/config/chain/v1"
"github.com/ignite/cli/ignite/pkg/chaincmd"
chaincmdrunner "github.com/ignite/cli/ignite/pkg/chaincmd/runner"
"github.com/ignite/cli/ignite/pkg/cliui/colors"
Expand Down Expand Up @@ -186,11 +187,19 @@ func (c *Chain) RPCPublicAddress() (string, error) {
if err != nil {
return "", err
}
validator := conf.Validators[0]
servers, err := validator.GetServers()
if err != nil {
return "", err

var servers chainconfigv1.Servers

if len(conf.Validators) == 0 {
servers = chainconfigv1.DefaultServers()
} else {
validator := conf.Validators[0]
servers, err = validator.GetServers()
if err != nil {
return "", err
}
}
jeronimoalbi marked this conversation as resolved.
Show resolved Hide resolved

rpcAddress = servers.RPC.Address
}
return rpcAddress, nil
Expand Down Expand Up @@ -295,9 +304,11 @@ func (c *Chain) DefaultHome() (string, error) {
if err != nil {
return "", err
}
validator := config.Validators[0]
if validator.Home != "" {
return validator.Home, nil
if len(config.Validators) > 0 {
validator := config.Validators[0]
if validator.Home != "" {
return validator.Home, nil
}
}

return c.appHome(), nil
Expand Down Expand Up @@ -369,17 +380,19 @@ func (c *Chain) KeyringBackend() (chaincmd.KeyringBackend, error) {
return "", err
}

// 2nd.
validator := config.Validators[0]
if validator.KeyringBackend != "" {
return chaincmd.KeyringBackendFromString(validator.KeyringBackend)
}
if len(config.Validators) > 0 {
// 2nd.
validator := config.Validators[0]
if validator.KeyringBackend != "" {
return chaincmd.KeyringBackendFromString(validator.KeyringBackend)
}

// 3rd.
if validator.Client != nil {
if backend, ok := validator.Client["keyring-backend"]; ok {
if backendStr, ok := backend.(string); ok {
return chaincmd.KeyringBackendFromString(backendStr)
// 3rd.
if validator.Client != nil {
if backend, ok := validator.Client["keyring-backend"]; ok {
if backendStr, ok := backend.(string); ok {
return chaincmd.KeyringBackendFromString(backendStr)
}
}
}
}
Expand Down Expand Up @@ -436,10 +449,16 @@ func (c *Chain) Commands(ctx context.Context) (chaincmdrunner.Runner, error) {
return chaincmdrunner.Runner{}, err
}

validator := config.Validators[0]
servers, err := validator.GetServers()
if err != nil {
return chaincmdrunner.Runner{}, err
var servers chainconfigv1.Servers

if len(config.Validators) == 0 {
servers = chainconfigv1.DefaultServers()
} else {
validator := config.Validators[0]
servers, err = validator.GetServers()
if err != nil {
return chaincmdrunner.Runner{}, err
}
}
lumtis marked this conversation as resolved.
Show resolved Hide resolved

nodeAddr, err := xurl.TCP(servers.RPC.Address)
Expand Down
56 changes: 33 additions & 23 deletions ignite/services/chain/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,33 @@ const (
moniker = "mynode"
)

// Init initializes the chain and applies all optional configurations.
func (c *Chain) Init(ctx context.Context, initAccounts bool) error {
conf, err := c.Config()
if err != nil {
return &CannotBuildAppError{err}
}

if err := c.InitChain(ctx); err != nil {
// Init initializes the chain
// initAccounts true intializes accounts for the chain from Ignite the config
// initConfiguration initilizes node configuration from the Ignite config
// initGenesis initializes genesis state for the chain from Ignite config
lumtis marked this conversation as resolved.
Show resolved Hide resolved
func (c *Chain) Init(ctx context.Context, initAccounts, initConfiguration, initGenesis bool) error {
lumtis marked this conversation as resolved.
Show resolved Hide resolved
if err := c.InitChain(ctx, initConfiguration, initGenesis); err != nil {
return err
}

if initAccounts {
conf, err := c.Config()
if err != nil {
return &CannotBuildAppError{err}
}

return c.InitAccounts(ctx, conf)
}
return nil
}

// InitChain initializes the chain.
func (c *Chain) InitChain(ctx context.Context) error {
func (c *Chain) InitChain(ctx context.Context, initConfiguration, initGenesis bool) error {
chainID, err := c.ID()
if err != nil {
return err
}

conf, err := c.Config()
if err != nil {
return err
}

// cleanup persistent data from previous `serve`.
home, err := c.Home()
if err != nil {
Expand All @@ -67,19 +65,31 @@ func (c *Chain) InitChain(ctx context.Context) error {
return err
}

// ovewrite app config files with the values defined in Ignite's config file
if err := c.Configure(home, conf); err != nil {
return err
var conf *config.ChainConfig
if initConfiguration || initGenesis {
conf, err = c.Config()
if err != nil {
return err
}
tbruyelle marked this conversation as resolved.
Show resolved Hide resolved
}

// make sure that chain id given during chain.New() has the most priority.
if conf.Genesis != nil {
conf.Genesis["chain_id"] = chainID
// ovewrite app config files with the values defined in Ignite's config file
if initConfiguration {
if err := c.Configure(home, conf); err != nil {
return err
}
}

// update genesis file with the genesis values defined in the config
if err := c.UpdateGenesisFile(conf.Genesis); err != nil {
return err
if initGenesis {
// make sure that chain id given during chain.New() has the most priority.
if conf.Genesis != nil {
conf.Genesis["chain_id"] = chainID
}

// update genesis file with the genesis values defined in the config
if err := c.UpdateGenesisFile(conf.Genesis); err != nil {
return err
}
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion ignite/services/chain/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ func (c *Chain) serve(
if !isInit || (appModified && !exportGenesisExists) {
c.ev.Send("Initializing the app...", events.ProgressUpdate())

if err := c.Init(ctx, true); err != nil {
if err := c.Init(ctx, true, true, true); err != nil {
return err
}
} else if appModified {
Expand Down
2 changes: 1 addition & 1 deletion ignite/services/network/networkchain/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (c *Chain) Init(ctx context.Context, cacheStorage cache.Storage) error {

c.ev.Send("Initializing the blockchain", events.ProgressStart())

if err = c.chain.Init(ctx, false); err != nil {
if err = c.chain.Init(ctx, false, false, false); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion ignite/services/network/networkchain/networkchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ func (c Chain) NodeID(ctx context.Context) (string, error) {
func (c Chain) CheckConfigVersion() error {
configPath := c.chain.ConfigPath()
if configPath == "" {
return config.ErrConfigNotFound
return nil
lumtis marked this conversation as resolved.
Show resolved Hide resolved
}

file, err := os.Open(configPath)
Expand Down