prysm-pulse/validator/client/runner_test.go
shayzluf 0732012459 Validator-multiple key (#2069)
* 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
2019-04-18 12:23:38 -05:00

187 lines
4.3 KiB
Go

package client
import (
"context"
"errors"
"testing"
"time"
pb "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1"
"github.com/prysmaticlabs/prysm/shared/testutil"
logTest "github.com/sirupsen/logrus/hooks/test"
)
func cancelledContext() context.Context {
ctx, cancel := context.WithCancel(context.Background())
cancel()
return ctx
}
func TestCancelledContext_CleansUpValidator(t *testing.T) {
v := &fakeValidator{}
run(cancelledContext(), v)
if !v.DoneCalled {
t.Error("Expected Done() to be called")
}
}
func TestCancelledContext_WaitsForChainStart(t *testing.T) {
v := &fakeValidator{}
run(cancelledContext(), v)
if !v.WaitForChainStartCalled {
t.Error("Expected WaitForChainStart() to be called")
}
}
func TestCancelledContext_WaitsForActivation(t *testing.T) {
v := &fakeValidator{}
run(cancelledContext(), v)
if !v.WaitForActivationCalled {
t.Error("Expected WaitForActivation() to be called")
}
}
func TestUpdateAssignments_NextSlot(t *testing.T) {
v := &fakeValidator{}
ctx, cancel := context.WithCancel(context.Background())
slot := uint64(55)
ticker := make(chan uint64)
v.NextSlotRet = ticker
go func() {
ticker <- slot
cancel()
}()
run(ctx, v)
if !v.UpdateAssignmentsCalled {
t.Fatalf("Expected UpdateAssignments(%d) to be called", slot)
}
if v.UpdateAssignmentsArg1 != slot {
t.Errorf("UpdateAssignments was called with wrong argument. Want=%d, got=%d", slot, v.UpdateAssignmentsArg1)
}
}
func TestUpdateAssignments_HandlesError(t *testing.T) {
hook := logTest.NewGlobal()
v := &fakeValidator{}
ctx, cancel := context.WithCancel(context.Background())
slot := uint64(55)
ticker := make(chan uint64)
v.NextSlotRet = ticker
go func() {
ticker <- slot
cancel()
}()
v.UpdateAssignmentsRet = errors.New("bad")
run(ctx, v)
testutil.AssertLogsContain(t, hook, "Failed to update assignments")
}
func TestRoleAt_NextSlot(t *testing.T) {
v := &fakeValidator{}
ctx, cancel := context.WithCancel(context.Background())
slot := uint64(55)
ticker := make(chan uint64)
v.NextSlotRet = ticker
go func() {
ticker <- slot
cancel()
}()
run(ctx, v)
if !v.RoleAtCalled {
t.Fatalf("Expected RoleAt(%d) to be called", slot)
}
if v.RoleAtArg1 != slot {
t.Errorf("RoleAt called with the wrong arg. Want=%d, got=%d", slot, v.RoleAtArg1)
}
}
func TestAttests_NextSlot(t *testing.T) {
v := &fakeValidator{}
ctx, cancel := context.WithCancel(context.Background())
slot := uint64(55)
ticker := make(chan uint64)
v.NextSlotRet = ticker
v.RoleAtRet = pb.ValidatorRole_ATTESTER
go func() {
ticker <- slot
cancel()
}()
timer := time.NewTimer(time.Duration(200 * time.Millisecond))
run(ctx, v)
<-timer.C
if !v.AttestToBlockHeadCalled {
t.Fatalf("AttestToBlockHead(%d) was not called", slot)
}
if v.AttestToBlockHeadArg1 != slot {
t.Errorf("AttestToBlockHead was called with wrong arg. Want=%d, got=%d", slot, v.AttestToBlockHeadArg1)
}
}
func TestProposes_NextSlot(t *testing.T) {
v := &fakeValidator{}
ctx, cancel := context.WithCancel(context.Background())
slot := uint64(55)
ticker := make(chan uint64)
v.NextSlotRet = ticker
v.RoleAtRet = pb.ValidatorRole_PROPOSER
go func() {
ticker <- slot
cancel()
}()
timer := time.NewTimer(time.Duration(200 * time.Millisecond))
run(ctx, v)
<-timer.C
if !v.ProposeBlockCalled {
t.Fatalf("ProposeBlock(%d) was not called", slot)
}
if v.ProposeBlockArg1 != slot {
t.Errorf("ProposeBlock was called with wrong arg. Want=%d, got=%d", slot, v.AttestToBlockHeadArg1)
}
}
func TestBothProposesAndAttests_NextSlot(t *testing.T) {
v := &fakeValidator{}
ctx, cancel := context.WithCancel(context.Background())
slot := uint64(55)
ticker := make(chan uint64)
v.NextSlotRet = ticker
v.RoleAtRet = pb.ValidatorRole_PROPOSER
go func() {
ticker <- slot
cancel()
}()
timer := time.NewTimer(time.Duration(200 * time.Millisecond))
run(ctx, v)
<-timer.C
if !v.AttestToBlockHeadCalled {
t.Fatalf("AttestToBlockHead(%d) was not called", slot)
}
if v.AttestToBlockHeadArg1 != slot {
t.Errorf("AttestToBlockHead was called with wrong arg. Want=%d, got=%d", slot, v.AttestToBlockHeadArg1)
}
if !v.ProposeBlockCalled {
t.Fatalf("ProposeBlock(%d) was not called", slot)
}
if v.ProposeBlockArg1 != slot {
t.Errorf("ProposeBlock was called with wrong arg. Want=%d, got=%d", slot, v.AttestToBlockHeadArg1)
}
}