Rename Precompute Balance vars to be more descriptive (#5706)

* Rename p *Balance and some Balance vars
* Rename more
* Fix comments
* Fix typo
* Forgot a rename
This commit is contained in:
Ivan Martinez
2020-05-01 15:43:04 -04:00
committed by GitHub
parent b42cc724c5
commit 125d022518
16 changed files with 108 additions and 107 deletions

View File

@@ -119,13 +119,13 @@ func (s *Service) archiveActiveSetChanges(ctx context.Context, headState *state.
// We compute participation metrics by first retrieving the head state and
// matching validator attestations during the epoch.
func (s *Service) archiveParticipation(ctx context.Context, epoch uint64) error {
p := s.participationFetcher.Participation(epoch)
pBal := s.participationFetcher.Participation(epoch)
participation := &ethpb.ValidatorParticipation{}
if p != nil {
if pBal != nil {
participation = &ethpb.ValidatorParticipation{
EligibleEther: p.PrevEpoch,
VotedEther: p.PrevEpochTargetAttesters,
GlobalParticipationRate: float32(p.PrevEpochTargetAttesters) / float32(p.PrevEpoch),
EligibleEther: pBal.ActivePrevEpoch,
VotedEther: pBal.PrevEpochTargetAttested,
GlobalParticipationRate: float32(pBal.PrevEpochTargetAttested) / float32(pBal.ActivePrevEpoch),
}
}
return s.beaconDB.SaveArchivedValidatorParticipation(ctx, epoch, participation)

View File

@@ -456,7 +456,7 @@ func setupService(t *testing.T) (*Service, db.Database) {
cancel: cancel,
stateNotifier: mockChainService.StateNotifier(),
participationFetcher: &mock.ChainService{
Balance: &precompute.Balance{PrevEpoch: totalBalance, PrevEpochTargetAttesters: 1}},
Balance: &precompute.Balance{ActivePrevEpoch: totalBalance, PrevEpochTargetAttested: 1}},
}, beaconDB
}

View File

@@ -179,7 +179,7 @@ func reportEpochMetrics(state *stateTrie.BeaconState) {
currentEth1DataDepositCount.Set(float64(state.Eth1Data().DepositCount))
if precompute.Balances != nil {
totalEligibleBalances.Set(float64(precompute.Balances.PrevEpoch))
totalVotedTargetBalances.Set(float64(precompute.Balances.PrevEpochTargetAttesters))
totalEligibleBalances.Set(float64(precompute.Balances.ActivePrevEpoch))
totalVotedTargetBalances.Set(float64(precompute.Balances.PrevEpochTargetAttested))
}
}

View File

@@ -23,7 +23,7 @@ func ProcessAttestations(
ctx context.Context,
state *stateTrie.BeaconState,
vp []*Validator,
bp *Balance,
pBal *Balance,
) ([]*Validator, *Balance, error) {
ctx, span := trace.StartSpan(ctx, "precomputeEpoch.ProcessAttestations")
defer span.End()
@@ -51,10 +51,10 @@ func ProcessAttestations(
vp = UpdateValidator(vp, v, indices, a, a.Data.Slot)
}
bp = UpdateBalance(vp, bp)
Balances = bp
pBal = UpdateBalance(vp, pBal)
Balances = pBal
return vp, bp, nil
return vp, pBal, nil
}
// AttestedCurrentEpoch returns true if attestation `a` attested once in current epoch and/or epoch boundary block.
@@ -156,25 +156,25 @@ func UpdateValidator(vp []*Validator, record *Validator, indices []uint64, a *pb
}
// UpdateBalance updates pre computed balance store.
func UpdateBalance(vp []*Validator, bp *Balance) *Balance {
func UpdateBalance(vp []*Validator, bBal *Balance) *Balance {
for _, v := range vp {
if !v.IsSlashed {
if v.IsCurrentEpochAttester {
bp.CurrentEpochAttesters += v.CurrentEpochEffectiveBalance
bBal.CurrentEpochAttested += v.CurrentEpochEffectiveBalance
}
if v.IsCurrentEpochTargetAttester {
bp.CurrentEpochTargetAttesters += v.CurrentEpochEffectiveBalance
bBal.CurrentEpochTargetAttested += v.CurrentEpochEffectiveBalance
}
if v.IsPrevEpochAttester {
bp.PrevEpochAttesters += v.CurrentEpochEffectiveBalance
bBal.PrevEpochAttested += v.CurrentEpochEffectiveBalance
}
if v.IsPrevEpochTargetAttester {
bp.PrevEpochTargetAttesters += v.CurrentEpochEffectiveBalance
bBal.PrevEpochTargetAttested += v.CurrentEpochEffectiveBalance
}
if v.IsPrevEpochHeadAttester {
bp.PrevEpochHeadAttesters += v.CurrentEpochEffectiveBalance
bBal.PrevEpochHeadAttested += v.CurrentEpochEffectiveBalance
}
}
}
return bp
return bBal
}

View File

@@ -59,15 +59,15 @@ func TestUpdateBalance(t *testing.T) {
{IsPrevEpochAttester: true, IsPrevEpochHeadAttester: true, CurrentEpochEffectiveBalance: 100},
{IsSlashed: true, IsCurrentEpochAttester: true, CurrentEpochEffectiveBalance: 100},
}
wantedBp := &precompute.Balance{
CurrentEpochAttesters: 200,
CurrentEpochTargetAttesters: 200,
PrevEpochAttesters: 300,
PrevEpochTargetAttesters: 100,
PrevEpochHeadAttesters: 200,
wantedPBal := &precompute.Balance{
CurrentEpochAttested: 200,
CurrentEpochTargetAttested: 200,
PrevEpochAttested: 300,
PrevEpochTargetAttested: 100,
PrevEpochHeadAttested: 200,
}
bp := precompute.UpdateBalance(vp, &precompute.Balance{})
if !reflect.DeepEqual(bp, wantedBp) {
pBal := precompute.UpdateBalance(vp, &precompute.Balance{})
if !reflect.DeepEqual(pBal, wantedPBal) {
t.Error("Incorrect balance calculations")
}
}
@@ -231,12 +231,12 @@ func TestProcessAttestations(t *testing.T) {
t.Fatal(err)
}
vp := make([]*precompute.Validator, validators)
for i := 0; i < len(vp); i++ {
vp[i] = &precompute.Validator{CurrentEpochEffectiveBalance: 100}
pVals := make([]*precompute.Validator, validators)
for i := 0; i < len(pVals); i++ {
pVals[i] = &precompute.Validator{CurrentEpochEffectiveBalance: 100}
}
bp := &precompute.Balance{}
vp, bp, err = precompute.ProcessAttestations(context.Background(), beaconState, vp, bp)
pBal := &precompute.Balance{}
pVals, pBal, err = precompute.ProcessAttestations(context.Background(), beaconState, pVals, pBal)
if err != nil {
t.Fatal(err)
}
@@ -247,7 +247,7 @@ func TestProcessAttestations(t *testing.T) {
}
indices := attestationutil.AttestingIndices(att1.AggregationBits, committee)
for _, i := range indices {
if !vp[i].IsPrevEpochAttester {
if !pVals[i].IsPrevEpochAttester {
t.Error("Not a prev epoch attester")
}
}
@@ -257,10 +257,10 @@ func TestProcessAttestations(t *testing.T) {
}
indices = attestationutil.AttestingIndices(att2.AggregationBits, committee)
for _, i := range indices {
if !vp[i].IsPrevEpochAttester {
if !pVals[i].IsPrevEpochAttester {
t.Error("Not a prev epoch attester")
}
if !vp[i].IsPrevEpochHeadAttester {
if !pVals[i].IsPrevEpochHeadAttester {
t.Error("Not a prev epoch head attester")
}
}

View File

@@ -10,7 +10,7 @@ import (
// ProcessJustificationAndFinalizationPreCompute processes justification and finalization during
// epoch processing. This is where a beacon node can justify and finalize a new epoch.
// Note: this is an optimized version by passing in precomputed total and attesting balances.
func ProcessJustificationAndFinalizationPreCompute(state *stateTrie.BeaconState, p *Balance) (*stateTrie.BeaconState, error) {
func ProcessJustificationAndFinalizationPreCompute(state *stateTrie.BeaconState, pBal *Balance) (*stateTrie.BeaconState, error) {
if state.Slot() <= helpers.StartSlot(2) {
return state, nil
}
@@ -34,7 +34,7 @@ func ProcessJustificationAndFinalizationPreCompute(state *stateTrie.BeaconState,
// We will use that paradigm here for consistency with the godoc spec definition.
// If 2/3 or more of total balance attested in the previous epoch.
if 3*p.PrevEpochTargetAttesters >= 2*p.CurrentEpoch {
if 3*pBal.PrevEpochTargetAttested >= 2*pBal.ActiveCurrentEpoch {
blockRoot, err := helpers.BlockRoot(state, prevEpoch)
if err != nil {
return nil, errors.Wrapf(err, "could not get block root for previous epoch %d", prevEpoch)
@@ -50,7 +50,7 @@ func ProcessJustificationAndFinalizationPreCompute(state *stateTrie.BeaconState,
}
// If 2/3 or more of the total balance attested in the current epoch.
if 3*p.CurrentEpochTargetAttesters >= 2*p.CurrentEpoch {
if 3*pBal.CurrentEpochTargetAttested >= 2*pBal.ActiveCurrentEpoch {
blockRoot, err := helpers.BlockRoot(state, currentEpoch)
if err != nil {
return nil, errors.Wrapf(err, "could not get block root for current epoch %d", prevEpoch)

View File

@@ -40,7 +40,7 @@ func TestProcessJustificationAndFinalizationPreCompute_ConsecutiveEpochs(t *test
t.Fatal(err)
}
attestedBalance := 4 * e * 3 / 2
b := &precompute.Balance{PrevEpochTargetAttesters: attestedBalance}
b := &precompute.Balance{PrevEpochTargetAttested: attestedBalance}
newState, err := precompute.ProcessJustificationAndFinalizationPreCompute(state, b)
if err != nil {
t.Fatal(err)
@@ -95,7 +95,7 @@ func TestProcessJustificationAndFinalizationPreCompute_JustifyCurrentEpoch(t *te
t.Fatal(err)
}
attestedBalance := 4 * e * 3 / 2
b := &precompute.Balance{PrevEpochTargetAttesters: attestedBalance}
b := &precompute.Balance{PrevEpochTargetAttested: attestedBalance}
newState, err := precompute.ProcessJustificationAndFinalizationPreCompute(state, b)
if err != nil {
t.Fatal(err)
@@ -149,7 +149,7 @@ func TestProcessJustificationAndFinalizationPreCompute_JustifyPrevEpoch(t *testi
t.Fatal(err)
}
attestedBalance := 4 * e * 3 / 2
b := &precompute.Balance{PrevEpochTargetAttesters: attestedBalance}
b := &precompute.Balance{PrevEpochTargetAttested: attestedBalance}
newState, err := precompute.ProcessJustificationAndFinalizationPreCompute(state, b)
if err != nil {
t.Fatal(err)

View File

@@ -19,8 +19,8 @@ import (
func New(ctx context.Context, state *stateTrie.BeaconState) ([]*Validator, *Balance, error) {
ctx, span := trace.StartSpan(ctx, "precomputeEpoch.New")
defer span.End()
vp := make([]*Validator, state.NumValidators())
bp := &Balance{}
pValidators := make([]*Validator, state.NumValidators())
pBal := &Balance{}
currentEpoch := helpers.CurrentEpoch(state)
prevEpoch := helpers.PrevEpoch(state)
@@ -28,30 +28,30 @@ func New(ctx context.Context, state *stateTrie.BeaconState) ([]*Validator, *Bala
if err := state.ReadFromEveryValidator(func(idx int, val *stateTrie.ReadOnlyValidator) error {
// Was validator withdrawable or slashed
withdrawable := currentEpoch >= val.WithdrawableEpoch()
p := &Validator{
pVal := &Validator{
IsSlashed: val.Slashed(),
IsWithdrawableCurrentEpoch: withdrawable,
CurrentEpochEffectiveBalance: val.EffectiveBalance(),
}
// Was validator active current epoch
if helpers.IsActiveValidatorUsingTrie(val, currentEpoch) {
p.IsActiveCurrentEpoch = true
bp.CurrentEpoch += val.EffectiveBalance()
pVal.IsActiveCurrentEpoch = true
pBal.ActiveCurrentEpoch += val.EffectiveBalance()
}
// Was validator active previous epoch
if helpers.IsActiveValidatorUsingTrie(val, prevEpoch) {
p.IsActivePrevEpoch = true
bp.PrevEpoch += val.EffectiveBalance()
pVal.IsActivePrevEpoch = true
pBal.ActivePrevEpoch += val.EffectiveBalance()
}
// Set inclusion slot and inclusion distance to be max, they will be compared and replaced
// with the lower values
p.InclusionSlot = params.BeaconConfig().FarFutureEpoch
p.InclusionDistance = params.BeaconConfig().FarFutureEpoch
pVal.InclusionSlot = params.BeaconConfig().FarFutureEpoch
pVal.InclusionDistance = params.BeaconConfig().FarFutureEpoch
vp[idx] = p
pValidators[idx] = pVal
return nil
}); err != nil {
return nil, nil, errors.Wrap(err, "failed to initialize precompute")
}
return vp, bp, nil
return pValidators, pBal, nil
}

View File

@@ -53,8 +53,8 @@ func TestNew(t *testing.T) {
}
wantedBalances := &precompute.Balance{
CurrentEpoch: 100,
PrevEpoch: 200,
ActiveCurrentEpoch: 100,
ActivePrevEpoch: 200,
}
if !reflect.DeepEqual(b, wantedBalances) {
t.Error("Incorrect wanted balance")

View File

@@ -13,7 +13,7 @@ import (
// This is an optimized version by passing in precomputed validator attesting records and and total epoch balances.
func ProcessRewardsAndPenaltiesPrecompute(
state *stateTrie.BeaconState,
bp *Balance,
pBal *Balance,
vp []*Validator,
) (*stateTrie.BeaconState, error) {
// Can't process rewards and penalties in genesis epoch.
@@ -27,11 +27,11 @@ func ProcessRewardsAndPenaltiesPrecompute(
return state, errors.New("precomputed registries not the same length as state registries")
}
attsRewards, attsPenalties, err := attestationDeltas(state, bp, vp)
attsRewards, attsPenalties, err := attestationDeltas(state, pBal, vp)
if err != nil {
return nil, errors.Wrap(err, "could not get attestation delta")
}
proposerRewards, err := proposerDeltaPrecompute(state, bp, vp)
proposerRewards, err := proposerDeltaPrecompute(state, pBal, vp)
if err != nil {
return nil, errors.Wrap(err, "could not get attestation delta")
}
@@ -59,33 +59,33 @@ func ProcessRewardsAndPenaltiesPrecompute(
// This computes the rewards and penalties differences for individual validators based on the
// voting records.
func attestationDeltas(state *stateTrie.BeaconState, bp *Balance, vp []*Validator) ([]uint64, []uint64, error) {
func attestationDeltas(state *stateTrie.BeaconState, pBal *Balance, vp []*Validator) ([]uint64, []uint64, error) {
numOfVals := state.NumValidators()
rewards := make([]uint64, numOfVals)
penalties := make([]uint64, numOfVals)
for i, v := range vp {
rewards[i], penalties[i] = attestationDelta(state, bp, v)
rewards[i], penalties[i] = attestationDelta(state, pBal, v)
}
return rewards, penalties, nil
}
func attestationDelta(state *stateTrie.BeaconState, bp *Balance, v *Validator) (uint64, uint64) {
func attestationDelta(state *stateTrie.BeaconState, pBal *Balance, v *Validator) (uint64, uint64) {
eligible := v.IsActivePrevEpoch || (v.IsSlashed && !v.IsWithdrawableCurrentEpoch)
if !eligible || bp.CurrentEpoch == 0 {
if !eligible || pBal.ActiveCurrentEpoch == 0 {
return 0, 0
}
e := helpers.PrevEpoch(state)
vb := v.CurrentEpochEffectiveBalance
br := vb * params.BeaconConfig().BaseRewardFactor / mathutil.IntegerSquareRoot(bp.CurrentEpoch) / params.BeaconConfig().BaseRewardsPerEpoch
br := vb * params.BeaconConfig().BaseRewardFactor / mathutil.IntegerSquareRoot(pBal.ActiveCurrentEpoch) / params.BeaconConfig().BaseRewardsPerEpoch
r, p := uint64(0), uint64(0)
// Process source reward / penalty
if v.IsPrevEpochAttester && !v.IsSlashed {
inc := params.BeaconConfig().EffectiveBalanceIncrement
rewardNumerator := br * bp.PrevEpochAttesters / inc
r += rewardNumerator / (bp.CurrentEpoch / inc)
rewardNumerator := br * pBal.PrevEpochAttested / inc
r += rewardNumerator / (pBal.ActiveCurrentEpoch / inc)
proposerReward := br / params.BeaconConfig().ProposerRewardQuotient
maxAtteserReward := br - proposerReward
r += maxAtteserReward / v.InclusionDistance
@@ -96,8 +96,8 @@ func attestationDelta(state *stateTrie.BeaconState, bp *Balance, v *Validator) (
// Process target reward / penalty
if v.IsPrevEpochTargetAttester && !v.IsSlashed {
inc := params.BeaconConfig().EffectiveBalanceIncrement
rewardNumerator := br * bp.PrevEpochAttesters / inc
r += rewardNumerator / (bp.CurrentEpoch / inc)
rewardNumerator := br * pBal.PrevEpochAttested / inc
r += rewardNumerator / (pBal.ActiveCurrentEpoch / inc)
} else {
p += br
}
@@ -105,8 +105,8 @@ func attestationDelta(state *stateTrie.BeaconState, bp *Balance, v *Validator) (
// Process head reward / penalty
if v.IsPrevEpochHeadAttester && !v.IsSlashed {
inc := params.BeaconConfig().EffectiveBalanceIncrement
rewardNumerator := br * bp.PrevEpochAttesters / inc
r += rewardNumerator / (bp.CurrentEpoch / inc)
rewardNumerator := br * pBal.PrevEpochAttested / inc
r += rewardNumerator / (pBal.ActiveCurrentEpoch / inc)
} else {
p += br
}
@@ -125,11 +125,11 @@ func attestationDelta(state *stateTrie.BeaconState, bp *Balance, v *Validator) (
// This computes the rewards and penalties differences for individual validators based on the
// proposer inclusion records.
func proposerDeltaPrecompute(state *stateTrie.BeaconState, bp *Balance, vp []*Validator) ([]uint64, error) {
func proposerDeltaPrecompute(state *stateTrie.BeaconState, pBal *Balance, vp []*Validator) ([]uint64, error) {
numofVals := state.NumValidators()
rewards := make([]uint64, numofVals)
totalBalance := bp.CurrentEpoch
totalBalance := pBal.ActiveCurrentEpoch
baseRewardFactor := params.BeaconConfig().BaseRewardFactor
balanceSqrt := mathutil.IntegerSquareRoot(totalBalance)

View File

@@ -179,18 +179,18 @@ func TestAttestationDeltas_ZeroEpoch(t *testing.T) {
t.Fatal(err)
}
vp, bp, err := New(context.Background(), state)
pVals, pBal, err := New(context.Background(), state)
if err != nil {
t.Error(err)
}
vp, bp, err = ProcessAttestations(context.Background(), state, vp, bp)
pVals, pBal, err = ProcessAttestations(context.Background(), state, pVals, pBal)
if err != nil {
t.Fatal(err)
}
bp.CurrentEpoch = 0 // Could cause a divide by zero panic.
pBal.ActiveCurrentEpoch = 0 // Could cause a divide by zero panic.
_, _, err = attestationDeltas(state, bp, vp)
_, _, err = attestationDeltas(state, pBal, pVals)
if err != nil {
t.Fatal(err)
}

View File

@@ -10,7 +10,7 @@ import (
// ProcessSlashingsPrecompute processes the slashed validators during epoch processing.
// This is an optimized version by passing in precomputed total epoch balances.
func ProcessSlashingsPrecompute(state *stateTrie.BeaconState, p *Balance) error {
func ProcessSlashingsPrecompute(state *stateTrie.BeaconState, pBal *Balance) error {
currentEpoch := helpers.CurrentEpoch(state)
exitLength := params.BeaconConfig().EpochsPerSlashingsVector
@@ -20,14 +20,15 @@ func ProcessSlashingsPrecompute(state *stateTrie.BeaconState, p *Balance) error
for _, slashing := range slashings {
totalSlashing += slashing
}
minSlashing := mathutil.Min(totalSlashing*3, p.CurrentEpoch)
minSlashing := mathutil.Min(totalSlashing*3, pBal.ActiveCurrentEpoch)
epochToWithdraw := currentEpoch + exitLength/2
increment := params.BeaconConfig().EffectiveBalanceIncrement
validatorFunc := func(idx int, val *ethpb.Validator) (bool, error) {
correctEpoch := epochToWithdraw == val.WithdrawableEpoch
if val.Slashed && correctEpoch {
penaltyNumerator := val.EffectiveBalance / increment * minSlashing
penalty := penaltyNumerator / p.CurrentEpoch * increment
penalty := penaltyNumerator / pBal.ActiveCurrentEpoch * increment
if err := helpers.DecreaseBalance(state, uint64(idx), penalty); err != nil {
return false, err
}

View File

@@ -21,8 +21,8 @@ func TestProcessSlashingsPrecompute_NotSlashed(t *testing.T) {
if err != nil {
t.Fatal(err)
}
bp := &precompute.Balance{CurrentEpoch: params.BeaconConfig().MaxEffectiveBalance}
if err := precompute.ProcessSlashingsPrecompute(s, bp); err != nil {
pBal := &precompute.Balance{ActiveCurrentEpoch: params.BeaconConfig().MaxEffectiveBalance}
if err := precompute.ProcessSlashingsPrecompute(s, pBal); err != nil {
t.Fatal(err)
}
@@ -110,14 +110,14 @@ func TestProcessSlashingsPrecompute_SlashedLess(t *testing.T) {
}
ab += b
}
bp := &precompute.Balance{CurrentEpoch: ab}
pBal := &precompute.Balance{ActiveCurrentEpoch: ab}
original := proto.Clone(tt.state)
state, err := beaconstate.InitializeFromProto(tt.state)
if err != nil {
t.Fatal(err)
}
if err := precompute.ProcessSlashingsPrecompute(state, bp); err != nil {
if err := precompute.ProcessSlashingsPrecompute(state, pBal); err != nil {
t.Fatal(err)
}

View File

@@ -40,21 +40,21 @@ type Validator struct {
// Balance stores the pre computation of the total participated balances for a given epoch
// Pre computing and storing such record is essential for process epoch optimizations.
type Balance struct {
// CurrentEpoch is the total effective balance of all active validators during current epoch.
CurrentEpoch uint64
// PrevEpoch is the total effective balance of all active validators during prev epoch.
PrevEpoch uint64
// CurrentEpochAttesters is the total effective balance of all validators who attested during current epoch.
CurrentEpochAttesters uint64
// CurrentEpochTargetAttesters is the total effective balance of all validators who attested
// ActiveCurrentEpoch is the total effective balance of all active validators during current epoch.
ActiveCurrentEpoch uint64
// ActivePrevEpoch is the total effective balance of all active validators during prev epoch.
ActivePrevEpoch uint64
// CurrentEpochAttested is the total effective balance of all validators who attested during current epoch.
CurrentEpochAttested uint64
// CurrentEpochTargetAttested is the total effective balance of all validators who attested
// for epoch boundary block during current epoch.
CurrentEpochTargetAttesters uint64
// PrevEpochAttesters is the total effective balance of all validators who attested during prev epoch.
PrevEpochAttesters uint64
// PrevEpochTargetAttesters is the total effective balance of all validators who attested
CurrentEpochTargetAttested uint64
// PrevEpochAttested is the total effective balance of all validators who attested during prev epoch.
PrevEpochAttested uint64
// PrevEpochTargetAttested is the total effective balance of all validators who attested
// for epoch boundary block during prev epoch.
PrevEpochTargetAttesters uint64
// PrevEpochHeadAttesters is the total effective balance of all validators who attested
PrevEpochTargetAttested uint64
// PrevEpochHeadAttested is the total effective balance of all validators who attested
// correctly for head block during prev epoch.
PrevEpochHeadAttesters uint64
PrevEpochHeadAttested uint64
}

View File

@@ -43,7 +43,7 @@ func TestCurrentEpoch_OK(t *testing.T) {
t.Fatal(err)
}
if tt.epoch != CurrentEpoch(state) {
t.Errorf("CurrentEpoch(%d) = %d, wanted: %d", state.Slot(), CurrentEpoch(state), tt.epoch)
t.Errorf("ActiveCurrentEpoch(%d) = %d, wanted: %d", state.Slot(), CurrentEpoch(state), tt.epoch)
}
}
}
@@ -63,7 +63,7 @@ func TestPrevEpoch_OK(t *testing.T) {
t.Fatal(err)
}
if tt.epoch != PrevEpoch(state) {
t.Errorf("PrevEpoch(%d) = %d, wanted: %d", state.Slot(), PrevEpoch(state), tt.epoch)
t.Errorf("ActivePrevEpoch(%d) = %d, wanted: %d", state.Slot(), PrevEpoch(state), tt.epoch)
}
}
}

View File

@@ -744,9 +744,9 @@ func (bs *Server) GetValidatorParticipation(
Epoch: requestedEpoch,
Finalized: requestedEpoch <= headState.FinalizedCheckpointEpoch(),
Participation: &ethpb.ValidatorParticipation{
GlobalParticipationRate: float32(b.PrevEpochTargetAttesters) / float32(b.PrevEpoch),
VotedEther: b.PrevEpochTargetAttesters,
EligibleEther: b.PrevEpoch,
GlobalParticipationRate: float32(b.PrevEpochTargetAttested) / float32(b.ActivePrevEpoch),
VotedEther: b.PrevEpochTargetAttested,
EligibleEther: b.ActivePrevEpoch,
},
}, nil
}
@@ -811,18 +811,18 @@ func (bs *Server) getValidatorParticipationUsingOldArchival(
)
}
p := bs.ParticipationFetcher.Participation(requestedEpoch)
if p == nil {
p = &precompute.Balance{}
pBal := bs.ParticipationFetcher.Participation(requestedEpoch)
if pBal == nil {
pBal = &precompute.Balance{}
}
participation := &ethpb.ValidatorParticipation{
EligibleEther: p.PrevEpoch,
VotedEther: p.PrevEpochTargetAttesters,
EligibleEther: pBal.ActivePrevEpoch,
VotedEther: pBal.PrevEpochTargetAttested,
}
participation.GlobalParticipationRate = float32(0)
// only divide if prevEpoch is non zero
if p.PrevEpoch != 0 {
participation.GlobalParticipationRate = float32(float64(p.PrevEpochTargetAttesters) / float64(p.PrevEpoch))
if pBal.ActivePrevEpoch != 0 {
participation.GlobalParticipationRate = float32(float64(pBal.PrevEpochTargetAttested) / float64(pBal.ActivePrevEpoch))
}
return &ethpb.ValidatorParticipationResponse{