fix(nicehash): count stale job hits

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-04 14:19:22 +00:00
parent f16c9033e3
commit f2fd83caad
2 changed files with 15 additions and 0 deletions

View file

@ -21,6 +21,7 @@ type NonceStorage struct {
job proxy.Job // current job from pool
prevJob proxy.Job // previous job (for stale submit validation)
cursor int // search starts here (round-robin allocation)
expired uint64 // stale job ID hits for the previous job
mu sync.Mutex
}
@ -132,6 +133,7 @@ func (s *NonceStorage) JobForID(id string) (job proxy.Job, valid bool, expired b
return s.job, true, false
}
if s.prevJob.IsValid() && s.prevJob.ClientID != "" && id == s.prevJob.JobID {
s.expired++
return s.prevJob, true, true
}
return proxy.Job{}, false, false
@ -166,6 +168,16 @@ func (s *NonceStorage) SlotCount() (free int, dead int, active int) {
return free, dead, active
}
// ExpiredCount returns the number of times the previous job ID has been accepted as stale.
//
// count := storage.ExpiredCount()
func (s *NonceStorage) ExpiredCount() uint64 {
s.mu.Lock()
defer s.mu.Unlock()
return s.expired
}
// Miners returns a snapshot of the active miner map.
func (s *NonceStorage) Miners() map[int64]*proxy.Miner {
s.mu.Lock()

View file

@ -60,6 +60,9 @@ func TestNonceStorage_IsValidJobID_Ugly(t *testing.T) {
if !storage.IsValidJobID("job-1") {
t.Fatal("expected previous job ID from same client to remain valid")
}
if got := storage.ExpiredCount(); got != 1 {
t.Fatalf("expected stale job lookups to increment the expired counter, got %d", got)
}
}
func TestNonceStorage_IsValidJobID_BadClientID(t *testing.T) {