/
vardiff.go
127 lines (110 loc) · 3.2 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package pool
import (
"time"
"github.com/HyperspaceApp/Hyperspace/build"
)
var (
targetDuration = build.Select(build.Var{
Standard: 5.0,
Dev: 5.0,
Testing: 3.0,
}).(float64) // targeted seconds between shares
retargetDuration = build.Select(build.Var{
Standard: 15.0,
Dev: 15.0,
Testing: 9.0,
}).(float64) // how often do we consider changing difficulty
variancePercent = build.Select(build.Var{
Standard: 15,
Dev: 15,
Testing: 15,
}).(int) // how much we let the share duration vary between retargetting
)
// Vardiff is a structure representing maximum and minimum share submission
// times, along with the size of the buffer over which share submission times
// should be monitored
type Vardiff struct {
tmax float64
tmin float64
bufSize uint64
}
func (s *Session) newVardiff() *Vardiff {
variance := float64(targetDuration) * (float64(variancePercent) / 100.0)
size := uint64(retargetDuration / targetDuration * 4)
if size > numSharesToAverage {
size = numSharesToAverage
}
v := &Vardiff{
tmax: targetDuration + variance,
tmin: targetDuration - variance,
bufSize: numSharesToAverage,
}
s.lastVardiffRetarget = time.Now().Add(time.Duration(-retargetDuration / 2.0))
s.lastVardiffTimestamp = time.Now()
return v
}
func (s *Session) checkDiffOnNewShare() bool {
s.lastVardiffTimestamp = time.Now()
if time.Now().Sub(s.lastVardiffRetarget).Seconds() < retargetDuration {
return false
}
// if s.log != nil {
// s.log.Printf("------------------------\n\n")
// s.log.Printf("Retargeted Duration: %f\n", time.Now().Sub(s.lastVardiffRetarget).Seconds())
// }
s.lastVardiffRetarget = time.Now()
unsubmitDuration, historyDuration := s.ShareDurationAverage()
if s.disableVarDiff {
// if s.log != nil {
// s.log.Printf("HistoryDuration: %f, var diff disabled, current diff: %f\n", historyDuration, s.currentDifficulty)
// }
return false
}
if unsubmitDuration > retargetDuration {
if s.IsStable() {
s.SetCurrentDifficulty(s.CurrentDifficulty() * 3 / 4)
} else {
s.SetCurrentDifficulty(s.CurrentDifficulty() * 1 / 2)
}
// if s.log != nil {
// s.log.Printf("UnsubmitDuration too long: %f, Set new difficulty to: %v\n", unsubmitDuration, s.currentDifficulty)
// }
return true
}
if historyDuration == 0 {
// if s.log != nil {
// s.log.Printf("No historyDuration yet\n")
// }
return false
}
if historyDuration < s.vardiff.tmax && historyDuration > s.vardiff.tmin { // close enough
// if s.log != nil {
// s.log.Printf("HistoryDuration: %f is inside range\n", historyDuration)
// }
return false
}
var deltaDiff float64
deltaDiff = float64(targetDuration) / float64(historyDuration)
if s.IsStable() {
deltaDiff = 1 - (1-deltaDiff)/8
} else {
deltaDiff = 1 - (1-deltaDiff)/2
}
if deltaDiff > 2.0 {
deltaDiff = 2.0
}
if deltaDiff < 0.5 {
deltaDiff = 0.5
}
// if s.log != nil {
// s.log.Printf("HistoryDuration: %f Delta %f\n", historyDuration, deltaDiff)
// }
// if s.log != nil {
// s.log.Printf("Old difficulty was %v\n", s.currentDifficulty)
// }
s.SetCurrentDifficulty(s.CurrentDifficulty() * deltaDiff)
if s.log != nil {
s.log.Printf("Set new difficulty to %v\n", s.currentDifficulty)
}
return true
}