Skip to content

Commit

Permalink
refactor(chain): make config optional for init phase (#3183)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* refactor error

* remove rpc address

* add init args

* thomas simplification

* comments

* fix keyring backend genesis show

Co-authored-by: Jerónimo Albi <[email protected]>
  • Loading branch information
lumtis and jeronimoalbi authored Dec 9, 2022
1 parent a645f1e commit 1f84fcf
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 60 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
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(), chain.InitArgsAll); 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
7 changes: 6 additions & 1 deletion ignite/cmd/network_chain_show_genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
52 changes: 21 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"

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"
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,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)
Expand Down
79 changes: 57 additions & 22 deletions ignite/services/chain/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
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 @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion ignite/services/network/networkchain/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

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 != chainconfig.ErrConfigNotFound {
return
}

Expand Down

0 comments on commit 1f84fcf

Please sign in to comment.