diff --git a/beacon-chain/rpc/beacon/validators.go b/beacon-chain/rpc/beacon/validators.go index 6d8b4412a28..2e679f904f8 100644 --- a/beacon-chain/rpc/beacon/validators.go +++ b/beacon-chain/rpc/beacon/validators.go @@ -492,10 +492,18 @@ func (bs *Server) GetValidatorParticipation( } // Get current slot state for current epoch attestations. - state, err := bs.StateGen.StateBySlot(ctx, currentSlot) + startSlot, err := helpers.StartSlot(requestedEpoch) + if err != nil { + return nil, err + } + // Use the last slot of requested epoch to obtain current and previous epoch attestations. + // This ensures that we don't miss previous attestations when input requested epochs. + startSlot += params.BeaconConfig().SlotsPerEpoch - 1 + state, err := bs.StateGen.StateBySlot(ctx, startSlot) if err != nil { return nil, status.Errorf(codes.Internal, "Could not get state: %v", err) } + v, b, err := precompute.New(ctx, state) if err != nil { return nil, status.Errorf(codes.Internal, "Could not set up pre compute instance: %v", err) @@ -510,34 +518,19 @@ func (bs *Server) GetValidatorParticipation( Finalized: requestedEpoch <= state.FinalizedCheckpointEpoch(), Participation: ðpb.ValidatorParticipation{ // TODO(7130): Remove these three deprecated fields. - CurrentEpochActiveGwei: b.ActiveCurrentEpoch, - CurrentEpochAttestingGwei: b.CurrentEpochAttested, - CurrentEpochTargetAttestingGwei: b.CurrentEpochTargetAttested, + GlobalParticipationRate: float32(b.PrevEpochTargetAttested) / float32(b.ActivePrevEpoch), + VotedEther: b.PrevEpochTargetAttested, + EligibleEther: b.ActivePrevEpoch, + CurrentEpochActiveGwei: b.ActiveCurrentEpoch, + CurrentEpochAttestingGwei: b.CurrentEpochAttested, + CurrentEpochTargetAttestingGwei: b.CurrentEpochTargetAttested, + PreviousEpochActiveGwei: b.ActivePrevEpoch, + PreviousEpochAttestingGwei: b.PrevEpochAttested, + PreviousEpochTargetAttestingGwei: b.PrevEpochTargetAttested, + PreviousEpochHeadAttestingGwei: b.PrevEpochHeadAttested, }, } - // Get head state for previous epoch attestations. - state, err = bs.HeadFetcher.HeadState(ctx) - if err != nil { - return nil, status.Errorf(codes.Internal, "Could not get state: %v", err) - } - v, b, err = precompute.New(ctx, state) - if err != nil { - return nil, status.Errorf(codes.Internal, "Could not set up pre compute instance: %v", err) - } - _, b, err = precompute.ProcessAttestations(ctx, state, v, b) - if err != nil { - return nil, status.Errorf(codes.Internal, "Could not pre compute attestations: %v", err) - } - - p.Participation.GlobalParticipationRate = float32(b.PrevEpochTargetAttested) / float32(b.ActivePrevEpoch) - p.Participation.VotedEther = b.PrevEpochTargetAttested - p.Participation.EligibleEther = b.ActivePrevEpoch - p.Participation.PreviousEpochActiveGwei = b.ActivePrevEpoch - p.Participation.PreviousEpochAttestingGwei = b.PrevEpochAttested - p.Participation.PreviousEpochTargetAttestingGwei = b.PrevEpochTargetAttested - p.Participation.PreviousEpochHeadAttestingGwei = b.PrevEpochHeadAttested - return p, nil } diff --git a/beacon-chain/rpc/beacon/validators_test.go b/beacon-chain/rpc/beacon/validators_test.go index 98b355b3b3e..7c017a82c2a 100644 --- a/beacon-chain/rpc/beacon/validators_test.go +++ b/beacon-chain/rpc/beacon/validators_test.go @@ -29,6 +29,7 @@ import ( "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" "github.com/prysmaticlabs/prysm/shared/testutil/require" + "github.com/prysmaticlabs/prysm/shared/timeutils" ) func TestServer_GetValidatorActiveSetChanges_CannotRequestFutureEpoch(t *testing.T) { @@ -1472,13 +1473,13 @@ func TestServer_GetValidatorParticipation_CurrentAndPrevEpoch(t *testing.T) { AggregationBits: bitfield.NewBitlist(2), }} headState := testutil.NewBeaconState() - require.NoError(t, headState.SetSlot(1)) + require.NoError(t, headState.SetSlot(2*params.BeaconConfig().SlotsPerEpoch-1)) require.NoError(t, headState.SetValidators(validators)) require.NoError(t, headState.SetBalances(balances)) + require.NoError(t, headState.SetCurrentEpochAttestations(atts)) require.NoError(t, headState.SetPreviousEpochAttestations(atts)) b := testutil.NewBeaconBlock() - b.Block.Slot = params.BeaconConfig().SlotsPerEpoch require.NoError(t, db.SaveBlock(ctx, b)) bRoot, err := b.Block.HashTreeRoot() require.NoError(t, db.SaveStateSummary(ctx, &pb.StateSummary{Root: bRoot[:]})) @@ -1493,11 +1494,11 @@ func TestServer_GetValidatorParticipation_CurrentAndPrevEpoch(t *testing.T) { HeadFetcher: m, StateGen: stategen.New(db, sc), GenesisTimeFetcher: &mock.ChainService{ - Genesis: time.Now(), + Genesis: timeutils.Now().Add(time.Duration(-1*int64(params.BeaconConfig().SlotsPerEpoch*params.BeaconConfig().SecondsPerSlot)) * time.Second), }, } - res, err := bs.GetValidatorParticipation(ctx, ðpb.GetValidatorParticipationRequest{QueryFilter: ðpb.GetValidatorParticipationRequest_Epoch{Epoch: 0}}) + res, err := bs.GetValidatorParticipation(ctx, ðpb.GetValidatorParticipationRequest{QueryFilter: ðpb.GetValidatorParticipationRequest_Epoch{Epoch: 1}}) require.NoError(t, err) wanted := ðpb.ValidatorParticipation{