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

Add a back compat helper for configs #174

Merged
merged 2 commits into from
Nov 17, 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
12 changes: 12 additions & 0 deletions pkg/config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"

"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -58,6 +59,7 @@ func LoadDefaultConfig() (*ChiaConfig, error) {
func commonLoad(configBytes []byte, rootPath string) (*ChiaConfig, error) {
config := &ChiaConfig{}

configBytes = FixBackCompat(configBytes)
err := yaml.Unmarshal(configBytes, config)
if err != nil {
return nil, err
Expand All @@ -70,6 +72,16 @@ func commonLoad(configBytes []byte, rootPath string) (*ChiaConfig, error) {
return config, nil
}

// FixBackCompat fixes any back compat issues with configs that might have been loaded by old versions of this package
func FixBackCompat(configBytes []byte) []byte {
// soa serial number incorrectly had string as a type for a while, and ended up quoted as a result
// remove the quotes since it's supposed to be a number
regex := regexp.MustCompile(`serial_number:\s*["'](\d+)["']`)
configBytes = regex.ReplaceAll(configBytes, []byte(`serial_number: $1`))

return configBytes
}

// Save saves the config at the path it was loaded from originally
func (c *ChiaConfig) Save() error {
if c.configPath == "" {
Expand Down
27 changes: 27 additions & 0 deletions pkg/config/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config_test

import (
"os"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -62,3 +63,29 @@ func TestFillDatabasePath(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, "db/blockchain_v2_CHALLENGE.sqlite", def.FullNode.DatabasePath)
}

func TestBackCompatSOASerialNumber(t *testing.T) {
badConfig := `seeder:
port: 8444
other_peers_port: 8444
dns_port: 53
peer_connect_timeout: 2
crawler_db_path: "crawler.db"
bootstrap_peers:
- "node.chia.net"
minimum_height: 240000
minimum_version_count: 100
domain_name: "seeder.example.com."
nameserver: "example.com."
ttl: 300
soa:
rname: "hostmaster.example.com"
serial_number: "1619105223"
refresh: 10800
retry: 10800
expire: 604800
minimum: 1800`

cfg := config.FixBackCompat([]byte(badConfig))
assert.True(t, strings.Contains(string(cfg), "serial_number: 1619105223"))
}