Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix default config file, network, and bootpeer selection #1811

Merged
merged 4 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
203 changes: 0 additions & 203 deletions cmd/go-quai/send.go

This file was deleted.

10 changes: 2 additions & 8 deletions cmd/go-quai/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/spf13/viper"

"github.com/dominant-strategies/go-quai/cmd/utils"
"github.com/dominant-strategies/go-quai/common"
"github.com/dominant-strategies/go-quai/log"
"github.com/dominant-strategies/go-quai/metrics_config"
"github.com/dominant-strategies/go-quai/p2p/node"
Expand Down Expand Up @@ -63,17 +62,12 @@ func startCmdPreRun(cmd *cobra.Command, args []string) error {
configDir := cmd.Flag(utils.ConfigDirFlag.Name).Value.String()
viper.Set(utils.KeyFileFlag.Name, filepath.Join(configDir, "private.key"))
}

// if no bootstrap peers are provided, use the default ones defined in config/bootnodes.go
if bootstrapPeers := viper.GetStringSlice(utils.BootPeersFlag.Name); len(bootstrapPeers) == 0 {
log.Global.Debugf("no bootstrap peers provided. Using default ones: %v", common.BootstrapPeers)
viper.Set(utils.BootPeersFlag.Name, common.BootstrapPeers)
}
return nil
}

