-
Notifications
You must be signed in to change notification settings - Fork 0
/
pow.go
138 lines (110 loc) · 2.75 KB
/
pow.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
128
129
130
131
132
133
134
135
136
137
138
package pow
import (
"crypto/rand"
"encoding/binary"
"encoding/hex"
"math/big"
"runtime"
"sync/atomic"
"time"
"github.com/VictoriaMetrics/fastcache"
"github.com/rs/zerolog/log"
"golang.org/x/crypto/sha3"
"golang.org/x/xerrors"
)
const (
maxComplexity = byte(127)
cmpRenewTimeout = time.Millisecond * 99
)
type pow struct {
cache *fastcache.Cache
complexity byte
hashing *int64
}
func New(c *fastcache.Cache, defaultComplexity byte, targetNumGoroutine int) PoW {
zero := int64(0)
p := &pow{
cache: c,
complexity: defaultComplexity,
hashing: &zero,
}
go p.balanceLoad(int64(targetNumGoroutine))
return p
}
type PoW interface {
CreateTask() (string, int, error)
ValidateTask(key string, nonce uint64) error
}
func (p *pow) balanceLoad(targetNumGoroutine int64) {
defaultC := p.complexity
maxC := int(defaultC) * 4
if maxC > int(maxComplexity) {
maxC = int(maxComplexity)
}
if targetNumGoroutine == 0 {
targetNumGoroutine = int64(runtime.NumCPU()/2 + 1)
}
for {
time.Sleep(cmpRenewTimeout)
n := atomic.LoadInt64(p.hashing)
if targetNumGoroutine < n && p.complexity < byte(maxC) {
p.complexity++
log.Debug().
Int64("target", targetNumGoroutine).
Int64("HashingGoroutines", n).
Int("complexity", int(p.complexity)).
Msg("complexity increased")
} else if targetNumGoroutine > n && p.complexity > defaultC {
p.complexity--
log.Debug().
Int64("target", targetNumGoroutine).
Int64("HashingGoroutines", n).
Int("complexity", int(p.complexity)).
Msg("complexity decreased")
}
}
}
func (p *pow) CreateTask() (key string, complexity int, err error) {
k := make([]byte, 16)
if _, err = rand.Read(k); err != nil {
return "", 0, xerrors.Errorf("reading random bytes: %w", err)
}
c := p.complexity
p.cache.Set(k, []byte{c})
key = hex.EncodeToString(k)
log.Debug().
Str("key", key).
Int("complexity", int(c)).
Msg("key created")
return key, int(c), nil
}
func (p *pow) ValidateTask(key string, nonce uint64) error {
atomic.AddInt64(p.hashing, 1)
defer atomic.AddInt64(p.hashing, -1)
bKey, err := hex.DecodeString(key)
if err != nil {
return xerrors.Errorf("decoding key string: %w", err)
}
c := p.cache.Get(nil, bKey)
n := make([]byte, 8)
binary.LittleEndian.PutUint64(n[0:], nonce)
s := sha3.Sum256(append(bKey, n...))
log.Debug().
Str("key", key).
Uint64("nonce", nonce).
Str("hash", hex.EncodeToString(s[:])).
Int("complexity", int(c[0])).
Msg("validating task")
if !p.checkHash(s[:], c[0]) {
return xerrors.New("incorrect solution")
}
p.cache.Del(bKey)
return nil
}
func (p *pow) checkHash(s []byte, c byte) bool {
var sumI big.Int
sumI.SetBytes(s)
target := big.NewInt(1)
target.Lsh(target, uint(256-int(c)))
return sumI.Cmp(target) == -1
}