From 6b46b4b861ec853b18d871135ed0d973cf70ffdb Mon Sep 17 00:00:00 2001 From: Chris Marslender Date: Sun, 17 Nov 2024 11:27:51 -0600 Subject: [PATCH 1/2] Add a back compat helper for configs --- pkg/config/load.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pkg/config/load.go b/pkg/config/load.go index e09773e..67e66bb 100644 --- a/pkg/config/load.go +++ b/pkg/config/load.go @@ -7,6 +7,7 @@ import ( "fmt" "os" "path/filepath" + "regexp" "strings" "gopkg.in/yaml.v3" @@ -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 @@ -70,6 +72,15 @@ func commonLoad(configBytes []byte, rootPath string) (*ChiaConfig, error) { return config, nil } +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 == "" { From a4aed1e38249e1b20d3db4411cf715a76b99d215 Mon Sep 17 00:00:00 2001 From: Chris Marslender Date: Sun, 17 Nov 2024 11:35:38 -0600 Subject: [PATCH 2/2] Tests for back compat fixes --- pkg/config/load.go | 5 +++-- pkg/config/load_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/pkg/config/load.go b/pkg/config/load.go index 67e66bb..1374189 100644 --- a/pkg/config/load.go +++ b/pkg/config/load.go @@ -59,7 +59,7 @@ func LoadDefaultConfig() (*ChiaConfig, error) { func commonLoad(configBytes []byte, rootPath string) (*ChiaConfig, error) { config := &ChiaConfig{} - configBytes = fixBackCompat(configBytes) + configBytes = FixBackCompat(configBytes) err := yaml.Unmarshal(configBytes, config) if err != nil { return nil, err @@ -72,7 +72,8 @@ func commonLoad(configBytes []byte, rootPath string) (*ChiaConfig, error) { return config, nil } -func fixBackCompat(configBytes []byte) []byte { +// 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+)["']`) diff --git a/pkg/config/load_test.go b/pkg/config/load_test.go index 4778ff2..9b4af10 100644 --- a/pkg/config/load_test.go +++ b/pkg/config/load_test.go @@ -2,6 +2,7 @@ package config_test import ( "os" + "strings" "testing" "github.com/stretchr/testify/assert" @@ -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")) +}