This repository has been archived by the owner on May 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathgetwork.go
138 lines (117 loc) · 3.1 KB
/
getwork.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 main
import (
"bytes"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
)
type getWorkResponseJson struct {
Result struct {
Data string
Target string
}
Error *struct {
Code int
Message string
}
}
type getWorkSubmitResponseJson struct {
Result bool
Error *struct {
Code int
Message string
}
}
var (
httpClient *http.Client
)
const (
MaxIdleConnections int = 20
RequestTimeout int = 5
)
// init HTTPClient
func init() {
httpClient = createHTTPClient()
}
// createHTTPClient for connection re-use
func createHTTPClient() *http.Client {
client := &http.Client{
Transport: &http.Transport{
MaxIdleConnsPerHost: MaxIdleConnections,
},
Timeout: time.Duration(RequestTimeout) * time.Second,
}
return client
}
// GetWork makes a getwork RPC call and returns the result (data and target)
func GetWork() (*Work, error) {
jsonStr := []byte(`{"jsonrpc": "2.0", "method": "getwork", "params": [], "id": 1}`)
req, err := http.NewRequest("POST", cfg.RPCConnect, bytes.NewBuffer(jsonStr))
req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(cfg.Username+":"+cfg.Password)))
req.Header.Set("Content-Type", "application/json")
resp, err := httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
if resp.Status != "200 OK" {
return nil, fmt.Errorf("HTTP %s: %s", resp.Status, body)
}
var res getWorkResponseJson
err = json.Unmarshal(body, &res)
if err != nil {
return nil, err
}
if res.Error != nil {
return nil, fmt.Errorf("JSONRPC Error %d: %s", res.Error.Code, res.Error.Message)
}
data, err := hex.DecodeString(res.Result.Data)
if err != nil {
return nil, err
}
if len(data) != 192 {
return nil, fmt.Errorf("Wrong data length: got %d, expected 192", len(data))
}
target, err := hex.DecodeString(res.Result.Target)
if err != nil {
return nil, err
}
if len(target) != 32 {
return nil, fmt.Errorf("Wrong target length: got %d, expected 32", len(target))
}
var w Work
copy(w.Data[:], data)
copy(w.Target[:], target)
return &w, nil
}
// GetWork makes a getwork RPC call and returns the result (data and target)
func GetWorkSubmit(data []byte) (bool, error) {
hexData := hex.EncodeToString(data)
jsonStr := []byte(`{"jsonrpc": "2.0", "method": "getwork", "params": ["` + hexData + `"], "id": 1}`)
req, err := http.NewRequest("POST", cfg.RPCConnect, bytes.NewBuffer(jsonStr))
req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(cfg.Username+":"+cfg.Password)))
req.Header.Set("Content-Type", "application/json")
resp, err := httpClient.Do(req)
if err != nil {
return false, err
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
if resp.Status != "200 OK" {
return false, fmt.Errorf("error calling getwork (%s): %s", resp.Status, body)
}
var res getWorkSubmitResponseJson
err = json.Unmarshal(body, &res)
if err != nil {
return false, err
}
if res.Error != nil {
return false, fmt.Errorf("JSONRPC Error %d: %s", res.Error.Code, res.Error.Message)
}
return res.Result, nil
}