From 7533e1a14720913241c6356dec5ed956fedaf849 Mon Sep 17 00:00:00 2001 From: Ivan Martinez Date: Mon, 27 Apr 2020 11:50:59 -0400 Subject: [PATCH 1/3] Add test for GetValidatorPerformance --- beacon-chain/rpc/beacon/validators.go | 4 + beacon-chain/rpc/beacon/validators_test.go | 96 ++++++++++++++++++++++ 2 files changed, 100 insertions(+) diff --git a/beacon-chain/rpc/beacon/validators.go b/beacon-chain/rpc/beacon/validators.go index aaf763019c02..e42cf05df7fc 100644 --- a/beacon-chain/rpc/beacon/validators.go +++ b/beacon-chain/rpc/beacon/validators.go @@ -2,6 +2,7 @@ package beacon import ( "context" + "fmt" "sort" "strconv" @@ -822,8 +823,11 @@ 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)) { // Not listed in validator summary yet; treat it as missing. missingValidators = append(missingValidators, key) diff --git a/beacon-chain/rpc/beacon/validators_test.go b/beacon-chain/rpc/beacon/validators_test.go index c515564e9971..d4848ad047a5 100644 --- a/beacon-chain/rpc/beacon/validators_test.go +++ b/beacon-chain/rpc/beacon/validators_test.go @@ -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" @@ -1838,6 +1841,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 := ðpb.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, ðpb.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) From 1296c28ae61ed986ffebed0d50c5711413758120 Mon Sep 17 00:00:00 2001 From: Ivan Martinez Date: Mon, 27 Apr 2020 11:57:53 -0400 Subject: [PATCH 2/3] Fix unneeded changes --- beacon-chain/rpc/beacon/validators.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/beacon-chain/rpc/beacon/validators.go b/beacon-chain/rpc/beacon/validators.go index 51824016daf1..3dbfcf28d8a4 100644 --- a/beacon-chain/rpc/beacon/validators.go +++ b/beacon-chain/rpc/beacon/validators.go @@ -2,7 +2,6 @@ package beacon import ( "context" - "fmt" "sort" "strconv" @@ -958,7 +957,6 @@ func (bs *Server) GetValidatorPerformance( missingValidators = append(missingValidators, key) continue } - if idx >= uint64(len(validatorSummary)) { // Not listed in validator summary yet; treat it as missing. missingValidators = append(missingValidators, key) From 2b8f32d7292771cdba2578d9fa179ce3d416579a Mon Sep 17 00:00:00 2001 From: Ivan Martinez Date: Mon, 27 Apr 2020 12:33:21 -0400 Subject: [PATCH 3/3] Gaz --- beacon-chain/rpc/beacon/BUILD.bazel | 2 ++ 1 file changed, 2 insertions(+) diff --git a/beacon-chain/rpc/beacon/BUILD.bazel b/beacon-chain/rpc/beacon/BUILD.bazel index c1faa77ef12c..387259b17cfa 100644 --- a/beacon-chain/rpc/beacon/BUILD.bazel +++ b/beacon-chain/rpc/beacon/BUILD.bazel @@ -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",