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 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 @@ -39,6 +39,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.
- [#3183](https://github.com/ignite/cli/pull/3183/) Make config optional for init phase.

### Breaking 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
55 changes: 24 additions & 31 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 @@ -177,25 +178,6 @@ func (c *Chain) appVersion() (v version, err error) {
return v, nil
}

// RPCPublicAddress points to the public address of Tendermint RPC, this is shared by
// other chains for relayer related actions.
func (c *Chain) RPCPublicAddress() (string, error) {
rpcAddress := os.Getenv("RPC_ADDRESS")
if rpcAddress == "" {
conf, err := c.Config()
if err != nil {
return "", err
}
validator := conf.Validators[0]
servers, err := validator.GetServers()
if err != nil {
return "", err
}
rpcAddress = servers.RPC.Address
}
return rpcAddress, nil
}

// ConfigPath returns the config path of the chain
// Empty string means that the chain has no defined config
func (c *Chain) ConfigPath() string {
Expand Down Expand Up @@ -306,9 +288,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 @@ -381,11 +365,14 @@ func (c *Chain) KeyringBackend() (chaincmd.KeyringBackend, error) {
}

// 2nd.
validator := config.Validators[0]
if validator.Client != nil {
if v, ok := validator.Client["keyring-backend"]; ok {
if backend, ok := v.(string); ok {
return chaincmd.KeyringBackendFromString(backend)
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)
}
}
}
}
Expand Down Expand Up @@ -442,10 +429,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 initializes chain accounts from the Ignite config
// initConfiguration initializes node configuration from the Ignite config
// initGenesis initializes genesis state for the chain from Ignite config
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 @@ -400,7 +400,7 @@ func (c *Chain) serve(
if initApp {
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 @@ -303,7 +303,7 @@ func (c Chain) CheckConfigVersion() error {
// Build builds chain sources, also checks if source was already built
func (c *Chain) Build(ctx context.Context, cacheStorage cache.Storage) (binaryName string, err error) {
// Check that the config version is the latest before building the binary
if err = c.CheckConfigVersion(); err != nil {
if err = c.CheckConfigVersion(); err != nil && err != config.ErrConfigNotFound {
return
}

Expand Down