From 535b38395ec8a47447192cefb283394f839f7e61 Mon Sep 17 00:00:00 2001 From: james-prysm <90280386+james-prysm@users.noreply.github.com> Date: Mon, 8 May 2023 09:33:26 -0500 Subject: [PATCH] migrating code from PR#12343 (#12371) --- validator/accounts/testing/mock.go | 2 +- validator/client/iface/validator.go | 2 +- validator/client/runner.go | 4 ++-- validator/client/testutil/mock_validator.go | 2 +- validator/client/validator.go | 12 ++++++++---- validator/client/validator_test.go | 6 +++--- 6 files changed, 16 insertions(+), 12 deletions(-) diff --git a/validator/accounts/testing/mock.go b/validator/accounts/testing/mock.go index 2fc526066..8de63f220 100644 --- a/validator/accounts/testing/mock.go +++ b/validator/accounts/testing/mock.go @@ -192,7 +192,7 @@ func (MockValidator) HasProposerSettings() bool { } // PushProposerSettings for mocking -func (_ MockValidator) PushProposerSettings(_ context.Context, _ keymanager.IKeymanager, _ time.Time) error { +func (_ MockValidator) PushProposerSettings(_ context.Context, _ keymanager.IKeymanager, _ primitives.Slot, _ time.Time) error { panic("implement me") } diff --git a/validator/client/iface/validator.go b/validator/client/iface/validator.go index 8d8c3a689..38b9e6d61 100644 --- a/validator/client/iface/validator.go +++ b/validator/client/iface/validator.go @@ -61,7 +61,7 @@ type Validator interface { ReceiveBlocks(ctx context.Context, connectionErrorChannel chan<- error) HandleKeyReload(ctx context.Context, currentKeys [][fieldparams.BLSPubkeyLength]byte) (bool, error) CheckDoppelGanger(ctx context.Context) error - PushProposerSettings(ctx context.Context, km keymanager.IKeymanager, deadline time.Time) error + PushProposerSettings(ctx context.Context, km keymanager.IKeymanager, slot primitives.Slot, deadline time.Time) error SignValidatorRegistrationRequest(ctx context.Context, signer SigningFunc, newValidatorRegistration *ethpb.ValidatorRegistrationV1) (*ethpb.SignedValidatorRegistrationV1, error) ProposerSettings() *validatorserviceconfig.ProposerSettings SetProposerSettings(*validatorserviceconfig.ProposerSettings) diff --git a/validator/client/runner.go b/validator/client/runner.go index 1805b174c..9fb4b1e7c 100644 --- a/validator/client/runner.go +++ b/validator/client/runner.go @@ -58,7 +58,7 @@ func run(ctx context.Context, v iface.Validator) { log.Infof("Validator client started with provided proposer settings that sets options such as fee recipient"+ " and will periodically update the beacon node and custom builder (if --%s)", flags.EnableBuilderFlag.Name) deadline := time.Now().Add(time.Duration(params.BeaconConfig().SecondsPerSlot) * time.Second) - if err := v.PushProposerSettings(ctx, km, deadline); err != nil { + if err := v.PushProposerSettings(ctx, km, headSlot, deadline); err != nil { if errors.Is(err, ErrBuilderValidatorRegistration) { log.WithError(err).Warn("Push proposer settings error") } else { @@ -119,7 +119,7 @@ func run(ctx context.Context, v iface.Validator) { go func() { //deadline set for end of epoch epochDeadline := v.SlotDeadline(slot + params.BeaconConfig().SlotsPerEpoch - 1) - if err := v.PushProposerSettings(ctx, km, epochDeadline); err != nil { + if err := v.PushProposerSettings(ctx, km, slot, epochDeadline); err != nil { log.WithError(err).Warn("Failed to update proposer settings") } }() diff --git a/validator/client/testutil/mock_validator.go b/validator/client/testutil/mock_validator.go index f12c170cf..4d49d4d92 100644 --- a/validator/client/testutil/mock_validator.go +++ b/validator/client/testutil/mock_validator.go @@ -259,7 +259,7 @@ func (*FakeValidator) HasProposerSettings() bool { } // PushProposerSettings for mocking -func (fv *FakeValidator) PushProposerSettings(ctx context.Context, _ keymanager.IKeymanager, deadline time.Time) error { +func (fv *FakeValidator) PushProposerSettings(ctx context.Context, km keymanager.IKeymanager, slot primitives.Slot, deadline time.Time) error { nctx, cancel := context.WithDeadline(ctx, deadline) ctx = nctx defer cancel() diff --git a/validator/client/validator.go b/validator/client/validator.go index 9c7927a4b..85ff158bf 100644 --- a/validator/client/validator.go +++ b/validator/client/validator.go @@ -984,7 +984,7 @@ func (v *validator) SetProposerSettings(settings *validatorserviceconfig.Propose } // PushProposerSettings calls the prepareBeaconProposer RPC to set the fee recipient and also the register validator API if using a custom builder. -func (v *validator) PushProposerSettings(ctx context.Context, km keymanager.IKeymanager, deadline time.Time) error { +func (v *validator) PushProposerSettings(ctx context.Context, km keymanager.IKeymanager, slot primitives.Slot, deadline time.Time) error { if km == nil { return errors.New("keymanager is nil when calling PrepareBeaconProposer") } @@ -1000,7 +1000,7 @@ func (v *validator) PushProposerSettings(ctx context.Context, km keymanager.IKey log.Info("No imported public keys. Skipping prepare proposer routine") return nil } - filteredKeys, err := v.filterAndCacheActiveKeys(ctx, pubkeys) + filteredKeys, err := v.filterAndCacheActiveKeys(ctx, pubkeys, slot) if err != nil { return err } @@ -1034,7 +1034,7 @@ func (v *validator) PushProposerSettings(ctx context.Context, km keymanager.IKey return nil } -func (v *validator) filterAndCacheActiveKeys(ctx context.Context, pubkeys [][fieldparams.BLSPubkeyLength]byte) ([][fieldparams.BLSPubkeyLength]byte, error) { +func (v *validator) filterAndCacheActiveKeys(ctx context.Context, pubkeys [][fieldparams.BLSPubkeyLength]byte, slot primitives.Slot) ([][fieldparams.BLSPubkeyLength]byte, error) { filteredKeys := make([][fieldparams.BLSPubkeyLength]byte, 0) statusRequestKeys := make([][]byte, 0) for _, k := range pubkeys { @@ -1061,7 +1061,11 @@ func (v *validator) filterAndCacheActiveKeys(ctx context.Context, pubkeys [][fie } for i, status := range resp.Statuses { // skip registration creation if validator is not active status - if status.Status != ethpb.ValidatorStatus_ACTIVE { + nonActive := status.Status != ethpb.ValidatorStatus_ACTIVE + // Handle edge case at the start of the epoch with newly activated validators + currEpoch := primitives.Epoch(slot / params.BeaconConfig().SlotsPerEpoch) + currActivated := status.Status == ethpb.ValidatorStatus_PENDING && currEpoch >= status.ActivationEpoch + if nonActive && !currActivated { log.WithFields(logrus.Fields{ "publickey": hexutil.Encode(resp.PublicKeys[i]), "status": status.Status.String(), diff --git a/validator/client/validator_test.go b/validator/client/validator_test.go index 67922cddb..69f729653 100644 --- a/validator/client/validator_test.go +++ b/validator/client/validator_test.go @@ -1960,7 +1960,7 @@ func TestValidator_PushProposerSettings(t *testing.T) { require.Equal(t, len(signedRegisterValidatorRequests), len(v.signedValidatorRegistrations)) } deadline := time.Now().Add(time.Duration(params.BeaconConfig().SecondsPerSlot) * time.Second) - if err := v.PushProposerSettings(ctx, km, deadline); tt.err != "" { + if err := v.PushProposerSettings(ctx, km, 0, deadline); tt.err != "" { assert.ErrorContains(t, tt.err, err) } if len(tt.logMessages) > 0 { @@ -2082,7 +2082,7 @@ func TestValidator_buildPrepProposerReqs_WithoutDefaultConfig(t *testing.T) { FeeRecipient: feeRecipient2[:], }, } - filteredKeys, err := v.filterAndCacheActiveKeys(ctx, pubkeys) + filteredKeys, err := v.filterAndCacheActiveKeys(ctx, pubkeys, 0) require.NoError(t, err) actual, err := v.buildPrepProposerReqs(ctx, filteredKeys) require.NoError(t, err) @@ -2196,7 +2196,7 @@ func TestValidator_buildPrepProposerReqs_WithDefaultConfig(t *testing.T) { FeeRecipient: defaultFeeRecipient[:], }, } - filteredKeys, err := v.filterAndCacheActiveKeys(ctx, pubkeys) + filteredKeys, err := v.filterAndCacheActiveKeys(ctx, pubkeys, 0) require.NoError(t, err) actual, err := v.buildPrepProposerReqs(ctx, filteredKeys) require.NoError(t, err)