-
Notifications
You must be signed in to change notification settings - Fork 6
/
normal.go
107 lines (86 loc) · 1.76 KB
/
normal.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
package registration
import (
"runtime"
"sync"
"time"
"github.com/deroproject/derohe/transaction"
"github.com/deroproject/derohe/walletapi"
)
type NormalReg struct {
Running bool
OnFound func(tx *transaction.Transaction)
wallet *walletapi.Wallet_Disk
hashRate map[int]uint64
hashCount map[int]uint64
mutex sync.RWMutex
}
func NewNormalReg() *NormalReg {
return &NormalReg{
hashRate: make(map[int]uint64),
hashCount: make(map[int]uint64),
}
}
func (s *NormalReg) Start(workers int, wallet *walletapi.Wallet_Disk) {
s.wallet = wallet
s.Running = true
for i := 0; i < workers; i++ {
go s.run(i)
}
}
func (s *NormalReg) Stop() {
s.Running = false
}
func (s *NormalReg) HashRate() uint64 {
s.mutex.RLock()
defer s.mutex.RUnlock()
sum := uint64(0)
for _, v := range s.hashRate {
sum += v
}
return sum
}
func (s *NormalReg) HashCount() uint64 {
s.mutex.RLock()
defer s.mutex.RUnlock()
sum := uint64(0)
for _, v := range s.hashCount {
sum += v
}
return sum
}
func (s *NormalReg) run(wIndex int) {
start := time.Now()
count := uint64(0)
hashRate := uint64(0)
for {
if !s.Running {
s.mutex.Lock()
s.hashCount = make(map[int]uint64)
s.hashRate = make(map[int]uint64)
s.mutex.Unlock()
return
}
for i := 0; i < runtime.GOMAXPROCS(0); i++ {
tx := s.wallet.GetRegistrationTX()
count++
hashRate++
if time.Now().Add(-1 * time.Second).After(start) {
s.mutex.Lock()
s.hashRate[wIndex] = hashRate
s.hashCount[wIndex] = count
s.mutex.Unlock()
start = time.Now()
hashRate = 0
}
hash := tx.GetHash()
if hash[0] == 0 && hash[1] == 0 && hash[2] == 0 {
if tx.IsRegistrationValid() {
s.Stop()
s.OnFound(tx)
break
}
}
}
time.Sleep(100 * time.Millisecond)
}
}