diff --git a/pkg/config/load.go b/pkg/config/load.go index 6ad86a5..e09773e 100644 --- a/pkg/config/load.go +++ b/pkg/config/load.go @@ -81,6 +81,7 @@ func (c *ChiaConfig) Save() error { // SavePath saves the config at the given path func (c *ChiaConfig) SavePath(configPath string) error { + c.unfillDatabasePath() out, err := yaml.Marshal(c) if err != nil { return fmt.Errorf("error marshalling config: %w", err) @@ -122,6 +123,10 @@ func (c *ChiaConfig) fillDatabasePath() { c.FullNode.DatabasePath = strings.Replace(c.FullNode.DatabasePath, "CHALLENGE", *c.FullNode.SelectedNetwork, 1) } +func (c *ChiaConfig) unfillDatabasePath() { + c.FullNode.DatabasePath = strings.Replace(c.FullNode.DatabasePath, *c.FullNode.SelectedNetwork, "CHALLENGE", 1) +} + // dealWithAnchors swaps out the distinct sections of the config with pointers to a shared instance // When loading the config, the anchor definition in the initial-config is the canonical version. The rest will be // changed to point back to that instance diff --git a/pkg/config/load_test.go b/pkg/config/load_test.go index aaa09e7..4778ff2 100644 --- a/pkg/config/load_test.go +++ b/pkg/config/load_test.go @@ -1,6 +1,7 @@ package config_test import ( + "os" "testing" "github.com/stretchr/testify/assert" @@ -39,3 +40,25 @@ func TestDealWithAnchors(t *testing.T) { assert.Equal(t, "unittestnet", *cfg.Wallet.SelectedNetwork) assert.Equal(t, "unittestnet", *cfg.DataLayer.SelectedNetwork) } + +func TestFillDatabasePath(t *testing.T) { + def, err := config.LoadDefaultConfig() + assert.NoError(t, err) + assert.Equal(t, "db/blockchain_v2_mainnet.sqlite", def.FullNode.DatabasePath) + + tmpDir, err := os.MkdirTemp("", "testfs") + if err != nil { + t.Fatalf("Failed to create temporary directory: %v", err) + } + defer func(path string) { + err := os.RemoveAll(path) + if err != nil { + t.Fatalf("Error cleaning up test directory: %v", err) + } + }(tmpDir) + + tmpFilePath := tmpDir + "/tempfile.txt" + err = def.SavePath(tmpFilePath) + assert.NoError(t, err) + assert.Equal(t, "db/blockchain_v2_CHALLENGE.sqlite", def.FullNode.DatabasePath) +}