github.com/prysmaticlabs/prysm@v1.4.4/beacon-chain/core/epoch/precompute/new.go (about) 1 // Package precompute provides gathering of nicely-structured 2 // data important to feed into epoch processing, such as attesting 3 // records and balances, for faster computation. 4 package precompute 5 6 import ( 7 "context" 8 9 "github.com/pkg/errors" 10 "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" 11 iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" 12 "github.com/prysmaticlabs/prysm/shared/params" 13 "go.opencensus.io/trace" 14 ) 15 16 // New gets called at the beginning of process epoch cycle to return 17 // pre computed instances of validators attesting records and total 18 // balances attested in an epoch. 19 func New(ctx context.Context, state iface.BeaconState) ([]*Validator, *Balance, error) { 20 ctx, span := trace.StartSpan(ctx, "precomputeEpoch.New") 21 defer span.End() 22 23 pValidators := make([]*Validator, state.NumValidators()) 24 pBal := &Balance{} 25 26 currentEpoch := helpers.CurrentEpoch(state) 27 prevEpoch := helpers.PrevEpoch(state) 28 29 if err := state.ReadFromEveryValidator(func(idx int, val iface.ReadOnlyValidator) error { 30 // Was validator withdrawable or slashed 31 withdrawable := prevEpoch+1 >= val.WithdrawableEpoch() 32 pVal := &Validator{ 33 IsSlashed: val.Slashed(), 34 IsWithdrawableCurrentEpoch: withdrawable, 35 CurrentEpochEffectiveBalance: val.EffectiveBalance(), 36 } 37 // Was validator active current epoch 38 if helpers.IsActiveValidatorUsingTrie(val, currentEpoch) { 39 pVal.IsActiveCurrentEpoch = true 40 pBal.ActiveCurrentEpoch += val.EffectiveBalance() 41 } 42 // Was validator active previous epoch 43 if helpers.IsActiveValidatorUsingTrie(val, prevEpoch) { 44 pVal.IsActivePrevEpoch = true 45 pBal.ActivePrevEpoch += val.EffectiveBalance() 46 } 47 // Set inclusion slot and inclusion distance to be max, they will be compared and replaced 48 // with the lower values 49 pVal.InclusionSlot = params.BeaconConfig().FarFutureSlot 50 pVal.InclusionDistance = params.BeaconConfig().FarFutureSlot 51 52 pValidators[idx] = pVal 53 return nil 54 }); err != nil { 55 return nil, nil, errors.Wrap(err, "failed to initialize precompute") 56 } 57 return pValidators, pBal, nil 58 }