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

put CHALLENGE back to the database path when writing to disk #161

Merged
merged 1 commit into from
Sep 24, 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
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)
}