Skip to content

Commit 11e544b

Browse files
authored
put CHALLENGE back to the database path when writing to disk (#161)
1 parent 228b312 commit 11e544b

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

pkg/config/load.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ func (c *ChiaConfig) Save() error {
8181

8282
// SavePath saves the config at the given path
8383
func (c *ChiaConfig) SavePath(configPath string) error {
84+
c.unfillDatabasePath()
8485
out, err := yaml.Marshal(c)
8586
if err != nil {
8687
return fmt.Errorf("error marshalling config: %w", err)
@@ -122,6 +123,10 @@ func (c *ChiaConfig) fillDatabasePath() {
122123
c.FullNode.DatabasePath = strings.Replace(c.FullNode.DatabasePath, "CHALLENGE", *c.FullNode.SelectedNetwork, 1)
123124
}
124125

126+
func (c *ChiaConfig) unfillDatabasePath() {
127+
c.FullNode.DatabasePath = strings.Replace(c.FullNode.DatabasePath, *c.FullNode.SelectedNetwork, "CHALLENGE", 1)
128+
}
129+
125130
// dealWithAnchors swaps out the distinct sections of the config with pointers to a shared instance
126131
// When loading the config, the anchor definition in the initial-config is the canonical version. The rest will be
127132
// changed to point back to that instance

pkg/config/load_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package config_test
22

33
import (
4+
"os"
45
"testing"
56

67
"github.com/stretchr/testify/assert"
@@ -39,3 +40,25 @@ func TestDealWithAnchors(t *testing.T) {
3940
assert.Equal(t, "unittestnet", *cfg.Wallet.SelectedNetwork)
4041
assert.Equal(t, "unittestnet", *cfg.DataLayer.SelectedNetwork)
4142
}
43+
44+
func TestFillDatabasePath(t *testing.T) {
45+
def, err := config.LoadDefaultConfig()
46+
assert.NoError(t, err)
47+
assert.Equal(t, "db/blockchain_v2_mainnet.sqlite", def.FullNode.DatabasePath)
48+
49+
tmpDir, err := os.MkdirTemp("", "testfs")
50+
if err != nil {
51+
t.Fatalf("Failed to create temporary directory: %v", err)
52+
}
53+
defer func(path string) {
54+
err := os.RemoveAll(path)
55+
if err != nil {
56+
t.Fatalf("Error cleaning up test directory: %v", err)
57+
}
58+
}(tmpDir)
59+
60+
tmpFilePath := tmpDir + "/tempfile.txt"
61+
err = def.SavePath(tmpFilePath)
62+
assert.NoError(t, err)
63+
assert.Equal(t, "db/blockchain_v2_CHALLENGE.sqlite", def.FullNode.DatabasePath)
64+
}

0 commit comments

Comments
 (0)