prysm-pulse/validator/client/mock_validator.go
Shay Zluf 4595789ac8
Make validator stable when beacon node goes offline (#8278)
* Make validator stable POC

* fix feedback raul and nishant

* fix wait till first iteration

* fix imports

* retry tests

* fix init

* test retry receive blocks

* remove redundant return statement

* terence feedback

* terence feedback

* remove log

* to check for context after fist call

* remove fatal

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
2021-02-01 16:29:54 +00:00

218 lines
6.3 KiB
Go

package client
import (
"context"
"time"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/shared/timeutils"
"github.com/prysmaticlabs/prysm/validator/keymanager"
)
var _ Validator = (*FakeValidator)(nil)
// FakeValidator for mocking.
type FakeValidator struct {
DoneCalled bool
WaitForWalletInitializationCalled bool
SlasherReadyCalled bool
NextSlotCalled bool
UpdateDutiesCalled bool
UpdateProtectionsCalled bool
RoleAtCalled bool
AttestToBlockHeadCalled bool
ProposeBlockCalled bool
LogValidatorGainsAndLossesCalled bool
SaveProtectionsCalled bool
DeleteProtectionCalled bool
SlotDeadlineCalled bool
WaitForChainStartCalled int
WaitForSyncCalled int
WaitForActivationCalled int
CanonicalHeadSlotCalled int
ReceiveBlocksCalled int
RetryTillSuccess int
ProposeBlockArg1 uint64
AttestToBlockHeadArg1 uint64
RoleAtArg1 uint64
UpdateDutiesArg1 uint64
NextSlotRet <-chan uint64
PublicKey string
UpdateDutiesRet error
RolesAtRet []ValidatorRole
Balances map[[48]byte]uint64
IndexToPubkeyMap map[uint64][48]byte
PubkeyToIndexMap map[[48]byte]uint64
PubkeysToStatusesMap map[[48]byte]ethpb.ValidatorStatus
Keymanager keymanager.IKeymanager
}
type ctxKey string
var allValidatorsAreExitedCtxKey = ctxKey("exited")
// Done for mocking.
func (fv *FakeValidator) Done() {
fv.DoneCalled = true
}
// WaitForWalletInitialization for mocking.
func (fv *FakeValidator) WaitForWalletInitialization(_ context.Context) error {
fv.WaitForWalletInitializationCalled = true
return nil
}
// WaitForChainStart for mocking.
func (fv *FakeValidator) WaitForChainStart(_ context.Context) error {
fv.WaitForChainStartCalled++
if fv.RetryTillSuccess >= fv.WaitForChainStartCalled {
return errConnectionIssue
}
return nil
}
// WaitForActivation for mocking.
func (fv *FakeValidator) WaitForActivation(_ context.Context, _ chan struct{}) error {
fv.WaitForActivationCalled++
if fv.RetryTillSuccess >= fv.WaitForActivationCalled {
return errConnectionIssue
}
return nil
}
// WaitForSync for mocking.
func (fv *FakeValidator) WaitForSync(_ context.Context) error {
fv.WaitForSyncCalled++
if fv.RetryTillSuccess >= fv.WaitForSyncCalled {
return errConnectionIssue
}
return nil
}
// SlasherReady for mocking.
func (fv *FakeValidator) SlasherReady(_ context.Context) error {
fv.SlasherReadyCalled = true
return nil
}
// CanonicalHeadSlot for mocking.
func (fv *FakeValidator) CanonicalHeadSlot(_ context.Context) (uint64, error) {
fv.CanonicalHeadSlotCalled++
if fv.RetryTillSuccess > fv.CanonicalHeadSlotCalled {
return 0, errConnectionIssue
}
return 0, nil
}
// SlotDeadline for mocking.
func (fv *FakeValidator) SlotDeadline(_ uint64) time.Time {
fv.SlotDeadlineCalled = true
return timeutils.Now()
}
// NextSlot for mocking.
func (fv *FakeValidator) NextSlot() <-chan uint64 {
fv.NextSlotCalled = true
return fv.NextSlotRet
}
// UpdateDuties for mocking.
func (fv *FakeValidator) UpdateDuties(_ context.Context, slot uint64) error {
fv.UpdateDutiesCalled = true
fv.UpdateDutiesArg1 = slot
return fv.UpdateDutiesRet
}
// UpdateProtections for mocking.
func (fv *FakeValidator) UpdateProtections(_ context.Context, _ uint64) error {
fv.UpdateProtectionsCalled = true
return nil
}
// LogValidatorGainsAndLosses for mocking.
func (fv *FakeValidator) LogValidatorGainsAndLosses(_ context.Context, _ uint64) error {
fv.LogValidatorGainsAndLossesCalled = true
return nil
}
// ResetAttesterProtectionData for mocking.
func (fv *FakeValidator) ResetAttesterProtectionData() {
fv.DeleteProtectionCalled = true
}
// RolesAt for mocking.
func (fv *FakeValidator) RolesAt(_ context.Context, slot uint64) (map[[48]byte][]ValidatorRole, error) {
fv.RoleAtCalled = true
fv.RoleAtArg1 = slot
vr := make(map[[48]byte][]ValidatorRole)
vr[[48]byte{1}] = fv.RolesAtRet
return vr, nil
}
// SubmitAttestation for mocking.
func (fv *FakeValidator) SubmitAttestation(_ context.Context, slot uint64, _ [48]byte) {
fv.AttestToBlockHeadCalled = true
fv.AttestToBlockHeadArg1 = slot
}
// ProposeBlock for mocking.
func (fv *FakeValidator) ProposeBlock(_ context.Context, slot uint64, _ [48]byte) {
fv.ProposeBlockCalled = true
fv.ProposeBlockArg1 = slot
}
// SubmitAggregateAndProof for mocking.
func (fv *FakeValidator) SubmitAggregateAndProof(_ context.Context, _ uint64, _ [48]byte) {}
// LogAttestationsSubmitted for mocking.
func (fv *FakeValidator) LogAttestationsSubmitted() {}
// LogNextDutyCountDown for mocking.
func (fv *FakeValidator) LogNextDutyTimeLeft(slot uint64) error {
return nil
}
// UpdateDomainDataCaches for mocking.
func (fv *FakeValidator) UpdateDomainDataCaches(context.Context, uint64) {}
// BalancesByPubkeys for mocking.
func (fv *FakeValidator) BalancesByPubkeys(_ context.Context) map[[48]byte]uint64 {
return fv.Balances
}
// IndicesToPubkeys for mocking.
func (fv *FakeValidator) IndicesToPubkeys(_ context.Context) map[uint64][48]byte {
return fv.IndexToPubkeyMap
}
// PubkeysToIndices for mocking.
func (fv *FakeValidator) PubkeysToIndices(_ context.Context) map[[48]byte]uint64 {
return fv.PubkeyToIndexMap
}
// PubkeysToStatuses for mocking.
func (fv *FakeValidator) PubkeysToStatuses(_ context.Context) map[[48]byte]ethpb.ValidatorStatus {
return fv.PubkeysToStatusesMap
}
// AllValidatorsAreExited for mocking
func (fv *FakeValidator) AllValidatorsAreExited(ctx context.Context) (bool, error) {
if ctx.Value(allValidatorsAreExitedCtxKey) == nil {
return false, nil
}
return ctx.Value(allValidatorsAreExitedCtxKey).(bool), nil
}
// GetKeymanager for mocking
func (fv *FakeValidator) GetKeymanager() keymanager.IKeymanager {
return fv.Keymanager
}
// ReceiveBlocks for mocking
func (fv *FakeValidator) ReceiveBlocks(ctx context.Context, connectionErrorChannel chan error) {
fv.ReceiveBlocksCalled++
if fv.RetryTillSuccess > fv.ReceiveBlocksCalled {
connectionErrorChannel <- errConnectionIssue
}
}