-
Notifications
You must be signed in to change notification settings - Fork 211
/
handler.go
248 lines (233 loc) · 7.6 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
package fetch
import (
"context"
"errors"
"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/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"
)
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.WithContext(ctx).With().Warning("serve: failed to get malicious IDs", log.Err(err))
return nil, err
}
h.logger.WithContext(ctx).
With().
Debug("serve: responded to malicious IDs request", 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
}
// 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
}
atxids, err := atxs.GetIDsByEpoch(h.cdb, epoch)
if err != nil {
h.logger.WithContext(ctx).With().Warning("serve: failed to get epoch atx IDs", epoch, log.Err(err))
return nil, err
}
ed := EpochData{
AtxIDs: atxids,
}
h.logger.WithContext(ctx).With().Debug("serve: responded to epoch info request",
epoch,
log.Int("atx_count", len(ed.AtxIDs)),
)
bts, err := codec.Encode(&ed)
if err != nil {
h.logger.WithContext(ctx).With().Fatal("serve: failed to serialize epoch atx", epoch, log.Err(err))
}
return bts, 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.WithContext(ctx).With().Warning("serve: failed to get layer ballots", lid, log.Err(err))
return nil, err
}
out, err := codec.Encode(&ld)
if err != nil {
h.logger.WithContext(ctx).With().Fatal("serve: failed to serialize layer data response", 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.WithContext(ctx).With().Error("serve: failed to get prev agg hash", lid, log.Err(err))
return nil, err
}
bid, err := certificates.CertifiedBlock(h.cdb, lid)
if err != nil && !errors.Is(err, sql.ErrNotFound) {
h.logger.WithContext(ctx).With().Error("serve: failed to get layer certified block", lid, log.Err(err))
return nil, err
}
if err == nil {
lo.Certified = &bid
}
out, err = codec.Encode(&lo)
if err != nil {
h.logger.WithContext(ctx).With().Fatal("serve: failed to serialize layer opinions response", 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.WithContext(ctx).With().Error("serve: failed to get certificate", 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.WithContext(ctx).With().Fatal("serve: failed to encode cert", log.Err(err))
}
return out, nil
}
}
}
return nil, err
}
func (h *handler) handleHashReq(ctx context.Context, data []byte) ([]byte, error) {
var requestBatch RequestBatch
if err := codec.Decode(data, &requestBatch); err != nil {
h.logger.WithContext(ctx).With().Warning("serve: failed to parse request", log.Err(err))
return nil, errBadRequest
}
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 {
totalHashReqs.WithLabelValues(string(r.Hint)).Add(1)
res, err := h.bs.Get(r.Hint, r.Hash.Bytes())
if err != nil {
h.logger.WithContext(ctx).With().Debug("serve: remote peer requested nonexistent hash",
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 res == nil {
h.logger.WithContext(ctx).With().Debug("serve: remote peer requested golden",
log.String("hash", r.Hash.ShortString()),
log.Int("dataSize", len(res)))
hashEmptyData.WithLabelValues(string(r.Hint)).Add(1)
continue
} else {
h.logger.WithContext(ctx).With().Debug("serve: responded to hash request",
log.String("hash", r.Hash.ShortString()),
log.Int("dataSize", len(res)))
}
// add response to batch
m := ResponseMessage{
Hash: r.Hash,
Data: res,
}
resBatch.Responses = append(resBatch.Responses, m)
}
bts, err := codec.Encode(&resBatch)
if err != nil {
h.logger.WithContext(ctx).With().Fatal("serve: failed to encode batch id",
log.Err(err),
log.String("batch_hash", resBatch.ID.ShortString()))
}
h.logger.WithContext(ctx).With().Debug("serve: returning response for batch",
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) 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.WithContext(ctx).With().Warning("serve: failed to parse mesh hash request", log.Err(err))
return nil, errBadRequest
}
if err := req.Validate(); err != nil {
h.logger.WithContext(ctx).With().Debug("failed to validate mesh hash request", log.Err(err))
return nil, err
}
hashes, err = layers.GetAggHashes(h.cdb, req.From, req.To, req.Step)
if err != nil {
h.logger.WithContext(ctx).With().Warning("serve: failed to get mesh hashes", log.Err(err))
return nil, err
}
data, err = codec.EncodeSlice(hashes)
if err != nil {
h.logger.WithContext(ctx).With().Fatal("serve: failed to encode hashes", log.Err(err))
}
h.logger.WithContext(ctx).With().Debug("serve: returning response for mesh hashes",
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
}