Skip to content

Commit

Permalink
fix(config): restore default config when it is deleted (#847)
Browse files Browse the repository at this point in the history
  • Loading branch information
b00f authored Dec 11, 2023
1 parent c2bd49c commit 0076a02
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 48 deletions.
61 changes: 36 additions & 25 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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
Expand Down
15 changes: 2 additions & 13 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package config
import (
"bytes"
_ "embed"
"fmt"
"os"
"strings"

"github.com/pactus-project/pactus/consensus"
"github.com/pactus-project/pactus/crypto"
Expand Down Expand Up @@ -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())
}

Expand Down
16 changes: 6 additions & 10 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 0076a02

Please sign in to comment.