From 0076a021521460a55bbdd243d339cdee79feaab9 Mon Sep 17 00:00:00 2001 From: b00f Date: Mon, 11 Dec 2023 18:30:54 +0800 Subject: [PATCH] fix(config): restore default config when it is deleted (#847) --- cmd/cmd.go | 61 +++++++++++++++++++++++++------------------ config/config.go | 15 ++--------- config/config_test.go | 16 +++++------- 3 files changed, 44 insertions(+), 48 deletions(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index c213d5ca2..4ba466bd7 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -289,20 +289,23 @@ func CreateNode(numValidators int, chain genesis.ChainType, workingDir string, case genesis.Mainnet: panic("not yet!") case genesis.Testnet: - if err := genesis.TestnetGenesis().SaveToFile(genPath); err != nil { + genDoc := genesis.TestnetGenesis() + if err := genDoc.SaveToFile(genPath); err != nil { return nil, nil, err } - - if err := config.SaveTestnetConfig(confPath); err != nil { + conf := config.DefaultConfigTestnet() + if err := conf.Save(confPath); err != nil { return nil, nil, err } case genesis.Localnet: - if err := makeLocalGenesis(*walletInstance).SaveToFile(genPath); err != nil { + genDoc := makeLocalGenesis(*walletInstance) + if err := genDoc.SaveToFile(genPath); err != nil { return nil, nil, err } - if err := config.SaveLocalnetConfig(confPath); err != nil { + conf := config.DefaultConfigLocalnet() + if err := conf.Save(confPath); err != nil { return nil, nil, err } } @@ -457,37 +460,45 @@ func tryLoadConfig(chainType genesis.ChainType, confPath string) (*config.Config conf, err := config.LoadFromFile(confPath, true, defConf) if err != nil { PrintWarnMsgf("Unable to load the config: %s", err) - PrintInfoMsgf("Attempting to restore the config to the default values...") + PrintInfoMsgf("Attempting to update or restore the config file...") + + // Try to attempt to load config in non-strict mode + conf, err = config.LoadFromFile(confPath, false, defConf) + // Create a backup of the config if util.PathExists(confPath) { - // Let's create a backup of the config confBackupPath := fmt.Sprintf("%v_bak_%s", confPath, time.Now().Format("2006-01-02T15:04:05")) - err = os.Rename(confPath, confBackupPath) - if err != nil { - return nil, err + renameErr := os.Rename(confPath, confBackupPath) + if renameErr != nil { + return nil, renameErr } } - // Now, attempt to restore the config file with the number of validators from the old config. - switch chainType { - case genesis.Mainnet: - panic("not yet implemented!") - - case genesis.Testnet: - err = config.SaveTestnetConfig(confPath) + if err == nil { + err := conf.Save(confPath) if err != nil { return nil, err } - - case genesis.Localnet: - err = config.SaveLocalnetConfig(confPath) - if err != nil { - return nil, err + PrintSuccessMsgf("Config updated.") + } else { + switch chainType { + case genesis.Mainnet: + err = config.SaveMainnetConfig(confPath) + if err != nil { + return nil, err + } + + case genesis.Testnet, + genesis.Localnet: + err = defConf.Save(confPath) + if err != nil { + return nil, err + } } - } - PrintSuccessMsgf("Config restored to the default values") - conf, _ = config.LoadFromFile(confPath, true, defConf) // This time it should be OK + PrintSuccessMsgf("Config restored to the default values") + conf, _ = config.LoadFromFile(confPath, true, defConf) // This time it should be OK + } } return conf, nil diff --git a/config/config.go b/config/config.go index 8e97c952a..b1e7cad29 100644 --- a/config/config.go +++ b/config/config.go @@ -3,9 +3,7 @@ package config import ( "bytes" _ "embed" - "fmt" "os" - "strings" "github.com/pactus-project/pactus/consensus" "github.com/pactus-project/pactus/crypto" @@ -151,21 +149,12 @@ func DefaultConfigLocalnet() *Config { return conf } -func SaveMainnetConfig(path string, numValidators int) error { +func SaveMainnetConfig(path string) error { conf := string(exampleConfigBytes) - conf = strings.Replace(conf, "%num_validators%", - fmt.Sprintf("%v", numValidators), 1) - return util.WriteFile(path, []byte(conf)) } -func SaveTestnetConfig(path string) error { - conf := DefaultConfigTestnet() - return util.WriteFile(path, conf.toTOML()) -} - -func SaveLocalnetConfig(path string) error { - conf := DefaultConfigLocalnet() +func (conf *Config) Save(path string) error { return util.WriteFile(path, conf.toTOML()) } diff --git a/config/config_test.go b/config/config_test.go index 9c73407b0..3eef8482b 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -11,7 +11,7 @@ import ( func TestSaveMainnetConfig(t *testing.T) { path := util.TempFilePath() - assert.NoError(t, SaveMainnetConfig(path, 7)) + assert.NoError(t, SaveMainnetConfig(path)) defConf := DefaultConfigMainnet() conf, err := LoadFromFile(path, true, defConf) @@ -20,9 +20,10 @@ func TestSaveMainnetConfig(t *testing.T) { assert.NoError(t, conf.BasicCheck()) } -func TestSaveTestnetConfig(t *testing.T) { +func TestSaveConfig(t *testing.T) { path := util.TempFilePath() - assert.NoError(t, SaveTestnetConfig(path)) + conf := defaultConfig() + assert.NoError(t, conf.Save(path)) defConf := DefaultConfigTestnet() conf, err := LoadFromFile(path, true, defConf) @@ -33,13 +34,8 @@ func TestSaveTestnetConfig(t *testing.T) { assert.Equal(t, conf.Network.DefaultPort, 21777) } -func TestSaveLocalnetConfig(t *testing.T) { - path := util.TempFilePath() - assert.NoError(t, SaveLocalnetConfig(path)) - - defConf := DefaultConfigLocalnet() - conf, err := LoadFromFile(path, true, defConf) - assert.NoError(t, err) +func TestLocalnetConfig(t *testing.T) { + conf := DefaultConfigLocalnet() assert.NoError(t, conf.BasicCheck()) assert.Empty(t, conf.Network.ListenAddrStrings)