Align difficulty math with RFC

This commit is contained in:
Virgil 2026-04-05 01:05:00 +00:00
parent 3f9da136e9
commit d0ae26a1a2
5 changed files with 12 additions and 15 deletions

View file

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

View file

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

View file

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

View file

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

View file

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