Skip to content

Commit

Permalink
Partially handle network related anchors in config (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
cmmarslender authored Sep 11, 2024
1 parent fa1e8d0 commit f5da054
Show file tree
Hide file tree
Showing 6 changed files with 233 additions and 73 deletions.
128 changes: 64 additions & 64 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"`
Expand All @@ -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"`
}
Expand All @@ -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"`
}
Expand Down Expand Up @@ -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"`
Expand All @@ -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
Expand All @@ -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"`
Expand Down Expand Up @@ -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"`
Expand Down
8 changes: 8 additions & 0 deletions pkg/config/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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])
Expand Down
35 changes: 28 additions & 7 deletions pkg/config/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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) {
Expand All @@ -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"))
Expand All @@ -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")
}

Expand Down
Loading

0 comments on commit f5da054

Please sign in to comment.