/
rpc_claymore.go
223 lines (193 loc) · 4.98 KB
/
rpc_claymore.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package rpcclaymore
import (
"fmt"
"net/rpc/jsonrpc"
"strconv"
"strings"
)
const (
methodGetInfo = "miner_getstat1"
methodRestartMiner = "miner_restart"
methodReboot = "miner_reboot"
)
var args = struct {
id string
jsonrpc string
psw string
}{"0", "2.0", ""}
// Crypto Information about this concrete crypto-currency
type Crypto struct {
HashRate int
Shares int
RejectedShares int
InvalidShares int
}
func (c Crypto) String() (s string) {
if c.HashRate+c.RejectedShares+c.InvalidShares+c.Shares == 0 {
return "Disabled\n"
}
s += fmt.Sprintf("HashRate: %8d Mh/s\n", c.HashRate)
s += fmt.Sprintf("Shares: %8d\n", c.Shares)
s += fmt.Sprintf("Rejected Shares: %8d\n", c.RejectedShares)
s += fmt.Sprintf("Invalid Shares: %8d\n", c.InvalidShares)
return s
}
// PoolInfo Information about the miner's connected pool
type PoolInfo struct {
Address string
Switches int
}
func (p PoolInfo) String() (s string) {
if p.Address == "" {
return "Disabled\n"
}
s += fmt.Sprintf("Address: %s\n", p.Address)
s += fmt.Sprintf("Switches: %"+strconv.Itoa(len(p.Address))+"d\n", p.Switches)
return s
}
// GPU Information about each concrete GPU
type GPU struct {
HashRate int
AltHashRate int
Temperature int
FanSpeed int
}
func (gpu GPU) String() (s string) {
s += fmt.Sprintf("Hash Rate: %8d Mh/s\n", gpu.HashRate)
s += fmt.Sprintf("Alt Hash Rate: %8d Mh/s\n", gpu.AltHashRate)
s += fmt.Sprintf("Temperature: %8d º\n", gpu.Temperature)
s += fmt.Sprintf("Fan Speed: %8d %%\n", gpu.FanSpeed)
return s
}
// IsStuck Return true if the GPU is not mining
func (gpu GPU) IsStuck() bool {
return gpu.HashRate == 0
}
// MinerInfo Information about the miner
type MinerInfo struct {
Version string
UpTime int
MainCrypto Crypto
AltCrypto Crypto
MainPool PoolInfo
AltPool PoolInfo
GPUS []GPU
}
// StuckGPUs Return the number of GPUs that are not mining
func (m MinerInfo) StuckGPUs() int {
var total int
for _, gpu := range m.GPUS {
if gpu.IsStuck() {
total++
}
}
return total
}
func (m MinerInfo) String() string {
var s string
s += fmt.Sprintf("Version: %10s\n", m.Version)
s += fmt.Sprintf("Up Time: %10d min\n", m.UpTime)
s += "\n"
s += fmt.Sprintf("Main Crypto\n%s\n", m.MainCrypto)
s += fmt.Sprintf("Alt Crypto\n%s\n", m.AltCrypto)
s += fmt.Sprintf("Main Pool\n%s\n", m.MainPool)
s += fmt.Sprintf("Alt Pool\n%s\n", m.AltPool)
for i, gpu := range m.GPUS {
s += fmt.Sprintf("GPU %d\n%s\n", i, gpu)
}
return s
}
// Miner creates an instance to get info of a miner
type Miner struct {
Address string
Password string
}
func (m Miner) String() (s string) {
return fmt.Sprintf("Miner {Address: %s}\n", m.Address)
}
// Restart Stop and start the miner
func (m Miner) Restart() error {
client, err := jsonrpc.Dial("tcp", m.Address)
if err != nil {
return err
}
defer client.Close()
args.psw = m.Password
return client.Call(methodRestartMiner, args, nil)
}
// Reboot Turn off and on again the computer
func (m Miner) Reboot() error {
client, err := jsonrpc.Dial("tcp", m.Address)
if err != nil {
return err
}
defer client.Close()
args.psw = m.Password
return client.Call(methodReboot, args, nil)
}
// GetInfo return the status of the miner or throw and error if it is not reachable
func (m Miner) GetInfo() (MinerInfo, error) {
var mi MinerInfo
var reply []string
client, err := jsonrpc.Dial("tcp", m.Address)
if err != nil {
return mi, err
}
defer client.Close()
args.psw = m.Password
err = client.Call(methodGetInfo, args, &reply)
if err != nil {
return mi, err
}
return parseResponse(reply), nil
}
func parseResponse(info []string) MinerInfo {
var mi MinerInfo
var group []string
mi.Version = strings.Replace(info[0], " - ETH", "", 1)
mi.UpTime = toInt(info[1])
group = splitGroup(info[2])
mi.MainCrypto.HashRate = toInt(group[0])
mi.MainCrypto.Shares = toInt(group[1])
mi.MainCrypto.RejectedShares = toInt(group[2])
group = splitGroup(info[4])
mi.AltCrypto.HashRate = toInt(group[0])
mi.AltCrypto.Shares = toInt(group[1])
mi.AltCrypto.RejectedShares = toInt(group[2])
group = splitGroup(info[7])
mi.MainPool.Address = group[0]
if len(group) > 1 {
mi.AltPool.Address = group[1]
}
group = splitGroup(info[8])
mi.MainCrypto.InvalidShares = toInt(group[0])
mi.MainPool.Switches = toInt(group[1])
mi.AltCrypto.InvalidShares = toInt(group[2])
mi.AltPool.Switches = toInt(group[3])
for _, hashrate := range splitGroup(info[3]) {
mi.GPUS = append(mi.GPUS, GPU{HashRate: toInt(hashrate)})
}
for i, val := range splitGroup(info[6]) {
if i%2 == 0 {
mi.GPUS[i/2].Temperature = toInt(val)
} else {
mi.GPUS[(i-1)/2].FanSpeed = toInt(val)
}
}
if mi.AltPool.Address != "" {
for i, val := range splitGroup(info[5]) {
hashrate, err := strconv.Atoi(val)
if err == nil {
mi.GPUS[i].AltHashRate = hashrate
}
}
}
return mi
}
func splitGroup(s string) []string {
return strings.Split(s, ";")
}
func toInt(s string) int {
i, _ := strconv.Atoi(s)
return i
}