prysm-pulse/validator/client/runner.go
frederickalcantara a170c69653 upgrading linter from gometalinter to golangci-lint (#2100)
* upgrading linter from gometalinter to golangci-lint

* fixed golangci-lint linting

* removed linting before_script command

* removed disable-all command

* Fixed golang config file

* fixed golang config file v2

* removed gosec issue rule

* formatting

* fixed travis build to run golangci-lint

* Add install golangci-lint command

* fixing golangci-lint script

* removed https://

* Added golangci-lint cmd script

* added go get for local lint install

* created a before_script

* add install before script

* Added get script

* added go mod download

* removed go mod downloads

* changed

* removed before script

* Added before script go get lint

* added exit zero to see what went wrong

* removed golang run script

* removed before script

* change lint command

* verbose output

* removed verbose

* change linter enable and disable configuration

* Update .golangci.yml

Removed gotype as a linter

* Update .golangci.yml

Added typecheck linter

* Update .golangci.yml

Added fixed lint version

* Update .golangci.yml

Added gotype

* Update .golangci.yml

Added typecheck

* removed env:lint

* Added env lint

* fixing lint upgrade

* Changing travis configuration

* FIxed spelling errors

* disabled typecheck

* Enabled typecheck

* remove binary

* Deleting lib binary

* adding more linters

* fixed constants

* fix spelling

* fixed all lint issues

* Revert "Changing travis configuration"

This reverts commit 334afe9d05e96261b01f275aa3ada20e7f36aac4.

* Merge branch 'master' of https://github.com/prysmaticlabs/prysm into update-linter

* Changed from Infof to Info

* Fixing commits

* fixing commits with linter config

* added install

* Fixing

* fix log statement
2019-04-26 14:24:01 +08:00

116 lines
3.7 KiB
Go

package client
import (
"context"
"time"
pb "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/sirupsen/logrus"
"go.opencensus.io/trace"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// Validator interface defines the primary methods of a validator client.
type Validator interface {
Done()
WaitForChainStart(ctx context.Context) error
WaitForActivation(ctx context.Context) error
CanonicalHeadSlot(ctx context.Context) (uint64, error)
NextSlot() <-chan uint64
SlotDeadline(slot uint64) time.Time
LogValidatorGainsAndLosses(ctx context.Context, slot uint64) error
UpdateAssignments(ctx context.Context, slot uint64) error
RolesAt(slot uint64) map[string]pb.ValidatorRole // validatorIndex -> role
AttestToBlockHead(ctx context.Context, slot uint64, idx string)
ProposeBlock(ctx context.Context, slot uint64, idx string)
}
// Run the main validator routine. This routine exits if the context is
// canceled.
//
// 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()
if err := v.WaitForChainStart(ctx); err != nil {
log.Fatalf("Could not determine if beacon chain started: %v", err)
}
if err := v.WaitForActivation(ctx); err != nil {
log.Fatalf("Could not wait for validator activation: %v", err)
}
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)
}
for {
ctx, span := trace.StartSpan(ctx, "processSlot")
defer span.End()
select {
case <-ctx.Done():
log.Info("Context canceled, stopping validator")
return // Exit if context is canceled.
case slot := <-v.NextSlot():
span.AddAttributes(trace.Int64Attribute("slot", int64(slot)))
slotCtx, cancel := context.WithDeadline(ctx, v.SlotDeadline(slot))
// Report this validator client's rewards and penalties throughout its lifecycle.
if err := v.LogValidatorGainsAndLosses(slotCtx, slot); err != nil {
log.Errorf("Could not report validator's rewards/penalties for slot %d: %v",
slot-params.BeaconConfig().GenesisSlot, err)
}
// Keep trying to update assignments if they are nil or if we are past an
// epoch transition in the beacon node's state.
if err := v.UpdateAssignments(slotCtx, slot); err != nil {
handleAssignmentError(err, slot)
cancel()
continue
}
for id, role := range v.RolesAt(slot) {
go func(role pb.ValidatorRole, id string) {
switch role {
case pb.ValidatorRole_ATTESTER:
v.AttestToBlockHead(slotCtx, slot, id)
case pb.ValidatorRole_PROPOSER:
v.ProposeBlock(slotCtx, slot, id)
v.AttestToBlockHead(slotCtx, slot, id)
case pb.ValidatorRole_UNKNOWN:
pk12Char := id
if len(id) > 12 {
pk12Char = id[:12]
}
log.WithFields(logrus.Fields{
"public_key": pk12Char,
"slot": slot - params.BeaconConfig().GenesisSlot,
"role": role,
}).Debug("No active assignment, doing nothing")
default:
// Do nothing :)
}
}(role, id)
}
}
}
}
func handleAssignmentError(err error, slot uint64) {
if errCode, ok := status.FromError(err); ok && errCode.Code() == codes.NotFound {
log.WithField(
"epoch", (slot/params.BeaconConfig().SlotsPerEpoch)-params.BeaconConfig().GenesisEpoch,
).Warn("Validator not yet assigned to epoch")
} else {
log.WithField("error", err).Error("Failed to update assignments")
}
}