Skip to content

Commit

Permalink
Fix GetValidatorPerformance before/after balances (#6293)
Browse files Browse the repository at this point in the history
* Add helper to prevent zero hashes

* Test

* Fix precompute validators to have before/after balances

* Update tests to use correct values

* Even better test setups

* Gaz

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
  • Loading branch information
terencechain and prylabs-bulldozer[bot] committed Jun 17, 2020
1 parent 523fc62 commit 802bdf5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
1 change: 1 addition & 0 deletions beacon-chain/rpc/beacon/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ 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",
Expand Down
4 changes: 4 additions & 0 deletions beacon-chain/rpc/beacon/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,10 @@ func (bs *Server) GetValidatorPerformance(
if err != nil {
return nil, err
}
headState, err = precompute.ProcessRewardsAndPenaltiesPrecompute(headState, bp, vp)
if err != nil {
return nil, err
}
validatorSummary := vp

responseCap := len(req.Indices) + len(req.PublicKeys)
Expand Down
41 changes: 35 additions & 6 deletions beacon-chain/rpc/beacon/validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ 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/db"
dbTest "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
Expand Down Expand Up @@ -2051,6 +2052,9 @@ func TestGetValidatorPerformance_OK(t *testing.T) {
if err := headState.SetValidators(validators); err != nil {
t.Fatal(err)
}
if err := headState.SetBalances([]uint64{100, 101, 102}); err != nil {
t.Fatal(err)
}
bs := &Server{
HeadFetcher: &mock.ChainService{
State: headState,
Expand All @@ -2067,7 +2071,7 @@ func TestGetValidatorPerformance_OK(t *testing.T) {
CorrectlyVotedSource: []bool{false, false},
CorrectlyVotedTarget: []bool{false, false},
CorrectlyVotedHead: []bool{false, false},
BalancesBeforeEpochTransition: []uint64{0, 0},
BalancesBeforeEpochTransition: []uint64{101, 102},
BalancesAfterEpochTransition: []uint64{0, 0},
MissingValidators: [][]byte{publicKey1[:]},
}
Expand Down Expand Up @@ -2121,7 +2125,6 @@ func TestGetValidatorPerformance_Indices(t *testing.T) {
if err := headState.SetValidators(validators); err != nil {
t.Fatal(err)
}

bs := &Server{
HeadFetcher: &mock.ChainService{
// 10 epochs into the future.
Expand All @@ -2130,6 +2133,19 @@ func TestGetValidatorPerformance_Indices(t *testing.T) {
SyncChecker: &mockSync.Sync{IsSyncing: false},
GenesisTimeFetcher: &mock.ChainService{Genesis: time.Now().Add(time.Duration(-1*int64(headState.Slot()*params.BeaconConfig().SecondsPerSlot)) * time.Second)},
}
c := headState.Copy()
vp, bp, err := precompute.New(ctx, c)
if err != nil {
t.Fatal(err)
}
vp, bp, err = precompute.ProcessAttestations(ctx, c, vp, bp)
if err != nil {
t.Fatal(err)
}
c, err = precompute.ProcessRewardsAndPenaltiesPrecompute(c, bp, vp)
if err != nil {
t.Fatal(err)
}
farFuture := params.BeaconConfig().FarFutureEpoch
want := &ethpb.ValidatorPerformanceResponse{
PublicKeys: [][]byte{publicKey2[:], publicKey3[:]},
Expand All @@ -2139,8 +2155,8 @@ func TestGetValidatorPerformance_Indices(t *testing.T) {
CorrectlyVotedSource: []bool{false, false},
CorrectlyVotedTarget: []bool{false, false},
CorrectlyVotedHead: []bool{false, false},
BalancesBeforeEpochTransition: []uint64{0, 0},
BalancesAfterEpochTransition: []uint64{0, 0},
BalancesBeforeEpochTransition: []uint64{extraBal, extraBal + params.BeaconConfig().GweiPerEth},
BalancesAfterEpochTransition: []uint64{vp[1].AfterEpochTransitionBalance, vp[2].AfterEpochTransitionBalance},
MissingValidators: [][]byte{publicKey1[:]},
}

Expand Down Expand Up @@ -2202,6 +2218,19 @@ func TestGetValidatorPerformance_IndicesPubkeys(t *testing.T) {
SyncChecker: &mockSync.Sync{IsSyncing: false},
GenesisTimeFetcher: &mock.ChainService{Genesis: time.Now().Add(time.Duration(-1*int64(headState.Slot()*params.BeaconConfig().SecondsPerSlot)) * time.Second)},
}
c := headState.Copy()
vp, bp, err := precompute.New(ctx, c)
if err != nil {
t.Fatal(err)
}
vp, bp, err = precompute.ProcessAttestations(ctx, c, vp, bp)
if err != nil {
t.Fatal(err)
}
c, err = precompute.ProcessRewardsAndPenaltiesPrecompute(c, bp, vp)
if err != nil {
t.Fatal(err)
}
farFuture := params.BeaconConfig().FarFutureEpoch
want := &ethpb.ValidatorPerformanceResponse{
PublicKeys: [][]byte{publicKey2[:], publicKey3[:]},
Expand All @@ -2211,8 +2240,8 @@ func TestGetValidatorPerformance_IndicesPubkeys(t *testing.T) {
CorrectlyVotedSource: []bool{false, false},
CorrectlyVotedTarget: []bool{false, false},
CorrectlyVotedHead: []bool{false, false},
BalancesBeforeEpochTransition: []uint64{0, 0},
BalancesAfterEpochTransition: []uint64{0, 0},
BalancesBeforeEpochTransition: []uint64{extraBal, extraBal + params.BeaconConfig().GweiPerEth},
BalancesAfterEpochTransition: []uint64{vp[1].AfterEpochTransitionBalance, vp[2].AfterEpochTransitionBalance},
MissingValidators: [][]byte{publicKey1[:]},
}
// Index 2 and publicKey3 points to the same validator.
Expand Down

0 comments on commit 802bdf5

Please sign in to comment.