precompute

package
v4.0.0-...-b8b0360 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 21, 2024 License: GPL-3.0 Imports: 16 Imported by: 0

Documentation

Overview

Package precompute provides gathering of nicely-structured data important to feed into epoch processing, such as attesting records and balances, for faster computation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AttestationsDelta

func AttestationsDelta(state state.ReadOnlyBeaconState, pBal *Balance, vp []*Validator) ([]uint64, []uint64, error)

AttestationsDelta computes and returns the rewards and penalties differences for individual validators based on the voting records.

func AttestedCurrentEpoch

func AttestedCurrentEpoch(s state.ReadOnlyBeaconState, a *zondpb.PendingAttestation) (bool, bool, error)

AttestedCurrentEpoch returns true if attestation `a` attested once in current epoch and/or epoch boundary block.

func AttestedPrevEpoch

func AttestedPrevEpoch(s state.ReadOnlyBeaconState, a *zondpb.PendingAttestation) (bool, bool, bool, error)

AttestedPrevEpoch returns true if attestation `a` attested once in previous epoch and epoch boundary block and/or the same head.

func EligibleForRewards

func EligibleForRewards(v *Validator) bool

EligibleForRewards for validator.

Spec code: if is_active_validator(v, previous_epoch) or (v.slashed and previous_epoch + 1 < v.withdrawable_epoch)

func New

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.

func ProcessAttestations

func ProcessAttestations(
	ctx context.Context,
	state state.ReadOnlyBeaconState,
	vp []*Validator,
	pBal *Balance,
) ([]*Validator, *Balance, error)

ProcessAttestations process the attestations in state and update individual validator's pre computes, it also tracks and updates epoch attesting balances.

func ProcessJustificationAndFinalizationPreCompute

func ProcessJustificationAndFinalizationPreCompute(state state.BeaconState, pBal *Balance) (state.BeaconState, error)

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.

Spec pseudocode definition:

def process_justification_and_finalization(state: BeaconState) -> None:
  # Initial FFG checkpoint values have a `0x00` stub for `root`.
  # Skip FFG updates in the first two epochs to avoid corner cases that might result in modifying this stub.
  if get_current_epoch(state) <= GENESIS_EPOCH + 1:
      return
  previous_attestations = get_matching_target_attestations(state, get_previous_epoch(state))
  current_attestations = get_matching_target_attestations(state, get_current_epoch(state))
  total_active_balance = get_total_active_balance(state)
  previous_target_balance = get_attesting_balance(state, previous_attestations)
  current_target_balance = get_attesting_balance(state, current_attestations)
  weigh_justification_and_finalization(state, total_active_balance, previous_target_balance, current_target_balance)

func ProcessRewardsAndPenaltiesPrecompute

func ProcessRewardsAndPenaltiesPrecompute(
	state state.BeaconState,
	pBal *Balance,
	vp []*Validator,
	attRewardsFunc attesterRewardsFunc,
	proRewardsFunc proposerRewardsFunc,
) (state.BeaconState, error)

ProcessRewardsAndPenaltiesPrecompute processes the rewards and penalties of individual validator. This is an optimized version by passing in precomputed validator attesting records and total epoch balances.

func ProcessSlashingsPrecompute

func ProcessSlashingsPrecompute(s state.BeaconState, pBal *Balance) error

ProcessSlashingsPrecompute processes the slashed validators during epoch processing. This is an optimized version by passing in precomputed total epoch balances.

func ProposersDelta

func ProposersDelta(state state.ReadOnlyBeaconState, pBal *Balance, vp []*Validator) ([]uint64, error)

ProposersDelta computes and returns the rewards and penalties differences for individual validators based on the proposer inclusion records.

func SameHead

SameHead returns true if attestation `a` attested to the same block by attestation slot in state.

func SameTarget

SameTarget returns true if attestation `a` attested to the same target block in state.

func UnrealizedCheckpoints

func UnrealizedCheckpoints(st state.BeaconState) (*zondpb.Checkpoint, *zondpb.Checkpoint, error)

UnrealizedCheckpoints returns the justification and finalization checkpoints of the given state as if it was progressed with empty slots until the next epoch. It also returns the total active balance during the epoch.

Types

type Balance

type Balance struct {
	// 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.
	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.
	PrevEpochTargetAttested uint64
	// PrevEpochHeadAttested is the total effective balance of all validators who attested
	// correctly for head block during prev epoch.
	PrevEpochHeadAttested uint64
}

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.

func EnsureBalancesLowerBound

func EnsureBalancesLowerBound(bBal *Balance) *Balance

EnsureBalancesLowerBound ensures all the balances such as active current epoch, active previous epoch and more have EffectiveBalanceIncrement(1 eth) as a lower bound.

func UpdateBalance

func UpdateBalance(vp []*Validator, bBal *Balance, stateVersion int) *Balance

UpdateBalance updates pre computed balance store.

type Validator

type Validator struct {
	// IsSlashed is true if the validator has been slashed.
	IsSlashed bool
	// IsWithdrawableCurrentEpoch is true if the validator can withdraw current epoch.
	IsWithdrawableCurrentEpoch bool
	// IsActiveCurrentEpoch is true if the validator was active current epoch.
	IsActiveCurrentEpoch bool
	// IsActivePrevEpoch is true if the validator was active prev epoch.
	IsActivePrevEpoch bool
	// IsCurrentEpochAttester is true if the validator attested current epoch.
	IsCurrentEpochAttester bool
	// IsCurrentEpochTargetAttester is true if the validator attested current epoch target.
	IsCurrentEpochTargetAttester bool
	// IsPrevEpochAttester is true if the validator attested previous epoch.
	IsPrevEpochAttester bool
	// IsPrevEpochSourceAttester is true if the validator attested to source previous epoch. [Only for Altair]
	IsPrevEpochSourceAttester bool
	// IsPrevEpochTargetAttester is true if the validator attested previous epoch target.
	IsPrevEpochTargetAttester bool
	// IsHeadAttester is true if the validator attested head.
	IsPrevEpochHeadAttester bool

	// CurrentEpochEffectiveBalance is how much effective balance this validator has current epoch.
	CurrentEpochEffectiveBalance uint64
	// InclusionSlot is the slot of when the attestation gets included in the chain.
	InclusionSlot primitives.Slot
	// InclusionDistance is the distance between the assigned slot and this validator's attestation was included in block.
	InclusionDistance primitives.Slot
	// ProposerIndex is the index of proposer at slot where this validator's attestation was included.
	ProposerIndex primitives.ValidatorIndex
	// BeforeEpochTransitionBalance is the validator balance prior to epoch transition.
	BeforeEpochTransitionBalance uint64
	// AfterEpochTransitionBalance is the validator balance after epoch transition.
	AfterEpochTransitionBalance uint64

	// InactivityScore of the validator. [New in Altair]
	InactivityScore uint64
}

Validator stores the pre computation of individual validator's attesting records these records consist of attestation votes, block inclusion record. Pre computing and storing such record is essential for process epoch optimizations.

func UpdateValidator

func UpdateValidator(vp []*Validator, record *Validator, indices []uint64, a *zondpb.PendingAttestation, aSlot primitives.Slot) []*Validator

UpdateValidator updates pre computed validator store.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL