2019-01-23 02:52:39 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/opentracing/opentracing-go"
|
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1"
|
2019-02-23 06:06:20 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
2019-01-23 02:52:39 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Validator interface defines the primary methods of a validator client.
|
|
|
|
type Validator interface {
|
|
|
|
Done()
|
2019-02-20 21:58:23 +00:00
|
|
|
WaitForChainStart(ctx context.Context) error
|
2019-01-23 02:52:39 +00:00
|
|
|
WaitForActivation(ctx context.Context)
|
|
|
|
NextSlot() <-chan uint64
|
2019-01-25 03:26:03 +00:00
|
|
|
UpdateAssignments(ctx context.Context, slot uint64) error
|
2019-01-23 02:52:39 +00:00
|
|
|
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()
|
2019-02-20 21:58:23 +00:00
|
|
|
if err := v.WaitForChainStart(ctx); err != nil {
|
|
|
|
log.Fatalf("Could not determine if beacon chain started: %v", err)
|
|
|
|
}
|
2019-01-23 02:52:39 +00:00
|
|
|
v.WaitForActivation(ctx)
|
2019-02-13 18:27:44 +00:00
|
|
|
if err := v.UpdateAssignments(ctx, params.BeaconConfig().GenesisSlot); err != nil {
|
|
|
|
log.WithField("error", err).Error("Failed to update assignments")
|
|
|
|
}
|
2019-01-30 12:28:53 +00:00
|
|
|
span, ctx := opentracing.StartSpanFromContext(ctx, "processSlot")
|
|
|
|
defer span.Finish()
|
2019-01-23 02:52:39 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
log.Info("Context cancelled, stopping validator")
|
|
|
|
return // Exit if context is cancelled.
|
|
|
|
case slot := <-v.NextSlot():
|
2019-01-25 03:26:03 +00:00
|
|
|
if err := v.UpdateAssignments(ctx, slot); err != nil {
|
|
|
|
log.WithField("error", err).Error("Failed to update assignments")
|
|
|
|
continue
|
|
|
|
}
|
2019-01-23 02:52:39 +00:00
|
|
|
role := v.RoleAt(slot)
|
|
|
|
|
|
|
|
switch role {
|
2019-02-18 22:51:49 +00:00
|
|
|
case pb.ValidatorRole_BOTH:
|
|
|
|
v.ProposeBlock(ctx, slot)
|
|
|
|
v.AttestToBlockHead(ctx, slot)
|
2019-01-23 02:52:39 +00:00
|
|
|
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 :)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|