-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathwsapi.go
263 lines (237 loc) · 6.45 KB
/
wsapi.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package main
import (
"fmt"
"encoding/json"
"log"
"time"
"sync/atomic"
"sync"
"os"
"github.com/mariuspass/recws"
"strconv"
"os/signal"
)
const (
hdproxyVersion = "20190423"
threshold = 30
frequency = 5
)
var currentMiningInfo atomic.Value
var lastHeartBeat atomic.Value
var available atomicBool
type websocketAPI struct {
server string
accountKey string
rc *recws.RecConn
ci clientInfo
sendMu *sync.Mutex // Prevent "concurrent write to websocket connection"
receiveMu *sync.Mutex
}
type clientInfo struct {
AccountKey string `json:"account_key"`
MinerName string `json:"miner_name"`
MinerMark string `json:"miner_mark"`
Capacity int64 `json:"capacity"`
}
type nonceSubmission struct {
AccountKey string `json:"account_key"`
MinerName string `json:"miner_name"`
MinerMark string `json:"miner_mark"`
Capacity int64 `json:"capacity"`
Submit []nonceData `json:"submit"`
}
type nonceData struct {
AccountID uint64 `json:"accountId"`
Height uint64 `json:"height"`
Nonce string `json:"nonce"`
Deadline uint64 `json:"deadline"`
Ts int64 `json:"ts"`
}
type websocketMiningInfo struct {
Cmd string `json:"cmd"`
Para miningInfo `json:"para"`
}
type websocketHeartBeat struct {
Cmd string `json:"cmd"`
Para clientInfo `json:"para"`
}
type websocketMessage struct {
Cmd string `json:"cmd"`
Para interface{} `json:"para"`
}
func newWebsocketAPI(server string, accountKey string, minerName string, capacityGB int64) (c *websocketAPI) {
ws := recws.RecConn{}
ci := clientInfo{accountKey, minerName, minerName + ".hdproxy.exe." + hdproxyVersion, capacityGB}
c = &websocketAPI{
server,
accountKey,
&ws,
ci,
&sync.Mutex{},
&sync.Mutex{}}
return
}
func (c *websocketAPI) UpdateSize(totalSize int64){
c.sendMu.Lock()
c.ci.Capacity = totalSize
c.sendMu.Unlock()
}
func (c *websocketAPI) Close() {
c.rc.Close()
}
func (c *websocketAPI) Connect() {
c.rc.SubscribeHandler = c.subscribe
c.rc.Dial(c.server, nil)
// message handler
go func() {
for {
c.receiveMu.Lock()
messageType, message, err := c.rc.ReadMessage()
c.receiveMu.Unlock()
if err != nil {
continue
}
// handle all text messages
switch messageType {
case 1:
onTextMessage(string(message))
}
}
}()
}
func (c *websocketAPI) subscribe() error {
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
// request initial mining info
c.sendMu.Lock()
if err := c.rc.WriteMessage(1, []byte("{\"cmd\":\"mining_info\",\"para\":{}}")); err != nil {
log.Printf("Error: WriteMessage %s", c.rc.GetURL())
return err
}
c.sendMu.Unlock()
// subscribe for future mining infos
channelName := "poolmgr.mining_info"
subscribeObject := getSubscribeEventObject(channelName, 0)
subscribeData := serializeDataIntoString(subscribeObject)
c.sendMu.Lock()
if err := c.rc.WriteMessage(1, []byte(subscribeData)); err != nil {
log.Printf("Error: WriteMessage %s", c.rc.GetURL())
return err
}
c.sendMu.Unlock()
// subscribe to heartbeat
// cancel existing
// create new
ct := time.Now()
lastHeartBeat.Store(ct)
ticker := time.NewTicker(time.Duration(frequency) * time.Second)
go func() {
for {
select {
case <- ticker.C:
// check last heartbeatACK
ht := lastHeartBeat.Load()
if int64(time.Now().Sub(ht.(time.Time)).Seconds()) > threshold {
// attempt reconnect
// stop heartbeat, will be restarted after connect
ticker.Stop();
log.Println("websocket api: heartbeat lost, trying to reconnect...")
ct := time.Now()
lastHeartBeat.Store(ct)
c.Close()
}
c.sendMu.Lock()
ci := clientInfo{c.accountKey,c.ci.MinerName,c.ci.MinerName+".hdproxy.exe."+hdproxyVersion, c.ci.Capacity}
hb := websocketMessage{"poolmgr.heartbeat",ci}
req, err := jsonx.MarshalToString(&hb);
if err != nil {
return
}
// debug
// log.Println(req)
c.rc.WriteMessage(1, []byte(req))
c.sendMu.Unlock()
case <- interrupt:
ticker.Stop()
os.Exit(0)
return
}
}
}()
return nil
}
func onTextMessage(message string) {
// debug log.Println("recv (text):", message)
var hi websocketMessage
if err := jsonx.UnmarshalFromString(message, &hi); err != nil {
return
}
switch hi.Cmd{
case "poolmgr.heartbeat":
ct := time.Now()
lastHeartBeat.Store(ct)
//log.Println("websocket api: heartbeat");
case "poolmgr.mining_info":
var mi websocketMiningInfo
if err := jsonx.UnmarshalFromString(message, &mi); err != nil {
return
}
mi.Para.bytes, _ = json.Marshal(map[string]string{
"height": fmt.Sprintf("%d", mi.Para.Height),
"baseTarget": fmt.Sprintf("%d", mi.Para.BaseTarget),
"generationSignature": mi.Para.GenSig})
mi.Para.StartTime = time.Now()
currentMiningInfo.Store(&mi.Para)
available.Set(true)
log.Println("websocket api: new mining info received");
case "mining_info":
var mi websocketMiningInfo
mi.Para.StartTime = time.Now()
if err := jsonx.UnmarshalFromString(message, &mi); err != nil {
return
}
mi.Para.bytes, _ = json.Marshal(map[string]string{
"height": fmt.Sprintf("%d", mi.Para.Height),
"baseTarget": fmt.Sprintf("%d", mi.Para.BaseTarget),
"generationSignature": mi.Para.GenSig})
mi.Para.StartTime = time.Now()
currentMiningInfo.Store(&mi.Para)
available.Set(true)
log.Println("websocket api: initial mining info received.");
return
}
}
func getSubscribeEventObject(channelName string, messageID int) emitEvent {
return emitEvent{
Event: "#subscribe",
Data: channel{Channel: channelName},
Cid: messageID,
}
}
type emitEvent struct {
Event string `json:"event"`
Data interface{} `json:"data"`
Cid int `json:"cid"`
}
type channel struct {
Channel string `json:"channel"`
Data interface{} `json:"data,omitempty"`
}
func serializeDataIntoString(data interface{}) string {
b, _ := json.Marshal(data)
return string(b)
}
func (c *websocketAPI) submitNonce(accountID uint64, height uint64, nonce uint64, deadline uint64){
c.sendMu.Lock()
nd := nonceData{accountID, height, strconv.FormatUint(nonce,10), deadline, time.Now().Unix()}
ns := nonceSubmission{c.ci.AccountKey,c.ci.MinerName,"",c.ci.Capacity,[]nonceData{nd}}
hb := websocketMessage{"poolmgr.submit_nonce",ns}
req, err := jsonx.MarshalToString(&hb);
// debug
// log.Println(req)
if err != nil {
return
}
c.rc.WriteMessage(1, []byte(req))
c.sendMu.Unlock()
}