-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdifficulty.go
More file actions
59 lines (56 loc) · 1.05 KB
/
Copy pathdifficulty.go
File metadata and controls
59 lines (56 loc) · 1.05 KB
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
// ABOUTME: DifficultyConfig and retargeting compute the proof-of-work
// ABOUTME: difficulty for each block based on observed block times.
package quark
type DifficultyConfig struct {
InitialDifficulty int32
RetargetInterval int
TargetBlockTime int64
MaxAdjustFactor int64
}
func DefaultDifficultyConfig() *DifficultyConfig {
return &DifficultyConfig{
InitialDifficulty: 8,
RetargetInterval: 0,
TargetBlockTime: 1,
MaxAdjustFactor: 4,
}
}
func adjustDifficulty(current int32, target, actual, maxFactor int64) int32 {
if actual <= 0 {
actual = 1
}
low := target / maxFactor
if low < 1 {
low = 1
}
high := target * maxFactor
if actual < low {
actual = low
}
if actual > high {
actual = high
}
if actual == target {
return current
}
if actual < target {
ratio := target / actual
var delta int32
for ratio >= 2 {
delta++
ratio /= 2
}
return current + delta
}
ratio := actual / target
var delta int32
for ratio >= 2 {
delta++
ratio /= 2
}
out := current - delta
if out < 1 {
out = 1
}
return out
}