refactor(proxy): improve splitter registry naming

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-05 00:36:29 +00:00
parent 55d44df9c2
commit 8ad123ecab
3 changed files with 21 additions and 11 deletions

View file

@ -31,8 +31,8 @@ func errorResult(err error) Result {
return Result{OK: false, Error: err}
}
var splitterRegistryMu sync.RWMutex
var splitterRegistry = map[string]func(*Config, *EventBus) Splitter{}
var splitterFactoriesMu sync.RWMutex
var splitterFactories = map[string]func(*Config, *EventBus) Splitter{}
// Register a mode-specific splitter constructor.
//
@ -40,15 +40,15 @@ var splitterRegistry = map[string]func(*Config, *EventBus) Splitter{}
// return simple.NewSimpleSplitter(cfg, bus, nil)
// })
func RegisterSplitterFactory(mode string, factory func(*Config, *EventBus) Splitter) {
splitterRegistryMu.Lock()
defer splitterRegistryMu.Unlock()
splitterRegistry[strings.ToLower(mode)] = factory
splitterFactoriesMu.Lock()
defer splitterFactoriesMu.Unlock()
splitterFactories[strings.ToLower(mode)] = factory
}
func getSplitterFactory(mode string) (func(*Config, *EventBus) Splitter, bool) {
splitterRegistryMu.RLock()
defer splitterRegistryMu.RUnlock()
factory, ok := splitterRegistry[strings.ToLower(mode)]
func lookupSplitterFactory(mode string) (func(*Config, *EventBus) Splitter, bool) {
splitterFactoriesMu.RLock()
defer splitterFactoriesMu.RUnlock()
factory, ok := splitterFactories[strings.ToLower(mode)]
return factory, ok
}
@ -150,11 +150,17 @@ func (b *EventBus) Dispatch(e Event) {
}
// IsValid returns true when the job contains a blob and job id.
//
// if !job.IsValid() {
// return
// }
func (j Job) IsValid() bool {
return j.Blob != "" && j.JobID != ""
}
// BlobWithFixedByte replaces the blob byte at position 39 with fixedByte.
//
// partitioned := job.BlobWithFixedByte(0x2A)
func (j Job) BlobWithFixedByte(fixedByte uint8) string {
if len(j.Blob) < 80 {
return j.Blob
@ -168,6 +174,8 @@ func (j Job) BlobWithFixedByte(fixedByte uint8) string {
}
// DifficultyFromTarget converts the target to a rough integer difficulty.
//
// diff := job.DifficultyFromTarget()
func (j Job) DifficultyFromTarget() uint64 {
if len(j.Target) != 8 {
return 0

4
job.go
View file

@ -2,7 +2,9 @@ package proxy
// Job holds one pool work unit and its metadata.
//
// j := proxy.Job{Blob: "0707d5ef...b01", JobID: "4BiGm3/RgGQzgkTI", Target: "b88d0600", Algo: "cn/r"}
// j := proxy.Job{Blob: strings.Repeat("0", 160), JobID: "4BiGm3/RgGQzgkTI", Target: "b88d0600", Algo: "cn/r"}
// _ = j.BlobWithFixedByte(0x2A)
// _ = j.DifficultyFromTarget()
type Job struct {
Blob string // hex-encoded block template (160 hex chars = 80 bytes)
JobID string // pool-assigned identifier

View file

@ -83,7 +83,7 @@ func New(config *Config) (*Proxy, Result) {
p.watcher = NewConfigWatcher(config.configPath, p.Reload)
}
if factory, ok := getSplitterFactory(config.Mode); ok {
if factory, ok := lookupSplitterFactory(config.Mode); ok {
p.splitter = factory(config, p.events)
} else {
p.splitter = &noopSplitter{}