ProcessRewardsAndPenaltiesPrecompute taking in rewards funcs (#8737)

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
This commit is contained in:
terence tsao 2021-04-12 21:55:45 -07:00 committed by GitHub
parent 6aa1297829
commit 8b494fb1bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 15 additions and 10 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -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
}

View File

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