-
Notifications
You must be signed in to change notification settings - Fork 211
/
handler.go
392 lines (335 loc) · 11.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
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
package proposals
import (
"context"
"errors"
"fmt"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/prometheus/client_golang/prometheus"
"github.com/spacemeshos/go-spacemesh/codec"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/log"
"github.com/spacemeshos/go-spacemesh/p2p/pubsub"
"github.com/spacemeshos/go-spacemesh/proposals/metrics"
"github.com/spacemeshos/go-spacemesh/system"
)
// NewProposalProtocol is the protocol indicator for gossip Proposals.
const NewProposalProtocol = "newProposal"
var (
errMalformedData = errors.New("malformed data")
errInitialize = errors.New("failed to initialize")
errInvalidATXID = errors.New("ballot has invalid ATXID")
errMissingBaseBallot = errors.New("base ballot is missing")
errMissingEpochData = errors.New("epoch data is missing in ref ballot")
errUnexpectedEpochData = errors.New("non-ref ballot declares epoch data")
errEmptyActiveSet = errors.New("ref ballot declares empty active set")
errMissingBeacon = errors.New("beacon is missing in ref ballot")
errNotEligible = errors.New("ballot not eligible")
errConflictingExceptions = errors.New("conflicting exceptions")
errExceptionsOverflow = errors.New("too many exceptions")
errDuplicateTX = errors.New("duplicate TxID in proposal")
errDuplicateATX = errors.New("duplicate ATXID in active set")
errKnownBallot = errors.New("known ballot")
errKnownProposal = errors.New("known proposal")
)
// Handler processes Proposal from gossip and, if deems it valid, propagates it to peers.
type Handler struct {
logger log.Log
cfg Config
fetcher system.Fetcher
mesh meshDB
proposalDB proposalDB
validator eligibilityValidator
}
// Config defines configuration for the handler.
type Config struct {
LayerSize uint32
LayersPerEpoch uint32
GoldenATXID types.ATXID
MaxExceptions int
EligibilityValidator eligibilityValidator
}
// defaultConfig for BlockHandler.
func defaultConfig() Config {
return Config{
MaxExceptions: 1000,
EligibilityValidator: nil,
}
}
// Opt for configuring Handler.
type Opt func(h *Handler)
// WithGoldenATXID defines the golden ATXID.
func WithGoldenATXID(atx types.ATXID) Opt {
return func(h *Handler) {
h.cfg.GoldenATXID = atx
}
}
// WithLayerSize defines the average number of proposal per layer.
func WithLayerSize(size uint32) Opt {
return func(h *Handler) {
h.cfg.LayerSize = size
}
}
// WithLayerPerEpoch defines the number of layers per epoch.
func WithLayerPerEpoch(layers uint32) Opt {
return func(h *Handler) {
h.cfg.LayersPerEpoch = layers
}
}
// withValidator defines eligibility Validator.
func withValidator(v eligibilityValidator) Opt {
return func(h *Handler) {
h.validator = v
}
}
// WithMaxExceptions defines max allowed exceptions in a ballot.
func WithMaxExceptions(max int) Opt {
return func(h *Handler) {
h.cfg.MaxExceptions = max
}
}
// WithLogger defines logger for Handler.
func WithLogger(logger log.Log) Opt {
return func(h *Handler) {
h.logger = logger
}
}
// NewHandler creates new Handler.
func NewHandler(f system.Fetcher, bc system.BeaconCollector, db atxDB, m meshDB, p proposalDB, opts ...Opt) *Handler {
b := &Handler{
logger: log.NewNop(),
cfg: defaultConfig(),
fetcher: f,
mesh: m,
proposalDB: p,
}
for _, opt := range opts {
opt(b)
}
if b.validator == nil {
b.validator = NewEligibilityValidator(b.cfg.LayerSize, b.cfg.LayersPerEpoch, db, bc, m, b.logger)
}
return b
}
// HandleProposal is the gossip receiver for Proposal.
func (h *Handler) HandleProposal(ctx context.Context, _ peer.ID, msg []byte) pubsub.ValidationResult {
newCtx := log.WithNewRequestID(ctx)
if err := h.handleProposalData(newCtx, msg); errors.Is(err, errKnownProposal) {
return pubsub.ValidationIgnore
} else if err != nil {
h.logger.WithContext(newCtx).With().Error("failed to process proposal gossip", log.Err(err))
return pubsub.ValidationIgnore
}
return pubsub.ValidationAccept
}
// HandleBallotData handles Ballot data from gossip and sync.
func (h *Handler) HandleBallotData(ctx context.Context, data []byte) error {
newCtx := log.WithNewRequestID(ctx)
logger := h.logger.WithContext(newCtx)
logger.Info("processing ballot")
var b types.Ballot
if err := codec.Decode(data, &b); err != nil {
logger.With().Error("malformed ballot", log.Err(err))
return errMalformedData
}
// set the ballot and smesher ID when received
if err := b.Initialize(); err != nil {
logger.With().Error("failed to initialize ballot", log.Err(err))
return errInitialize
}
logger = logger.WithFields(b.ID(), b.LayerIndex)
err := h.processBallot(ctx, &b, logger)
if err != nil && err != errKnownBallot {
return err
} else if err == errKnownBallot {
return nil
}
if err = h.mesh.AddBallot(&b); err != nil {
return fmt.Errorf("save ballot: %w", err)
}
return nil
}
// HandleProposalData handles Proposal data from sync.
func (h *Handler) HandleProposalData(ctx context.Context, data []byte) error {
err := h.handleProposalData(ctx, data)
if errors.Is(err, errKnownProposal) {
return nil
}
return err
}
func (h *Handler) handleProposalData(ctx context.Context, data []byte) error {
logger := h.logger.WithContext(ctx)
logger.Info("processing proposal")
var p types.Proposal
if err := codec.Decode(data, &p); err != nil {
logger.With().Error("malformed proposal", log.Err(err))
return errMalformedData
}
// set the proposal ID when received
if err := p.Initialize(); err != nil {
logger.With().Warning("failed to initialize proposal", log.Err(err))
return errInitialize
}
logger = logger.WithFields(p.ID(), p.Ballot.ID(), p.LayerIndex)
if h.proposalDB.HasProposal(p.ID()) {
logger.Info("known proposal")
return fmt.Errorf("%w proposal %s", errKnownProposal, p.ID())
}
logger.With().Info("new proposal", log.Inline(&p))
if err := h.processBallot(ctx, &p.Ballot, logger); err != nil && err != errKnownBallot {
logger.With().Warning("failed to process ballot", log.Err(err))
return err
}
if err := h.checkTransactions(ctx, &p); err != nil {
logger.With().Warning("failed to fetch proposal TXs", log.Err(err))
return err
}
h.logger.WithContext(ctx).With().Debug("proposal is syntactically valid")
reportProposalMetrics(&p)
if err := h.proposalDB.AddProposal(ctx, &p); err != nil {
logger.With().Error("failed to save proposal", log.Err(err))
return fmt.Errorf("save proposal: %w", err)
}
return nil
}
func (h *Handler) processBallot(ctx context.Context, b *types.Ballot, logger log.Log) error {
if h.mesh.HasBallot(b.ID()) {
logger.Info("known ballot")
return errKnownBallot
}
logger.With().Info("new ballot", log.Inline(b))
if err := h.checkBallotSyntacticValidity(ctx, b); err != nil {
logger.With().Error("ballot syntactically invalid", log.Err(err))
return fmt.Errorf("syntactic-check ballot: %w", err)
}
reportBallotMetrics(b)
return nil
}
func (h *Handler) checkBallotSyntacticValidity(ctx context.Context, b *types.Ballot) error {
h.logger.WithContext(ctx).With().Debug("checking proposal syntactic validity")
if err := h.checkBallotDataIntegrity(b); err != nil {
h.logger.WithContext(ctx).With().Warning("ballot integrity check failed", log.Err(err))
return err
}
if err := h.checkBallotDataAvailability(ctx, b); err != nil {
h.logger.WithContext(ctx).With().Warning("ballot data availability check failed", log.Err(err))
return err
}
if eligible, err := h.validator.CheckEligibility(ctx, b); err != nil || !eligible {
h.logger.WithContext(ctx).With().Warning("ballot eligibility check failed", log.Err(err))
return errNotEligible
}
h.logger.WithContext(ctx).With().Debug("ballot is syntactically valid")
return nil
}
func (h *Handler) checkBallotDataIntegrity(b *types.Ballot) error {
if b.AtxID == *types.EmptyATXID || b.AtxID == h.cfg.GoldenATXID {
return errInvalidATXID
}
if b.Votes.Base == types.EmptyBallotID {
return errMissingBaseBallot
}
if b.RefBallot == types.EmptyBallotID {
// this is the smesher's first Ballot in this epoch, should contain EpochData
if b.EpochData == nil {
return errMissingEpochData
}
if b.EpochData.Beacon == types.EmptyBeacon {
return errMissingBeacon
}
if len(b.EpochData.ActiveSet) == 0 {
return errEmptyActiveSet
}
// check for duplicate ATXIDs in active set
set := make(map[types.ATXID]struct{}, len(b.EpochData.ActiveSet))
for _, atx := range b.EpochData.ActiveSet {
if _, exist := set[atx]; exist {
return errDuplicateATX
}
set[atx] = struct{}{}
}
} else {
if b.EpochData != nil {
return errUnexpectedEpochData
}
}
if err := checkExceptions(b, h.cfg.MaxExceptions); err != nil {
return err
}
return nil
}
func checkExceptions(ballot *types.Ballot, max int) error {
exceptions := map[types.BlockID]struct{}{}
// TODO maybe validate that explicit votes do not conflict with abstain layers
for _, diff := range [][]types.BlockID{ballot.Votes.Support, ballot.Votes.Against} {
for _, bid := range diff {
_, exist := exceptions[bid]
if exist {
return fmt.Errorf("%w: block %s is referenced multiple times in exceptions of ballot %s",
errConflictingExceptions, bid, ballot.ID())
}
exceptions[bid] = struct{}{}
}
}
if len(exceptions) > max {
return fmt.Errorf("%w: %d exceptions with max allowed %d in ballot %s",
errExceptionsOverflow, len(exceptions), max, ballot.ID())
}
return nil
}
func ballotBlockView(b *types.Ballot) []types.BlockID {
combined := make([]types.BlockID, 0, len(b.Votes.Support)+len(b.Votes.Against))
combined = append(combined, b.Votes.Support...)
combined = append(combined, b.Votes.Against...)
return combined
}
func (h *Handler) checkBallotDataAvailability(ctx context.Context, b *types.Ballot) error {
ballots := []types.BallotID{b.Votes.Base}
if b.RefBallot != types.EmptyBallotID {
ballots = append(ballots, b.RefBallot)
}
if err := h.fetcher.GetBallots(ctx, ballots); err != nil {
return fmt.Errorf("fetch ballots: %w", err)
}
if err := h.fetchReferencedATXs(ctx, b); err != nil {
return fmt.Errorf("fetch referenced ATXs: %w", err)
}
if err := h.fetcher.GetBlocks(ctx, ballotBlockView(b)); err != nil {
return fmt.Errorf("fetch blocks: %w", err)
}
return nil
}
func (h *Handler) fetchReferencedATXs(ctx context.Context, b *types.Ballot) error {
atxs := []types.ATXID{b.AtxID}
if b.EpochData != nil {
atxs = append(atxs, b.EpochData.ActiveSet...)
}
if err := h.fetcher.GetAtxs(ctx, atxs); err != nil {
return fmt.Errorf("proposal get ATXs: %w", err)
}
return nil
}
func (h *Handler) checkTransactions(ctx context.Context, p *types.Proposal) error {
if len(p.TxIDs) == 0 {
return nil
}
set := make(map[types.TransactionID]struct{}, len(p.TxIDs))
for _, tx := range p.TxIDs {
if _, exist := set[tx]; exist {
return errDuplicateTX
}
set[tx] = struct{}{}
}
if err := h.fetcher.GetTxs(ctx, p.TxIDs); err != nil {
return fmt.Errorf("proposal get TXs: %w", err)
}
return nil
}
func reportProposalMetrics(p *types.Proposal) {
metrics.ProposalSize.WithLabelValues().Observe(float64(len(p.Bytes())))
metrics.NumTxsInProposal.WithLabelValues().Observe(float64(len(p.TxIDs)))
}
func reportBallotMetrics(b *types.Ballot) {
metrics.NumBlocksInException.With(prometheus.Labels{metrics.DiffTypeLabel: metrics.DiffTypeAgainst}).Observe(float64(len(b.Votes.Against)))
metrics.NumBlocksInException.With(prometheus.Labels{metrics.DiffTypeLabel: metrics.DiffTypeFor}).Observe(float64(len(b.Votes.Support)))
metrics.NumBlocksInException.With(prometheus.Labels{metrics.DiffTypeLabel: metrics.DiffTypeNeutral}).Observe(float64(len(b.Votes.Abstain)))
}