-
Notifications
You must be signed in to change notification settings - Fork 104
/
ExMessage.go
211 lines (171 loc) · 4.93 KB
/
ExMessage.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
package main
import (
"bytes"
"encoding/binary"
)
// ex-message的magic number
const ExMessageMagicNumber uint8 = 0x7F
// types
const (
CMD_REGISTER_WORKER uint8 = 0x01 // Agent -> Pool
CMD_SUBMIT_SHARE uint8 = 0x02 // Agent -> Pool, mining.submit(...)
CMD_SUBMIT_SHARE_WITH_TIME uint8 = 0x03 // Agent -> Pool, mining.submit(..., nTime)
CMD_UNREGISTER_WORKER uint8 = 0x04 // Agent -> Pool
CMD_MINING_SET_DIFF uint8 = 0x05 // Pool -> Agent, mining.set_difficulty(diff)
CMD_SUBMIT_RESPONSE uint8 = 0x10 // Pool -> Agent, response of the submit (optional)
CMD_SUBMIT_SHARE_WITH_VER uint8 = 0x12 // Agent -> Pool, mining.submit(..., nVersionMask)
CMD_SUBMIT_SHARE_WITH_TIME_VER uint8 = 0x13 // Agent -> Pool, mining.submit(..., nTime, nVersionMask)
CMD_SUBMIT_SHARE_WITH_MIX_HASH uint8 = 0x14 // Agent -> Pool, for ETH
CMD_SET_EXTRA_NONCE uint8 = 0x22 // Pool -> Agent, pool nonce prefix allocation result (Ethereum)
)
type SerializableExMessage interface {
Serialize() []byte
}
type UnserializableExMessage interface {
Unserialize(data []byte) (err error)
}
type ExMessageHeader struct {
MagicNumber uint8
Type uint8
Size uint16
}
type ExMessage struct {
ExMessageHeader
Body []byte
}
type ExMessageRegisterWorker struct {
SessionID uint16
ClientAgent string
WorkerName string
}
func (msg *ExMessageRegisterWorker) Serialize() []byte {
header := ExMessageHeader{
ExMessageMagicNumber,
CMD_REGISTER_WORKER,
uint16(4 + 2 + len(msg.ClientAgent) + 1 + len(msg.WorkerName) + 1)}
buf := new(bytes.Buffer)
binary.Write(buf, binary.LittleEndian, &header)
binary.Write(buf, binary.LittleEndian, msg.SessionID)
buf.WriteString(msg.ClientAgent)
buf.WriteByte(0)
buf.WriteString(msg.WorkerName)
buf.WriteByte(0)
return buf.Bytes()
}
type ExMessageUnregisterWorker struct {
SessionID uint16
}
func (msg *ExMessageUnregisterWorker) Serialize() []byte {
header := ExMessageHeader{
ExMessageMagicNumber,
CMD_UNREGISTER_WORKER,
uint16(4 + 2)}
buf := new(bytes.Buffer)
binary.Write(buf, binary.LittleEndian, &header)
binary.Write(buf, binary.LittleEndian, msg.SessionID)
return buf.Bytes()
}
type ExMessageSubmitShareBTC struct {
Base struct {
JobID uint8
SessionID uint16
ExtraNonce2 uint32
Nonce uint32
}
Time uint32
VersionMask uint32
IsFakeJob bool
}
func (msg *ExMessageSubmitShareBTC) Serialize() []byte {
var header ExMessageHeader
header.MagicNumber = ExMessageMagicNumber
if msg.Time == 0 {
if msg.VersionMask == 0 {
header.Type = CMD_SUBMIT_SHARE
header.Size = 4 + 1 + 2 + 4 + 4
} else {
header.Type = CMD_SUBMIT_SHARE_WITH_VER
header.Size = 4 + 1 + 2 + 4 + 4 + 4
}
} else {
if msg.VersionMask == 0 {
header.Type = CMD_SUBMIT_SHARE_WITH_TIME
header.Size = 4 + 1 + 2 + 4 + 4 + 4
} else {
header.Type = CMD_SUBMIT_SHARE_WITH_TIME_VER
header.Size = 4 + 1 + 2 + 4 + 4 + 4 + 4
}
}
buf := new(bytes.Buffer)
binary.Write(buf, binary.LittleEndian, &header)
binary.Write(buf, binary.LittleEndian, &msg.Base)
if msg.Time != 0 {
binary.Write(buf, binary.LittleEndian, msg.Time)
}
if msg.VersionMask != 0 {
binary.Write(buf, binary.LittleEndian, msg.VersionMask)
}
return buf.Bytes()
}
type ExMessageSubmitShareETH struct {
SessionID uint16
Nonce uint64
JobID []byte
MixHash []byte
IsFakeJob bool
}
func (msg *ExMessageSubmitShareETH) Serialize() []byte {
var header ExMessageHeader
header.MagicNumber = ExMessageMagicNumber
if msg.MixHash == nil {
header.Type = CMD_SUBMIT_SHARE
header.Size = 4 + 2 + 8 + uint16(len(msg.JobID))
} else {
header.Type = CMD_SUBMIT_SHARE_WITH_MIX_HASH
header.Size = 4 + 2 + 8 + uint16(len(msg.JobID)) + uint16(len(msg.MixHash))
}
buf := new(bytes.Buffer)
binary.Write(buf, binary.LittleEndian, &header)
binary.Write(buf, binary.LittleEndian, &msg.SessionID)
binary.Write(buf, binary.LittleEndian, &msg.Nonce)
buf.Write(msg.JobID)
if msg.MixHash != nil {
buf.Write(msg.MixHash)
}
return buf.Bytes()
}
type ExMessageMiningSetDiff struct {
Base struct {
DiffExp uint8
Count uint16
}
SessionIDs []uint16
}
func (msg *ExMessageMiningSetDiff) Unserialize(data []byte) (err error) {
buf := bytes.NewReader(data)
err = binary.Read(buf, binary.LittleEndian, &msg.Base)
if err != nil || msg.Base.Count == 0 {
return
}
msg.SessionIDs = make([]uint16, msg.Base.Count)
err = binary.Read(buf, binary.LittleEndian, msg.SessionIDs)
return
}
type ExMessageSubmitResponse struct {
Index uint16
Status StratumStatus
}
func (msg *ExMessageSubmitResponse) Unserialize(data []byte) (err error) {
buf := bytes.NewReader(data)
err = binary.Read(buf, binary.LittleEndian, msg)
return
}
type ExMessageSetExtranonce struct {
SessionID uint16
ExtraNonce uint32
}
func (msg *ExMessageSetExtranonce) Unserialize(data []byte) (err error) {
buf := bytes.NewReader(data)
err = binary.Read(buf, binary.LittleEndian, msg)
return
}