From 1f84fcfe4911984e1c3569f7de04570f50f67e30 Mon Sep 17 00:00:00 2001 From: Lucas Bertrand Date: Fri, 9 Dec 2022 14:56:56 +0100 Subject: [PATCH] refactor(`chain`): make config optional for init phase (#3183) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * init config optional * option change * fix validator * fix chain * changelog * fix some errors * fix keyring backend * fix prepare * Update ignite/services/chain/init.go Co-authored-by: Jerónimo Albi * refactor error * remove rpc address * add init args * thomas simplification * comments * fix keyring backend genesis show Co-authored-by: Jerónimo Albi --- changelog.md | 1 + ignite/cmd/chain_init.go | 2 +- ignite/cmd/network_chain_init.go | 5 +- ignite/cmd/network_chain_prepare.go | 5 +- ignite/cmd/network_chain_show_genesis.go | 7 +- ignite/services/chain/chain.go | 52 +++++------- ignite/services/chain/init.go | 79 +++++++++++++------ ignite/services/chain/serve.go | 2 +- ignite/services/network/networkchain/init.go | 3 +- .../network/networkchain/networkchain.go | 2 +- 10 files changed, 98 insertions(+), 60 deletions(-) diff --git a/changelog.md b/changelog.md index bdbc84b429..f89ce6de56 100644 --- a/changelog.md +++ b/changelog.md @@ -41,6 +41,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. - [#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 - [#3244](https://github.com/ignite/cli/pull/3244) updated actions.yml for resolving deprecation message diff --git a/ignite/cmd/chain_init.go b/ignite/cmd/chain_init.go index 1c6cb789d1..25beff1c54 100644 --- a/ignite/cmd/chain_init.go +++ b/ignite/cmd/chain_init.go @@ -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(), chain.InitArgsAll); err != nil { return err } diff --git a/ignite/cmd/network_chain_init.go b/ignite/cmd/network_chain_init.go index f3da6c7079..3826a313b2 100644 --- a/ignite/cmd/network_chain_init.go +++ b/ignite/cmd/network_chain_init.go @@ -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" @@ -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()) diff --git a/ignite/cmd/network_chain_prepare.go b/ignite/cmd/network_chain_prepare.go index c5d6268c94..79330ae51a 100644 --- a/ignite/cmd/network_chain_prepare.go +++ b/ignite/cmd/network_chain_prepare.go @@ -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" @@ -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()) diff --git a/ignite/cmd/network_chain_show_genesis.go b/ignite/cmd/network_chain_show_genesis.go index 5baba69ee0..39241d06c2 100644 --- a/ignite/cmd/network_chain_show_genesis.go +++ b/ignite/cmd/network_chain_show_genesis.go @@ -6,6 +6,7 @@ import ( "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/icons" "github.com/ignite/cli/ignite/pkg/xos" @@ -51,7 +52,11 @@ func networkChainShowGenesisHandler(cmd *cobra.Command, args []string) error { return err } - c, err := nb.Chain(networkchain.SourceLaunch(chainLaunch)) + networkOptions := []networkchain.Option{ + networkchain.WithKeyringBackend(chaincmd.KeyringBackendTest), + } + + c, err := nb.Chain(networkchain.SourceLaunch(chainLaunch), networkOptions...) if err != nil { return err } diff --git a/ignite/services/chain/chain.go b/ignite/services/chain/chain.go index 7e482153b5..7c5e43d124 100644 --- a/ignite/services/chain/chain.go +++ b/ignite/services/chain/chain.go @@ -9,6 +9,7 @@ import ( "github.com/tendermint/spn/pkg/chainid" chainconfig "github.com/ignite/cli/ignite/config/chain" + 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" @@ -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 { @@ -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 @@ -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) + } } } } @@ -442,10 +429,13 @@ 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 + servers := chainconfigv1.DefaultServers() + if len(config.Validators) > 0 { + validator := config.Validators[0] + servers, err = validator.GetServers() + if err != nil { + return chaincmdrunner.Runner{}, err + } } nodeAddr, err := xurl.TCP(servers.RPC.Address) diff --git a/ignite/services/chain/init.go b/ignite/services/chain/init.go index 44c5ea201b..8527533fd2 100644 --- a/ignite/services/chain/init.go +++ b/ignite/services/chain/init.go @@ -15,39 +15,62 @@ import ( "github.com/ignite/cli/ignite/pkg/events" ) +type ( + // InitArgs represents argument to add additional initialization for 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 + InitArgs struct { + InitAccounts bool + InitConfiguration bool + InitGenesis bool + } +) + 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} +var ( + // InitArgsAll performs all initialization for the chain + InitArgsAll = InitArgs{ + InitAccounts: true, + InitConfiguration: true, + InitGenesis: true, } - if err := c.InitChain(ctx); err != nil { + // InitArgsNone performs minimal initialization for the chain by only initializating a node + InitArgsNone = InitArgs{ + InitAccounts: false, + InitConfiguration: false, + InitGenesis: false, + } +) + +// Init initializes the chain +func (c *Chain) Init(ctx context.Context, args InitArgs) error { + if err := c.InitChain(ctx, args.InitConfiguration, args.InitGenesis); err != nil { return err } - if initAccounts { + if args.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 { @@ -67,19 +90,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 *chainconfig.Config + if initConfiguration || initGenesis { + conf, err = c.Config() + if err != nil { + return err + } } - // 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 diff --git a/ignite/services/chain/serve.go b/ignite/services/chain/serve.go index 9d4a6e0e4a..51487ffa6c 100644 --- a/ignite/services/chain/serve.go +++ b/ignite/services/chain/serve.go @@ -401,7 +401,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, InitArgsAll); err != nil { return err } } else if appModified { diff --git a/ignite/services/network/networkchain/init.go b/ignite/services/network/networkchain/init.go index bc3a06cc36..e4b02a389f 100644 --- a/ignite/services/network/networkchain/init.go +++ b/ignite/services/network/networkchain/init.go @@ -11,6 +11,7 @@ import ( "github.com/ignite/cli/ignite/pkg/cache" cosmosgenesis "github.com/ignite/cli/ignite/pkg/cosmosutil/genesis" "github.com/ignite/cli/ignite/pkg/events" + "github.com/ignite/cli/ignite/services/chain" ) // Init initializes blockchain by building the binaries and running the init command and @@ -33,7 +34,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, chain.InitArgsNone); err != nil { return err } diff --git a/ignite/services/network/networkchain/networkchain.go b/ignite/services/network/networkchain/networkchain.go index a66fa98133..11339f17d6 100644 --- a/ignite/services/network/networkchain/networkchain.go +++ b/ignite/services/network/networkchain/networkchain.go @@ -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 != chainconfig.ErrConfigNotFound { return }