From d0ae26a1a2a380f0623b638df0070ca4e4d8228e Mon Sep 17 00:00:00 2001 From: Virgil Date: Sun, 5 Apr 2026 01:05:00 +0000 Subject: [PATCH] Align difficulty math with RFC --- api_rows.go | 4 ++-- core_impl.go | 9 +++------ customdiff_test.go | 2 +- job_test.go | 4 ++-- miner_login_test.go | 8 ++++---- 5 files changed, 12 insertions(+), 15 deletions(-) diff --git a/api_rows.go b/api_rows.go index 1c4c79e..323cf10 100644 --- a/api_rows.go +++ b/api_rows.go @@ -1,9 +1,9 @@ package proxy -// WorkerRow{"rig-alpha", "10.0.0.1", 1, 10, 0, 0, 100000, 1712232000, 1.0, 1.0, 1.0, 1.0, 1.0} +// WorkerRow{"rig-alpha", "10.0.0.1", 1, 10, 0, 0, 10000, 1712232000, 1.0, 1.0, 1.0, 1.0, 1.0} type WorkerRow [13]any -// MinerRow{1, "10.0.0.1:49152", 4096, 512, 2, 100000, "WALLET", "********", "rig-alpha", "XMRig/6.21.0"} +// MinerRow{1, "10.0.0.1:49152", 4096, 512, 2, 10000, "WALLET", "********", "rig-alpha", "XMRig/6.21.0"} type MinerRow [10]any // doc := p.SummaryDocument() diff --git a/core_impl.go b/core_impl.go index 142996a..54e28d6 100644 --- a/core_impl.go +++ b/core_impl.go @@ -173,7 +173,7 @@ func (j Job) BlobWithFixedByte(fixedByte uint8) string { return string(blob) } -// DifficultyFromTarget converts the target to a rough integer difficulty. +// DifficultyFromTarget converts the 8-char little-endian target into a difficulty. // // diff := job.DifficultyFromTarget() func (j Job) DifficultyFromTarget() uint64 { @@ -188,10 +188,7 @@ func (j Job) DifficultyFromTarget() uint64 { if target == 0 { return 0 } - if target == math.MaxUint32 { - return 1 - } - return uint64((uint64(math.MaxUint32) * 10) / uint64(target)) + return uint64(math.MaxUint32) / uint64(target) } func targetFromDifficulty(diff uint64) string { @@ -199,7 +196,7 @@ func targetFromDifficulty(diff uint64) string { return "ffffffff" } maxTarget := uint64(math.MaxUint32) - target := (maxTarget*10 + diff - 1) / diff + target := (maxTarget + diff - 1) / diff if target == 0 { target = 1 } diff --git a/customdiff_test.go b/customdiff_test.go index 220b7ee..19823e8 100644 --- a/customdiff_test.go +++ b/customdiff_test.go @@ -30,7 +30,7 @@ func TestCustomDiff_OnLogin(t *testing.T) { } func TestEffectiveShareDifficulty_CustomDiffCapsPoolDifficulty(t *testing.T) { - job := Job{Target: "b88d0600"} + job := Job{Target: "01000000"} miner := &Miner{customDiff: 25000} if got := EffectiveShareDifficulty(job, miner); got != 25000 { diff --git a/job_test.go b/job_test.go index 4267c35..1e3d36f 100644 --- a/job_test.go +++ b/job_test.go @@ -18,8 +18,8 @@ func TestJob_BlobWithFixedByte(t *testing.T) { func TestJob_DifficultyFromTarget(t *testing.T) { job := Job{Target: "b88d0600"} - if got := job.DifficultyFromTarget(); got != 100000 { - t.Fatalf("expected difficulty 100000, got %d", got) + if got := job.DifficultyFromTarget(); got != 10000 { + t.Fatalf("expected difficulty 10000, got %d", got) } } diff --git a/miner_login_test.go b/miner_login_test.go index 02cad22..26b76b3 100644 --- a/miner_login_test.go +++ b/miner_login_test.go @@ -183,12 +183,12 @@ func TestMiner_HandleLogin_CustomDiffCap_Good(t *testing.T) { miner := NewMiner(minerConn, 3333, nil) miner.onLogin = func(m *Miner) { m.SetRouteID(1) - m.customDiff = 50000 + m.customDiff = 5000 } miner.currentJob = Job{ Blob: strings.Repeat("0", 160), JobID: "job-1", - Target: targetFromDifficulty(100000), + Target: "01000000", } params, err := json.Marshal(loginParams{ @@ -219,8 +219,8 @@ func TestMiner_HandleLogin_CustomDiffCap_Good(t *testing.T) { originalDiff := miner.currentJob.DifficultyFromTarget() cappedDiff := Job{Target: payload.Result.Job.Target}.DifficultyFromTarget() - if cappedDiff == 0 || cappedDiff > 50000 { - t.Fatalf("expected capped difficulty at or below 50000, got %d", cappedDiff) + if cappedDiff == 0 || cappedDiff > 5000 { + t.Fatalf("expected capped difficulty at or below 5000, got %d", cappedDiff) } if cappedDiff >= originalDiff { t.Fatalf("expected lowered target difficulty below %d, got %d", originalDiff, cappedDiff)