From 34f95071d9716ddfbd1c9843d869689546ed2ca0 Mon Sep 17 00:00:00 2001 From: Virgil Date: Sat, 4 Apr 2026 23:36:55 +0000 Subject: [PATCH] fix(proxy): align difficulty conversion with RFC examples Co-Authored-By: Virgil --- core_impl.go | 7 +++++-- job_test.go | 11 +++++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/core_impl.go b/core_impl.go index 9bc0e68..1df005b 100644 --- a/core_impl.go +++ b/core_impl.go @@ -186,7 +186,10 @@ func (j Job) DifficultyFromTarget() uint64 { if target == 0 { return 0 } - return uint64(math.MaxUint32 / uint64(target)) + if target == math.MaxUint32 { + return 1 + } + return uint64((uint64(math.MaxUint32) * 10) / uint64(target)) } func targetFromDifficulty(diff uint64) string { @@ -194,7 +197,7 @@ func targetFromDifficulty(diff uint64) string { return "ffffffff" } maxTarget := uint64(math.MaxUint32) - target := (maxTarget + diff - 1) / diff + target := (maxTarget*10 + diff - 1) / diff if target == 0 { target = 1 } diff --git a/job_test.go b/job_test.go index 6477874..4267c35 100644 --- a/job_test.go +++ b/job_test.go @@ -18,7 +18,14 @@ func TestJob_BlobWithFixedByte(t *testing.T) { func TestJob_DifficultyFromTarget(t *testing.T) { job := Job{Target: "b88d0600"} - if got := job.DifficultyFromTarget(); got == 0 { - t.Fatalf("expected non-zero difficulty") + if got := job.DifficultyFromTarget(); got != 100000 { + t.Fatalf("expected difficulty 100000, got %d", got) + } +} + +func TestJob_DifficultyFromTarget_MaxTarget(t *testing.T) { + job := Job{Target: "ffffffff"} + if got := job.DifficultyFromTarget(); got != 1 { + t.Fatalf("expected minimum difficulty 1, got %d", got) } }