func runStart(cmd *cobra.Command, args []string) error {
log.Global.Info("Starting go-quai")
network := viper.GetString(utils.EnvironmentFlag.Name)
log.Global.Infof("Starting go-quai on the %s network", network)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

Expand Down
7 changes: 5 additions & 2 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ var (

EnvironmentFlag = Flag{
Name: c_NodeFlagPrefix + "environment",
Value: params.LocalName,
Value: params.ColosseumName,
Djadih marked this conversation as resolved.
Show resolved Hide resolved
Usage: "environment to run in (local, colosseum, garden, orchard, lighthouse, dev)" + generateEnvDoc(c_NodeFlagPrefix+"environment"),
}

Expand Down Expand Up @@ -1522,7 +1522,7 @@ func addFlagsToCategory(flags []Flag) {
}
}

// Write a function to write the default values of each flag to a file
// Write the default values of each flag to a file
func WriteDefaultConfigFile(configDir string, configFileName string, configType string) error {
if configDir == "" {
log.Global.Fatalf("No config file path provided")
Expand Down Expand Up @@ -1560,6 +1560,9 @@ func WriteDefaultConfigFile(configDir string, configFileName string, configType
addFlagsToCategory(RPCFlags)
addFlagsToCategory(MetricsFlags)

// Remove bootpeers data from the configData to be written.
delete(configData["node"], "bootpeers")

var output []byte
var marshalErr error

Expand Down
49 changes: 7 additions & 42 deletions cmd/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package utils
import (
"errors"
"io/fs"
"os"

"github.com/spf13/viper"

"github.com/dominant-strategies/go-quai/common"
"github.com/dominant-strategies/go-quai/common/constants"
"github.com/dominant-strategies/go-quai/log"
)
Expand All @@ -30,49 +30,14 @@ func InitConfig() {
panic(err)
}
}
if !viper.IsSet(BootPeersFlag.Name) {
network := viper.GetString(EnvironmentFlag.Name)
bootpeers := common.BootstrapPeers[network]
log.Global.Debugf("No bootpeers specified. Using defaults for %s: %s", network, bootpeers)
viper.Set(BootPeersFlag.Name, bootpeers)
}

log.Global.Infof("Loading config from environment variables with prefix: '%s_'", constants.ENV_PREFIX)
viper.SetEnvPrefix(constants.ENV_PREFIX)
viper.AutomaticEnv()
}

// saves the config file with the current config parameters.
//
// If the config file does not exist, it creates it.
//
// If the config file exists, it creates a backup copy ending with .bak
// and overwrites the existing config file.
// TODO: consider using one single utility function to save/update/append files throughout the codebase
func SaveConfig() error {
Djadih marked this conversation as resolved.
Show resolved Hide resolved
// check if config file exists
configFile := viper.ConfigFileUsed()
log.Global.Debugf("saving/updating config file: %s", configFile)
if _, err := os.Stat(configFile); err == nil {
// config file exists, create backup copy
err := os.Rename(configFile, configFile+".bak")
if err != nil {
return err
}
} else if os.IsNotExist(err) {
// config file does not exist, create directory if it does not exist
if _, err := os.Stat(configFile); os.IsNotExist(err) {
configDir := viper.GetString(ConfigDirFlag.Name)
if err := os.MkdirAll(configDir, 0755); err != nil {
return err
}
}
_, err := os.Create(configFile)
if err != nil {
return err
}
} else {
return err
}

// write config file
err := viper.WriteConfigAs(configFile)
if err != nil {
return err
}
return nil
}
34 changes: 0 additions & 34 deletions cmd/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,6 @@ import (
"github.com/stretchr/testify/require"
)

// Verifies that the config file is saved or updated with the current config parameters.
func TestSaveConfig(t *testing.T) {
// set configPath to a temporary mocked XDG config folder
mockConfigPath := "/tmp/xdg_config_home/"
tempFile := createMockXDGConfigFile(t, mockConfigPath)
defer tempFile.Close()
defer os.RemoveAll(mockConfigPath)
// write LOG_LEVEL config to mock config.yaml file
_, err := tempFile.WriteString(LogLevelFlag.Name + " : " + "debug\n")
require.NoError(t, err)
// Clear viper instance to simulate a fresh start
viper.Reset()

// Set config path to the temporary config directory
viper.SetConfigFile(tempFile.Name())

// Set PORT to 8080 as environment variable
err = os.Setenv("GO_QUAI_PORT", "8080")
require.NoError(t, err)
defer os.Unsetenv("GO_QUAI_PORT")
InitConfig()
// Save config file
err = SaveConfig()
require.NoError(t, err)
// Load config from mock file into viper and assert that the new config parameters were saved
err = viper.ReadInConfig()
require.NoError(t, err)
assert.Equal(t, "8080", viper.GetString(P2PPortFlag.Name))
// Assert a .bak config file was created
backupFile, err := os.Stat(mockConfigPath + constants.CONFIG_FILE_NAME + ".bak")
assert.False(t, os.IsNotExist(err))
assert.Equal(t, constants.CONFIG_FILE_NAME+".bak", backupFile.Name())
}

// testXDGConfigLoading tests the loading of the config file from the XDG config home
// and verifies values are correctly set in viper.
// This test is nested within the TestCobraFlagConfigLoading test.
Expand Down
12 changes: 7 additions & 5 deletions common/bootnodes.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package common

var (
BootstrapPeers = []string{
"/ip4/146.148.66.22/tcp/4001/p2p/12D3KooWRQrLVEeJtfyKoJDYWYjryBKR8qxkDooMMzyf2ZpLaZRR",
"/ip4/35.190.147.237/tcp/4001/p2p/12D3KooWSb49ccXFWPCsvi7rzCbqBUK2xfuRC2xbo6KnUZk3YaVg",
"/ip4/35.194.7.78/tcp/4001/p2p/12D3KooWR3xMB6sCpsowQcvtdMKmKbTaiDcDFAXuWABdZVPWaVuo",
"/ip4/34.136.140.151/tcp/4001/p2p/12D3KooWJnWmBukEbZtGPPJvT1r4tQ97CRSGmnjHewcrjNB8oRxU",
BootstrapPeers = map[string][]string{
"garden": {
"/ip4/146.148.66.22/tcp/4001/p2p/12D3KooWRQrLVEeJtfyKoJDYWYjryBKR8qxkDooMMzyf2ZpLaZRR",
"/ip4/35.190.147.237/tcp/4001/p2p/12D3KooWSb49ccXFWPCsvi7rzCbqBUK2xfuRC2xbo6KnUZk3YaVg",
"/ip4/35.194.7.78/tcp/4001/p2p/12D3KooWR3xMB6sCpsowQcvtdMKmKbTaiDcDFAXuWABdZVPWaVuo",
"/ip4/34.136.140.151/tcp/4001/p2p/12D3KooWJnWmBukEbZtGPPJvT1r4tQ97CRSGmnjHewcrjNB8oRxU",
},
}
)
Loading