Skip to content

Commit

Permalink
put CHALLENGE back to the database path when writing to disk (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
cmmarslender authored Sep 24, 2024
1 parent 228b312 commit 11e544b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions pkg/config/load_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config_test

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -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)
}

0 comments on commit 11e544b

Please sign in to comment.