forked from prysmaticlabs/prysm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner.go
67 lines (62 loc) · 1.82 KB
/
runner.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
package client
import (
"context"
"github.com/opentracing/opentracing-go"
pb "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1"
"github.com/sirupsen/logrus"
)
// Validator interface defines the primary methods of a validator client.
type Validator interface {
Done()
WaitForChainStart(ctx context.Context)
WaitForActivation(ctx context.Context)
NextSlot() <-chan uint64
UpdateAssignments(ctx context.Context, slot uint64) error
RoleAt(slot uint64) pb.ValidatorRole
AttestToBlockHead(ctx context.Context, slot uint64)
ProposeBlock(ctx context.Context, slot uint64)
}
// Run the main validator routine. This routine exits if the context is
// cancelled.
//
// Order of operations:
// 1 - Initialize validator data
// 2 - Wait for validator activation
// 3 - Wait for the next slot start
// 4 - Update assignments
// 5 - Determine role at current slot
// 6 - Perform assigned role, if any
func run(ctx context.Context, v Validator) {
defer v.Done()
v.WaitForChainStart(ctx)
v.WaitForActivation(ctx)
span, ctx := opentracing.StartSpanFromContext(ctx, "processSlot")
defer span.Finish()
for {
select {
case <-ctx.Done():
log.Info("Context cancelled, stopping validator")
return // Exit if context is cancelled.
case slot := <-v.NextSlot():
if err := v.UpdateAssignments(ctx, slot); err != nil {
log.WithField("error", err).Error("Failed to update assignments")
continue
}
role := v.RoleAt(slot)
switch role {
case pb.ValidatorRole_ATTESTER:
v.AttestToBlockHead(ctx, slot)
case pb.ValidatorRole_PROPOSER:
v.ProposeBlock(ctx, slot)
case pb.ValidatorRole_UNKNOWN:
// This shouldn't happen normally, so it is considered a warning.
log.WithFields(logrus.Fields{
"slot": slot,
"role": role,
}).Warn("Unknown role, doing nothing")
default:
// Do nothing :)
}
}
}
}