-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathvardiff.go
94 lines (74 loc) · 2.06 KB
/
vardiff.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package vardiff
import (
"time"
"github.com/minernl/go-miningcore-pool/config"
)
type VarDiff struct {
Options *config.VarDiffOptions
BufferSize int64
MaxTargetTime float64
MinTargetTime float64
TimeBuffer *RingBuffer
LastRtc int64
LastTimestamp int64
}
func NewVarDiff(options *config.VarDiffOptions) *VarDiff {
timestamp := time.Now().Unix()
bufferSize := options.RetargetTime / options.TargetTime * 4
return &VarDiff{
Options: options,
BufferSize: bufferSize,
MaxTargetTime: float64(options.TargetTime) * (1 + options.VariancePercent),
MinTargetTime: float64(options.TargetTime) * (1 - options.VariancePercent),
TimeBuffer: NewRingBuffer(bufferSize),
LastRtc: timestamp - options.RetargetTime/2,
LastTimestamp: timestamp,
}
}
//func (vd *VarDiff) ManagePort(, ) {
// stratumPort := client.Socket.LocalAddr().(*net.TCPAddr).Port
//}
// On SubmitEvent
// then client.EnqueueNextDifficulty(newDiff)
func (vd *VarDiff) CalcNextDiff(currentDiff float64) (newDiff float64) {
timestamp := time.Now().Unix()
if vd.LastRtc == 0 {
vd.LastRtc = timestamp - vd.Options.RetargetTime/2
vd.LastTimestamp = timestamp
return
}
sinceLast := timestamp - vd.LastTimestamp
vd.TimeBuffer.Append(sinceLast)
vd.LastTimestamp = timestamp
if (timestamp-vd.LastRtc) < vd.Options.RetargetTime && vd.TimeBuffer.Size() > 0 {
return
}
vd.LastRtc = timestamp
avg := vd.TimeBuffer.Avg()
ddiff := float64(time.Duration(vd.Options.TargetTime)*time.Second) / avg
// currentDiff, _ := client.Difficulty.Float64()
if avg > vd.MaxTargetTime && currentDiff > vd.Options.MinDiff {
if vd.Options.X2Mode {
ddiff = 0.5
}
if ddiff*currentDiff < vd.Options.MinDiff {
ddiff = vd.Options.MinDiff / currentDiff
}
} else if avg < vd.MinTargetTime {
if vd.Options.X2Mode {
ddiff = 2
}
diffMax := vd.Options.MaxDiff
if ddiff*currentDiff > diffMax {
ddiff = diffMax / currentDiff
}
} else {
return currentDiff
}
newDiff = currentDiff * ddiff
if newDiff <= 0 {
newDiff = currentDiff
}
vd.TimeBuffer.Clear()
return
}