Skip to content

Commit

Permalink
Use requested epoch for GetValidatorParticipation (#7768)
Browse files Browse the repository at this point in the history
* Ensure request epoch is used

* Update test

* Comment

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
  • Loading branch information
3 people committed Nov 11, 2020
1 parent 3c5bf9b commit 1a05fca
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 30 deletions.
45 changes: 19 additions & 26 deletions beacon-chain/rpc/beacon/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -510,34 +518,19 @@ func (bs *Server) GetValidatorParticipation(
Finalized: requestedEpoch <= state.FinalizedCheckpointEpoch(),
Participation: &ethpb.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
}

Expand Down
9 changes: 5 additions & 4 deletions beacon-chain/rpc/beacon/validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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[:]}))
Expand All @@ -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, &ethpb.GetValidatorParticipationRequest{QueryFilter: &ethpb.GetValidatorParticipationRequest_Epoch{Epoch: 0}})
res, err := bs.GetValidatorParticipation(ctx, &ethpb.GetValidatorParticipationRequest{QueryFilter: &ethpb.GetValidatorParticipationRequest_Epoch{Epoch: 1}})
require.NoError(t, err)

wanted := &ethpb.ValidatorParticipation{
Expand Down

0 comments on commit 1a05fca

Please sign in to comment.