diff --git a/pkg/config/env.go b/pkg/config/env.go index 34ad038..da55060 100644 --- a/pkg/config/env.go +++ b/pkg/config/env.go @@ -8,6 +8,8 @@ import ( "strconv" "strings" + "gopkg.in/yaml.v3" + "github.com/chia-network/go-chia-libs/pkg/types" ) @@ -191,6 +193,18 @@ func setFieldByPath(v reflect.Value, path []string, value any) error { return nil } + // Handle YAML (and therefore JSON) parsing for passing in entire structs/maps + // This is particularly useful if you want to pass in a whole blob of network constants at once + if fieldValue.Kind() == reflect.Struct || fieldValue.Kind() == reflect.Map { + if strValue, ok := value.(string); ok { + yamlData := []byte(strValue) + if err := yaml.Unmarshal(yamlData, fieldValue.Addr().Interface()); err != nil { + return fmt.Errorf("failed to unmarshal yaml into field: %w", err) + } + } + return nil + } + val := reflect.ValueOf(value) if fieldValue.Type() != val.Type() { diff --git a/pkg/config/env_test.go b/pkg/config/env_test.go index 369df34..39d74e0 100644 --- a/pkg/config/env_test.go +++ b/pkg/config/env_test.go @@ -43,6 +43,36 @@ func TestChiaConfig_SetFieldByPath(t *testing.T) { assert.Equal(t, defaultConfig.Logging.LogLevel, "INFO") } +// TestChiaConfig_SetFieldByPath_FullObjects Tests that we can pass in and correctly parse a whole section of config +// as json or yaml and that it gets set properly +func TestChiaConfig_SetFieldByPath_FullObjects(t *testing.T) { + defaultConfig, err := config.LoadDefaultConfig() + assert.NoError(t, err) + // Make assertions about the default state, to ensure the assumed initial values are correct + assert.Equal(t, uint16(8444), defaultConfig.FullNode.Port) + assert.Equal(t, uint16(8555), defaultConfig.FullNode.RPCPort) + assert.NotNil(t, defaultConfig.NetworkOverrides.Constants["mainnet"]) + assert.Equal(t, defaultConfig.NetworkOverrides.Constants["mainnet"].DifficultyConstantFactor, types.Uint128{}) + assert.Equal(t, defaultConfig.SelectedNetwork, "mainnet") + assert.Equal(t, defaultConfig.Logging.LogLevel, "WARNING") + + // Test passing in json blobs + err = defaultConfig.SetFieldByPath([]string{"network_overrides", "constants"}, `{"jsonnet":{"DIFFICULTY_CONSTANT_FACTOR":44445555,"GENESIS_CHALLENGE":e739da31bcc4ab1767d9f1ca99eb3cec765fb3b3508f82e090374d5913d24806}}`) + assert.NoError(t, err) + assert.NotNil(t, defaultConfig.NetworkOverrides.Constants["jsonnet"]) + assert.Equal(t, types.Uint128From64(44445555), defaultConfig.NetworkOverrides.Constants["jsonnet"].DifficultyConstantFactor) + assert.Equal(t, "e739da31bcc4ab1767d9f1ca99eb3cec765fb3b3508f82e090374d5913d24806", defaultConfig.NetworkOverrides.Constants["jsonnet"].GenesisChallenge) + + // Test passing in yaml blobs + err = defaultConfig.SetFieldByPath([]string{"network_overrides", "constants"}, `yamlnet: + DIFFICULTY_CONSTANT_FACTOR: 44445555 + GENESIS_CHALLENGE: e739da31bcc4ab1767d9f1ca99eb3cec765fb3b3508f82e090374d5913d24806`) + assert.NoError(t, err) + assert.NotNil(t, defaultConfig.NetworkOverrides.Constants["yamlnet"]) + assert.Equal(t, types.Uint128From64(44445555), defaultConfig.NetworkOverrides.Constants["yamlnet"].DifficultyConstantFactor) + assert.Equal(t, "e739da31bcc4ab1767d9f1ca99eb3cec765fb3b3508f82e090374d5913d24806", defaultConfig.NetworkOverrides.Constants["yamlnet"].GenesisChallenge) +} + func TestChiaConfig_FillValuesFromEnvironment(t *testing.T) { defaultConfig, err := config.LoadDefaultConfig() assert.NoError(t, err) diff --git a/pkg/rpc/daemon.go b/pkg/rpc/daemon.go index 9df45e9..4c741d3 100644 --- a/pkg/rpc/daemon.go +++ b/pkg/rpc/daemon.go @@ -169,7 +169,7 @@ func (s *DaemonService) IsRunning(opts *IsRunningOptions) (*IsRunningResponse, * } // DaemonDeleteAllKeysOpts options for delete all keys request -type DaemonDeleteAllKeysOpts struct {} +type DaemonDeleteAllKeysOpts struct{} // DaemonDeleteAllKeysResponse response when deleting all keys type DaemonDeleteAllKeysResponse struct {