mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-25 04:47:18 +00:00
0732012459
* first version - broken * working proto changes * resolve review remarks * fix goimport issues * fix service issues * first logic version-broken * first running version - no new tests * fix validator client test * add wait group to goroutines * remove unused var in function call * fix review remarks and tests * merge master changes and fix conflicts * gazzele fix * fix prestonvanloon requested changes * merge and some of terenc3t remarks addressed * _,pk bug fix in log * fix account file name suffix and filter not active validator out * merge with master and fix missing parameters * run over all public keys in hasvalidators * add test for error when no all the validators has index in the db and hasvalidators is called * fix runner tests fail due to timing issues * goimports * smaller sleep time in proposer tests * fix UpdateAssignments loging * fix goimports * added && false commented TestUpdateAssignments_DoesNothingWhenNotEpochStartAndAlreadyExistingAssignments * hasvalidators without missing publickeys list * fix some of prestone review remarks * fixes for prestone comments * review changes applied * expect context call in TestWaitForActivation_ValidatorOriginallyExists * changed hasvalidators to return true if one validator exists * fix init problem to getkeys * hasvalidators requiers all validators to be in db * validator attest assignments update * fix ap var name * Change name to hasallvalidators * fix tests * update script, fix any vs all validator calls * fix wait for activation * filter validator * reuse the reply block * fix imports * Remove dup * better lookup of active validators * better filter active vlaidators, still need to fix committee assignment tests * lint * use activated keys * fix for postchainstart * fix logging * move state transitions * hasanyvalidator and hasallvalidators * fix tests with updatechainhead missing * add tests * fix TestCommitteeAssignment_OK * fix test * fix validator tests * fix TestCommitteeAssignment_multipleKeys_OK and TestWaitForActivation_ValidatorOriginallyExists * fix goimports * removed unused param from assignment * change string(pk) to hex.EncodeString(pk) fix change requests * add inactive validator status to assignments * fix logging mess due to multi validator setup * set no assignment to debug level * log assignments every epoch * logging fixes * fixed runtime by using the right assignments * correct activation request * fix the validator panic * correct assignment * fix test fail and waitforactivation * performance log issue fix * fix goimports * add log message with truncated pk for attest * add truncated pk to attest and propose logs * Add comment to script, change 9 to 8 * Update assignment log * Add comment, report number of assignments * Use WithError, add validator as field, merge block proposal log * Update validator_propose.go * fix * use entry.String() * fix fmt
115 lines
3.7 KiB
Go
115 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
|
|
// 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()
|
|
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, _ := 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)
|
|
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")
|
|
}
|
|
}
|