From 167ecc2bdc81fa55ad79ff089d658a5c4cbffa3e Mon Sep 17 00:00:00 2001 From: Virgil Date: Sat, 4 Apr 2026 23:10:35 +0000 Subject: [PATCH] refactor(proxy): clarify agent-facing names Co-Authored-By: Virgil --- core_impl.go | 14 ++++----- splitter/nicehash/impl.go | 12 ++++---- splitter/simple/impl.go | 8 ++--- state_impl.go | 62 +++++++++++++++++++-------------------- 4 files changed, 48 insertions(+), 48 deletions(-) diff --git a/core_impl.go b/core_impl.go index 217b2b7..c1bb13b 100644 --- a/core_impl.go +++ b/core_impl.go @@ -56,12 +56,12 @@ func LoadConfig(path string) (*Config, Result) { return nil, errorResult(err) } - cfg := &Config{} - if err := json.Unmarshal(data, cfg); err != nil { + config := &Config{} + if err := json.Unmarshal(data, config); err != nil { return nil, errorResult(err) } - cfg.configPath = path - return cfg, cfg.Validate() + config.configPath = path + return config, config.Validate() } // Validate checks that mandatory bind and pool settings are present. @@ -324,9 +324,9 @@ func (w *ConfigWatcher) Start() { mod := info.ModTime() if mod.After(w.lastMod) { w.lastMod = mod - cfg, result := LoadConfig(w.path) - if result.OK && cfg != nil { - w.onChange(cfg) + config, result := LoadConfig(w.path) + if result.OK && config != nil { + w.onChange(config) } } case <-w.done: diff --git a/splitter/nicehash/impl.go b/splitter/nicehash/impl.go index e160dd2..1d8ef9f 100644 --- a/splitter/nicehash/impl.go +++ b/splitter/nicehash/impl.go @@ -8,20 +8,20 @@ import ( ) func init() { - proxy.RegisterSplitterFactory("nicehash", func(cfg *proxy.Config, events *proxy.EventBus) proxy.Splitter { - return NewNonceSplitter(cfg, events, pool.NewStrategyFactory(cfg)) + proxy.RegisterSplitterFactory("nicehash", func(config *proxy.Config, eventBus *proxy.EventBus) proxy.Splitter { + return NewNonceSplitter(config, eventBus, pool.NewStrategyFactory(config)) }) } // NewNonceSplitter creates a NiceHash splitter. -func NewNonceSplitter(config *proxy.Config, events *proxy.EventBus, factory pool.StrategyFactory) *NonceSplitter { +func NewNonceSplitter(config *proxy.Config, eventBus *proxy.EventBus, factory pool.StrategyFactory) *NonceSplitter { if factory == nil { factory = pool.NewStrategyFactory(config) } return &NonceSplitter{ byID: make(map[int64]*NonceMapper), config: config, - events: events, + events: eventBus, strategyFactory: factory, } } @@ -212,13 +212,13 @@ func (s *NonceSplitter) addMapperLocked() *NonceMapper { } // NewNonceMapper creates a mapper for one upstream connection. -func NewNonceMapper(id int64, cfg *proxy.Config, strategy pool.Strategy) *NonceMapper { +func NewNonceMapper(id int64, config *proxy.Config, strategy pool.Strategy) *NonceMapper { return &NonceMapper{ id: id, storage: NewNonceStorage(), strategy: strategy, pending: make(map[int64]SubmitContext), - config: cfg, + config: config, } } diff --git a/splitter/simple/impl.go b/splitter/simple/impl.go index 4fb2d41..536272e 100644 --- a/splitter/simple/impl.go +++ b/splitter/simple/impl.go @@ -8,13 +8,13 @@ import ( ) func init() { - proxy.RegisterSplitterFactory("simple", func(cfg *proxy.Config, events *proxy.EventBus) proxy.Splitter { - return NewSimpleSplitter(cfg, events, pool.NewStrategyFactory(cfg)) + proxy.RegisterSplitterFactory("simple", func(config *proxy.Config, eventBus *proxy.EventBus) proxy.Splitter { + return NewSimpleSplitter(config, eventBus, pool.NewStrategyFactory(config)) }) } // NewSimpleSplitter creates the passthrough splitter. -func NewSimpleSplitter(config *proxy.Config, events *proxy.EventBus, factory pool.StrategyFactory) *SimpleSplitter { +func NewSimpleSplitter(config *proxy.Config, eventBus *proxy.EventBus, factory pool.StrategyFactory) *SimpleSplitter { if factory == nil { factory = pool.NewStrategyFactory(config) } @@ -22,7 +22,7 @@ func NewSimpleSplitter(config *proxy.Config, events *proxy.EventBus, factory poo active: make(map[int64]*SimpleMapper), idle: make(map[int64]*SimpleMapper), config: config, - events: events, + events: eventBus, factory: factory, } } diff --git a/state_impl.go b/state_impl.go index 92cae92..47494d8 100644 --- a/state_impl.go +++ b/state_impl.go @@ -963,17 +963,17 @@ type stratumRequest struct { } func (m *Miner) handleLine(line []byte) bool { - var req stratumRequest - if err := json.Unmarshal(line, &req); err != nil { + var request stratumRequest + if err := json.Unmarshal(line, &request); err != nil { return false } - switch req.Method { + switch request.Method { case "login": - m.handleLogin(req) + m.handleLogin(request) case "submit": - m.handleSubmit(req) + m.handleSubmit(request) case "keepalived": - m.handleKeepalived(req) + m.handleKeepalived(request) } return true } @@ -986,17 +986,17 @@ type loginParams struct { RigID string `json:"rigid"` } -func (m *Miner) handleLogin(req stratumRequest) { +func (m *Miner) handleLogin(request stratumRequest) { if m.state != MinerStateWaitLogin { return } var params loginParams - if err := json.Unmarshal(req.Params, ¶ms); err != nil || strings.TrimSpace(params.Login) == "" { - m.ReplyWithError(requestID(req.ID), "Invalid payment address provided") + if err := json.Unmarshal(request.Params, ¶ms); err != nil || strings.TrimSpace(params.Login) == "" { + m.ReplyWithError(requestID(request.ID), "Invalid payment address provided") return } if m.accessPassword != "" && params.Pass != m.accessPassword { - m.ReplyWithError(requestID(req.ID), "Invalid password") + m.ReplyWithError(requestID(request.ID), "Invalid password") return } m.user, m.customDiff = parseLoginUser(params.Login, m.globalDiff) @@ -1018,16 +1018,16 @@ func (m *Miner) handleLogin(req stratumRequest) { if m.MapperID() < 0 { m.state = MinerStateWaitLogin m.rpcID = "" - m.ReplyWithError(requestID(req.ID), "Proxy is full, try again later") + m.ReplyWithError(requestID(request.ID), "Proxy is full, try again later") return } } else if m.RouteID() < 0 { m.state = MinerStateWaitLogin m.rpcID = "" - m.ReplyWithError(requestID(req.ID), "Proxy is unavailable, try again later") + m.ReplyWithError(requestID(request.ID), "Proxy is unavailable, try again later") return } - m.replyLoginSuccess(requestID(req.ID)) + m.replyLoginSuccess(requestID(request.ID)) } func parseLoginUser(login string, globalDiff uint64) (string, uint64) { @@ -1044,9 +1044,9 @@ func parseLoginUser(login string, globalDiff uint64) (string, uint64) { return login, 0 } -func (m *Miner) handleSubmit(req stratumRequest) { +func (m *Miner) handleSubmit(request stratumRequest) { if m.state != MinerStateReady { - m.ReplyWithError(requestID(req.ID), "Unauthenticated") + m.ReplyWithError(requestID(request.ID), "Unauthenticated") return } var params struct { @@ -1056,20 +1056,20 @@ func (m *Miner) handleSubmit(req stratumRequest) { Result string `json:"result"` Algo string `json:"algo"` } - if err := json.Unmarshal(req.Params, ¶ms); err != nil { - m.ReplyWithError(requestID(req.ID), "Invalid nonce") + if err := json.Unmarshal(request.Params, ¶ms); err != nil { + m.ReplyWithError(requestID(request.ID), "Invalid nonce") return } if params.ID != m.rpcID { - m.ReplyWithError(requestID(req.ID), "Unauthenticated") + m.ReplyWithError(requestID(request.ID), "Unauthenticated") return } if params.JobID == "" { - m.ReplyWithError(requestID(req.ID), "Missing job id") + m.ReplyWithError(requestID(request.ID), "Missing job id") return } if !isLowerHex8(params.Nonce) { - m.ReplyWithError(requestID(req.ID), "Invalid nonce") + m.ReplyWithError(requestID(request.ID), "Invalid nonce") return } if m.onSubmit != nil { @@ -1079,15 +1079,15 @@ func (m *Miner) handleSubmit(req stratumRequest) { Nonce: params.Nonce, Result: params.Result, Algo: params.Algo, - RequestID: requestID(req.ID), + RequestID: requestID(request.ID), }) } m.touchActivity() } -func (m *Miner) handleKeepalived(req stratumRequest) { +func (m *Miner) handleKeepalived(request stratumRequest) { m.touchActivity() - m.Success(requestID(req.ID), "KEEPALIVED") + m.Success(requestID(request.ID), "KEEPALIVED") } func requestID(id any) int64 { @@ -1432,18 +1432,18 @@ func insertTopDiff(top *[10]uint64, diff uint64) { // // workers := proxy.NewWorkers(proxy.WorkersByRigID, bus) // workers.OnLogin(proxy.Event{Miner: miner}) -func NewWorkers(mode WorkersMode, bus *EventBus) *Workers { +func NewWorkers(mode WorkersMode, eventBus *EventBus) *Workers { workers := &Workers{ mode: mode, nameIndex: make(map[string]int), idIndex: make(map[int64]int), } - workers.bindEvents(bus) + workers.bindEvents(eventBus) return workers } -func (w *Workers) bindEvents(bus *EventBus) { - if w == nil || bus == nil { +func (w *Workers) bindEvents(eventBus *EventBus) { + if w == nil || eventBus == nil { return } w.mu.Lock() @@ -1451,10 +1451,10 @@ func (w *Workers) bindEvents(bus *EventBus) { if w.subscribed { return } - bus.Subscribe(EventLogin, w.OnLogin) - bus.Subscribe(EventAccept, w.OnAccept) - bus.Subscribe(EventReject, w.OnReject) - bus.Subscribe(EventClose, w.OnClose) + eventBus.Subscribe(EventLogin, w.OnLogin) + eventBus.Subscribe(EventAccept, w.OnAccept) + eventBus.Subscribe(EventReject, w.OnReject) + eventBus.Subscribe(EventClose, w.OnClose) w.subscribed = true }