-
Notifications
You must be signed in to change notification settings - Fork 211
/
mesh_data.go
416 lines (381 loc) · 11.5 KB
/
mesh_data.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
package fetch
import (
"context"
"errors"
"fmt"
"sync/atomic"
"golang.org/x/sync/errgroup"
"github.com/spacemeshos/go-spacemesh/activation"
"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"
"github.com/spacemeshos/go-spacemesh/system"
)
var errBadRequest = errors.New("invalid request")
// GetAtxs gets the data for given atx IDs and validates them. returns an error if at least one ATX cannot be fetched.
func (f *Fetch) GetAtxs(ctx context.Context, ids []types.ATXID, opts ...system.GetAtxOpt) error {
if len(ids) == 0 {
return nil
}
options := system.GetAtxOpts{}
for _, opt := range opts {
opt(&options)
}
f.logger.WithContext(ctx).With().
Debug("requesting atxs from peer", log.Int("num_atxs", len(ids)), log.Bool("limiting", !options.LimitingOff))
hashes := types.ATXIDsToHashes(ids)
if options.LimitingOff {
return f.getHashes(ctx, hashes, datastore.ATXDB, f.validators.atx.HandleMessage)
}
return f.getHashes(ctx, hashes, datastore.ATXDB, f.validators.atx.HandleMessage, withLimiter(f.getAtxsLimiter))
}
type dataReceiver func(context.Context, types.Hash32, p2p.Peer, []byte) error
type getHashesOpt func(*getHashesOpts)
func withLimiter(l limiter) getHashesOpt {
return func(o *getHashesOpts) {
o.limiter = l
}
}
func (f *Fetch) getHashes(
ctx context.Context,
hashes []types.Hash32,
hint datastore.Hint,
receiver dataReceiver,
opts ...getHashesOpt,
) error {
options := getHashesOpts{
limiter: noLimit{},
}
for _, opt := range opts {
opt(&options)
}
pendingMetric := pendingHashReqs.WithLabelValues(string(hint))
pendingMetric.Add(float64(len(hashes)))
var eg errgroup.Group
var failed atomic.Uint64
for i, hash := range hashes {
if err := options.limiter.Acquire(ctx, 1); err != nil {
pendingMetric.Add(float64(i - len(hashes)))
return fmt.Errorf("acquiring slot to get hash: %w", err)
}
p, err := f.getHash(ctx, hash, hint, receiver)
if err != nil {
options.limiter.Release(1)
pendingMetric.Add(float64(i - len(hashes)))
return err
}
if p == nil {
// data is available locally
options.limiter.Release(1)
pendingMetric.Add(-1)
continue
}
h := hash
eg.Go(func() error {
select {
case <-ctx.Done():
options.limiter.Release(1)
pendingMetric.Add(-1)
return ctx.Err()
case <-p.completed:
options.limiter.Release(1)
pendingMetric.Add(-1)
if p.err != nil {
f.logger.Debug("failed to get hash",
log.String("hint", string(hint)),
log.Stringer("hash", h),
log.Err(p.err),
)
failed.Add(1)
}
return nil
}
})
}
eg.Wait()
if failed.Load() > 0 {
return fmt.Errorf("failed to fetch %d hashes out of %d", failed.Load(), len(hashes))
}
return nil
}
// GetActiveSet downloads activeset.
func (f *Fetch) GetActiveSet(ctx context.Context, set types.Hash32) error {
f.logger.WithContext(ctx).With().Debug("request active set", log.ShortStringer("id", set))
return f.getHashes(ctx, []types.Hash32{set}, datastore.ActiveSet, f.validators.activeset.HandleMessage)
}
// GetMalfeasanceProofs gets malfeasance proofs for the specified NodeIDs and validates them.
func (f *Fetch) GetMalfeasanceProofs(ctx context.Context, ids []types.NodeID) error {
if len(ids) == 0 {
return nil
}
f.logger.WithContext(ctx).With().Debug("requesting malfeasance proofs from peer", log.Int("num_proofs", len(ids)))
hashes := types.NodeIDsToHashes(ids)
return f.getHashes(ctx, hashes, datastore.Malfeasance, f.validators.malfeasance.HandleMessage)
}
// GetBallots gets data for the specified BallotIDs and validates them.
func (f *Fetch) GetBallots(ctx context.Context, ids []types.BallotID) error {
if len(ids) == 0 {
return nil
}
f.logger.WithContext(ctx).With().Debug("requesting ballots from peer", log.Int("num_ballots", len(ids)))
hashes := types.BallotIDsToHashes(ids)
return f.getHashes(ctx, hashes, datastore.BallotDB, f.validators.ballot.HandleMessage)
}
// GetProposals gets the data for given proposal IDs from peers.
func (f *Fetch) GetProposals(ctx context.Context, ids []types.ProposalID) error {
if len(ids) == 0 {
return nil
}
f.logger.WithContext(ctx).With().Debug("requesting proposals from peer", log.Int("num_proposals", len(ids)))
hashes := types.ProposalIDsToHashes(ids)
return f.getHashes(ctx, hashes, datastore.ProposalDB, f.validators.proposal.HandleMessage)
}
// GetBlocks gets the data for given block IDs from peers.
func (f *Fetch) GetBlocks(ctx context.Context, ids []types.BlockID) error {
if len(ids) == 0 {
return nil
}
f.logger.WithContext(ctx).With().Debug("requesting blocks from peer", log.Int("num_blocks", len(ids)))
hashes := types.BlockIDsToHashes(ids)
return f.getHashes(ctx, hashes, datastore.BlockDB, f.validators.block.HandleMessage)
}
// GetProposalTxs fetches the txs provided as IDs and validates them, returns an error if one TX failed to be fetched.
func (f *Fetch) GetProposalTxs(ctx context.Context, ids []types.TransactionID) error {
return f.getTxs(ctx, ids, f.validators.txProposal.HandleMessage)
}
// GetBlockTxs fetches the txs provided as IDs and saves them, they will be validated
// before block is applied.
func (f *Fetch) GetBlockTxs(ctx context.Context, ids []types.TransactionID) error {
return f.getTxs(ctx, ids, f.validators.txBlock.HandleMessage)
}
func (f *Fetch) getTxs(ctx context.Context, ids []types.TransactionID, receiver dataReceiver) error {
if len(ids) == 0 {
return nil
}
f.logger.WithContext(ctx).With().Debug("requesting txs from peer", log.Int("num_txs", len(ids)))
hashes := types.TransactionIDsToHashes(ids)
return f.getHashes(ctx, hashes, datastore.TXDB, receiver)
}
// GetPoetProof gets poet proof from remote peer.
func (f *Fetch) GetPoetProof(ctx context.Context, id types.Hash32) error {
f.logger.WithContext(ctx).With().Debug("getting poet proof", log.Stringer("hash", id))
pm, err := f.getHash(ctx, id, datastore.POETDB, f.validators.poet.HandleMessage)
if err != nil {
return err
}
if pm == nil {
// data is available locally
return nil
}
select {
case <-ctx.Done():
return ctx.Err()
case <-pm.completed:
}
switch {
case pm.err == nil:
return nil
case errors.Is(pm.err, activation.ErrObjectExists):
// PoET proofs are concurrently stored in DB in two places:
// fetcher and nipost builder. Hence it might happen that
// a proof had been inserted into the DB while the fetcher
// was fetching.
return nil
default:
f.logger.WithContext(ctx).With().Warning("failed to get hash",
log.String("hint", string(datastore.POETDB)),
log.Stringer("hash", id),
log.Err(pm.err))
return pm.err
}
}
func (f *Fetch) GetMaliciousIDs(
ctx context.Context,
peers []p2p.Peer,
okCB func([]byte, p2p.Peer),
errCB func(error, p2p.Peer),
) error {
return poll(ctx, f.servers[malProtocol], peers, []byte{}, okCB, errCB)
}
// GetLayerData get layer data from peers.
func (f *Fetch) GetLayerData(
ctx context.Context,
peers []p2p.Peer,
lid types.LayerID,
okCB func([]byte, p2p.Peer),
errCB func(error, p2p.Peer),
) error {
lidBytes, err := codec.Encode(&lid)
if err != nil {
return err
}
return poll(ctx, f.servers[lyrDataProtocol], peers, lidBytes, okCB, errCB)
}
func (f *Fetch) GetLayerOpinions(
ctx context.Context,
peers []p2p.Peer,
lid types.LayerID,
okCB func([]byte, p2p.Peer),
errCB func(error, p2p.Peer),
) error {
req := OpinionRequest{
Layer: lid,
}
reqData, err := codec.Encode(&req)
if err != nil {
return err
}
return poll(ctx, f.servers[OpnProtocol], peers, reqData, okCB, errCB)
}
func poll(
ctx context.Context,
srv requester,
peers []p2p.Peer,
req []byte,
okCB func([]byte, p2p.Peer),
errCB func(error, p2p.Peer),
) error {
for _, p := range peers {
peer := p
okFunc := func(data []byte) {
okCB(data, peer)
}
errFunc := func(err error) {
errCB(err, peer)
}
if err := srv.Request(ctx, peer, req, okFunc, errFunc); err != nil {
errFunc(err)
}
}
return nil
}
// PeerEpochInfo get the epoch info published in the given epoch from the specified peer.
func (f *Fetch) PeerEpochInfo(ctx context.Context, peer p2p.Peer, epoch types.EpochID) (*EpochData, error) {
f.logger.WithContext(ctx).With().Debug("requesting epoch info from peer",
log.Stringer("peer", peer),
log.Stringer("epoch", epoch))
var (
done = make(chan error, 1)
ed EpochData
)
okCB := func(data []byte) {
done <- codec.Decode(data, &ed)
}
errCB := func(perr error) {
done <- perr
}
epochBytes, err := codec.Encode(epoch)
if err != nil {
return nil, err
}
if err := f.servers[atxProtocol].Request(ctx, peer, epochBytes, okCB, errCB); err != nil {
return nil, err
}
select {
case err := <-done:
if err != nil {
return nil, err
}
f.RegisterPeerHashes(peer, types.ATXIDsToHashes(ed.AtxIDs))
return &ed, nil
case <-ctx.Done():
f.logger.WithContext(ctx).With().Debug("context done")
return nil, ctx.Err()
}
}
func (f *Fetch) PeerMeshHashes(ctx context.Context, peer p2p.Peer, req *MeshHashRequest) (*MeshHashes, error) {
f.logger.WithContext(ctx).With().Debug("requesting mesh hashes from peer",
log.Stringer("peer", peer),
log.Object("req", req),
)
var (
done = make(chan error, 1)
hashes []types.Hash32
reqData []byte
)
reqData, err := codec.Encode(req)
if err != nil {
f.logger.With().Fatal("failed to encode mesh hash request", log.Err(err))
}
okCB := func(data []byte) {
h, err := codec.DecodeSlice[types.Hash32](data)
hashes = h
done <- err
}
errCB := func(perr error) {
done <- perr
}
if err = f.servers[meshHashProtocol].Request(ctx, peer, reqData, okCB, errCB); err != nil {
return nil, err
}
select {
case err := <-done:
if err != nil {
return nil, err
}
return &MeshHashes{
Hashes: hashes,
}, nil
case <-ctx.Done():
return nil, ctx.Err()
}
}
func (f *Fetch) GetCert(
ctx context.Context,
lid types.LayerID,
bid types.BlockID,
peers []p2p.Peer,
) (*types.Certificate, error) {
f.logger.WithContext(ctx).With().Debug("requesting block certificate from peers",
lid, bid, log.Int("num peer", len(peers)))
req := &OpinionRequest{
Layer: lid,
Block: &bid,
}
reqData, err := codec.Encode(req)
if err != nil {
f.logger.With().Fatal("failed to encode cert request", log.Err(err))
}
out := make(chan *types.Certificate, 1)
for _, peer := range peers {
done := make(chan error, 1)
okCB := func(data []byte) {
var peerCert types.Certificate
if err = codec.Decode(data, &peerCert); err != nil {
done <- err
return
}
// for generic data fetches by hash (ID for atx/block/proposal/ballot/tx), the check on whether the returned
// data matching the hash was done on the data handlers' path. for block certificate, there is no ID associated
// with it, hence the check here.
// however, certificate doesn't go through that path. it's requested by a separate protocol because a block
// certificate doesn't have an ID.
if peerCert.BlockID != bid {
done <- fmt.Errorf("peer %v served wrong cert. want %s got %s", peer, bid.String(), peerCert.BlockID.String())
return
}
out <- &peerCert
}
errCB := func(perr error) {
done <- perr
}
if err := f.servers[OpnProtocol].Request(ctx, peer, reqData, okCB, errCB); err != nil {
done <- err
}
select {
case err := <-done:
f.logger.With().Debug("failed to get cert from peer",
log.Stringer("peer", peer),
log.Err(err),
)
continue
case cert := <-out:
return cert, nil
case <-ctx.Done():
return nil, ctx.Err()
}
}
return nil, fmt.Errorf("failed to get cert %v/%s from %d peers", lid, bid.String(), len(peers))
}