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"
|
2022-11-02 18:16:18 +00:00
|
|
|
"math/big"
|
2019-10-17 00:48:26 +00:00
|
|
|
|
2020-04-13 04:11:09 +00:00
|
|
|
"github.com/pkg/errors"
|
2024-02-15 05:46:47 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v5/beacon-chain/core/helpers"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/beacon-chain/core/time"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/beacon-chain/state"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/config/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-07-23 16:11:21 +00:00
|
|
|
func New(ctx context.Context, s state.BeaconState) ([]*Validator, *Balance, error) {
|
2023-12-15 16:49:27 +00:00
|
|
|
_, span := trace.StartSpan(ctx, "precomputeEpoch.New")
|
2019-10-17 00:48:26 +00:00
|
|
|
defer span.End()
|
2021-03-08 22:37:33 +00:00
|
|
|
|
2021-07-23 16:11:21 +00:00
|
|
|
pValidators := make([]*Validator, s.NumValidators())
|
2022-11-02 18:16:18 +00:00
|
|
|
pBal := NewBalance()
|
2019-10-17 00:48:26 +00:00
|
|
|
|
2021-09-30 19:00:14 +00:00
|
|
|
currentEpoch := time.CurrentEpoch(s)
|
|
|
|
prevEpoch := time.PrevEpoch(s)
|
2019-10-17 00:48:26 +00:00
|
|
|
|
2021-07-23 16:11:21 +00:00
|
|
|
if err := s.ReadFromEveryValidator(func(idx int, val state.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()
|
2022-11-02 18:16:18 +00:00
|
|
|
effectiveBalance := new(big.Int).SetUint64(val.EffectiveBalance())
|
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
|
2022-11-02 18:16:18 +00:00
|
|
|
pBal.ActiveCurrentEpoch.Add(pBal.ActiveCurrentEpoch, 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
|
2022-11-02 18:16:18 +00:00
|
|
|
pBal.ActivePrevEpoch.Add(pBal.ActivePrevEpoch, 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
|
|
|
}
|
2022-11-02 18:16:18 +00:00
|
|
|
|
|
|
|
func NewBalance() *Balance {
|
|
|
|
return &Balance{
|
|
|
|
ActiveCurrentEpoch: big.NewInt(0),
|
|
|
|
ActivePrevEpoch: big.NewInt(0),
|
|
|
|
CurrentEpochAttested: big.NewInt(0),
|
|
|
|
CurrentEpochTargetAttested: big.NewInt(0),
|
|
|
|
PrevEpochAttested: big.NewInt(0),
|
|
|
|
PrevEpochHeadAttested: big.NewInt(0),
|
|
|
|
PrevEpochTargetAttested: big.NewInt(0),
|
|
|
|
}
|
|
|
|
}
|