github.com/prysmaticlabs/prysm@v1.4.4/validator/client/testutil/mock_validator.go (about) 1 package testutil 2 3 import ( 4 "bytes" 5 "context" 6 "time" 7 8 types "github.com/prysmaticlabs/eth2-types" 9 ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1" 10 "github.com/prysmaticlabs/prysm/shared/timeutils" 11 "github.com/prysmaticlabs/prysm/validator/client/iface" 12 "github.com/prysmaticlabs/prysm/validator/keymanager" 13 ) 14 15 var _ iface.Validator = (*FakeValidator)(nil) 16 17 // FakeValidator for mocking. 18 type FakeValidator struct { 19 DoneCalled bool 20 WaitForWalletInitializationCalled bool 21 SlasherReadyCalled bool 22 NextSlotCalled bool 23 UpdateDutiesCalled bool 24 UpdateProtectionsCalled bool 25 RoleAtCalled bool 26 AttestToBlockHeadCalled bool 27 ProposeBlockCalled bool 28 LogValidatorGainsAndLossesCalled bool 29 SaveProtectionsCalled bool 30 DeleteProtectionCalled bool 31 SlotDeadlineCalled bool 32 HandleKeyReloadCalled bool 33 WaitForChainStartCalled int 34 WaitForSyncCalled int 35 WaitForActivationCalled int 36 CanonicalHeadSlotCalled int 37 ReceiveBlocksCalled int 38 RetryTillSuccess int 39 ProposeBlockArg1 uint64 40 AttestToBlockHeadArg1 uint64 41 RoleAtArg1 uint64 42 UpdateDutiesArg1 uint64 43 NextSlotRet <-chan types.Slot 44 PublicKey string 45 UpdateDutiesRet error 46 RolesAtRet []iface.ValidatorRole 47 Balances map[[48]byte]uint64 48 IndexToPubkeyMap map[uint64][48]byte 49 PubkeyToIndexMap map[[48]byte]uint64 50 PubkeysToStatusesMap map[[48]byte]ethpb.ValidatorStatus 51 Keymanager keymanager.IKeymanager 52 } 53 54 type ctxKey string 55 56 // AllValidatorsAreExitedCtxKey represents the metadata context key used for exits. 57 var AllValidatorsAreExitedCtxKey = ctxKey("exited") 58 59 // Done for mocking. 60 func (fv *FakeValidator) Done() { 61 fv.DoneCalled = true 62 } 63 64 // WaitForWalletInitialization for mocking. 65 func (fv *FakeValidator) WaitForWalletInitialization(_ context.Context) error { 66 fv.WaitForWalletInitializationCalled = true 67 return nil 68 } 69 70 // WaitForChainStart for mocking. 71 func (fv *FakeValidator) WaitForChainStart(_ context.Context) error { 72 fv.WaitForChainStartCalled++ 73 if fv.RetryTillSuccess >= fv.WaitForChainStartCalled { 74 return iface.ErrConnectionIssue 75 } 76 return nil 77 } 78 79 // WaitForActivation for mocking. 80 func (fv *FakeValidator) WaitForActivation(_ context.Context, _ chan [][48]byte) error { 81 fv.WaitForActivationCalled++ 82 if fv.RetryTillSuccess >= fv.WaitForActivationCalled { 83 return iface.ErrConnectionIssue 84 } 85 return nil 86 } 87 88 // WaitForSync for mocking. 89 func (fv *FakeValidator) WaitForSync(_ context.Context) error { 90 fv.WaitForSyncCalled++ 91 if fv.RetryTillSuccess >= fv.WaitForSyncCalled { 92 return iface.ErrConnectionIssue 93 } 94 return nil 95 } 96 97 // SlasherReady for mocking. 98 func (fv *FakeValidator) SlasherReady(_ context.Context) error { 99 fv.SlasherReadyCalled = true 100 return nil 101 } 102 103 // CanonicalHeadSlot for mocking. 104 func (fv *FakeValidator) CanonicalHeadSlot(_ context.Context) (types.Slot, error) { 105 fv.CanonicalHeadSlotCalled++ 106 if fv.RetryTillSuccess > fv.CanonicalHeadSlotCalled { 107 return 0, iface.ErrConnectionIssue 108 } 109 return 0, nil 110 } 111 112 // SlotDeadline for mocking. 113 func (fv *FakeValidator) SlotDeadline(_ types.Slot) time.Time { 114 fv.SlotDeadlineCalled = true 115 return timeutils.Now() 116 } 117 118 // NextSlot for mocking. 119 func (fv *FakeValidator) NextSlot() <-chan types.Slot { 120 fv.NextSlotCalled = true 121 return fv.NextSlotRet 122 } 123 124 // UpdateDuties for mocking. 125 func (fv *FakeValidator) UpdateDuties(_ context.Context, slot types.Slot) error { 126 fv.UpdateDutiesCalled = true 127 fv.UpdateDutiesArg1 = uint64(slot) 128 return fv.UpdateDutiesRet 129 } 130 131 // UpdateProtections for mocking. 132 func (fv *FakeValidator) UpdateProtections(_ context.Context, _ uint64) error { 133 fv.UpdateProtectionsCalled = true 134 return nil 135 } 136 137 // LogValidatorGainsAndLosses for mocking. 138 func (fv *FakeValidator) LogValidatorGainsAndLosses(_ context.Context, _ types.Slot) error { 139 fv.LogValidatorGainsAndLossesCalled = true 140 return nil 141 } 142 143 // ResetAttesterProtectionData for mocking. 144 func (fv *FakeValidator) ResetAttesterProtectionData() { 145 fv.DeleteProtectionCalled = true 146 } 147 148 // RolesAt for mocking. 149 func (fv *FakeValidator) RolesAt(_ context.Context, slot types.Slot) (map[[48]byte][]iface.ValidatorRole, error) { 150 fv.RoleAtCalled = true 151 fv.RoleAtArg1 = uint64(slot) 152 vr := make(map[[48]byte][]iface.ValidatorRole) 153 vr[[48]byte{1}] = fv.RolesAtRet 154 return vr, nil 155 } 156 157 // SubmitAttestation for mocking. 158 func (fv *FakeValidator) SubmitAttestation(_ context.Context, slot types.Slot, _ [48]byte) { 159 fv.AttestToBlockHeadCalled = true 160 fv.AttestToBlockHeadArg1 = uint64(slot) 161 } 162 163 // ProposeBlock for mocking. 164 func (fv *FakeValidator) ProposeBlock(_ context.Context, slot types.Slot, _ [48]byte) { 165 fv.ProposeBlockCalled = true 166 fv.ProposeBlockArg1 = uint64(slot) 167 } 168 169 // SubmitAggregateAndProof for mocking. 170 func (fv *FakeValidator) SubmitAggregateAndProof(_ context.Context, _ types.Slot, _ [48]byte) {} 171 172 // LogAttestationsSubmitted for mocking. 173 func (fv *FakeValidator) LogAttestationsSubmitted() {} 174 175 // LogNextDutyTimeLeft for mocking. 176 func (fv *FakeValidator) LogNextDutyTimeLeft(slot types.Slot) error { 177 return nil 178 } 179 180 // UpdateDomainDataCaches for mocking. 181 func (fv *FakeValidator) UpdateDomainDataCaches(context.Context, types.Slot) {} 182 183 // BalancesByPubkeys for mocking. 184 func (fv *FakeValidator) BalancesByPubkeys(_ context.Context) map[[48]byte]uint64 { 185 return fv.Balances 186 } 187 188 // IndicesToPubkeys for mocking. 189 func (fv *FakeValidator) IndicesToPubkeys(_ context.Context) map[uint64][48]byte { 190 return fv.IndexToPubkeyMap 191 } 192 193 // PubkeysToIndices for mocking. 194 func (fv *FakeValidator) PubkeysToIndices(_ context.Context) map[[48]byte]uint64 { 195 return fv.PubkeyToIndexMap 196 } 197 198 // PubkeysToStatuses for mocking. 199 func (fv *FakeValidator) PubkeysToStatuses(_ context.Context) map[[48]byte]ethpb.ValidatorStatus { 200 return fv.PubkeysToStatusesMap 201 } 202 203 // AllValidatorsAreExited for mocking 204 func (fv *FakeValidator) AllValidatorsAreExited(ctx context.Context) (bool, error) { 205 if ctx.Value(AllValidatorsAreExitedCtxKey) == nil { 206 return false, nil 207 } 208 return ctx.Value(AllValidatorsAreExitedCtxKey).(bool), nil 209 } 210 211 // GetKeymanager for mocking 212 func (fv *FakeValidator) GetKeymanager() keymanager.IKeymanager { 213 return fv.Keymanager 214 } 215 216 // CheckDoppelGanger for mocking 217 func (fv *FakeValidator) CheckDoppelGanger(ctx context.Context) error { 218 return nil 219 } 220 221 // ReceiveBlocks for mocking 222 func (fv *FakeValidator) ReceiveBlocks(ctx context.Context, connectionErrorChannel chan<- error) { 223 fv.ReceiveBlocksCalled++ 224 if fv.RetryTillSuccess > fv.ReceiveBlocksCalled { 225 connectionErrorChannel <- iface.ErrConnectionIssue 226 } 227 } 228 229 // HandleKeyReload for mocking 230 func (fv *FakeValidator) HandleKeyReload(_ context.Context, newKeys [][48]byte) (anyActive bool, err error) { 231 fv.HandleKeyReloadCalled = true 232 for _, key := range newKeys { 233 if bytes.Equal(key[:], ActiveKey[:]) { 234 return true, nil 235 } 236 } 237 return false, nil 238 }