diff --git a/beacon-chain/core/helpers/validators_test.go b/beacon-chain/core/helpers/validators_test.go index d6fa85241..d30f6a9fb 100644 --- a/beacon-chain/core/helpers/validators_test.go +++ b/beacon-chain/core/helpers/validators_test.go @@ -1,6 +1,7 @@ package helpers import ( + "reflect" "testing" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" @@ -248,3 +249,159 @@ func TestDomain_OK(t *testing.T) { } } } + +// Test basic functionality of ActiveValidatorIndices without caching. This test will need to be +// rewritten when releasing some cache flag. +func TestActiveValidatorIndices(t *testing.T) { + farFutureEpoch := params.BeaconConfig().FarFutureEpoch + type args struct { + state *pb.BeaconState + epoch uint64 + } + tests := []struct { + name string + args args + want []uint64 + wantErr bool + }{ + { + name: "all_active_epoch_10", + args: args{ + state: &pb.BeaconState{ + Validators: []*ethpb.Validator{ + ðpb.Validator{ + ActivationEpoch: 0, + ExitEpoch: farFutureEpoch, + }, + ðpb.Validator{ + ActivationEpoch: 0, + ExitEpoch: farFutureEpoch, + }, + ðpb.Validator{ + ActivationEpoch: 0, + ExitEpoch: farFutureEpoch, + }, + }, + }, + epoch: 10, + }, + want: []uint64{0, 1, 2}, + }, + { + name: "some_active_epoch_10", + args: args{ + state: &pb.BeaconState{ + Validators: []*ethpb.Validator{ + ðpb.Validator{ + ActivationEpoch: 0, + ExitEpoch: farFutureEpoch, + }, + ðpb.Validator{ + ActivationEpoch: 0, + ExitEpoch: farFutureEpoch, + }, + ðpb.Validator{ + ActivationEpoch: 0, + ExitEpoch: 1, + }, + }, + }, + epoch: 10, + }, + want: []uint64{0, 1}, + }, + { + name: "some_active_with_recent_new_epoch_10", + args: args{ + state: &pb.BeaconState{ + Validators: []*ethpb.Validator{ + ðpb.Validator{ + ActivationEpoch: 0, + ExitEpoch: farFutureEpoch, + }, + ðpb.Validator{ + ActivationEpoch: 0, + ExitEpoch: farFutureEpoch, + }, + ðpb.Validator{ + ActivationEpoch: 0, + ExitEpoch: 1, + }, + ðpb.Validator{ + ActivationEpoch: 0, + ExitEpoch: farFutureEpoch, + }, + }, + }, + epoch: 10, + }, + want: []uint64{0, 1, 3}, + }, + { + name: "some_active_with_recent_new_epoch_10", + args: args{ + state: &pb.BeaconState{ + Validators: []*ethpb.Validator{ + ðpb.Validator{ + ActivationEpoch: 0, + ExitEpoch: farFutureEpoch, + }, + ðpb.Validator{ + ActivationEpoch: 0, + ExitEpoch: farFutureEpoch, + }, + ðpb.Validator{ + ActivationEpoch: 0, + ExitEpoch: 1, + }, + ðpb.Validator{ + ActivationEpoch: 0, + ExitEpoch: farFutureEpoch, + }, + }, + }, + epoch: 10, + }, + want: []uint64{0, 1, 3}, + }, + { + name: "some_active_with_recent_new_epoch_10", + args: args{ + state: &pb.BeaconState{ + Validators: []*ethpb.Validator{ + ðpb.Validator{ + ActivationEpoch: 0, + ExitEpoch: farFutureEpoch, + }, + ðpb.Validator{ + ActivationEpoch: 0, + ExitEpoch: 1, + }, + ðpb.Validator{ + ActivationEpoch: 0, + ExitEpoch: farFutureEpoch, + }, + ðpb.Validator{ + ActivationEpoch: 0, + ExitEpoch: farFutureEpoch, + }, + }, + }, + epoch: 10, + }, + want: []uint64{0, 2, 3}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := ActiveValidatorIndices(tt.args.state, tt.args.epoch) + if (err != nil) != tt.wantErr { + t.Errorf("ActiveValidatorIndices() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("ActiveValidatorIndices() got = %v, want %v", got, tt.want) + } + }) + } +}