diff --git a/config_runtime.go b/config_runtime.go index 3d842ee..ac185c2 100644 --- a/config_runtime.go +++ b/config_runtime.go @@ -46,11 +46,16 @@ func (c *Config) Validate() error { if c == nil { return errors.New("config is nil") } - if c.Mode != "" && c.Mode != "nicehash" && c.Mode != "simple" { + if strings.TrimSpace(c.Mode) == "" { + return errors.New("mode is empty") + } + if c.Mode != "nicehash" && c.Mode != "simple" { return errors.New("mode is invalid") } - if c.Workers != "" && - c.Workers != WorkersByRigID && + if strings.TrimSpace(string(c.Workers)) == "" { + return errors.New("workers mode is empty") + } + if c.Workers != WorkersByRigID && c.Workers != WorkersByUser && c.Workers != WorkersByPass && c.Workers != WorkersByPassword && @@ -101,12 +106,13 @@ func (w *ConfigWatcher) Start() { return } + if info, errorValue := os.Stat(w.path); errorValue == nil { + w.lastModifiedAt = info.ModTime() + } + go func() { ticker := time.NewTicker(time.Second) defer ticker.Stop() - if info, errorValue := os.Stat(w.path); errorValue == nil { - w.lastModifiedAt = info.ModTime() - } for { select { diff --git a/config_runtime_test.go b/config_runtime_test.go index fd78fde..52d1609 100644 --- a/config_runtime_test.go +++ b/config_runtime_test.go @@ -8,9 +8,10 @@ import ( func TestConfig_Validate_Good(t *testing.T) { cfg := &Config{ - Mode: "nicehash", - Bind: []BindAddr{{Host: "127.0.0.1", Port: 3333}}, - Pools: []PoolConfig{{URL: "pool-a:3333", Enabled: true}}, + Mode: "nicehash", + Workers: WorkersByRigID, + Bind: []BindAddr{{Host: "127.0.0.1", Port: 3333}}, + Pools: []PoolConfig{{URL: "pool-a:3333", Enabled: true}}, } if errorValue := cfg.Validate(); errorValue != nil { @@ -30,6 +31,17 @@ func TestConfig_Validate_Bad(t *testing.T) { } } +func TestConfig_Validate_EmptyMode(t *testing.T) { + cfg := &Config{ + Bind: []BindAddr{{Host: "127.0.0.1", Port: 3333}}, + Pools: []PoolConfig{{URL: "pool-a:3333", Enabled: true}}, + } + + if errorValue := cfg.Validate(); errorValue == nil { + t.Fatal("expected empty mode to fail validation") + } +} + func TestConfig_Validate_Ugly(t *testing.T) { cfg := &Config{ Mode: "simple", @@ -43,6 +55,18 @@ func TestConfig_Validate_Ugly(t *testing.T) { } } +func TestConfig_Validate_EmptyWorkers(t *testing.T) { + cfg := &Config{ + Mode: "simple", + Bind: []BindAddr{{Host: "127.0.0.1", Port: 3333}}, + Pools: []PoolConfig{{URL: "pool-a:3333", Enabled: true}}, + } + + if errorValue := cfg.Validate(); errorValue == nil { + t.Fatal("expected empty workers mode to fail validation") + } +} + func TestConfig_Validate_EnabledPoolURL(t *testing.T) { cfg := &Config{ Mode: "simple", @@ -57,7 +81,7 @@ func TestConfig_Validate_EnabledPoolURL(t *testing.T) { func TestConfigWatcher_Start_Bad(t *testing.T) { path := t.TempDir() + "/config.json" - errorValue := os.WriteFile(path, []byte(`{"bind":[{"host":"127.0.0.1","port":3333}],"pools":[{"url":"pool-a:3333","enabled":true}]}`), 0o644) + errorValue := os.WriteFile(path, []byte(`{"mode":"nicehash","workers":"rig-id","bind":[{"host":"127.0.0.1","port":3333}],"pools":[{"url":"pool-a:3333","enabled":true}]}`), 0o644) if errorValue != nil { t.Fatal(errorValue) } @@ -69,7 +93,7 @@ func TestConfigWatcher_Start_Bad(t *testing.T) { watcher.Start() defer watcher.Stop() - errorValue = os.WriteFile(path, []byte(`{"bind":[{"host":"127.0.0.1","port":3333}],"pools":[{"url":"pool-b:3333","enabled":true}]}`), 0o644) + errorValue = os.WriteFile(path, []byte(`{"mode":"nicehash","workers":"rig-id","bind":[{"host":"127.0.0.1","port":3333}],"pools":[{"url":"pool-b:3333","enabled":true}]}`), 0o644) if errorValue != nil { t.Fatal(errorValue) } diff --git a/splitter/simple/splitter_test.go b/splitter/simple/splitter_test.go index 06174da..0496954 100644 --- a/splitter/simple/splitter_test.go +++ b/splitter/simple/splitter_test.go @@ -166,7 +166,7 @@ func TestSimpleMapper_JobForID_BadClientID(t *testing.T) { func TestConfigWatcher_Start_Ugly(t *testing.T) { path := t.TempDir() + "/config.json" - errorValue := os.WriteFile(path, []byte(`{"bind":[{"host":"127.0.0.1","port":3333}],"pools":[{"url":"pool-a:3333","enabled":true}]}`), 0o644) + errorValue := os.WriteFile(path, []byte(`{"mode":"simple","workers":"rig-id","bind":[{"host":"127.0.0.1","port":3333}],"pools":[{"url":"pool-a:3333","enabled":true}]}`), 0o644) if errorValue != nil { t.Fatal(errorValue) } @@ -184,7 +184,7 @@ func TestConfigWatcher_Start_Ugly(t *testing.T) { case <-time.After(1200 * time.Millisecond): } - if errorValue = os.WriteFile(path, []byte(`{"bind":[{"host":"127.0.0.1","port":3333}],"pools":[{"url":"pool-b:3333","enabled":true}]}`), 0o644); errorValue != nil { + if errorValue = os.WriteFile(path, []byte(`{"mode":"simple","workers":"rig-id","bind":[{"host":"127.0.0.1","port":3333}],"pools":[{"url":"pool-b:3333","enabled":true}]}`), 0o644); errorValue != nil { t.Fatal(errorValue) }