fix(proxy): align difficulty conversion with RFC examples

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-04 23:36:55 +00:00
parent 4e5311215d
commit 34f95071d9
2 changed files with 14 additions and 4 deletions

View file

@ -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
}

View file

@ -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)
}
}