package epoch import ( "reflect" "testing" "github.com/prysmaticlabs/go-bitfield" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1" "github.com/prysmaticlabs/prysm/shared/params" ) func TestComputeValidatorParticipation(t *testing.T) { params.OverrideBeaconConfig(params.MinimalSpecConfig()) epoch := uint64(1) attestedBalance := uint64(1) validatorCount := uint64(100) validators := make([]*ethpb.Validator, validatorCount) balances := make([]uint64, validatorCount) for i := 0; i < len(validators); i++ { validators[i] = ðpb.Validator{ ExitEpoch: params.BeaconConfig().FarFutureEpoch, EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance, } balances[i] = params.BeaconConfig().MaxEffectiveBalance } atts := []*pb.PendingAttestation{{Data: ðpb.AttestationData{Crosslink: ðpb.Crosslink{Shard: 0}, Target: ðpb.Checkpoint{}}}} var crosslinks []*ethpb.Crosslink for i := uint64(0); i < params.BeaconConfig().ShardCount; i++ { crosslinks = append(crosslinks, ðpb.Crosslink{ StartEpoch: 0, DataRoot: []byte{'A'}, }) } s := &pb.BeaconState{ Slot: epoch*params.BeaconConfig().SlotsPerEpoch + 1, Validators: validators, Balances: balances, BlockRoots: make([][]byte, 128), Slashings: []uint64{0, 1e9, 1e9}, RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector), ActiveIndexRoots: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector), CompactCommitteesRoots: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector), CurrentCrosslinks: crosslinks, CurrentEpochAttestations: atts, FinalizedCheckpoint: ðpb.Checkpoint{}, JustificationBits: bitfield.Bitvector4{0x00}, CurrentJustifiedCheckpoint: ðpb.Checkpoint{}, } res, err := ComputeValidatorParticipation(s) if err != nil { t.Fatal(err) } wanted := ðpb.ValidatorParticipation{ VotedEther: attestedBalance, EligibleEther: validatorCount * params.BeaconConfig().MaxEffectiveBalance, GlobalParticipationRate: float32(attestedBalance) / float32(validatorCount*params.BeaconConfig().MaxEffectiveBalance), } if !reflect.DeepEqual(res, wanted) { t.Errorf("Incorrect validator participation, wanted %v received %v", wanted, res) } }