From 8b494fb1bcd64c91d9b6a4694356c5380802e405 Mon Sep 17 00:00:00 2001 From: terence tsao Date: Mon, 12 Apr 2021 21:55:45 -0700 Subject: [PATCH] ProcessRewardsAndPenaltiesPrecompute taking in rewards funcs (#8737) Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com> --- .../core/epoch/precompute/reward_penalty.go | 13 +++++++++---- .../core/epoch/precompute/reward_penalty_test.go | 2 +- .../epoch/spectest/rewards_and_penalties_test.go | 2 +- beacon-chain/core/state/transition.go | 2 +- beacon-chain/rpc/beacon/validators.go | 2 +- beacon-chain/rpc/beacon/validators_test.go | 4 ++-- 6 files changed, 15 insertions(+), 10 deletions(-) diff --git a/beacon-chain/core/epoch/precompute/reward_penalty.go b/beacon-chain/core/epoch/precompute/reward_penalty.go index 74064c72c0c3..f523b3dc1389 100644 --- a/beacon-chain/core/epoch/precompute/reward_penalty.go +++ b/beacon-chain/core/epoch/precompute/reward_penalty.go @@ -9,12 +9,17 @@ import ( "github.com/prysmaticlabs/prysm/shared/params" ) +type attesterRewardsFunc func(iface.ReadOnlyBeaconState, *Balance, []*Validator) ([]uint64, []uint64, error) +type proposerRewardsFunc func(iface.ReadOnlyBeaconState, *Balance, []*Validator) ([]uint64, error) + // ProcessRewardsAndPenaltiesPrecompute processes the rewards and penalties of individual validator. // This is an optimized version by passing in precomputed validator attesting records and and total epoch balances. func ProcessRewardsAndPenaltiesPrecompute( state iface.BeaconState, pBal *Balance, vp []*Validator, + attRewardsFunc attesterRewardsFunc, + proRewardsFunc proposerRewardsFunc, ) (iface.BeaconState, error) { // Can't process rewards and penalties in genesis epoch. if helpers.CurrentEpoch(state) == 0 { @@ -27,13 +32,13 @@ func ProcessRewardsAndPenaltiesPrecompute( return state, errors.New("precomputed registries not the same length as state registries") } - attsRewards, attsPenalties, err := AttestationsDelta(state, pBal, vp) + attsRewards, attsPenalties, err := attRewardsFunc(state, pBal, vp) if err != nil { - return nil, errors.Wrap(err, "could not get attestation delta") + return nil, errors.Wrap(err, "could not get attester attestation delta") } - proposerRewards, err := ProposersDelta(state, pBal, vp) + proposerRewards, err := proRewardsFunc(state, pBal, vp) if err != nil { - return nil, errors.Wrap(err, "could not get attestation delta") + return nil, errors.Wrap(err, "could not get proposer attestation delta") } validatorBals := state.Balances() for i := 0; i < numOfVals; i++ { diff --git a/beacon-chain/core/epoch/precompute/reward_penalty_test.go b/beacon-chain/core/epoch/precompute/reward_penalty_test.go index d5cd8f94954f..f163f64df526 100644 --- a/beacon-chain/core/epoch/precompute/reward_penalty_test.go +++ b/beacon-chain/core/epoch/precompute/reward_penalty_test.go @@ -42,7 +42,7 @@ func TestProcessRewardsAndPenaltiesPrecompute(t *testing.T) { vp, bp, err = ProcessAttestations(context.Background(), beaconState, vp, bp) require.NoError(t, err) - processedState, err := ProcessRewardsAndPenaltiesPrecompute(beaconState, bp, vp) + processedState, err := ProcessRewardsAndPenaltiesPrecompute(beaconState, bp, vp, AttestationsDelta, ProposersDelta) require.NoError(t, err) beaconState, ok := processedState.(*stateV0.BeaconState) require.Equal(t, true, ok) diff --git a/beacon-chain/core/epoch/spectest/rewards_and_penalties_test.go b/beacon-chain/core/epoch/spectest/rewards_and_penalties_test.go index 4ec7a65bb9cd..dd9e1ee6ccac 100644 --- a/beacon-chain/core/epoch/spectest/rewards_and_penalties_test.go +++ b/beacon-chain/core/epoch/spectest/rewards_and_penalties_test.go @@ -34,7 +34,7 @@ func processRewardsAndPenaltiesPrecomputeWrapper(t *testing.T, st iface.BeaconSt vp, bp, err = precompute.ProcessAttestations(ctx, st, vp, bp) require.NoError(t, err) - st, err = precompute.ProcessRewardsAndPenaltiesPrecompute(st, bp, vp) + st, err = precompute.ProcessRewardsAndPenaltiesPrecompute(st, bp, vp, precompute.AttestationsDelta, precompute.ProposersDelta) require.NoError(t, err, "Could not process reward") return st, nil diff --git a/beacon-chain/core/state/transition.go b/beacon-chain/core/state/transition.go index eb3d617aef0e..187e81585831 100644 --- a/beacon-chain/core/state/transition.go +++ b/beacon-chain/core/state/transition.go @@ -407,7 +407,7 @@ func ProcessEpochPrecompute(ctx context.Context, state iface.BeaconState) (iface return nil, errors.Wrap(err, "could not process justification") } - state, err = precompute.ProcessRewardsAndPenaltiesPrecompute(state, bp, vp) + state, err = precompute.ProcessRewardsAndPenaltiesPrecompute(state, bp, vp, precompute.AttestationsDelta, precompute.ProposersDelta) if err != nil { return nil, errors.Wrap(err, "could not process rewards and penalties") } diff --git a/beacon-chain/rpc/beacon/validators.go b/beacon-chain/rpc/beacon/validators.go index 71c4458a4620..0c36fdc07744 100644 --- a/beacon-chain/rpc/beacon/validators.go +++ b/beacon-chain/rpc/beacon/validators.go @@ -668,7 +668,7 @@ func (bs *Server) GetValidatorPerformance( if err != nil { return nil, err } - headState, err = precompute.ProcessRewardsAndPenaltiesPrecompute(headState, bp, vp) + headState, err = precompute.ProcessRewardsAndPenaltiesPrecompute(headState, bp, vp, precompute.AttestationsDelta, precompute.ProposersDelta) if err != nil { return nil, err } diff --git a/beacon-chain/rpc/beacon/validators_test.go b/beacon-chain/rpc/beacon/validators_test.go index f9245c5569ae..a9ed16124de4 100644 --- a/beacon-chain/rpc/beacon/validators_test.go +++ b/beacon-chain/rpc/beacon/validators_test.go @@ -1783,7 +1783,7 @@ func TestGetValidatorPerformance_Indices(t *testing.T) { require.NoError(t, err) vp, bp, err = precompute.ProcessAttestations(ctx, c, vp, bp) require.NoError(t, err) - _, err = precompute.ProcessRewardsAndPenaltiesPrecompute(c, bp, vp) + _, err = precompute.ProcessRewardsAndPenaltiesPrecompute(c, bp, vp, precompute.AttestationsDelta, precompute.ProposersDelta) require.NoError(t, err) farFuture := params.BeaconConfig().FarFutureSlot want := ðpb.ValidatorPerformanceResponse{ @@ -1856,7 +1856,7 @@ func TestGetValidatorPerformance_IndicesPubkeys(t *testing.T) { require.NoError(t, err) vp, bp, err = precompute.ProcessAttestations(ctx, c, vp, bp) require.NoError(t, err) - _, err = precompute.ProcessRewardsAndPenaltiesPrecompute(c, bp, vp) + _, err = precompute.ProcessRewardsAndPenaltiesPrecompute(c, bp, vp, precompute.AttestationsDelta, precompute.ProposersDelta) require.NoError(t, err) farFuture := params.BeaconConfig().FarFutureSlot want := ðpb.ValidatorPerformanceResponse{