Skip to content

Commit 619bd55

Browse files
fix: path when genesis file already exists. (#2361)
Fix incorrect use of `homePath` for genesis file path. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **Refactor** - Improved handling of genesis file paths by consistently using the explicit genesis file location in messages and file operations across multiple components. - **New Features** - Added a utility to generate the full path to the genesis file from the home directory. <!-- end of auto-generated comment: release notes by coderabbit.ai --> Co-authored-by: Marko <marko@baricevic.me>
1 parent a3a1c1f commit 619bd55

4 files changed

Lines changed: 15 additions & 6 deletions

File tree

apps/evm/based/cmd/init.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,18 @@ func InitCmd() *cobra.Command {
6262

6363
// Initialize genesis without app state
6464
err = rollgenesis.CreateGenesis(homePath, chainID, 1, proposerAddress)
65+
genesisPath := rollgenesis.GenesisPath(homePath)
6566
if errors.Is(err, rollgenesis.ErrGenesisExists) {
6667
// check if existing genesis file is valid
67-
if genesis, err := rollgenesis.LoadGenesis(homePath); err == nil {
68+
if genesis, err := rollgenesis.LoadGenesis(genesisPath); err == nil {
6869
if err := genesis.Validate(); err != nil {
6970
return fmt.Errorf("existing genesis file is invalid: %w", err)
7071
}
7172
} else {
7273
return fmt.Errorf("error loading existing genesis file: %w", err)
7374
}
7475

75-
cmd.Printf("Genesis file already exists at %s, skipping creation.\n", homePath)
76+
cmd.Printf("Genesis file already exists at %s, skipping creation.\n", genesisPath)
7677
} else if err != nil {
7778
return fmt.Errorf("error initializing genesis file: %w", err)
7879
}

apps/evm/single/cmd/init.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,18 @@ func InitCmd() *cobra.Command {
6262

6363
// Initialize genesis without app state
6464
err = rollgenesis.CreateGenesis(homePath, chainID, 1, proposerAddress)
65+
genesisPath := rollgenesis.GenesisPath(homePath)
6566
if errors.Is(err, rollgenesis.ErrGenesisExists) {
6667
// check if existing genesis file is valid
67-
if genesis, err := rollgenesis.LoadGenesis(homePath); err == nil {
68+
if genesis, err := rollgenesis.LoadGenesis(genesisPath); err == nil {
6869
if err := genesis.Validate(); err != nil {
6970
return fmt.Errorf("existing genesis file is invalid: %w", err)
7071
}
7172
} else {
7273
return fmt.Errorf("error loading existing genesis file: %w", err)
7374
}
7475

75-
cmd.Printf("Genesis file already exists at %s, skipping creation.\n", homePath)
76+
cmd.Printf("Genesis file already exists at %s, skipping creation.\n", genesisPath)
7677
} else if err != nil {
7778
return fmt.Errorf("error initializing genesis file: %w", err)
7879
}

apps/testapp/cmd/init.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,18 @@ func InitCmd() *cobra.Command {
6262

6363
// Initialize genesis without app state
6464
err = rollgenesis.CreateGenesis(homePath, chainID, 1, proposerAddress)
65+
genesisPath := rollgenesis.GenesisPath(homePath)
6566
if errors.Is(err, rollgenesis.ErrGenesisExists) {
6667
// check if existing genesis file is valid
67-
if genesis, err := rollgenesis.LoadGenesis(homePath); err == nil {
68+
if genesis, err := rollgenesis.LoadGenesis(genesisPath); err == nil {
6869
if err := genesis.Validate(); err != nil {
6970
return fmt.Errorf("existing genesis file is invalid: %w", err)
7071
}
7172
} else {
7273
return fmt.Errorf("error loading existing genesis file: %w", err)
7374
}
7475

75-
cmd.Printf("Genesis file already exists at %s, skipping creation.\n", homePath)
76+
cmd.Printf("Genesis file already exists at %s, skipping creation.\n", genesisPath)
7677
} else if err != nil {
7778
return fmt.Errorf("error initializing genesis file: %w", err)
7879
}

pkg/genesis/io.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ import (
1010

1111
var ErrGenesisExists = fmt.Errorf("genesis file already exists")
1212

13+
// GenesisPath returns the genesis file path from a home directory.
14+
func GenesisPath(homePath string) string {
15+
configDir := filepath.Join(homePath, "config")
16+
return filepath.Join(configDir, "genesis.json")
17+
}
18+
1319
// CreateGenesis creates and saves a genesis file with the given app state.
1420
// If the genesis file already exists, it skips the creation and returns ErrGenesisExists.
1521
// The genesis file is saved in the config directory of the specified home path.

0 commit comments

Comments
 (0)