Add test for GetValidatorPerformance (#5652)

* Add test for GetValidatorPerformance

* Fix unneeded changes

* Gaz

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
This commit is contained in:
Ivan Martinez 2020-04-27 14:52:12 -04:00 committed by GitHub
parent e9191e7d6c
commit 8327c9d371
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 100 additions and 0 deletions

View File

@ -75,11 +75,13 @@ go_test(
deps = [
"//beacon-chain/blockchain/testing:go_default_library",
"//beacon-chain/cache:go_default_library",
"//beacon-chain/core/epoch/precompute:go_default_library",
"//beacon-chain/core/feed:go_default_library",
"//beacon-chain/core/feed/block:go_default_library",
"//beacon-chain/core/feed/operation:go_default_library",
"//beacon-chain/core/feed/state:go_default_library",
"//beacon-chain/core/helpers:go_default_library",
"//beacon-chain/core/state:go_default_library",
"//beacon-chain/db:go_default_library",
"//beacon-chain/db/testing:go_default_library",
"//beacon-chain/flags:go_default_library",

View File

@ -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)) {

View File

@ -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"
@ -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)