Skip to content

Commit

Permalink
ProcessRewardsAndPenaltiesPrecompute taking in rewards funcs (#8737)
Browse files Browse the repository at this point in the history
Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
  • Loading branch information
terencechain and prylabs-bulldozer[bot] authored Apr 13, 2021
1 parent 6aa1297 commit 8b494fb
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 10 deletions.
13 changes: 9 additions & 4 deletions beacon-chain/core/epoch/precompute/reward_penalty.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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++ {
Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/core/epoch/precompute/reward_penalty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/core/state/transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/rpc/beacon/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions beacon-chain/rpc/beacon/validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 := &ethpb.ValidatorPerformanceResponse{
Expand Down Expand Up @@ -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 := &ethpb.ValidatorPerformanceResponse{
Expand Down

0 comments on commit 8b494fb

Please sign in to comment.