-
Notifications
You must be signed in to change notification settings - Fork 211
/
mesh_data.go
331 lines (296 loc) · 10.2 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
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, peer p2p.Peer) ([]byte, error) {
return f.servers[malProtocol].Request(ctx, peer, []byte{})
}
// GetLayerData get layer data from peers.
func (f *Fetch) GetLayerData(ctx context.Context, peer p2p.Peer, lid types.LayerID) ([]byte, error) {
lidBytes, err := codec.Encode(&lid)
if err != nil {
return nil, err
}
return f.servers[lyrDataProtocol].Request(ctx, peer, lidBytes)
}
func (f *Fetch) GetLayerOpinions(ctx context.Context, peer p2p.Peer, lid types.LayerID) ([]byte, error) {
req := OpinionRequest{
Layer: lid,
}
reqData, err := codec.Encode(&req)
if err != nil {
return nil, err
}
return f.servers[OpnProtocol].Request(ctx, peer, reqData)
}
// 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))
epochBytes, err := codec.Encode(epoch)
if err != nil {
return nil, err
}
data, err := f.servers[atxProtocol].Request(ctx, peer, epochBytes)
if err != nil {
return nil, err
}
var ed EpochData
if err := codec.Decode(data, &ed); err != nil {
return nil, fmt.Errorf("decoding epoch data: %w", err)
}
f.RegisterPeerHashes(peer, types.ATXIDsToHashes(ed.AtxIDs))
return &ed, nil
}
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),
)
reqData, err := codec.Encode(req)
if err != nil {
f.logger.With().Fatal("failed to encode mesh hash request", log.Err(err))
}
data, err := f.servers[meshHashProtocol].Request(ctx, peer, reqData)
if err != nil {
return nil, err
}
hashes, err := codec.DecodeSlice[types.Hash32](data)
if err != nil {
return nil, fmt.Errorf("decoding hashes response: %w", err)
}
return &MeshHashes{
Hashes: hashes,
}, nil
}
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 := codec.MustEncode(req)
for _, peer := range peers {
data, err := f.servers[OpnProtocol].Request(ctx, peer, reqData)
if err != nil {
f.logger.With().Debug("failed to get cert", log.Stringer("peer", peer), log.Err(err))
continue
}
var peerCert types.Certificate
if err = codec.Decode(data, &peerCert); err != nil {
f.logger.With().Debug("failed to decode cert", log.Stringer("peer", peer), log.Err(err))
continue
}
// 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 {
f.logger.With().Debug(
"peer served wrong cert",
log.Stringer("want", bid),
log.Stringer("got", peerCert.BlockID),
log.Stringer("peer", peer),
)
}
return &peerCert, nil
}
return nil, fmt.Errorf("failed to get cert %v/%s from %d peers: %w", lid, bid.String(), len(peers), ctx.Err())
}