Skip to content

Commit

Permalink
Add a back compat helper for configs (#174)
Browse files Browse the repository at this point in the history
* Add a back compat helper for configs

* Tests for back compat fixes
  • Loading branch information
cmmarslender authored Nov 17, 2024
1 parent d5eef52 commit afcf4f8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
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"))
}

0 comments on commit afcf4f8

Please sign in to comment.