Skip to content

Commit

Permalink
Merge pull request maticnetwork#926 from cffls/heimdall_init_fix
Browse files Browse the repository at this point in the history
Don't overwrite heimdall-config in heimdalld init
  • Loading branch information
cffls authored Jan 3, 2023
2 parents 09a9460 + 73de579 commit e925af3
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 14 deletions.
43 changes: 30 additions & 13 deletions cmd/heimdalld/service/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,41 @@ type initHeimdallConfig struct {
}

func heimdallInit(_ *server.Context, cdc *codec.Codec, initConfig *initHeimdallConfig, config *cfg.Config) error {
// do not execute init if forceInit is false and genesis.json already exists (or we do not have permission to write to file)
if !initConfig.forceInit {
_, err := os.Stat(config.GenesisFile())
if err == nil || !errors.Is(err, os.ErrNotExist) { // https://stackoverflow.com/questions/12518876/how-to-check-if-a-file-exists-in-go
return nil
}
}

WriteDefaultHeimdallConfig(filepath.Join(config.RootDir, "config/heimdall-config.toml"), helper.GetDefaultHeimdallConfig())

nodeID, valPubKey, _, err := InitializeNodeValidatorFiles(config)
if err != nil {
return err
}

genesisCreated, err := helper.WriteGenesisFile(initConfig.chain, config.GenesisFile(), cdc)
if err != nil {
return err
} else if genesisCreated {
// do not execute init if forceInit is false and genesis.json already exists (or we do not have permission to write to file)
writeGenesis := initConfig.forceInit

if !writeGenesis {
// When not forcing, check if genesis file exists
_, err := os.Stat(config.GenesisFile())
if err != nil && errors.Is(err, os.ErrNotExist) {
logger.Info(fmt.Sprintf("Genesis file %v not found, writing genesis file\n", config.GenesisFile()))

writeGenesis = true
} else if err == nil {
logger.Info(fmt.Sprintf("Found genesis file %v, skipping writing genesis file\n", config.GenesisFile()))
} else {
logger.Error(fmt.Sprintf("Error checking if genesis file %v exists: %v\n", config.GenesisFile(), err))
return err
}
} else {
logger.Info(fmt.Sprintf("Force writing genesis file to %v\n", config.GenesisFile()))
}

if writeGenesis {
genesisCreated, err := helper.WriteGenesisFile(initConfig.chain, config.GenesisFile(), cdc)
if err != nil {
return err
} else if genesisCreated {
return nil
}
} else {
return nil
}

Expand Down Expand Up @@ -158,7 +174,7 @@ func initCmd(ctx *server.Context, cdc *codec.Codec) *cobra.Command {
chain: viper.GetString(helper.ChainFlag),
validatorID: viper.GetInt64(stakingcli.FlagValidatorID),
clientHome: viper.GetString(helper.FlagClientHome),
forceInit: true,
forceInit: viper.GetBool(helper.OverwriteGenesisFlag),
}
config := ctx.Config
config.SetRoot(viper.GetString(cli.HomeFlag))
Expand All @@ -170,6 +186,7 @@ func initCmd(ctx *server.Context, cdc *codec.Codec) *cobra.Command {
cmd.Flags().String(helper.FlagClientHome, helper.DefaultCLIHome, "client's home directory")
cmd.Flags().String(client.FlagChainID, "", "genesis file chain-id, if left blank will be randomly created")
cmd.Flags().Int(stakingcli.FlagValidatorID, 1, "--id=<validator ID here>, if left blank will be assigned 1")
cmd.Flags().Bool(helper.OverwriteGenesisFlag, false, "overwrite the genesis.json file if it exists")

return cmd
}
10 changes: 9 additions & 1 deletion cmd/heimdalld/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"math/big"
Expand Down Expand Up @@ -739,7 +740,14 @@ func InitializeNodeValidatorFiles(

// WriteDefaultHeimdallConfig writes default heimdall config to the given path
func WriteDefaultHeimdallConfig(path string, conf helper.Configuration) {
helper.WriteConfigFile(path, &conf)
// Don't write if config file in path already exists
if _, err := os.Stat(path); err == nil {
logger.Info(fmt.Sprintf("Config file %s already exists. Skip writing default heimdall config.", path))
} else if errors.Is(err, os.ErrNotExist) {
helper.WriteConfigFile(path, &conf)
} else {
logger.Error("Error while checking for config file", "Error", err)
}
}

func CryptoKeyToPubkey(key crypto.PubKey) hmTypes.PubKey {
Expand Down
1 change: 1 addition & 0 deletions helper/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const (
WithHeimdallConfigFlag = "heimdall-config"
HomeFlag = "home"
FlagClientHome = "home-client"
OverwriteGenesisFlag = "overwrite-genesis"
RestServerFlag = "rest-server"
BridgeFlag = "bridge"
LogLevel = "log_level"
Expand Down

0 comments on commit e925af3

Please sign in to comment.