2021-08-24 16:07:11 +00:00
|
|
|
package altair
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
e "github.com/prysmaticlabs/prysm/beacon-chain/core/epoch"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/epoch/precompute"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state"
|
2021-09-21 19:59:25 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/config/params"
|
2021-12-08 19:02:53 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/runtime/version"
|
2021-08-24 16:07:11 +00:00
|
|
|
"go.opencensus.io/trace"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ProcessEpoch describes the per epoch operations that are performed on the beacon state.
|
|
|
|
// It's optimized by pre computing validator attested info and epoch total/attested balances upfront.
|
|
|
|
//
|
|
|
|
// Spec code:
|
|
|
|
// def process_epoch(state: BeaconState) -> None:
|
|
|
|
// process_justification_and_finalization(state) # [Modified in Altair]
|
|
|
|
// process_inactivity_updates(state) # [New in Altair]
|
|
|
|
// process_rewards_and_penalties(state) # [Modified in Altair]
|
|
|
|
// process_registry_updates(state)
|
|
|
|
// process_slashings(state) # [Modified in Altair]
|
|
|
|
// process_eth1_data_reset(state)
|
|
|
|
// process_effective_balance_updates(state)
|
|
|
|
// process_slashings_reset(state)
|
|
|
|
// process_randao_mixes_reset(state)
|
|
|
|
// process_historical_roots_update(state)
|
|
|
|
// process_participation_flag_updates(state) # [New in Altair]
|
|
|
|
// process_sync_committee_updates(state) # [New in Altair]
|
2021-12-08 19:02:53 +00:00
|
|
|
func ProcessEpoch(ctx context.Context, state state.BeaconState) (state.BeaconStateAltair, error) {
|
2021-08-24 16:07:11 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "altair.ProcessEpoch")
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
if state == nil || state.IsNil() {
|
|
|
|
return nil, errors.New("nil state")
|
|
|
|
}
|
2021-09-22 15:07:05 +00:00
|
|
|
vp, bp, err := InitializePrecomputeValidators(ctx, state)
|
2021-08-24 16:07:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// New in Altair.
|
|
|
|
vp, bp, err = ProcessEpochParticipation(ctx, state, bp, vp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
state, err = precompute.ProcessJustificationAndFinalizationPreCompute(state, bp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not process justification")
|
|
|
|
}
|
|
|
|
|
|
|
|
// New in Altair.
|
|
|
|
state, vp, err = ProcessInactivityScores(ctx, state, vp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not process inactivity updates")
|
|
|
|
}
|
|
|
|
|
|
|
|
// New in Altair.
|
|
|
|
state, err = ProcessRewardsAndPenaltiesPrecompute(state, bp, vp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not process rewards and penalties")
|
|
|
|
}
|
|
|
|
|
2021-09-26 15:27:57 +00:00
|
|
|
state, err = e.ProcessRegistryUpdates(ctx, state)
|
2021-08-24 16:07:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not process registry updates")
|
|
|
|
}
|
|
|
|
|
2021-12-08 19:02:53 +00:00
|
|
|
// Modified in Altair and Merge.
|
|
|
|
cfg := params.BeaconConfig()
|
|
|
|
switch state.Version() {
|
|
|
|
case version.Altair:
|
|
|
|
state, err = e.ProcessSlashings(state, cfg.ProportionalSlashingMultiplierAltair)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
case version.Merge:
|
|
|
|
state, err = e.ProcessSlashings(state, cfg.ProportionalSlashingMultiplierMerge)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return nil, errors.Errorf("invalid state type version: %T", state.Version())
|
2021-08-24 16:07:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
state, err = e.ProcessEth1DataReset(state)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
state, err = e.ProcessEffectiveBalanceUpdates(state)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
state, err = e.ProcessSlashingsReset(state)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
state, err = e.ProcessRandaoMixesReset(state)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
state, err = e.ProcessHistoricalRootsUpdate(state)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// New in Altair.
|
|
|
|
state, err = ProcessParticipationFlagUpdates(state)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// New in Altair.
|
|
|
|
state, err = ProcessSyncCommitteeUpdates(ctx, state)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return state, nil
|
|
|
|
}
|