-
Notifications
You must be signed in to change notification settings - Fork 211
/
handler.go
524 lines (479 loc) · 14.5 KB
/
handler.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
package fetch
import (
"context"
"errors"
"fmt"
"io"
"github.com/spacemeshos/go-scale"
"github.com/spacemeshos/go-spacemesh/codec"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/datastore"
"github.com/spacemeshos/go-spacemesh/log"
"github.com/spacemeshos/go-spacemesh/p2p/server"
"github.com/spacemeshos/go-spacemesh/sql"
"github.com/spacemeshos/go-spacemesh/sql/atxs"
"github.com/spacemeshos/go-spacemesh/sql/ballots"
"github.com/spacemeshos/go-spacemesh/sql/certificates"
"github.com/spacemeshos/go-spacemesh/sql/identities"
"github.com/spacemeshos/go-spacemesh/sql/layers"
)
const (
fetchSubKey sql.QueryCacheSubKey = "epoch-info-req"
)
type handler struct {
logger log.Log
cdb *datastore.CachedDB
bs *datastore.BlobStore
}
func newHandler(
cdb *datastore.CachedDB,
bs *datastore.BlobStore,
lg log.Log,
) *handler {
return &handler{
logger: lg,
cdb: cdb,
bs: bs,
}
}
// handleMaliciousIDsReq returns the IDs of all known malicious nodes.
func (h *handler) handleMaliciousIDsReq(ctx context.Context, _ []byte) ([]byte, error) {
nodes, err := identities.GetMalicious(h.cdb)
if err != nil {
h.logger.With().Warning("serve: failed to get malicious IDs",
log.Context(ctx), log.Err(err))
return nil, err
}
h.logger.With().
Debug("serve: responded to malicious IDs request",
log.Context(ctx), log.Int("num_malicious", len(nodes)))
malicious := &MaliciousIDs{
NodeIDs: nodes,
}
data, err := codec.Encode(malicious)
if err != nil {
h.logger.With().Fatal("serve: failed to encode malicious IDs", log.Err(err))
}
return data, nil
}
func (h *handler) handleMaliciousIDsReqStream(ctx context.Context, msg []byte, s io.ReadWriter) error {
if err := h.streamIDs(ctx, s, func(cbk retrieveCallback) error {
nodeIDs, err := identities.GetMalicious(h.cdb)
if err != nil {
h.logger.With().Warning("serve: failed to get malicious IDs",
log.Context(ctx), log.Err(err))
return err
}
for _, nodeID := range nodeIDs {
cbk(len(nodeIDs), nodeID[:])
}
return nil
}); err != nil {
h.logger.With().
Debug("serve: failed to stream malicious node IDs",
log.Context(ctx),
log.Err(err))
}
return nil
}
// handleEpochInfoReq returns the ATXs published in the specified epoch.
func (h *handler) handleEpochInfoReq(ctx context.Context, msg []byte) ([]byte, error) {
var epoch types.EpochID
if err := codec.Decode(msg, &epoch); err != nil {
return nil, err
}
cacheKey := sql.QueryCacheKey(atxs.CacheKindEpochATXs, epoch.String())
return sql.WithCachedSubKey(ctx, h.cdb, cacheKey, fetchSubKey, func(ctx context.Context) ([]byte, error) {
atxids, err := atxs.GetIDsByEpoch(ctx, h.cdb, epoch)
if err != nil {
h.logger.With().Warning("serve: failed to get epoch atx IDs",
epoch, log.Err(err), log.Context(ctx))
return nil, err
}
ed := EpochData{
AtxIDs: atxids,
}
h.logger.With().Debug("serve: responded to epoch info request",
epoch, log.Context(ctx), log.Int("atx_count", len(ed.AtxIDs)))
bts, err := codec.Encode(&ed)
if err != nil {
h.logger.With().Fatal("serve: failed to serialize epoch atx",
epoch, log.Context(ctx), log.Err(err))
}
return bts, nil
})
}
// handleEpochInfoReq streams the ATXs published in the specified epoch.
func (h *handler) handleEpochInfoReqStream(ctx context.Context, msg []byte, s io.ReadWriter) error {
var epoch types.EpochID
if err := codec.Decode(msg, &epoch); err != nil {
return err
}
if err := h.streamIDs(ctx, s, func(cbk retrieveCallback) error {
atxids, err := atxs.GetIDsByEpoch(ctx, h.cdb, epoch)
if err != nil {
h.logger.With().Warning("serve: failed to get epoch atx IDs",
epoch, log.Err(err), log.Context(ctx))
return err
}
for _, atxID := range atxids {
cbk(len(atxids), atxID[:])
}
return nil
}); err != nil {
h.logger.With().Debug("serve: failed to stream epoch atx IDs",
log.Context(ctx), epoch, log.Err(err))
}
return nil
}
type (
retrieveCallback func(total int, id []byte) error
retrieveFunc func(retrieveCallback) error
)
func (h *handler) streamIDs(ctx context.Context, s io.ReadWriter, retrieve retrieveFunc) error {
started := false
if err := retrieve(func(total int, id []byte) error {
if !started {
started = true
respSize := scale.LenSize(uint32(total)) + uint32(total*len(id))
if _, err := codec.EncodeLen(s, respSize); err != nil {
return err
}
if _, err := codec.EncodeLen(s, uint32(total)); err != nil {
return err
}
}
if _, err := s.Write(id[:]); err != nil {
return err
}
return nil
},
); err != nil {
if !started {
if wrErr := server.WriteErrorResponse(s, err); wrErr != nil {
h.logger.With().
Debug("serve: failed to write error response",
log.Context(ctx), log.Err(wrErr))
}
}
return err
}
// If any IDs were sent:
// Response.Data already sent
// Response.Error has length 0
lens := []uint32{0}
if !started {
// If no ATX IDs were sent:
// Response.Data is just a single zero byte (length 0),
// but the length of Response.Data is 1 so we must send it
// Response.Error has length 0
lens = []uint32{1, 0, 0}
}
for _, l := range lens {
if _, err := codec.EncodeLen(s, l); err != nil {
return err
}
}
return nil
}
// handleLayerDataReq returns all data in a layer, described in LayerData.
func (h *handler) handleLayerDataReq(ctx context.Context, req []byte) ([]byte, error) {
var (
lid types.LayerID
ld LayerData
err error
)
if err := codec.Decode(req, &lid); err != nil {
return nil, err
}
ld.Ballots, err = ballots.IDsInLayer(h.cdb, lid)
if err != nil && !errors.Is(err, sql.ErrNotFound) {
h.logger.With().Warning("serve: failed to get layer ballots",
lid, log.Err(err), log.Context(ctx))
return nil, err
}
out, err := codec.Encode(&ld)
if err != nil {
h.logger.With().Fatal(
"serve: failed to serialize layer data response",
log.Context(ctx), log.Err(err))
}
return out, nil
}
func (h *handler) handleLayerOpinionsReq2(ctx context.Context, data []byte) ([]byte, error) {
var req OpinionRequest
if err := codec.Decode(data, &req); err != nil {
return nil, err
}
if req.Block != nil {
return h.handleCertReq(ctx, req.Layer, *req.Block)
}
var (
lid = req.Layer
lo LayerOpinion
out []byte
err error
)
opnReqV2.Inc()
lo.PrevAggHash, err = layers.GetAggregatedHash(h.cdb, lid.Sub(1))
if err != nil && !errors.Is(err, sql.ErrNotFound) {
h.logger.With().Error("serve: failed to get prev agg hash", log.Context(ctx), lid, log.Err(err))
return nil, err
}
bid, err := certificates.CertifiedBlock(h.cdb, lid)
if err != nil && !errors.Is(err, sql.ErrNotFound) {
h.logger.With().Error("serve: failed to get layer certified block",
log.Context(ctx), lid, log.Err(err))
return nil, err
}
if err == nil {
lo.Certified = &bid
}
out, err = codec.Encode(&lo)
if err != nil {
h.logger.With().Fatal("serve: failed to serialize layer opinions response",
log.Context(ctx), log.Err(err))
}
return out, nil
}
func (h *handler) handleCertReq(ctx context.Context, lid types.LayerID, bid types.BlockID) ([]byte, error) {
certReq.Inc()
certs, err := certificates.Get(h.cdb, lid)
if err != nil && !errors.Is(err, sql.ErrNotFound) {
h.logger.With().Error("serve: failed to get certificate", log.Context(ctx), lid, log.Err(err))
return nil, err
}
if err == nil {
for _, cert := range certs {
if cert.Block == bid {
out, err := codec.Encode(cert.Cert)
if err != nil {
h.logger.With().Fatal("serve: failed to encode cert",
log.Context(ctx), log.Err(err))
}
return out, nil
}
}
}
return nil, err
}
func (h *handler) handleHashReq(ctx context.Context, data []byte) ([]byte, error) {
return h.doHandleHashReq(ctx, data, datastore.NoHint)
}
func (h *handler) doHandleHashReq(ctx context.Context, data []byte, hint datastore.Hint) ([]byte, error) {
var requestBatch RequestBatch
if err := codec.Decode(data, &requestBatch); err != nil {
h.logger.With().Warning("serve: failed to parse request", log.Context(ctx), log.Err(err))
return nil, errBadRequest
}
if hint != datastore.NoHint && len(requestBatch.Requests) > 1 {
return nil, fmt.Errorf("batch of size 1 expected for %s", hint)
}
resBatch := ResponseBatch{
ID: requestBatch.ID,
Responses: make([]ResponseMessage, 0, len(requestBatch.Requests)),
}
// this will iterate all requests and populate appropriate Responses, if there are any missing items they will not
// be included in the response at all
for _, r := range requestBatch.Requests {
if hint != datastore.NoHint && r.Hint != hint {
return nil, fmt.Errorf("bad hint: %q (expected %q)", r.Hint, hint)
}
totalHashReqs.WithLabelValues(string(r.Hint)).Add(1)
var blob sql.Blob
if err := h.bs.LoadBlob(ctx, r.Hint, r.Hash.Bytes(), &blob); err != nil {
if !errors.Is(err, datastore.ErrNotFound) {
h.logger.With().Debug("serve: database error",
log.Context(ctx),
log.String("hash", r.Hash.ShortString()),
log.String("hint", string(r.Hint)),
log.Err(err))
return nil, err
}
h.logger.With().Debug("serve: remote peer requested nonexistent hash",
log.Context(ctx),
log.String("hash", r.Hash.ShortString()),
log.String("hint", string(r.Hint)),
log.Err(err))
hashMissing.WithLabelValues(string(r.Hint)).Add(1)
continue
} else if len(blob.Bytes) == 0 {
h.logger.With().Debug("serve: remote peer requested golden",
log.Context(ctx),
log.String("hash", r.Hash.ShortString()),
log.Int("dataSize", len(blob.Bytes)))
hashEmptyData.WithLabelValues(string(r.Hint)).Add(1)
continue
} else {
h.logger.With().Debug("serve: responded to hash request",
log.Context(ctx),
log.String("hash", r.Hash.ShortString()),
log.Int("dataSize", len(blob.Bytes)))
}
// add response to batch
m := ResponseMessage{
Hash: r.Hash,
Data: blob.Bytes,
}
resBatch.Responses = append(resBatch.Responses, m)
}
bts, err := codec.Encode(&resBatch)
if err != nil {
h.logger.With().Fatal("serve: failed to encode batch id",
log.Context(ctx),
log.Err(err),
log.String("batch_hash", resBatch.ID.ShortString()))
return nil, err
}
h.logger.With().Debug("serve: returning response for batch",
log.Context(ctx),
log.String("batch_hash", resBatch.ID.ShortString()),
log.Int("count_responses", len(resBatch.Responses)),
log.Int("data_size", len(bts)))
return bts, nil
}
func (h *handler) handleHashReqStream(ctx context.Context, msg []byte, s io.ReadWriter) error {
return h.doHandleHashReqStream(ctx, msg, s, datastore.NoHint)
}
func (h *handler) doHandleHashReqStream(
ctx context.Context,
msg []byte,
s io.ReadWriter,
hint datastore.Hint,
) error {
var requestBatch RequestBatch
if err := codec.Decode(msg, &requestBatch); err != nil {
h.logger.With().Warning("serve: failed to parse request", log.Context(ctx), log.Err(err))
return errBadRequest
}
if hint != datastore.NoHint && len(requestBatch.Requests) > 1 {
return fmt.Errorf("batch of size 1 expected for %s", hint)
}
idsByHint := make(map[datastore.Hint][][]byte)
for _, r := range requestBatch.Requests {
if hint != datastore.NoHint && r.Hint != hint {
return fmt.Errorf("bad hint: %q (expected %q)", r.Hint, hint)
}
idsByHint[r.Hint] = append(idsByHint[r.Hint], r.Hash.Bytes())
}
totalSize := uint32(types.Hash32Length)
var count uint32
for hint, ids := range idsByHint {
goodIDs := make([][]byte, 0, len(ids))
sizes, err := h.bs.GetBlobSizes(hint, ids)
if err != nil {
// At this point, nothing has been written yet, so we can report
// and error
if err := server.WriteErrorResponse(s, err); err != nil {
return err
}
return nil
}
for n, size := range sizes {
if size > 0 {
goodIDs = append(goodIDs, ids[n])
count++
totalSize += types.Hash32Length + scale.LenSize(uint32(size)) + uint32(size)
}
}
idsByHint[hint] = goodIDs
}
totalSize += scale.LenSize(count)
if _, err := codec.EncodeLen(s, totalSize); err != nil {
return err
}
if _, err := codec.EncodeTo(s, &requestBatch.ID); err != nil {
return err
}
if _, err := codec.EncodeLen(s, count); err != nil {
return err
}
var blob sql.Blob
for hint, ids := range idsByHint {
for _, id := range ids {
if err := h.bs.LoadBlob(ctx, hint, id, &blob); err != nil {
return err
}
if _, err := s.Write(id); err != nil {
return err
}
if _, err := codec.EncodeLen(s, uint32(len(blob.Bytes))); err != nil {
return err
}
if _, err := s.Write(blob.Bytes); err != nil {
return err
}
}
}
if _, err := codec.EncodeLen(s, 0); err != nil {
return err
}
return nil
}
func (h *handler) handleMeshHashReq(ctx context.Context, reqData []byte) ([]byte, error) {
var (
req MeshHashRequest
hashes []types.Hash32
data []byte
err error
)
if err = codec.Decode(reqData, &req); err != nil {
h.logger.With().Warning("serve: failed to parse mesh hash request",
log.Context(ctx), log.Err(err))
return nil, errBadRequest
}
if err := req.Validate(); err != nil {
h.logger.With().Debug("failed to validate mesh hash request",
log.Context(ctx), log.Err(err))
return nil, err
}
hashes, err = layers.GetAggHashes(h.cdb, req.From, req.To, req.Step)
if err != nil {
h.logger.With().Warning("serve: failed to get mesh hashes",
log.Context(ctx), log.Err(err))
return nil, err
}
data, err = codec.EncodeSlice(hashes)
if err != nil {
h.logger.With().Fatal("serve: failed to encode hashes",
log.Context(ctx), log.Err(err))
}
h.logger.With().Debug("serve: returning response for mesh hashes",
log.Context(ctx),
log.Stringer("layer_from", req.From),
log.Stringer("layer_to", req.To),
log.Uint32("by", req.Step),
log.Int("count_hashes", len(hashes)),
)
return data, nil
}
func (h *handler) handleMeshHashReqStream(ctx context.Context, reqData []byte, s io.ReadWriter) error {
var req MeshHashRequest
if err := codec.Decode(reqData, &req); err != nil {
h.logger.With().Warning("serve: failed to parse mesh hash request",
log.Context(ctx), log.Err(err))
return errBadRequest
}
if err := h.streamIDs(ctx, s, func(cbk retrieveCallback) error {
if err := req.Validate(); err != nil {
h.logger.With().Debug("failed to validate mesh hash request",
log.Context(ctx), log.Err(err))
return err
}
hashes, err := layers.GetAggHashes(h.cdb, req.From, req.To, req.Step)
if err != nil {
h.logger.With().Warning("serve: failed to get mesh hashes",
log.Context(ctx), log.Err(err))
return err
}
for _, id := range hashes {
cbk(len(hashes), id[:])
}
return nil
}); err != nil {
h.logger.With().
Debug("serve: failed to stream mesh hashes",
log.Context(ctx), log.Err(err))
}
return nil
}