2020-04-29 17:40:33 +00:00
|
|
|
// Package precompute provides gathering of nicely-structured
|
|
|
|
// data important to feed into epoch processing, such as attesting
|
|
|
|
// records and balances, for faster computation.
|
2019-10-17 00:48:26 +00:00
|
|
|
package precompute
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2020-04-13 04:11:09 +00:00
|
|
|
"github.com/pkg/errors"
|
2019-10-17 00:48:26 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
|
2021-03-03 02:26:24 +00:00
|
|
|
iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface"
|
2019-10-19 04:47:54 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
2019-10-17 00:48:26 +00:00
|
|
|
"go.opencensus.io/trace"
|
|
|
|
)
|
|
|
|
|
|
|
|
// New gets called at the beginning of process epoch cycle to return
|
|
|
|
// pre computed instances of validators attesting records and total
|
|
|
|
// balances attested in an epoch.
|
2021-03-08 22:37:33 +00:00
|
|
|
func New(ctx context.Context, state iface.BeaconState) ([]*Validator, *Balance, error) {
|
2019-10-17 00:48:26 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "precomputeEpoch.New")
|
|
|
|
defer span.End()
|
2021-03-08 22:37:33 +00:00
|
|
|
|
2020-05-01 19:43:04 +00:00
|
|
|
pValidators := make([]*Validator, state.NumValidators())
|
|
|
|
pBal := &Balance{}
|
2019-10-17 00:48:26 +00:00
|
|
|
|
|
|
|
currentEpoch := helpers.CurrentEpoch(state)
|
|
|
|
prevEpoch := helpers.PrevEpoch(state)
|
|
|
|
|
2021-03-03 02:26:24 +00:00
|
|
|
if err := state.ReadFromEveryValidator(func(idx int, val iface.ReadOnlyValidator) error {
|
2019-10-17 00:48:26 +00:00
|
|
|
// Was validator withdrawable or slashed
|
2020-06-09 22:40:48 +00:00
|
|
|
withdrawable := prevEpoch+1 >= val.WithdrawableEpoch()
|
2020-05-01 19:43:04 +00:00
|
|
|
pVal := &Validator{
|
2020-01-31 20:57:01 +00:00
|
|
|
IsSlashed: val.Slashed(),
|
2019-10-17 00:48:26 +00:00
|
|
|
IsWithdrawableCurrentEpoch: withdrawable,
|
2020-01-31 20:57:01 +00:00
|
|
|
CurrentEpochEffectiveBalance: val.EffectiveBalance(),
|
2019-10-17 00:48:26 +00:00
|
|
|
}
|
|
|
|
// Was validator active current epoch
|
2020-01-31 20:57:01 +00:00
|
|
|
if helpers.IsActiveValidatorUsingTrie(val, currentEpoch) {
|
2020-05-01 19:43:04 +00:00
|
|
|
pVal.IsActiveCurrentEpoch = true
|
|
|
|
pBal.ActiveCurrentEpoch += val.EffectiveBalance()
|
2019-10-17 00:48:26 +00:00
|
|
|
}
|
|
|
|
// Was validator active previous epoch
|
2020-01-31 20:57:01 +00:00
|
|
|
if helpers.IsActiveValidatorUsingTrie(val, prevEpoch) {
|
2020-05-01 19:43:04 +00:00
|
|
|
pVal.IsActivePrevEpoch = true
|
|
|
|
pBal.ActivePrevEpoch += val.EffectiveBalance()
|
2019-10-17 00:48:26 +00:00
|
|
|
}
|
2019-10-19 04:47:54 +00:00
|
|
|
// Set inclusion slot and inclusion distance to be max, they will be compared and replaced
|
|
|
|
// with the lower values
|
2021-02-16 07:45:34 +00:00
|
|
|
pVal.InclusionSlot = params.BeaconConfig().FarFutureSlot
|
|
|
|
pVal.InclusionDistance = params.BeaconConfig().FarFutureSlot
|
2019-10-19 04:47:54 +00:00
|
|
|
|
2020-05-01 19:43:04 +00:00
|
|
|
pValidators[idx] = pVal
|
2020-01-31 20:57:01 +00:00
|
|
|
return nil
|
2020-04-13 04:11:09 +00:00
|
|
|
}); err != nil {
|
|
|
|
return nil, nil, errors.Wrap(err, "failed to initialize precompute")
|
|
|
|
}
|
2020-05-01 19:43:04 +00:00
|
|
|
return pValidators, pBal, nil
|
2019-10-17 00:48:26 +00:00
|
|
|
}
|