forked from btcsuite/btcd
-
Notifications
You must be signed in to change notification settings - Fork 2
/
workermsg.go
360 lines (289 loc) · 7.61 KB
/
workermsg.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package main
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
)
const (
CmdWork = "work"
CmdGetWork = "getwork"
CmdGetStartHeaders = "getsheaders"
CmdStartHeaders = "startheaders"
CmdResult = "result"
)
type remoteWorkerMsgHeader struct {
magic uint32 // 4 bytes
command string // 12 bytes
length uint32 // 4 bytes
}
func (hdr *remoteWorkerMsgHeader) Serialize(w io.Writer) {
// Write magic
buf := make([]byte, 4)
binary.BigEndian.PutUint32(buf, hdr.magic)
w.Write(buf)
// Write command
if hdr.command == "" {
panic("remoteWorkerMsgHeader Serialize command empty")
}
var command [CommandSize]byte
copy(command[:], []byte(hdr.command))
w.Write(command[:])
// Write length
binary.BigEndian.PutUint32(buf, hdr.length)
w.Write(buf)
}
func readWorkerMsgHeader(reader io.Reader) (*remoteWorkerMsgHeader, error) {
rwhdr := &remoteWorkerMsgHeader{}
headerBuf := make([]byte, HeaderSize)
_, err := io.ReadFull(reader, headerBuf)
if err != nil {
return nil, err
}
rwhdr.magic = binary.BigEndian.Uint32(headerBuf[:4])
if rwhdr.magic != MagicBytes {
err = fmt.Errorf("readMsgHeader read wrong magic bytes of "+
"%v, when it should have read %v",
rwhdr.magic, MagicBytes)
return nil, err
}
// Read command.
commandBuf := make([]byte, CommandSize)
copy(commandBuf, headerBuf[4:4+12])
// Strip trailing zeros from command string.
rwhdr.command = string(bytes.TrimRight(commandBuf, "\x00"))
rwhdr.length = binary.BigEndian.Uint32(headerBuf[16 : 16+4])
return rwhdr, nil
}
func WriteWorkerMessage(w io.Writer, msg WorkerMessage) error {
// Encode the message payload.
var bw bytes.Buffer
err := msg.Encode(&bw)
if err != nil {
return err
}
payload := bw.Bytes()
lenp := len(payload)
hdr := remoteWorkerMsgHeader{}
hdr.magic = MagicBytes
// Enforce max command size.
hdr.command = msg.Command()
if len(hdr.command) > int(CommandSize) {
err := fmt.Errorf("command [%s] is too long [max %v]",
hdr.command, CommandSize)
return err
}
hdr.length = uint32(lenp)
// Write header
var hw bytes.Buffer
hdr.Serialize(&hw)
w.Write(hw.Bytes())
// Only write the payload if there is one
if len(payload) > 0 {
_, err = w.Write(payload)
}
return err
}
func ReadWorkerMessage(r io.Reader) (WorkerMessage, []byte, error) {
hdr, err := readWorkerMsgHeader(r)
if err != nil {
return nil, nil, err
}
// Create struct of appropriate message type based on the command.
msg, err := makeEmptyMessage(hdr.command)
if err != nil {
return nil, nil, err
}
// Read payload.
payload := make([]byte, hdr.length)
_, err = io.ReadFull(r, payload)
if err != nil {
return nil, nil, err
}
pr := bytes.NewBuffer(payload)
err = msg.Decode(pr)
if err != nil {
return nil, nil, err
}
return msg, payload, nil
}
// makeEmptyMessage creates a message of the appropriate concrete type based
// on the command.
func makeEmptyMessage(command string) (WorkerMessage, error) {
var msg WorkerMessage
switch command {
case CmdWork:
msg = &MsgWork{}
case CmdResult:
msg = &MsgResult{}
case CmdGetWork:
msg = &MsgGetWork{}
case CmdGetStartHeaders:
msg = &MsgGetStartHeaders{}
case CmdStartHeaders:
msg = &MsgStartHeaders{}
default:
return nil, fmt.Errorf("unhandled command [%s]", command)
}
return msg, nil
}
// WorkerMessage is the message sent between the remote worker and the coordinator
// to communicate work and result for verifying a utreexo root hint.
type WorkerMessage interface {
Encode(io.Writer) error
Decode(io.Reader) error
Command() string
}
// MsgWork is the message sent from the coordinator node to queue a utreexo root
// hint (and metadata) to the worker.
type MsgWork struct {
work *work
}
func (msg *MsgWork) Encode(w io.Writer) error {
buf := make([]byte, 4)
binary.BigEndian.PutUint32(buf, uint32(msg.work.uRootHintHeight))
w.Write(buf)
return nil
}
func (msg *MsgWork) Decode(r io.Reader) error {
heightBuf := make([]byte, 4)
_, err := io.ReadFull(r, heightBuf)
if err != nil {
return err
}
work := work{}
work.uRootHintHeight = int32(binary.BigEndian.Uint32(heightBuf[:]))
msg.work = &work
return nil
}
func (msg *MsgWork) Command() string {
return CmdWork
}
// MsgGetWork is the message sent from the worker node to get a work from the coordinator.
type MsgGetWork struct {
// getWork has no payload
}
func (msg *MsgGetWork) Encode(w io.Writer) error {
return nil
}
func (msg *MsgGetWork) Decode(r io.Reader) error {
return nil
}
func (msg *MsgGetWork) Command() string {
return CmdGetWork
}
// MsgGetStartHeaders is the message sent from the worker node to get all the headers required
// for verifying a utreexo root hint
type MsgGetStartHeaders struct {
// getStartHeaders has no payload
}
func (msg *MsgGetStartHeaders) Encode(w io.Writer) error {
return nil
}
func (msg *MsgGetStartHeaders) Decode(r io.Reader) error {
return nil
}
func (msg *MsgGetStartHeaders) Command() string {
return CmdGetStartHeaders
}
// MsgStartHeaders is the message sent from the worker node to get all the headers required
// for verifying a utreexo root hint
type MsgStartHeaders struct {
headers *startHeaders
}
func (msg *MsgStartHeaders) Encode(w io.Writer) error {
buf := make([]byte, 4)
binary.BigEndian.PutUint32(buf, uint32(msg.headers.lastHeight))
w.Write(buf)
for _, header := range msg.headers.headers {
// only writer matters. Other 2 fields don't do anything in the actual
// encoding function
err := header.BtcEncode(w, 0, wire.WitnessEncoding)
if err != nil {
panic(err)
}
}
for _, hash := range msg.headers.hashes {
// only writer matters. Other 2 fields don't do anything in the actual
// encoding function
_, err := w.Write(hash[:])
if err != nil {
panic(err)
}
}
return nil
}
func (msg *MsgStartHeaders) Decode(r io.Reader) error {
heightBuf := make([]byte, 4)
_, err := io.ReadFull(r, heightBuf)
if err != nil {
return err
}
lastHeight := int32(binary.BigEndian.Uint32(heightBuf[:]))
startH := startHeaders{
headers: make([]*wire.BlockHeader, 0, lastHeight),
hashes: make([]chainhash.Hash, lastHeight),
lastHeight: lastHeight,
}
for i := int32(0); i < lastHeight; i++ {
header := wire.BlockHeader{}
// only reader matters. Other 2 fields don't do anything in the actual
// encoding function
err = header.BtcDecode(r, 0, wire.WitnessEncoding)
if err != nil {
panic(err)
}
startH.headers = append(startH.headers, &header)
}
var hash [32]byte
for i := int32(0); i < lastHeight; i++ {
// only writer matters. Other 2 fields don't do anything in the actual
// encoding function
_, err := r.Read(hash[:])
if err != nil {
panic(err)
}
hash, err := chainhash.NewHash(hash[:])
if err != nil {
panic(err)
}
startH.hashes[i] = *hash
}
msg.headers = &startH
return nil
}
func (msg *MsgStartHeaders) Command() string {
return CmdStartHeaders
}
// MsgWork is the message sent from the worker node to queue a verification result
// to the coordinator.
type MsgResult struct {
result *result
}
func (msg *MsgResult) Encode(w io.Writer) error {
w.Write(msg.result.valid[:])
err := binary.Write(w, binary.BigEndian, msg.result.uRootHintHeight)
if err != nil {
return err
}
return nil
}
func (msg *MsgResult) Decode(r io.Reader) error {
resultBuf := make([]byte, 5)
_, err := r.Read(resultBuf)
if err != nil {
return err
}
res := result{}
verification := resultBuf[:1]
copy(res.valid[:], verification)
height := binary.BigEndian.Uint32(resultBuf[1:])
res.uRootHintHeight = int32(height)
msg.result = &res
return nil
}
func (msg *MsgResult) Command() string {
return CmdResult
}