Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for GetValidatorPerformance #5652

Merged
merged 9 commits into from
Apr 27, 2020
2 changes: 2 additions & 0 deletions beacon-chain/rpc/beacon/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -953,6 +953,8 @@ func (bs *Server) GetValidatorPerformance(
}
currentEpoch := helpers.CurrentEpoch(headState)
if !helpers.IsActiveValidator(val, currentEpoch) {
// Inactive validator; treat it as missing.
missingValidators = append(missingValidators, key)
continue
}
if idx >= uint64(len(validatorSummary)) {
Expand Down
96 changes: 96 additions & 0 deletions beacon-chain/rpc/beacon/validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@ import (
"github.com/prysmaticlabs/go-ssz"
mock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing"
"github.com/prysmaticlabs/prysm/beacon-chain/cache"
"github.com/prysmaticlabs/prysm/beacon-chain/core/epoch/precompute"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
"github.com/prysmaticlabs/prysm/beacon-chain/core/state"
"github.com/prysmaticlabs/prysm/beacon-chain/db"
dbTest "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
"github.com/prysmaticlabs/prysm/beacon-chain/flags"
stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state"
"github.com/prysmaticlabs/prysm/beacon-chain/state/stategen"
pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/featureconfig"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/testutil"
Expand Down Expand Up @@ -1889,6 +1892,99 @@ func TestServer_GetValidatorParticipation_FromArchive_FinalizedEpoch(t *testing.
}
}

func TestGetValidatorPerformance_OK(t *testing.T) {
ctx := context.Background()
epoch := uint64(1)
defaultBal := params.BeaconConfig().MaxEffectiveBalance
extraBal := params.BeaconConfig().MaxEffectiveBalance + params.BeaconConfig().GweiPerEth
state.ValidatorSummary = []*precompute.Validator{
{
// nil to make sure it skips inactive validators.
},
{
CurrentEpochEffectiveBalance: defaultBal,
BeforeEpochTransitionBalance: defaultBal,
AfterEpochTransitionBalance: extraBal,
InclusionSlot: 3,
InclusionDistance: 1,
IsPrevEpochAttester: true,
IsPrevEpochTargetAttester: true,
IsPrevEpochHeadAttester: true,
},
{
CurrentEpochEffectiveBalance: defaultBal,
BeforeEpochTransitionBalance: extraBal,
AfterEpochTransitionBalance: params.BeaconConfig().MaxEffectiveBalance + (2 * params.BeaconConfig().GweiPerEth),
InclusionSlot: 5,
InclusionDistance: 2,
IsPrevEpochAttester: false,
IsPrevEpochTargetAttester: false,
IsPrevEpochHeadAttester: true,
},
}
headState := testutil.NewBeaconState()
if err := headState.SetSlot(helpers.StartSlot(epoch + 1)); err != nil {
t.Fatal(err)
}
balances := []uint64{defaultBal, extraBal, extraBal + params.BeaconConfig().GweiPerEth}
if err := headState.SetBalances(balances); err != nil {
t.Fatal(err)
}
publicKey1 := bytesutil.ToBytes32([]byte{1})
publicKey2 := bytesutil.ToBytes32([]byte{2})
publicKey3 := bytesutil.ToBytes32([]byte{3})
validators := []*ethpb.Validator{
{
PublicKey: publicKey1[:],
ActivationEpoch: 5,
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
},
{
PublicKey: publicKey2[:],
EffectiveBalance: defaultBal,
ActivationEpoch: 0,
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
},
{
PublicKey: publicKey3[:],
EffectiveBalance: defaultBal,
ActivationEpoch: 0,
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
},
}
if err := headState.SetValidators(validators); err != nil {
t.Fatal(err)
}

bs := &Server{
HeadFetcher: &mock.ChainService{
// 10 epochs into the future.
State: headState,
},
}
want := &ethpb.ValidatorPerformanceResponse{
CurrentEffectiveBalances: []uint64{params.BeaconConfig().MaxEffectiveBalance, params.BeaconConfig().MaxEffectiveBalance},
InclusionSlots: []uint64{3, 5},
InclusionDistances: []uint64{1, 2},
CorrectlyVotedSource: []bool{true, false},
CorrectlyVotedTarget: []bool{true, false},
CorrectlyVotedHead: []bool{true, true},
BalancesBeforeEpochTransition: []uint64{defaultBal, extraBal},
BalancesAfterEpochTransition: []uint64{extraBal, extraBal + params.BeaconConfig().GweiPerEth},
MissingValidators: [][]byte{publicKey1[:]},
}

res, err := bs.GetValidatorPerformance(ctx, &ethpb.ValidatorPerformanceRequest{
PublicKeys: [][]byte{publicKey1[:], publicKey2[:], publicKey3[:]},
})
if err != nil {
t.Fatal(err)
}
if !proto.Equal(want, res) {
t.Errorf("Wanted %v\nReceived %v", want, res)
}
}

func BenchmarkListValidatorBalances(b *testing.B) {
b.StopTimer()
db := dbTest.SetupDB(b)
Expand Down