From f5da054325d7b642dcc1b99dc4754d2a64f3ba01 Mon Sep 17 00:00:00 2001 From: Chris Marslender Date: Wed, 11 Sep 2024 08:01:08 -0500 Subject: [PATCH] Partially handle network related anchors in config (#147) --- pkg/config/config.go | 128 ++++++++++++++++----------------- pkg/config/env.go | 8 +++ pkg/config/env_test.go | 35 +++++++-- pkg/config/load.go | 88 ++++++++++++++++++++++- pkg/config/load_test.go | 41 +++++++++++ pkg/peerprotocol/connection.go | 6 +- 6 files changed, 233 insertions(+), 73 deletions(-) create mode 100644 pkg/config/load_test.go diff --git a/pkg/config/config.go b/pkg/config/config.go index f1cde2b..722ff70 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -18,8 +18,8 @@ type ChiaConfig struct { DaemonAllowTLS12 bool `yaml:"daemon_allow_tls_1_2"` InboundRateLimitPercent uint8 `yaml:"inbound_rate_limit_percent"` OutboundRateLimitPercent uint8 `yaml:"outbound_rate_limit_percent"` - NetworkOverrides NetworkOverrides `yaml:"network_overrides"` // @TODO this would usually be an anchor - SelectedNetwork string `yaml:"selected_network"` + NetworkOverrides *NetworkOverrides `yaml:"network_overrides"` + SelectedNetwork *string `yaml:"selected_network"` AlertsURL string `yaml:"ALERTS_URL"` ChiaAlertsPubkey string `yaml:"CHIA_ALERTS_PUBKEY"` PrivateSSLCA CAConfig `yaml:"private_ssl_ca"` @@ -115,22 +115,22 @@ type LoggingConfig struct { // SeederConfig seeder configuration section type SeederConfig struct { - Port uint16 `yaml:"port"` - OtherPeersPort uint16 `yaml:"other_peers_port"` - DNSPort uint16 `yaml:"dns_port"` - PeerConnectTimeout uint16 `yaml:"peer_connect_timeout"` - CrawlerDBPath string `yaml:"crawler_db_path"` - BootstrapPeers []string `yaml:"bootstrap_peers"` - MinimumHeight uint32 `yaml:"minimum_height"` - MinimumVersionCount uint32 `yaml:"minimum_version_count"` - DomainName string `yaml:"domain_name"` - Nameserver string `yaml:"nameserver"` - TTL uint16 `yaml:"ttl"` - SOA SeederSOA `yaml:"soa"` - NetworkOverrides NetworkOverrides `yaml:"network_overrides"` - SelectedNetwork string `yaml:"selected_network"` - Logging LoggingConfig `yaml:"logging"` - CrawlerConfig CrawlerConfig `yaml:"crawler"` + Port uint16 `yaml:"port"` + OtherPeersPort uint16 `yaml:"other_peers_port"` + DNSPort uint16 `yaml:"dns_port"` + PeerConnectTimeout uint16 `yaml:"peer_connect_timeout"` + CrawlerDBPath string `yaml:"crawler_db_path"` + BootstrapPeers []string `yaml:"bootstrap_peers"` + MinimumHeight uint32 `yaml:"minimum_height"` + MinimumVersionCount uint32 `yaml:"minimum_version_count"` + DomainName string `yaml:"domain_name"` + Nameserver string `yaml:"nameserver"` + TTL uint16 `yaml:"ttl"` + SOA SeederSOA `yaml:"soa"` + NetworkOverrides *NetworkOverrides `yaml:"network_overrides"` + SelectedNetwork *string `yaml:"selected_network"` + Logging LoggingConfig `yaml:"logging"` + CrawlerConfig CrawlerConfig `yaml:"crawler"` } // SeederSOA dns SOA for seeder @@ -158,8 +158,8 @@ type HarvesterConfig struct { PlotsRefreshParameter PlotsRefreshParameter `yaml:"plots_refresh_parameter"` ParallelRead bool `yaml:"parallel_read"` Logging LoggingConfig `yaml:"logging"` - NetworkOverrides NetworkOverrides `yaml:"network_overrides"` - SelectedNetwork string `yaml:"selected_network"` + NetworkOverrides *NetworkOverrides `yaml:"network_overrides"` + SelectedNetwork *string `yaml:"selected_network"` PlotDirectories []string `yaml:"plot_directories"` RecursivePlotScan bool `yaml:"recursive_plot_scan"` PortConfig `yaml:",inline"` @@ -186,23 +186,23 @@ type PlotsRefreshParameter struct { // PoolConfig configures pool settings type PoolConfig struct { - XCHTargetAddress string `yaml:"xch_target_address,omitempty"` - Logging LoggingConfig `yaml:"logging"` - NetworkOverrides NetworkOverrides `yaml:"network_overrides"` - SelectedNetwork string `yaml:"selected_network"` + XCHTargetAddress string `yaml:"xch_target_address,omitempty"` + Logging LoggingConfig `yaml:"logging"` + NetworkOverrides *NetworkOverrides `yaml:"network_overrides"` + SelectedNetwork *string `yaml:"selected_network"` } // FarmerConfig farmer configuration section type FarmerConfig struct { - FullNodePeers []Peer `yaml:"full_node_peers"` - PoolPublicKeys types.WonkySet `yaml:"pool_public_keys"` - XCHTargetAddress string `yaml:"xch_target_address,omitempty"` - StartRPCServer bool `yaml:"start_rpc_server"` - EnableProfiler bool `yaml:"enable_profiler"` - PoolShareThreshold uint32 `yaml:"pool_share_threshold"` - Logging LoggingConfig `yaml:"logging"` - NetworkOverrides NetworkOverrides `yaml:"network_overrides"` - SelectedNetwork string `yaml:"selected_network"` + FullNodePeers []Peer `yaml:"full_node_peers"` + PoolPublicKeys types.WonkySet `yaml:"pool_public_keys"` + XCHTargetAddress string `yaml:"xch_target_address,omitempty"` + StartRPCServer bool `yaml:"start_rpc_server"` + EnableProfiler bool `yaml:"enable_profiler"` + PoolShareThreshold uint32 `yaml:"pool_share_threshold"` + Logging LoggingConfig `yaml:"logging"` + NetworkOverrides *NetworkOverrides `yaml:"network_overrides"` + SelectedNetwork *string `yaml:"selected_network"` PortConfig `yaml:",inline"` SSL SSLConfig `yaml:"ssl"` } @@ -217,19 +217,19 @@ type TimelordLauncherConfig struct { // TimelordConfig timelord configuration section type TimelordConfig struct { - VDFClients VDFClients `yaml:"vdf_clients"` - FullNodePeers []Peer `yaml:"full_node_peers"` - MaxConnectionTime uint16 `yaml:"max_connection_time"` - VDFServer Peer `yaml:"vdf_server"` - Logging LoggingConfig `yaml:"logging"` - NetworkOverrides NetworkOverrides `yaml:"network_overrides"` - SelectedNetwork string `yaml:"selected_network"` - FastAlgorithm bool `yaml:"fast_algorithm"` - BlueboxMode bool `yaml:"bluebox_mode"` - SlowBluebox bool `yaml:"slow_bluebox"` - SlowBlueboxProcessCount uint8 `yaml:"slow_bluebox_process_count"` - MultiprocessingStartMethod string `yaml:"multiprocessing_start_method"` - StartRPCServer bool `yaml:"start_rpc_server"` + VDFClients VDFClients `yaml:"vdf_clients"` + FullNodePeers []Peer `yaml:"full_node_peers"` + MaxConnectionTime uint16 `yaml:"max_connection_time"` + VDFServer Peer `yaml:"vdf_server"` + Logging LoggingConfig `yaml:"logging"` + NetworkOverrides *NetworkOverrides `yaml:"network_overrides"` + SelectedNetwork *string `yaml:"selected_network"` + FastAlgorithm bool `yaml:"fast_algorithm"` + BlueboxMode bool `yaml:"bluebox_mode"` + SlowBluebox bool `yaml:"slow_bluebox"` + SlowBlueboxProcessCount uint8 `yaml:"slow_bluebox_process_count"` + MultiprocessingStartMethod string `yaml:"multiprocessing_start_method"` + StartRPCServer bool `yaml:"start_rpc_server"` PortConfig `yaml:",inline"` SSL SSLConfig `yaml:"ssl"` } @@ -283,8 +283,8 @@ type FullNodeConfig struct { DNSServers []string `yaml:"dns_servers"` IntroducerPeer Peer `yaml:"introducer_peer"` Logging LoggingConfig `yaml:"logging"` - NetworkOverrides NetworkOverrides `yaml:"network_overrides"` - SelectedNetwork string `yaml:"selected_network"` + NetworkOverrides *NetworkOverrides `yaml:"network_overrides"` + SelectedNetwork *string `yaml:"selected_network"` TrustedPeers map[string]string `yaml:"trusted_peers"` SSL SSLConfig `yaml:"ssl"` UseChiaLoopPolicy bool `yaml:"use_chia_loop_policy"` @@ -293,25 +293,25 @@ type FullNodeConfig struct { // UIConfig settings for the UI type UIConfig struct { PortConfig `yaml:",inline"` - SSHFilename string `yaml:"ssh_filename"` - Logging LoggingConfig `yaml:"logging"` - NetworkOverrides NetworkOverrides `yaml:"network_overrides"` - SelectedNetwork string `yaml:"selected_network"` - DaemonHost string `yaml:"daemon_host"` - DaemonPort uint16 `yaml:"daemon_port"` - DaemonSSL SSLConfig `yaml:"daemon_ssl"` + SSHFilename string `yaml:"ssh_filename"` + Logging LoggingConfig `yaml:"logging"` + NetworkOverrides *NetworkOverrides `yaml:"network_overrides"` + SelectedNetwork *string `yaml:"selected_network"` + DaemonHost string `yaml:"daemon_host"` + DaemonPort uint16 `yaml:"daemon_port"` + DaemonSSL SSLConfig `yaml:"daemon_ssl"` } // IntroducerConfig settings for introducers type IntroducerConfig struct { Host string `yaml:"host"` PortConfig `yaml:",inline"` - MaxPeersToSend uint16 `yaml:"max_peers_to_send"` - RecentPeerThreshold uint16 `yaml:"recent_peer_threshold"` - Logging LoggingConfig `yaml:"logging"` - NetworkOverrides NetworkOverrides `yaml:"network_overrides"` - SelectedNetwork string `yaml:"selected_network"` - SSL SSLConfig `yaml:"ssl"` + MaxPeersToSend uint16 `yaml:"max_peers_to_send"` + RecentPeerThreshold uint16 `yaml:"recent_peer_threshold"` + Logging LoggingConfig `yaml:"logging"` + NetworkOverrides *NetworkOverrides `yaml:"network_overrides"` + SelectedNetwork *string `yaml:"selected_network"` + SSL SSLConfig `yaml:"ssl"` } // WalletConfig wallet configuration section @@ -335,8 +335,8 @@ type WalletConfig struct { WalletPeersFilePath string `yaml:"wallet_peers_file_path"` LogSqliteCmds bool `yaml:"log_sqlite_cmds"` Logging LoggingConfig `yaml:"logging"` - NetworkOverrides NetworkOverrides `yaml:"network_overrides"` - SelectedNetwork string `yaml:"selected_network"` + NetworkOverrides *NetworkOverrides `yaml:"network_overrides"` + SelectedNetwork *string `yaml:"selected_network"` TargetPeerCount uint16 `yaml:"target_peer_count"` PeerConnectInterval uint8 `yaml:"peer_connect_interval"` RecentPeerThreshold uint16 `yaml:"recent_peer_threshold"` @@ -378,7 +378,7 @@ type DataLayerConfig struct { HostIP string `yaml:"host_ip"` HostPort uint16 `yaml:"host_port"` ManageDataInterval uint16 `yaml:"manage_data_interval"` - SelectedNetwork string `yaml:"selected_network"` + SelectedNetwork *string `yaml:"selected_network"` StartRPCServer bool `yaml:"start_rpc_server"` RPCServerMaxRequestBodySize uint32 `yaml:"rpc_server_max_request_body_size"` LogSqliteCmds bool `yaml:"log_sqlite_cmds"` diff --git a/pkg/config/env.go b/pkg/config/env.go index da55060..5732084 100644 --- a/pkg/config/env.go +++ b/pkg/config/env.go @@ -133,6 +133,11 @@ func setFieldByPath(v reflect.Value, path []string, value any) error { return fmt.Errorf("invalid path") } + // Deal with pointers + if v.Kind() == reflect.Ptr { + v = v.Elem() + } + for i := 0; i < v.NumField(); i++ { field := v.Type().Field(i) yamlTagRaw := field.Tag.Get("yaml") @@ -148,6 +153,9 @@ func setFieldByPath(v reflect.Value, path []string, value any) error { // If we only have 1 element left in paths, then we can set the value // Otherwise, we can recursively call setFieldByPath again, with the remaining elements of path fieldValue := v.Field(i) + if fieldValue.Kind() == reflect.Ptr { + fieldValue = fieldValue.Elem() + } if len(path) > 1 { if fieldValue.Kind() == reflect.Map { mapKey := reflect.ValueOf(path[1]) diff --git a/pkg/config/env_test.go b/pkg/config/env_test.go index 39d74e0..49e5cd7 100644 --- a/pkg/config/env_test.go +++ b/pkg/config/env_test.go @@ -18,7 +18,7 @@ func TestChiaConfig_SetFieldByPath(t *testing.T) { 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.SelectedNetwork, "mainnet") assert.Equal(t, defaultConfig.Logging.LogLevel, "WARNING") err = defaultConfig.SetFieldByPath([]string{"full_node", "port"}, "1234") @@ -36,7 +36,18 @@ func TestChiaConfig_SetFieldByPath(t *testing.T) { err = defaultConfig.SetFieldByPath([]string{"selected_network"}, "unittestnet") assert.NoError(t, err) - assert.Equal(t, defaultConfig.SelectedNetwork, "unittestnet") + assert.Equal(t, *defaultConfig.SelectedNetwork, "unittestnet") + // Ensure all the other selected networks got set too + assert.Equal(t, *defaultConfig.Seeder.SelectedNetwork, "unittestnet") + assert.Equal(t, *defaultConfig.Harvester.SelectedNetwork, "unittestnet") + assert.Equal(t, *defaultConfig.Pool.SelectedNetwork, "unittestnet") + assert.Equal(t, *defaultConfig.Farmer.SelectedNetwork, "unittestnet") + assert.Equal(t, *defaultConfig.Timelord.SelectedNetwork, "unittestnet") + assert.Equal(t, *defaultConfig.FullNode.SelectedNetwork, "unittestnet") + assert.Equal(t, *defaultConfig.UI.SelectedNetwork, "unittestnet") + assert.Equal(t, *defaultConfig.Introducer.SelectedNetwork, "unittestnet") + assert.Equal(t, *defaultConfig.Wallet.SelectedNetwork, "unittestnet") + assert.Equal(t, *defaultConfig.DataLayer.SelectedNetwork, "unittestnet") err = defaultConfig.SetFieldByPath([]string{"logging", "log_level"}, "INFO") assert.NoError(t, err) @@ -53,7 +64,7 @@ func TestChiaConfig_SetFieldByPath_FullObjects(t *testing.T) { 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.SelectedNetwork, "mainnet") assert.Equal(t, defaultConfig.Logging.LogLevel, "WARNING") // Test passing in json blobs @@ -62,15 +73,25 @@ func TestChiaConfig_SetFieldByPath_FullObjects(t *testing.T) { 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) + // Ensure this applied to the other areas of config as well + assert.Equal(t, "e739da31bcc4ab1767d9f1ca99eb3cec765fb3b3508f82e090374d5913d24806", defaultConfig.Seeder.NetworkOverrides.Constants["jsonnet"].GenesisChallenge) + assert.Equal(t, "e739da31bcc4ab1767d9f1ca99eb3cec765fb3b3508f82e090374d5913d24806", defaultConfig.Harvester.NetworkOverrides.Constants["jsonnet"].GenesisChallenge) + assert.Equal(t, "e739da31bcc4ab1767d9f1ca99eb3cec765fb3b3508f82e090374d5913d24806", defaultConfig.Pool.NetworkOverrides.Constants["jsonnet"].GenesisChallenge) + assert.Equal(t, "e739da31bcc4ab1767d9f1ca99eb3cec765fb3b3508f82e090374d5913d24806", defaultConfig.Farmer.NetworkOverrides.Constants["jsonnet"].GenesisChallenge) + assert.Equal(t, "e739da31bcc4ab1767d9f1ca99eb3cec765fb3b3508f82e090374d5913d24806", defaultConfig.Timelord.NetworkOverrides.Constants["jsonnet"].GenesisChallenge) + assert.Equal(t, "e739da31bcc4ab1767d9f1ca99eb3cec765fb3b3508f82e090374d5913d24806", defaultConfig.FullNode.NetworkOverrides.Constants["jsonnet"].GenesisChallenge) + assert.Equal(t, "e739da31bcc4ab1767d9f1ca99eb3cec765fb3b3508f82e090374d5913d24806", defaultConfig.UI.NetworkOverrides.Constants["jsonnet"].GenesisChallenge) + assert.Equal(t, "e739da31bcc4ab1767d9f1ca99eb3cec765fb3b3508f82e090374d5913d24806", defaultConfig.Introducer.NetworkOverrides.Constants["jsonnet"].GenesisChallenge) + assert.Equal(t, "e739da31bcc4ab1767d9f1ca99eb3cec765fb3b3508f82e090374d5913d24806", defaultConfig.Wallet.NetworkOverrides.Constants["jsonnet"].GenesisChallenge) // Test passing in yaml blobs err = defaultConfig.SetFieldByPath([]string{"network_overrides", "constants"}, `yamlnet: DIFFICULTY_CONSTANT_FACTOR: 44445555 - GENESIS_CHALLENGE: e739da31bcc4ab1767d9f1ca99eb3cec765fb3b3508f82e090374d5913d24806`) + GENESIS_CHALLENGE: 9eb3cec765fb3b3508f82e090374d5913d24806e739da31bcc4ab1767d9f1ca9`) 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) + assert.Equal(t, "9eb3cec765fb3b3508f82e090374d5913d24806e739da31bcc4ab1767d9f1ca9", defaultConfig.NetworkOverrides.Constants["yamlnet"].GenesisChallenge) } func TestChiaConfig_FillValuesFromEnvironment(t *testing.T) { @@ -81,7 +102,7 @@ func TestChiaConfig_FillValuesFromEnvironment(t *testing.T) { 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.SelectedNetwork, "mainnet") assert.Equal(t, defaultConfig.Logging.LogLevel, "WARNING") assert.NoError(t, os.Setenv("chia.full_node.port", "1234")) @@ -94,7 +115,7 @@ func TestChiaConfig_FillValuesFromEnvironment(t *testing.T) { assert.Equal(t, uint16(1234), defaultConfig.FullNode.Port) assert.Equal(t, uint16(5678), defaultConfig.FullNode.RPCPort) assert.Equal(t, types.Uint128From64(44445555), defaultConfig.NetworkOverrides.Constants["mainnet"].DifficultyConstantFactor) - assert.Equal(t, defaultConfig.SelectedNetwork, "unittestnet") + assert.Equal(t, *defaultConfig.SelectedNetwork, "unittestnet") assert.Equal(t, defaultConfig.Logging.LogLevel, "INFO") } diff --git a/pkg/config/load.go b/pkg/config/load.go index bc453a4..c9c853b 100644 --- a/pkg/config/load.go +++ b/pkg/config/load.go @@ -59,6 +59,7 @@ func commonLoad(configBytes []byte, rootPath string) (*ChiaConfig, error) { config.ChiaRoot = rootPath config.fillDatabasePath() + config.dealWithAnchors() return config, nil } @@ -88,5 +89,90 @@ func (c *ChiaConfig) GetFullPath(filename string) string { } func (c *ChiaConfig) fillDatabasePath() { - c.FullNode.DatabasePath = strings.Replace(c.FullNode.DatabasePath, "CHALLENGE", c.FullNode.SelectedNetwork, 1) + c.FullNode.DatabasePath = strings.Replace(c.FullNode.DatabasePath, "CHALLENGE", *c.FullNode.SelectedNetwork, 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 +// .self_hostname +// +// .harvester.farmer_peers[0].host +// .farmer.full_node_peers[0].host +// .timelord_launcher.host +// .timelord.vdf_clients.ip[0] +// .timelord.full_node_peers[0].host +// .timelord.vdf_server.host +// .ui.daemon_host +// .introducer.host +// .full_node_peers[0].host +// +// .selected_network +// +// .seeder.selected_network +// .harvester.selected_network +// .pool.selected_network +// .farmer.selected_network +// .timelord.selected_network +// .full_node.selected_network +// .ui.selected_network +// .introducer.selected_network +// .wallet.selected_network +// .data_layer.selected_network +// +// .network_overrides +// +// .seeder.network_overrides +// .harvester.network_overrides +// .pool.network_overrides +// .farmer.network_overrides +// .timelord.network_overrides +// .full_node.network_overrides +// .ui.network_overrides +// .introducer.network_overrides +// .wallet.network_overrides +// +// .logging +// +// .seeder.logging +// .harvester.logging +// .pool.logging +// .farmer.logging +// .timelord_launcher.logging +// .timelord.logging +// .full_node.logging +// .ui.logging +// .introducer.logging +// .wallet.logging +// .data_layer.logging +func (c *ChiaConfig) dealWithAnchors() { + // For now, just doing network_overrides and selected_network + // The rest have some edge case usefulness in not being treated like anchors always + if c.NetworkOverrides == nil { + c.NetworkOverrides = &NetworkOverrides{} + } + c.Seeder.NetworkOverrides = c.NetworkOverrides + c.Harvester.NetworkOverrides = c.NetworkOverrides + c.Pool.NetworkOverrides = c.NetworkOverrides + c.Farmer.NetworkOverrides = c.NetworkOverrides + c.Timelord.NetworkOverrides = c.NetworkOverrides + c.FullNode.NetworkOverrides = c.NetworkOverrides + c.UI.NetworkOverrides = c.NetworkOverrides + c.Introducer.NetworkOverrides = c.NetworkOverrides + c.Wallet.NetworkOverrides = c.NetworkOverrides + + if c.SelectedNetwork == nil { + mainnet := "mainnet" + c.SelectedNetwork = &mainnet + } + c.Seeder.SelectedNetwork = c.SelectedNetwork + c.Harvester.SelectedNetwork = c.SelectedNetwork + c.Pool.SelectedNetwork = c.SelectedNetwork + c.Farmer.SelectedNetwork = c.SelectedNetwork + c.Timelord.SelectedNetwork = c.SelectedNetwork + c.FullNode.SelectedNetwork = c.SelectedNetwork + c.UI.SelectedNetwork = c.SelectedNetwork + c.Introducer.SelectedNetwork = c.SelectedNetwork + c.Wallet.SelectedNetwork = c.SelectedNetwork + c.DataLayer.SelectedNetwork = c.SelectedNetwork } diff --git a/pkg/config/load_test.go b/pkg/config/load_test.go new file mode 100644 index 0000000..aaa09e7 --- /dev/null +++ b/pkg/config/load_test.go @@ -0,0 +1,41 @@ +package config_test + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/chia-network/go-chia-libs/pkg/config" +) + +func TestDealWithAnchors(t *testing.T) { + cfg, err := config.LoadDefaultConfig() + assert.NoError(t, err) + + mainnetConstants := cfg.NetworkOverrides.Constants["mainnet"] + mainnetConstants.GenesisChallenge = "testing123" + cfg.NetworkOverrides.Constants["mainnet"] = mainnetConstants + + assert.Equal(t, "testing123", cfg.Seeder.NetworkOverrides.Constants["mainnet"].GenesisChallenge) + assert.Equal(t, "testing123", cfg.Harvester.NetworkOverrides.Constants["mainnet"].GenesisChallenge) + assert.Equal(t, "testing123", cfg.Pool.NetworkOverrides.Constants["mainnet"].GenesisChallenge) + assert.Equal(t, "testing123", cfg.Farmer.NetworkOverrides.Constants["mainnet"].GenesisChallenge) + assert.Equal(t, "testing123", cfg.Timelord.NetworkOverrides.Constants["mainnet"].GenesisChallenge) + assert.Equal(t, "testing123", cfg.FullNode.NetworkOverrides.Constants["mainnet"].GenesisChallenge) + assert.Equal(t, "testing123", cfg.UI.NetworkOverrides.Constants["mainnet"].GenesisChallenge) + assert.Equal(t, "testing123", cfg.Introducer.NetworkOverrides.Constants["mainnet"].GenesisChallenge) + assert.Equal(t, "testing123", cfg.Wallet.NetworkOverrides.Constants["mainnet"].GenesisChallenge) + + *cfg.SelectedNetwork = "unittestnet" + + assert.Equal(t, "unittestnet", *cfg.Seeder.SelectedNetwork) + assert.Equal(t, "unittestnet", *cfg.Harvester.SelectedNetwork) + assert.Equal(t, "unittestnet", *cfg.Pool.SelectedNetwork) + assert.Equal(t, "unittestnet", *cfg.Farmer.SelectedNetwork) + assert.Equal(t, "unittestnet", *cfg.Timelord.SelectedNetwork) + assert.Equal(t, "unittestnet", *cfg.FullNode.SelectedNetwork) + assert.Equal(t, "unittestnet", *cfg.UI.SelectedNetwork) + assert.Equal(t, "unittestnet", *cfg.Introducer.SelectedNetwork) + assert.Equal(t, "unittestnet", *cfg.Wallet.SelectedNetwork) + assert.Equal(t, "unittestnet", *cfg.DataLayer.SelectedNetwork) +} diff --git a/pkg/peerprotocol/connection.go b/pkg/peerprotocol/connection.go index 63d4d42..41abc36 100644 --- a/pkg/peerprotocol/connection.go +++ b/pkg/peerprotocol/connection.go @@ -4,6 +4,7 @@ import ( "context" "crypto/sha256" "crypto/tls" + "errors" "fmt" "net" "net/http" @@ -68,7 +69,10 @@ func NewConnection(ip *net.IP, options ...ConnectionOptionFunc) (*Connection, er if err := c.loadChiaConfig(); err != nil { return nil, err } - c.networkID = c.chiaConfig.SelectedNetwork + if c.chiaConfig.SelectedNetwork == nil { + return nil, errors.New("selected_network empty in config") + } + c.networkID = *c.chiaConfig.SelectedNetwork } // Generate the websocket dialer