-
Notifications
You must be signed in to change notification settings - Fork 212
/
handlers.go
520 lines (443 loc) · 16.8 KB
/
handlers.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
package beacon
import (
"context"
"encoding/hex"
"errors"
"fmt"
"math/big"
"time"
"github.com/spacemeshos/go-spacemesh/codec"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/log"
"github.com/spacemeshos/go-spacemesh/metrics"
"github.com/spacemeshos/go-spacemesh/p2p"
"github.com/spacemeshos/go-spacemesh/p2p/pubsub"
"github.com/spacemeshos/go-spacemesh/signing"
)
type category uint8
const (
valid category = 1
potentiallyValid category = 2
invalid category = 3
)
var (
errVRFNotVerified = errors.New("proposal failed vrf verification")
errAlreadyProposed = errors.New("already proposed")
errAlreadyVoted = errors.New("already voted")
errMinerNotActive = errors.New("miner ATX not found in previous epoch")
errProtocolNotRunning = errors.New("beacon protocol not running")
errEpochNotActive = errors.New("epoch not active")
errMalformedMessage = errors.New("malformed msg")
errUntimelyMessage = errors.New("untimely msg")
)
// HandleWeakCoinProposal handles weakcoin proposal from gossip.
func (pd *ProtocolDriver) HandleWeakCoinProposal(ctx context.Context, peer p2p.Peer, msg []byte) pubsub.ValidationResult {
if pd.isClosed() || !pd.isInProtocol() {
return pubsub.ValidationIgnore
}
return pd.weakCoin.HandleProposal(ctx, peer, msg)
}
// HandleProposal handles beacon proposal from gossip.
func (pd *ProtocolDriver) HandleProposal(ctx context.Context, peer p2p.Peer, msg []byte) pubsub.ValidationResult {
if pd.isClosed() {
return pubsub.ValidationIgnore
}
receivedTime := time.Now()
err := pd.handleProposal(ctx, peer, msg, receivedTime)
switch {
case err == nil:
return pubsub.ValidationAccept
case errors.Is(err, errMalformedMessage):
return pubsub.ValidationReject
default:
return pubsub.ValidationIgnore
}
}
func (pd *ProtocolDriver) handleProposal(ctx context.Context, peer p2p.Peer, msg []byte, receivedTime time.Time) error {
logger := pd.logger.WithContext(ctx)
var m ProposalMessage
if err := codec.Decode(msg, &m); err != nil {
logger.With().Warning("received malformed beacon proposal", log.Stringer("sender", peer), log.Err(err))
return errMalformedMessage
}
latency := receivedTime.Sub(pd.msgTimes.proposalSendTime(m.EpochID))
metrics.ReportMessageLatency(pubsub.BeaconProtocol, pubsub.BeaconProposalProtocol, latency)
if !pd.isProposalTimely(&m, receivedTime) {
logger.With().Debug("proposal too early", m.EpochID, log.Time("received_at", receivedTime))
return errUntimelyMessage
}
logger = pd.logger.WithContext(ctx).WithFields(m.EpochID, log.Stringer("smesher", m.NodeID))
proposal := ProposalFromVrf(m.VRFSignature)
logger.With().Debug("new beacon proposal", log.String("proposal", hex.EncodeToString(proposal[:])))
st, err := pd.initEpochStateIfNotPresent(logger, m.EpochID)
if err != nil {
return err
}
atx, err := pd.minerAtxHdr(m.EpochID, m.NodeID)
if err != nil {
return err
}
if err = pd.verifyProposalMessage(logger, m); err != nil {
return err
}
cat := pd.classifyProposal(logger, m, atx.Received, receivedTime, st.proposalChecker)
return pd.addProposal(m, cat)
}
func (pd *ProtocolDriver) classifyProposal(
logger log.Log,
m ProposalMessage,
atxReceived, receivedTime time.Time,
checker eligibilityChecker,
) category {
epochStart := pd.clock.LayerToTime(m.EpochID.FirstLayer())
proposal := ProposalFromVrf(m.VRFSignature)
logger = logger.WithFields(
log.String("proposal", hex.EncodeToString(proposal[:])),
log.Time("atx_timestamp", atxReceived),
log.Stringer("next_epoch_start", epochStart),
log.Time("received_time", receivedTime),
log.Duration("grace_period", pd.config.GracePeriodDuration),
)
// partition the proposals into three sets:
// let W1 be the weight at δ before the end of the previous epoch, with eligibility threshold X1
// let W2 be the weight at the end of the previous epoch, with eligibility threshold X2
// - valid:
// * the proposer's ATX was received before the end of the previous epoch
// * the proposal is received before the proposal phase ends.
// * the proposal is lower than the threshold X2
// - potentially valid:
// * the proposer's ATX was received within δ after the end of the previous epoch.
// * the proposal is received within δ after the proposal phase ends.
// * the proposal is lower than the threshold X1
// - invalid
// the proposal is neither valid nor potentially valid
//
// note that honest users cannot disagree on timing by more than δ, so if a proposal is timely for
// any honest user, it cannot be late for any honest user (and vice versa).
var (
atxDelay = atxReceived.Sub(epochStart)
endTime = pd.getProposalPhaseFinishedTime(m.EpochID)
proposalDelay time.Duration
)
if !endTime.IsZero() {
proposalDelay = receivedTime.Sub(endTime)
}
switch {
case atxDelay <= 0 &&
proposalDelay <= 0 &&
checker.PassStrictThreshold(m.VRFSignature):
logger.With().Debug("valid beacon proposal",
log.Duration("atx delay", atxDelay),
log.Duration("proposal delay", proposalDelay),
log.String("proposal", hex.EncodeToString(proposal[:])),
)
return valid
case atxDelay <= pd.config.GracePeriodDuration &&
proposalDelay <= pd.config.GracePeriodDuration &&
checker.PassThreshold(m.VRFSignature):
logger.With().Debug("potentially valid beacon proposal",
log.Duration("atx delay", atxDelay),
log.Duration("proposal delay", proposalDelay),
log.String("proposal", hex.EncodeToString(proposal[:])),
)
return potentiallyValid
default:
if atxDelay > pd.config.GracePeriodDuration || proposalDelay > pd.config.GracePeriodDuration {
logger.With().Warning("invalid beacon proposal",
log.Duration("atx delay", atxDelay),
log.Duration("proposal delay", proposalDelay),
log.String("proposal", hex.EncodeToString(proposal[:])),
)
} else {
logger.With().Debug("proposal did not pass thresholds",
log.Duration("atx delay", atxDelay),
log.Duration("proposal delay", proposalDelay),
log.String("proposal", hex.EncodeToString(proposal[:])),
)
}
}
return invalid
}
func (pd *ProtocolDriver) addProposal(m ProposalMessage, cat category) error {
p := ProposalFromVrf(m.VRFSignature)
pd.mu.Lock()
defer pd.mu.Unlock()
if _, ok := pd.states[m.EpochID]; !ok {
return errEpochNotActive
}
switch cat {
case valid:
pd.states[m.EpochID].addValidProposal(p)
case potentiallyValid:
pd.states[m.EpochID].addPotentiallyValidProposal(p)
}
return nil
}
func (pd *ProtocolDriver) verifyProposalMessage(logger log.Log, m ProposalMessage) error {
nonce, err := pd.nonceFetcher.VRFNonce(m.NodeID, m.EpochID)
if err != nil {
logger.With().Warning("[proposal] failed to get VRF nonce", log.Err(err))
return fmt.Errorf("[proposal] get VRF nonce (miner ID %s): %w", m.NodeID, err)
}
currentEpochProposal := buildProposal(logger, m.EpochID, nonce)
if !pd.vrfVerifier.Verify(m.NodeID, currentEpochProposal, m.VRFSignature) {
// TODO(nkryuchkov): attach telemetry
logger.With().Warning("[proposal] failed to verify VRF signature")
return fmt.Errorf("[proposal] verify VRF (miner ID %s): %w", m.NodeID, errVRFNotVerified)
}
if err = pd.registerProposed(logger, m.EpochID, m.NodeID); err != nil {
logger.With().Warning("[proposal] failed to register miner proposed", log.Err(err))
return fmt.Errorf("[proposal] register proposal (miner ID %s): %w", m.NodeID, err)
}
return nil
}
// HandleFirstVotes handles beacon first votes from gossip.
func (pd *ProtocolDriver) HandleFirstVotes(ctx context.Context, peer p2p.Peer, msg []byte) pubsub.ValidationResult {
if pd.isClosed() || !pd.isInProtocol() {
pd.logger.WithContext(ctx).Debug("beacon protocol shutting down or not running, dropping msg")
return pubsub.ValidationIgnore
}
logger := pd.logger.WithContext(ctx).WithFields(log.Stringer("sender", peer))
logger.Debug("new first votes")
err := pd.handleFirstVotes(ctx, peer, msg)
switch {
case err == nil:
return pubsub.ValidationAccept
case errors.Is(err, errMalformedMessage):
return pubsub.ValidationReject
default:
return pubsub.ValidationIgnore
}
}
func (pd *ProtocolDriver) handleFirstVotes(ctx context.Context, peer p2p.Peer, msg []byte) error {
receivedTime := time.Now()
logger := pd.logger.WithContext(ctx).WithFields(types.FirstRound, log.Stringer("sender", peer))
var m FirstVotingMessage
if err := codec.Decode(msg, &m); err != nil {
logger.With().Warning("received invalid first votes", log.Err(err))
return errMalformedMessage
}
latency := receivedTime.Sub(pd.msgTimes.firstVoteSendTime(m.EpochID))
metrics.ReportMessageLatency(pubsub.BeaconProtocol, pubsub.BeaconFirstVotesProtocol, latency)
currentEpoch := pd.currentEpoch()
if m.EpochID != currentEpoch {
logger.With().Debug("first votes from different epoch",
log.Uint32("current_epoch", uint32(currentEpoch)),
log.Uint32("message_epoch", uint32(m.EpochID)))
return errEpochNotActive
}
// don't accept more first vote after the round ends
currentRound := pd.currentRound()
if currentRound > types.FirstRound {
logger.With().Debug("first votes too late",
log.Uint32("current_round", uint32(currentRound)),
log.Uint32("message_round", uint32(types.FirstRound)))
return errUntimelyMessage
}
minerPK, err := pd.verifyFirstVotes(ctx, m)
if err != nil {
return err
}
logger.Debug("received first voting message, storing its votes")
return pd.storeFirstVotes(m, minerPK)
}
func (pd *ProtocolDriver) verifyFirstVotes(ctx context.Context, m FirstVotingMessage) (types.NodeID, error) {
logger := pd.logger.WithContext(ctx).WithFields(m.EpochID, types.FirstRound)
messageBytes, err := codec.Encode(&m.FirstVotingMessageBody)
if err != nil {
logger.With().Fatal("failed to serialize first voting message", log.Err(err))
}
if !pd.edVerifier.Verify(signing.BEACON, m.SmesherID, messageBytes, m.Signature) {
return types.EmptyNodeID, fmt.Errorf("[round %v] verify signature %s: failed", types.FirstRound, m.Signature)
}
logger = logger.WithFields(log.Stringer("smesher", m.SmesherID))
if err = pd.registerVoted(logger, m.EpochID, m.SmesherID, types.FirstRound); err != nil {
return types.EmptyNodeID, fmt.Errorf("[round %v] register proposal (miner ID %v): %w", types.FirstRound, m.SmesherID.ShortString(), err)
}
return m.SmesherID, nil
}
func (pd *ProtocolDriver) storeFirstVotes(m FirstVotingMessage, nodeID types.NodeID) error {
if !pd.isInProtocol() {
pd.logger.Debug("beacon not in protocol, not storing first votes")
return errProtocolNotRunning
}
atx, err := pd.minerAtxHdr(m.EpochID, nodeID)
if err != nil {
return err
}
voteWeight := new(big.Int).SetUint64(atx.GetWeight())
pd.mu.Lock()
defer pd.mu.Unlock()
if _, ok := pd.states[m.EpochID]; !ok {
return errEpochNotActive
}
for _, proposal := range m.ValidProposals {
pd.states[m.EpochID].addVote(proposal, up, voteWeight)
}
for _, proposal := range m.PotentiallyValidProposals {
pd.states[m.EpochID].addVote(proposal, down, voteWeight)
}
// this is used for bit vector calculation
voteList := append(m.ValidProposals, m.PotentiallyValidProposals...)
if uint32(len(voteList)) > pd.config.VotesLimit {
voteList = voteList[:pd.config.VotesLimit]
}
pd.states[m.EpochID].setMinerFirstRoundVote(nodeID, voteList)
return nil
}
// HandleFollowingVotes handles beacon following votes from gossip.
func (pd *ProtocolDriver) HandleFollowingVotes(ctx context.Context, peer p2p.Peer, msg []byte) pubsub.ValidationResult {
receivedTime := time.Now()
if pd.isClosed() || !pd.isInProtocol() {
pd.logger.WithContext(ctx).Debug("beacon protocol shutting down or not running, dropping msg")
return pubsub.ValidationIgnore
}
logger := pd.logger.WithContext(ctx).WithFields(log.String("sender", peer.String()))
logger.Debug("new following votes")
err := pd.handleFollowingVotes(ctx, peer, msg, receivedTime)
switch {
case err == nil:
return pubsub.ValidationAccept
case errors.Is(err, errMalformedMessage):
return pubsub.ValidationReject
default:
return pubsub.ValidationIgnore
}
}
func (pd *ProtocolDriver) handleFollowingVotes(ctx context.Context, peer p2p.Peer, msg []byte, receivedTime time.Time) error {
logger := pd.logger.WithContext(ctx).WithFields(log.String("sender", peer.String()))
var m FollowingVotingMessage
if err := codec.Decode(msg, &m); err != nil {
logger.With().Warning("received malformed following votes", log.Err(err))
return errMalformedMessage
}
currentEpoch := pd.currentEpoch()
if m.EpochID != currentEpoch {
logger.With().Debug("following votes from different epoch",
log.Uint32("current_epoch", uint32(currentEpoch)),
log.Uint32("message_epoch", uint32(m.EpochID)))
return errEpochNotActive
}
latency := receivedTime.Sub(pd.msgTimes.followupVoteSendTime(m.EpochID, m.RoundID))
metrics.ReportMessageLatency(pubsub.BeaconProtocol, pubsub.BeaconFollowingVotesProtocol, latency)
// don't accept votes from future rounds
if !pd.isVoteTimely(&m, receivedTime) {
logger.With().Debug("following votes too early", m.RoundID, log.Time("received_at", receivedTime))
return errUntimelyMessage
}
nodeID, err := pd.verifyFollowingVotes(ctx, m)
if err != nil {
return err
}
logger.Debug("received following voting message, counting its votes")
if err = pd.storeFollowingVotes(m, nodeID); err != nil {
logger.With().Warning("failed to store following votes", log.Err(err))
return err
}
return nil
}
func (pd *ProtocolDriver) verifyFollowingVotes(ctx context.Context, m FollowingVotingMessage) (types.NodeID, error) {
round := m.RoundID
messageBytes, err := codec.Encode(&m.FollowingVotingMessageBody)
if err != nil {
pd.logger.With().Fatal("failed to serialize voting message", log.Err(err))
}
if !pd.edVerifier.Verify(signing.BEACON, m.SmesherID, messageBytes, m.Signature) {
return types.EmptyNodeID, fmt.Errorf("[round %v] verify signature %s: failed", types.FirstRound, m.Signature)
}
logger := pd.logger.WithContext(ctx).WithFields(m.EpochID, round, log.Stringer("smesher", m.SmesherID))
if err := pd.registerVoted(logger, m.EpochID, m.SmesherID, m.RoundID); err != nil {
return types.EmptyNodeID, err
}
return m.SmesherID, nil
}
func (pd *ProtocolDriver) storeFollowingVotes(m FollowingVotingMessage, nodeID types.NodeID) error {
if !pd.isInProtocol() {
pd.logger.Debug("beacon not in protocol, not storing following votes")
return errProtocolNotRunning
}
atx, err := pd.minerAtxHdr(m.EpochID, nodeID)
if err != nil {
return err
}
voteWeight := new(big.Int).SetUint64(atx.GetWeight())
firstRoundVotes, err := pd.getFirstRoundVote(m.EpochID, nodeID)
if err != nil {
return fmt.Errorf("get miner first round votes %v: %w", nodeID.ShortString(), err)
}
thisRoundVotes := decodeVotes(m.VotesBitVector, firstRoundVotes)
return pd.addToVoteMargin(m.EpochID, thisRoundVotes, voteWeight)
}
func (pd *ProtocolDriver) getProposalPhaseFinishedTime(epoch types.EpochID) time.Time {
pd.mu.RLock()
defer pd.mu.RUnlock()
if s, ok := pd.states[epoch]; ok {
return s.proposalPhaseFinishedTime
}
// if this epoch doesn't exist, it is finished. returns something
// always in the past but different from time.Time{}
return time.Time{}.Add(time.Second)
}
func (pd *ProtocolDriver) addToVoteMargin(epoch types.EpochID, thisRoundVotes allVotes, voteWeight *big.Int) error {
pd.mu.Lock()
defer pd.mu.Unlock()
if _, ok := pd.states[epoch]; !ok {
return errEpochNotActive
}
for proposal := range thisRoundVotes.support {
pd.states[epoch].addVote(proposal, up, voteWeight)
}
for proposal := range thisRoundVotes.against {
pd.states[epoch].addVote(proposal, down, voteWeight)
}
return nil
}
func (pd *ProtocolDriver) currentEpoch() types.EpochID {
pd.mu.RLock()
defer pd.mu.RUnlock()
return pd.clock.CurrentLayer().GetEpoch()
}
func (pd *ProtocolDriver) currentRound() types.RoundID {
pd.mu.RLock()
defer pd.mu.RUnlock()
return pd.roundInProgress
}
func (pd *ProtocolDriver) isProposalTimely(p *ProposalMessage, receivedTime time.Time) bool {
pd.mu.RLock()
defer pd.mu.RUnlock()
currentEpoch := pd.clock.CurrentLayer().GetEpoch()
switch p.EpochID {
case currentEpoch:
return true
case currentEpoch + 1:
return receivedTime.After(pd.earliestProposalTime)
}
return false
}
func (pd *ProtocolDriver) isVoteTimely(m *FollowingVotingMessage, receivedTime time.Time) bool {
pd.mu.RLock()
defer pd.mu.RUnlock()
switch m.RoundID {
case pd.roundInProgress:
return true
case pd.roundInProgress + 1:
return receivedTime.After(pd.earliestVoteTime)
}
return false
}
func (pd *ProtocolDriver) registerProposed(logger log.Log, epoch types.EpochID, nodeID types.NodeID) error {
pd.mu.Lock()
defer pd.mu.Unlock()
if _, ok := pd.states[epoch]; !ok {
return errEpochNotActive
}
return pd.states[epoch].registerProposed(logger, nodeID)
}
func (pd *ProtocolDriver) registerVoted(logger log.Log, epoch types.EpochID, nodeID types.NodeID, round types.RoundID) error {
pd.mu.Lock()
defer pd.mu.Unlock()
if _, ok := pd.states[epoch]; !ok {
return errEpochNotActive
}
return pd.states[epoch].registerVoted(logger, nodeID, round)
}