-
Notifications
You must be signed in to change notification settings - Fork 211
/
handler.go
236 lines (222 loc) · 7.2 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
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"
"github.com/spacemeshos/go-spacemesh/system"
)
type handler struct {
logger log.Log
cdb *datastore.CachedDB
bs *datastore.BlobStore
msh meshProvider
beacon system.BeaconGetter
}
func newHandler(cdb *datastore.CachedDB, bs *datastore.BlobStore, m meshProvider, b system.BeaconGetter, lg log.Log) *handler {
return &handler{
logger: lg,
cdb: cdb,
bs: bs,
msh: m,
beacon: b,
}
}
// handleEpochInfoReq returns the ATXs published in the specified epoch.
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("failed to get malicious IDs", log.Err(err))
return nil, err
}
h.logger.WithContext(ctx).With().Debug("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("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("failed to get epoch atx IDs", epoch, log.Err(err))
return nil, err
}
ed := EpochData{
AtxIDs: atxids,
}
h.logger.WithContext(ctx).With().Debug("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("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("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("failed to serialize layer data response", log.Err(err))
}
return out, nil
}
// handleLayerOpinionsReq returns the opinions on data in the specified layer, described in LayerOpinion.
func (h *handler) handleLayerOpinionsReq(ctx context.Context, req []byte) ([]byte, error) {
var (
lid types.LayerID
lo LayerOpinion
out []byte
err error
)
if err := codec.Decode(req, &lid); err != nil {
return nil, err
}
lo.PrevAggHash, err = layers.GetAggregatedHash(h.cdb, lid.Sub(1))
if err != nil && !errors.Is(err, sql.ErrNotFound) {
h.logger.WithContext(ctx).With().Warning("failed to get prev agg hash", lid, log.Err(err))
return nil, err
}
certs, err := certificates.Get(h.cdb, lid)
if err != nil && !errors.Is(err, sql.ErrNotFound) {
h.logger.WithContext(ctx).With().Warning("failed to get certificate", lid, log.Err(err))
return nil, err
}
if err == nil {
var validCert *types.Certificate
for _, cert := range certs {
if !cert.Valid {
continue
}
if validCert != nil {
validCert = nil
break
}
validCert = cert.Cert
}
lo.Cert = validCert
}
out, err = codec.Encode(&lo)
if err != nil {
h.logger.WithContext(ctx).With().Fatal("failed to serialize layer opinions response", log.Err(err))
}
return out, nil
}
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("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("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("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("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("failed to serialize batch id",
log.Err(err),
log.String("batch_hash", resBatch.ID.ShortString()))
}
h.logger.WithContext(ctx).With().Debug("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("failed to parse mesh hash request", log.Err(err))
return nil, errBadRequest
}
lids, err := iterateLayers(&req)
if err != nil {
h.logger.WithContext(ctx).With().Debug("failed to iterate layers", log.Err(err))
return nil, err
}
if len(lids) > 0 {
hashes, err = layers.GetAggHashes(h.cdb, lids)
if err != nil {
h.logger.WithContext(ctx).With().Warning("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("failed to serialize hashes", log.Err(err))
}
h.logger.WithContext(ctx).With().Debug("returning response for mesh hashes",
log.Array("layers", log.ArrayMarshalerFunc(func(encoder log.ArrayEncoder) error {
for _, lid := range lids {
encoder.AppendUint32(lid.Uint32())
}
return nil
})))
return data, nil
}