2019-01-23 02:52:39 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-03-25 15:21:21 +00:00
|
|
|
"time"
|
2019-01-23 02:52:39 +00:00
|
|
|
|
|
|
|
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"
|
2019-02-28 03:55:47 +00:00
|
|
|
"go.opencensus.io/trace"
|
2019-03-18 15:45:28 +00:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2019-01-23 02:52:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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-03-27 03:30:22 +00:00
|
|
|
WaitForActivation(ctx context.Context) error
|
2019-03-28 01:03:25 +00:00
|
|
|
CanonicalHeadSlot(ctx context.Context) (uint64, error)
|
2019-01-23 02:52:39 +00:00
|
|
|
NextSlot() <-chan uint64
|
2019-03-25 15:21:21 +00:00
|
|
|
SlotDeadline(slot uint64) time.Time
|
2019-03-18 15:45:28 +00:00
|
|
|
LogValidatorGainsAndLosses(ctx context.Context, slot uint64) error
|
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-03-27 03:30:22 +00:00
|
|
|
if err := v.WaitForActivation(ctx); err != nil {
|
|
|
|
log.Fatalf("Could not wait for validator activation: %v", err)
|
2019-03-06 00:09:41 +00:00
|
|
|
}
|
2019-03-28 01:03:25 +00:00
|
|
|
headSlot, err := v.CanonicalHeadSlot(ctx)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Could not get current canonical head slot: %v", err)
|
|
|
|
}
|
|
|
|
if err := v.UpdateAssignments(ctx, headSlot); err != nil {
|
|
|
|
handleAssignmentError(err, headSlot)
|
2019-02-13 18:27:44 +00:00
|
|
|
}
|
2019-01-23 02:52:39 +00:00
|
|
|
for {
|
2019-02-28 03:55:47 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "processSlot")
|
|
|
|
defer span.End()
|
|
|
|
|
2019-01-23 02:52:39 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2019-03-18 15:45:28 +00:00
|
|
|
log.Info("Context canceled, stopping validator")
|
|
|
|
return // Exit if context is canceled.
|
2019-01-23 02:52:39 +00:00
|
|
|
case slot := <-v.NextSlot():
|
2019-02-28 03:55:47 +00:00
|
|
|
span.AddAttributes(trace.Int64Attribute("slot", int64(slot)))
|
2019-03-25 15:21:21 +00:00
|
|
|
slotCtx, _ := context.WithDeadline(ctx, v.SlotDeadline(slot))
|
2019-03-18 15:45:28 +00:00
|
|
|
// Report this validator client's rewards and penalties throughout its lifecycle.
|
2019-03-25 15:21:21 +00:00
|
|
|
if err := v.LogValidatorGainsAndLosses(slotCtx, slot); err != nil {
|
2019-03-27 20:58:02 +00:00
|
|
|
log.Errorf("Could not report validator's rewards/penalties for slot %d: %v",
|
|
|
|
slot-params.BeaconConfig().GenesisSlot, err)
|
2019-03-18 15:45:28 +00:00
|
|
|
}
|
2019-02-28 03:55:47 +00:00
|
|
|
|
2019-03-18 15:45:28 +00:00
|
|
|
// Keep trying to update assignments if they are nil or if we are past an
|
|
|
|
// epoch transition in the beacon node's state.
|
2019-03-25 15:21:21 +00:00
|
|
|
if err := v.UpdateAssignments(slotCtx, slot); err != nil {
|
2019-03-18 15:45:28 +00:00
|
|
|
handleAssignmentError(err, slot)
|
2019-01-25 03:26:03 +00:00
|
|
|
continue
|
|
|
|
}
|
2019-01-23 02:52:39 +00:00
|
|
|
role := v.RoleAt(slot)
|
|
|
|
|
|
|
|
switch role {
|
|
|
|
case pb.ValidatorRole_ATTESTER:
|
2019-03-25 15:21:21 +00:00
|
|
|
v.AttestToBlockHead(slotCtx, slot)
|
2019-01-23 02:52:39 +00:00
|
|
|
case pb.ValidatorRole_PROPOSER:
|
2019-03-25 15:21:21 +00:00
|
|
|
v.ProposeBlock(slotCtx, slot)
|
2019-03-26 05:14:03 +00:00
|
|
|
v.AttestToBlockHead(slotCtx, slot)
|
2019-01-23 02:52:39 +00:00
|
|
|
case pb.ValidatorRole_UNKNOWN:
|
|
|
|
log.WithFields(logrus.Fields{
|
2019-02-25 02:09:45 +00:00
|
|
|
"slot": slot - params.BeaconConfig().GenesisSlot,
|
2019-01-23 02:52:39 +00:00
|
|
|
"role": role,
|
2019-03-18 15:45:28 +00:00
|
|
|
}).Info("No active assignment, doing nothing")
|
2019-01-23 02:52:39 +00:00
|
|
|
default:
|
|
|
|
// Do nothing :)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-18 15:45:28 +00:00
|
|
|
|
|
|
|
func handleAssignmentError(err error, slot uint64) {
|
|
|
|
if errCode, ok := status.FromError(err); ok && errCode.Code() == codes.NotFound {
|
|
|
|
log.WithField(
|
2019-03-27 20:58:02 +00:00
|
|
|
"epoch", (slot/params.BeaconConfig().SlotsPerEpoch)-params.BeaconConfig().GenesisEpoch,
|
2019-03-18 15:45:28 +00:00
|
|
|
).Warn("Validator not yet assigned to epoch")
|
|
|
|
} else {
|
|
|
|
log.WithField("error", err).Error("Failed to update assignments")
|
|
|
|
}
|
|
|
|
}
|