Compare commits

...

2 Commits

Author SHA1 Message Date
nisdas
c18e22e861 Merge branch 'develop' of https://github.com/prysmaticlabs/geth-sharding into moveCoreMethods 2022-06-06 16:42:14 +08:00
nisdas
6f8ff002f3 change it 2022-06-01 08:58:01 +08:00
40 changed files with 330 additions and 184 deletions

View File

@@ -263,8 +263,8 @@ func reportEpochMetrics(ctx context.Context, postState, headState state.BeaconSt
currentEth1DataDepositCount.Set(float64(postState.Eth1Data().DepositCount))
processedDepositsCount.Set(float64(postState.Eth1DepositIndex() + 1))
var b *precompute.Balance
var v []*precompute.Validator
var b *types.Balance
var v []*types.Validator
var err error
switch headState.Version() {
case version.Phase0:

View File

@@ -11,7 +11,6 @@ go_library(
],
deps = [
"//async/event:go_default_library",
"//beacon-chain/core/epoch/precompute:go_default_library",
"//beacon-chain/core/feed:go_default_library",
"//beacon-chain/core/feed/block:go_default_library",
"//beacon-chain/core/feed/operation:go_default_library",

View File

@@ -10,7 +10,6 @@ import (
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/async/event"
"github.com/prysmaticlabs/prysm/beacon-chain/core/epoch/precompute"
"github.com/prysmaticlabs/prysm/beacon-chain/core/feed"
blockfeed "github.com/prysmaticlabs/prysm/beacon-chain/core/feed/block"
opfeed "github.com/prysmaticlabs/prysm/beacon-chain/core/feed/operation"
@@ -40,7 +39,7 @@ type ChainService struct {
CurrentJustifiedCheckPoint *ethpb.Checkpoint
PreviousJustifiedCheckPoint *ethpb.Checkpoint
Slot *types.Slot // Pointer because 0 is a useful value, so checking against it can be incorrect.
Balance *precompute.Balance
Balance *types.Balance
CanonicalRoots map[[32]byte]bool
Fork *ethpb.Fork
ETH1Data *ethpb.Eth1Data
@@ -338,7 +337,7 @@ func (s *ChainService) CurrentSlot() types.Slot {
}
// Participation mocks the same method in the chain service.
func (s *ChainService) Participation(_ uint64) *precompute.Balance {
func (s *ChainService) Participation(_ uint64) *types.Balance {
return s.Balance
}

View File

@@ -66,7 +66,6 @@ go_test(
embed = [":go_default_library"],
deps = [
"//beacon-chain/core/epoch:go_default_library",
"//beacon-chain/core/epoch/precompute:go_default_library",
"//beacon-chain/core/helpers:go_default_library",
"//beacon-chain/core/signing:go_default_library",
"//beacon-chain/core/time:go_default_library",

View File

@@ -9,16 +9,17 @@ import (
"github.com/prysmaticlabs/prysm/beacon-chain/core/time"
"github.com/prysmaticlabs/prysm/beacon-chain/state"
"github.com/prysmaticlabs/prysm/config/params"
types "github.com/prysmaticlabs/prysm/consensus-types/primitives"
"github.com/prysmaticlabs/prysm/math"
"go.opencensus.io/trace"
)
// InitializePrecomputeValidators precomputes individual validator for its attested balances and the total sum of validators attested balances of the epoch.
func InitializePrecomputeValidators(ctx context.Context, beaconState state.BeaconState) ([]*precompute.Validator, *precompute.Balance, error) {
func InitializePrecomputeValidators(ctx context.Context, beaconState state.BeaconState) ([]*types.Validator, *types.Balance, error) {
_, span := trace.StartSpan(ctx, "altair.InitializePrecomputeValidators")
defer span.End()
vals := make([]*precompute.Validator, beaconState.NumValidators())
bal := &precompute.Balance{}
vals := make([]*types.Validator, beaconState.NumValidators())
bal := &types.Balance{}
prevEpoch := time.PrevEpoch(beaconState)
currentEpoch := time.CurrentEpoch(beaconState)
inactivityScores, err := beaconState.InactivityScores()
@@ -33,7 +34,7 @@ func InitializePrecomputeValidators(ctx context.Context, beaconState state.Beaco
}
if err := beaconState.ReadFromEveryValidator(func(idx int, val state.ReadOnlyValidator) error {
// Set validator's balance, inactivity score and slashed/withdrawable status.
v := &precompute.Validator{
v := &types.Validator{
CurrentEpochEffectiveBalance: val.EffectiveBalance(),
InactivityScore: inactivityScores[idx],
IsSlashed: val.Slashed(),
@@ -74,57 +75,18 @@ func InitializePrecomputeValidators(ctx context.Context, beaconState state.Beaco
func ProcessInactivityScores(
ctx context.Context,
beaconState state.BeaconState,
vals []*precompute.Validator,
) (state.BeaconState, []*precompute.Validator, error) {
vals []*types.Validator,
) (state.BeaconState, []*types.Validator, error) {
_, span := trace.StartSpan(ctx, "altair.ProcessInactivityScores")
defer span.End()
cfg := params.BeaconConfig()
if time.CurrentEpoch(beaconState) == cfg.GenesisEpoch {
return beaconState, vals, nil
}
inactivityScores, err := beaconState.InactivityScores()
currEpoch := time.CurrentEpoch(beaconState)
prevEpoch := time.PrevEpoch(beaconState)
finalizedEpoch := beaconState.FinalizedCheckpointEpoch()
vals, err := beaconState.ProcessInactivityScores(ctx, currEpoch, prevEpoch, finalizedEpoch, vals)
if err != nil {
return nil, nil, err
}
bias := cfg.InactivityScoreBias
recoveryRate := cfg.InactivityScoreRecoveryRate
prevEpoch := time.PrevEpoch(beaconState)
finalizedEpoch := beaconState.FinalizedCheckpointEpoch()
for i, v := range vals {
if !precompute.EligibleForRewards(v) {
continue
}
if v.IsPrevEpochTargetAttester && !v.IsSlashed {
// Decrease inactivity score when validator gets target correct.
if v.InactivityScore > 0 {
v.InactivityScore -= 1
}
} else {
v.InactivityScore, err = math.Add64(v.InactivityScore, bias)
if err != nil {
return nil, nil, err
}
}
if !helpers.IsInInactivityLeak(prevEpoch, finalizedEpoch) {
score := recoveryRate
// Prevents underflow below 0.
if score > v.InactivityScore {
score = v.InactivityScore
}
v.InactivityScore -= score
}
inactivityScores[i] = v.InactivityScore
}
if err := beaconState.SetInactivityScores(inactivityScores); err != nil {
return nil, nil, err
}
return beaconState, vals, nil
}
@@ -141,9 +103,9 @@ func ProcessInactivityScores(
func ProcessEpochParticipation(
ctx context.Context,
beaconState state.BeaconState,
bal *precompute.Balance,
vals []*precompute.Validator,
) ([]*precompute.Validator, *precompute.Balance, error) {
bal *types.Balance,
vals []*types.Validator,
) ([]*types.Validator, *types.Balance, error) {
_, span := trace.StartSpan(ctx, "altair.ProcessEpochParticipation")
defer span.End()
@@ -209,8 +171,8 @@ func ProcessEpochParticipation(
// This is an optimized version by passing in precomputed validator attesting records and and total epoch balances.
func ProcessRewardsAndPenaltiesPrecompute(
beaconState state.BeaconState,
bal *precompute.Balance,
vals []*precompute.Validator,
bal *types.Balance,
vals []*types.Validator,
) (state.BeaconState, error) {
// Don't process rewards and penalties in genesis epoch.
cfg := params.BeaconConfig()
@@ -253,7 +215,7 @@ func ProcessRewardsAndPenaltiesPrecompute(
// AttestationsDelta computes and returns the rewards and penalties differences for individual validators based on the
// voting records.
func AttestationsDelta(beaconState state.BeaconState, bal *precompute.Balance, vals []*precompute.Validator) (rewards, penalties []uint64, err error) {
func AttestationsDelta(beaconState state.BeaconState, bal *types.Balance, vals []*types.Validator) (rewards, penalties []uint64, err error) {
numOfVals := beaconState.NumValidators()
rewards = make([]uint64, numOfVals)
penalties = make([]uint64, numOfVals)
@@ -285,8 +247,8 @@ func AttestationsDelta(beaconState state.BeaconState, bal *precompute.Balance, v
}
func attestationDelta(
bal *precompute.Balance,
val *precompute.Validator,
bal *types.Balance,
val *types.Validator,
baseRewardMultiplier, inactivityDenominator uint64,
inactivityLeak bool) (reward, penalty uint64, err error) {
eligible := val.IsActivePrevEpoch || (val.IsSlashed && !val.IsWithdrawableCurrentEpoch)

View File

@@ -5,7 +5,6 @@ import (
"math"
"testing"
"github.com/prysmaticlabs/prysm/beacon-chain/core/epoch/precompute"
"github.com/prysmaticlabs/prysm/beacon-chain/state"
stateAltair "github.com/prysmaticlabs/prysm/beacon-chain/state/v2"
v3 "github.com/prysmaticlabs/prysm/beacon-chain/state/v3"
@@ -35,29 +34,29 @@ func TestInitializeEpochValidators_Ok(t *testing.T) {
require.NoError(t, err)
v, b, err := InitializePrecomputeValidators(context.Background(), s)
require.NoError(t, err)
assert.DeepEqual(t, &precompute.Validator{
assert.DeepEqual(t, &types.Validator{
IsSlashed: true,
CurrentEpochEffectiveBalance: 100,
InactivityScore: 0,
}, v[0], "Incorrect validator 0 status")
assert.DeepEqual(t, &precompute.Validator{
assert.DeepEqual(t, &types.Validator{
IsWithdrawableCurrentEpoch: true,
CurrentEpochEffectiveBalance: 100,
InactivityScore: 1,
}, v[1], "Incorrect validator 1 status")
assert.DeepEqual(t, &precompute.Validator{
assert.DeepEqual(t, &types.Validator{
IsActivePrevEpoch: true,
IsActiveCurrentEpoch: true,
CurrentEpochEffectiveBalance: 100,
InactivityScore: 2,
}, v[2], "Incorrect validator 2 status")
assert.DeepEqual(t, &precompute.Validator{
assert.DeepEqual(t, &types.Validator{
IsActivePrevEpoch: true,
CurrentEpochEffectiveBalance: 100,
InactivityScore: 3,
}, v[3], "Incorrect validator 3 status")
wantedBalances := &precompute.Balance{
wantedBalances := &types.Balance{
ActiveCurrentEpoch: 100,
ActivePrevEpoch: 200,
}
@@ -96,13 +95,13 @@ func TestProcessEpochParticipation(t *testing.T) {
require.NoError(t, err)
validators, balance, err = ProcessEpochParticipation(context.Background(), s, balance, validators)
require.NoError(t, err)
require.DeepEqual(t, &precompute.Validator{
require.DeepEqual(t, &types.Validator{
IsActiveCurrentEpoch: true,
IsActivePrevEpoch: true,
IsWithdrawableCurrentEpoch: true,
CurrentEpochEffectiveBalance: params.BeaconConfig().MaxEffectiveBalance,
}, validators[0])
require.DeepEqual(t, &precompute.Validator{
require.DeepEqual(t, &types.Validator{
IsActiveCurrentEpoch: true,
IsActivePrevEpoch: true,
IsWithdrawableCurrentEpoch: true,
@@ -111,7 +110,7 @@ func TestProcessEpochParticipation(t *testing.T) {
IsPrevEpochAttester: true,
IsPrevEpochSourceAttester: true,
}, validators[1])
require.DeepEqual(t, &precompute.Validator{
require.DeepEqual(t, &types.Validator{
IsActiveCurrentEpoch: true,
IsActivePrevEpoch: true,
IsWithdrawableCurrentEpoch: true,
@@ -122,7 +121,7 @@ func TestProcessEpochParticipation(t *testing.T) {
IsCurrentEpochTargetAttester: true,
IsPrevEpochTargetAttester: true,
}, validators[2])
require.DeepEqual(t, &precompute.Validator{
require.DeepEqual(t, &types.Validator{
IsActiveCurrentEpoch: true,
IsActivePrevEpoch: true,
IsWithdrawableCurrentEpoch: true,
@@ -174,13 +173,13 @@ func TestProcessEpochParticipation_InactiveValidator(t *testing.T) {
require.NoError(t, err)
validators, balance, err = ProcessEpochParticipation(context.Background(), st, balance, validators)
require.NoError(t, err)
require.DeepEqual(t, &precompute.Validator{
require.DeepEqual(t, &types.Validator{
IsActiveCurrentEpoch: false,
IsActivePrevEpoch: false,
IsWithdrawableCurrentEpoch: true,
CurrentEpochEffectiveBalance: params.BeaconConfig().MaxEffectiveBalance,
}, validators[0])
require.DeepEqual(t, &precompute.Validator{
require.DeepEqual(t, &types.Validator{
IsActiveCurrentEpoch: false,
IsActivePrevEpoch: true,
IsPrevEpochAttester: true,
@@ -189,7 +188,7 @@ func TestProcessEpochParticipation_InactiveValidator(t *testing.T) {
IsWithdrawableCurrentEpoch: true,
CurrentEpochEffectiveBalance: params.BeaconConfig().MaxEffectiveBalance,
}, validators[1])
require.DeepEqual(t, &precompute.Validator{
require.DeepEqual(t, &types.Validator{
IsActiveCurrentEpoch: true,
IsActivePrevEpoch: true,
IsWithdrawableCurrentEpoch: true,
@@ -413,7 +412,7 @@ func TestProcessRewardsAndPenaltiesPrecompute_BadState(t *testing.T) {
require.NoError(t, err)
_, balance, err = ProcessEpochParticipation(context.Background(), s, balance, validators)
require.NoError(t, err)
_, err = ProcessRewardsAndPenaltiesPrecompute(s, balance, []*precompute.Validator{})
_, err = ProcessRewardsAndPenaltiesPrecompute(s, balance, []*types.Validator{})
require.ErrorContains(t, "validator registries not the same length as state's validator registries", err)
}

View File

@@ -8,7 +8,6 @@ go_library(
"new.go",
"reward_penalty.go",
"slashing.go",
"type.go",
],
importpath = "github.com/prysmaticlabs/prysm/beacon-chain/core/epoch/precompute",
visibility = [

View File

@@ -22,13 +22,13 @@ import (
func ProcessAttestations(
ctx context.Context,
state state.ReadOnlyBeaconState,
vp []*Validator,
pBal *Balance,
) ([]*Validator, *Balance, error) {
vp []*types.Validator,
pBal *types.Balance,
) ([]*types.Validator, *types.Balance, error) {
ctx, span := trace.StartSpan(ctx, "precomputeEpoch.ProcessAttestations")
defer span.End()
v := &Validator{}
v := &types.Validator{}
var err error
prevAtt, err := state.PreviousEpochAttestations()
@@ -141,7 +141,7 @@ func SameHead(state state.ReadOnlyBeaconState, a *ethpb.PendingAttestation) (boo
}
// UpdateValidator updates pre computed validator store.
func UpdateValidator(vp []*Validator, record *Validator, indices []uint64, a *ethpb.PendingAttestation, aSlot types.Slot) []*Validator {
func UpdateValidator(vp []*types.Validator, record *types.Validator, indices []uint64, a *ethpb.PendingAttestation, aSlot types.Slot) []*types.Validator {
inclusionSlot := aSlot + a.InclusionDelay
for _, i := range indices {
@@ -171,7 +171,7 @@ func UpdateValidator(vp []*Validator, record *Validator, indices []uint64, a *et
}
// UpdateBalance updates pre computed balance store.
func UpdateBalance(vp []*Validator, bBal *Balance, stateVersion int) *Balance {
func UpdateBalance(vp []*types.Validator, bBal *types.Balance, stateVersion int) *types.Balance {
for _, v := range vp {
if !v.IsSlashed {
if v.IsCurrentEpochAttester {
@@ -200,7 +200,7 @@ func UpdateBalance(vp []*Validator, bBal *Balance, stateVersion int) *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 EnsureBalancesLowerBound(bBal *Balance) *Balance {
func EnsureBalancesLowerBound(bBal *types.Balance) *types.Balance {
ebi := params.BeaconConfig().EffectiveBalanceIncrement
if ebi > bBal.ActiveCurrentEpoch {
bBal.ActiveCurrentEpoch = ebi

View File

@@ -8,6 +8,7 @@ import (
"github.com/prysmaticlabs/prysm/beacon-chain/core/epoch/precompute"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
"github.com/prysmaticlabs/prysm/config/params"
types "github.com/prysmaticlabs/prysm/consensus-types/primitives"
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1/attestation"
"github.com/prysmaticlabs/prysm/runtime/version"
@@ -18,36 +19,36 @@ import (
func TestUpdateValidator_Works(t *testing.T) {
e := params.BeaconConfig().FarFutureSlot
vp := []*precompute.Validator{{}, {InclusionSlot: e}, {}, {InclusionSlot: e}, {}, {InclusionSlot: e}}
record := &precompute.Validator{IsCurrentEpochAttester: true, IsCurrentEpochTargetAttester: true,
vp := []*types.Validator{{}, {InclusionSlot: e}, {}, {InclusionSlot: e}, {}, {InclusionSlot: e}}
record := &types.Validator{IsCurrentEpochAttester: true, IsCurrentEpochTargetAttester: true,
IsPrevEpochAttester: true, IsPrevEpochTargetAttester: true, IsPrevEpochHeadAttester: true}
a := &ethpb.PendingAttestation{InclusionDelay: 1, ProposerIndex: 2}
// Indices 1 3 and 5 attested
vp = precompute.UpdateValidator(vp, record, []uint64{1, 3, 5}, a, 100)
wanted := &precompute.Validator{IsCurrentEpochAttester: true, IsCurrentEpochTargetAttester: true,
wanted := &types.Validator{IsCurrentEpochAttester: true, IsCurrentEpochTargetAttester: true,
IsPrevEpochAttester: true, IsPrevEpochTargetAttester: true, IsPrevEpochHeadAttester: true,
ProposerIndex: 2, InclusionDistance: 1, InclusionSlot: 101}
wantedVp := []*precompute.Validator{{}, wanted, {}, wanted, {}, wanted}
wantedVp := []*types.Validator{{}, wanted, {}, wanted, {}, wanted}
assert.DeepEqual(t, wantedVp, vp, "Incorrect attesting validator calculations")
}
func TestUpdateValidator_InclusionOnlyCountsPrevEpoch(t *testing.T) {
e := params.BeaconConfig().FarFutureSlot
vp := []*precompute.Validator{{InclusionSlot: e}}
record := &precompute.Validator{IsCurrentEpochAttester: true, IsCurrentEpochTargetAttester: true}
vp := []*types.Validator{{InclusionSlot: e}}
record := &types.Validator{IsCurrentEpochAttester: true, IsCurrentEpochTargetAttester: true}
a := &ethpb.PendingAttestation{InclusionDelay: 1, ProposerIndex: 2}
// Verify inclusion info doesnt get updated.
vp = precompute.UpdateValidator(vp, record, []uint64{0}, a, 100)
wanted := &precompute.Validator{IsCurrentEpochAttester: true, IsCurrentEpochTargetAttester: true, InclusionSlot: e}
wantedVp := []*precompute.Validator{wanted}
wanted := &types.Validator{IsCurrentEpochAttester: true, IsCurrentEpochTargetAttester: true, InclusionSlot: e}
wantedVp := []*types.Validator{wanted}
assert.DeepEqual(t, wantedVp, vp, "Incorrect attesting validator calculations")
}
func TestUpdateBalance(t *testing.T) {
vp := []*precompute.Validator{
vp := []*types.Validator{
{IsCurrentEpochAttester: true, CurrentEpochEffectiveBalance: 100 * params.BeaconConfig().EffectiveBalanceIncrement},
{IsCurrentEpochTargetAttester: true, IsCurrentEpochAttester: true, CurrentEpochEffectiveBalance: 100 * params.BeaconConfig().EffectiveBalanceIncrement},
{IsCurrentEpochTargetAttester: true, CurrentEpochEffectiveBalance: 100 * params.BeaconConfig().EffectiveBalanceIncrement},
@@ -57,7 +58,7 @@ func TestUpdateBalance(t *testing.T) {
{IsPrevEpochAttester: true, IsPrevEpochHeadAttester: true, CurrentEpochEffectiveBalance: 100 * params.BeaconConfig().EffectiveBalanceIncrement},
{IsSlashed: true, IsCurrentEpochAttester: true, CurrentEpochEffectiveBalance: 100 * params.BeaconConfig().EffectiveBalanceIncrement},
}
wantedPBal := &precompute.Balance{
wantedPBal := &types.Balance{
ActiveCurrentEpoch: params.BeaconConfig().EffectiveBalanceIncrement,
ActivePrevEpoch: params.BeaconConfig().EffectiveBalanceIncrement,
CurrentEpochAttested: 200 * params.BeaconConfig().EffectiveBalanceIncrement,
@@ -66,12 +67,12 @@ func TestUpdateBalance(t *testing.T) {
PrevEpochTargetAttested: 100 * params.BeaconConfig().EffectiveBalanceIncrement,
PrevEpochHeadAttested: 200 * params.BeaconConfig().EffectiveBalanceIncrement,
}
pBal := precompute.UpdateBalance(vp, &precompute.Balance{}, version.Phase0)
pBal := precompute.UpdateBalance(vp, &types.Balance{}, version.Phase0)
assert.DeepEqual(t, wantedPBal, pBal, "Incorrect balance calculations")
}
func TestUpdateBalanceBellatrixVersion(t *testing.T) {
vp := []*precompute.Validator{
vp := []*types.Validator{
{IsCurrentEpochAttester: true, CurrentEpochEffectiveBalance: 100 * params.BeaconConfig().EffectiveBalanceIncrement},
{IsCurrentEpochTargetAttester: true, IsCurrentEpochAttester: true, CurrentEpochEffectiveBalance: 100 * params.BeaconConfig().EffectiveBalanceIncrement},
{IsCurrentEpochTargetAttester: true, CurrentEpochEffectiveBalance: 100 * params.BeaconConfig().EffectiveBalanceIncrement},
@@ -81,7 +82,7 @@ func TestUpdateBalanceBellatrixVersion(t *testing.T) {
{IsPrevEpochAttester: true, IsPrevEpochHeadAttester: true, CurrentEpochEffectiveBalance: 100 * params.BeaconConfig().EffectiveBalanceIncrement},
{IsSlashed: true, IsCurrentEpochAttester: true, CurrentEpochEffectiveBalance: 100 * params.BeaconConfig().EffectiveBalanceIncrement},
}
wantedPBal := &precompute.Balance{
wantedPBal := &types.Balance{
ActiveCurrentEpoch: params.BeaconConfig().EffectiveBalanceIncrement,
ActivePrevEpoch: params.BeaconConfig().EffectiveBalanceIncrement,
CurrentEpochAttested: 200 * params.BeaconConfig().EffectiveBalanceIncrement,
@@ -90,7 +91,7 @@ func TestUpdateBalanceBellatrixVersion(t *testing.T) {
PrevEpochTargetAttested: 100 * params.BeaconConfig().EffectiveBalanceIncrement,
PrevEpochHeadAttested: 200 * params.BeaconConfig().EffectiveBalanceIncrement,
}
pBal := precompute.UpdateBalance(vp, &precompute.Balance{}, version.Bellatrix)
pBal := precompute.UpdateBalance(vp, &types.Balance{}, version.Bellatrix)
assert.DeepEqual(t, wantedPBal, pBal, "Incorrect balance calculations")
}
@@ -199,11 +200,11 @@ func TestProcessAttestations(t *testing.T) {
err = beaconState.AppendCurrentEpochAttestations(&ethpb.PendingAttestation{Data: att2.Data, AggregationBits: bf, InclusionDelay: 1})
require.NoError(t, err)
pVals := make([]*precompute.Validator, validators)
pVals := make([]*types.Validator, validators)
for i := 0; i < len(pVals); i++ {
pVals[i] = &precompute.Validator{CurrentEpochEffectiveBalance: 100}
pVals[i] = &types.Validator{CurrentEpochEffectiveBalance: 100}
}
pVals, _, err = precompute.ProcessAttestations(context.Background(), beaconState, pVals, &precompute.Balance{})
pVals, _, err = precompute.ProcessAttestations(context.Background(), beaconState, pVals, &types.Balance{})
require.NoError(t, err)
committee, err := helpers.BeaconCommitteeFromState(context.Background(), beaconState, att1.Data.Slot, att1.Data.CommitteeIndex)
@@ -227,7 +228,7 @@ func TestProcessAttestations(t *testing.T) {
}
func TestEnsureBalancesLowerBound(t *testing.T) {
b := &precompute.Balance{}
b := &types.Balance{}
b = precompute.EnsureBalancesLowerBound(b)
balanceIncrement := params.BeaconConfig().EffectiveBalanceIncrement
assert.Equal(t, balanceIncrement, b.ActiveCurrentEpoch, "Did not get wanted active current balance")

View File

@@ -6,6 +6,7 @@ import (
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
"github.com/prysmaticlabs/prysm/beacon-chain/core/time"
"github.com/prysmaticlabs/prysm/beacon-chain/state"
types "github.com/prysmaticlabs/prysm/consensus-types/primitives"
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/time/slots"
)
@@ -44,7 +45,7 @@ func UnrealizedCheckpoints(st state.BeaconState) (*ethpb.Checkpoint, *ethpb.Chec
// 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 ProcessJustificationAndFinalizationPreCompute(state state.BeaconState, pBal *Balance) (state.BeaconState, error) {
func ProcessJustificationAndFinalizationPreCompute(state state.BeaconState, pBal *types.Balance) (state.BeaconState, error) {
canProcessSlot, err := slots.EpochStart(2 /*epoch*/)
if err != nil {
return nil, err

View File

@@ -43,7 +43,7 @@ func TestProcessJustificationAndFinalizationPreCompute_ConsecutiveEpochs(t *test
state, err := v1.InitializeFromProto(base)
require.NoError(t, err)
attestedBalance := 4 * uint64(e) * 3 / 2
b := &precompute.Balance{PrevEpochTargetAttested: attestedBalance}
b := &types.Balance{PrevEpochTargetAttested: attestedBalance}
newState, err := precompute.ProcessJustificationAndFinalizationPreCompute(state, b)
require.NoError(t, err)
rt := [32]byte{byte(64)}
@@ -80,7 +80,7 @@ func TestProcessJustificationAndFinalizationPreCompute_JustifyCurrentEpoch(t *te
state, err := v1.InitializeFromProto(base)
require.NoError(t, err)
attestedBalance := 4 * uint64(e) * 3 / 2
b := &precompute.Balance{PrevEpochTargetAttested: attestedBalance}
b := &types.Balance{PrevEpochTargetAttested: attestedBalance}
newState, err := precompute.ProcessJustificationAndFinalizationPreCompute(state, b)
require.NoError(t, err)
rt := [32]byte{byte(64)}
@@ -116,7 +116,7 @@ func TestProcessJustificationAndFinalizationPreCompute_JustifyPrevEpoch(t *testi
state, err := v1.InitializeFromProto(base)
require.NoError(t, err)
attestedBalance := 4 * uint64(e) * 3 / 2
b := &precompute.Balance{PrevEpochTargetAttested: attestedBalance}
b := &types.Balance{PrevEpochTargetAttested: attestedBalance}
newState, err := precompute.ProcessJustificationAndFinalizationPreCompute(state, b)
require.NoError(t, err)
rt := [32]byte{byte(64)}

View File

@@ -11,18 +11,19 @@ import (
"github.com/prysmaticlabs/prysm/beacon-chain/core/time"
"github.com/prysmaticlabs/prysm/beacon-chain/state"
"github.com/prysmaticlabs/prysm/config/params"
types "github.com/prysmaticlabs/prysm/consensus-types/primitives"
"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.
func New(ctx context.Context, s state.BeaconState) ([]*Validator, *Balance, error) {
func New(ctx context.Context, s state.BeaconState) ([]*types.Validator, *types.Balance, error) {
_, span := trace.StartSpan(ctx, "precomputeEpoch.New")
defer span.End()
pValidators := make([]*Validator, s.NumValidators())
pBal := &Balance{}
pValidators := make([]*types.Validator, s.NumValidators())
pBal := &types.Balance{}
currentEpoch := time.CurrentEpoch(s)
prevEpoch := time.PrevEpoch(s)
@@ -30,7 +31,7 @@ func New(ctx context.Context, s state.BeaconState) ([]*Validator, *Balance, erro
if err := s.ReadFromEveryValidator(func(idx int, val state.ReadOnlyValidator) error {
// Was validator withdrawable or slashed
withdrawable := prevEpoch+1 >= val.WithdrawableEpoch()
pVal := &Validator{
pVal := &types.Validator{
IsSlashed: val.Slashed(),
IsWithdrawableCurrentEpoch: withdrawable,
CurrentEpochEffectiveBalance: val.EffectiveBalance(),

View File

@@ -7,6 +7,7 @@ import (
"github.com/prysmaticlabs/prysm/beacon-chain/core/epoch/precompute"
v1 "github.com/prysmaticlabs/prysm/beacon-chain/state/v1"
"github.com/prysmaticlabs/prysm/config/params"
types "github.com/prysmaticlabs/prysm/consensus-types/primitives"
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/testing/assert"
"github.com/prysmaticlabs/prysm/testing/require"
@@ -31,33 +32,33 @@ func TestNew(t *testing.T) {
e := params.BeaconConfig().FarFutureSlot
v, b, err := precompute.New(context.Background(), s)
require.NoError(t, err)
assert.DeepEqual(t, &precompute.Validator{
assert.DeepEqual(t, &types.Validator{
IsSlashed: true,
CurrentEpochEffectiveBalance: 100,
InclusionDistance: e,
InclusionSlot: e,
}, v[0], "Incorrect validator 0 status")
assert.DeepEqual(t, &precompute.Validator{
assert.DeepEqual(t, &types.Validator{
IsWithdrawableCurrentEpoch: true,
CurrentEpochEffectiveBalance: 100,
InclusionDistance: e,
InclusionSlot: e,
}, v[1], "Incorrect validator 1 status")
assert.DeepEqual(t, &precompute.Validator{
assert.DeepEqual(t, &types.Validator{
IsActiveCurrentEpoch: true,
IsActivePrevEpoch: true,
CurrentEpochEffectiveBalance: 100,
InclusionDistance: e,
InclusionSlot: e,
}, v[2], "Incorrect validator 2 status")
assert.DeepEqual(t, &precompute.Validator{
assert.DeepEqual(t, &types.Validator{
IsActivePrevEpoch: true,
CurrentEpochEffectiveBalance: 100,
InclusionDistance: e,
InclusionSlot: e,
}, v[3], "Incorrect validator 3 status")
wantedBalances := &precompute.Balance{
wantedBalances := &types.Balance{
ActiveCurrentEpoch: 100,
ActivePrevEpoch: 200,
}

View File

@@ -10,15 +10,15 @@ import (
"github.com/prysmaticlabs/prysm/math"
)
type attesterRewardsFunc func(state.ReadOnlyBeaconState, *Balance, []*Validator) ([]uint64, []uint64, error)
type proposerRewardsFunc func(state.ReadOnlyBeaconState, *Balance, []*Validator) ([]uint64, error)
type attesterRewardsFunc func(state.ReadOnlyBeaconState, *types.Balance, []*types.Validator) ([]uint64, []uint64, error)
type proposerRewardsFunc func(state.ReadOnlyBeaconState, *types.Balance, []*types.Validator) ([]uint64, error)
// ProcessRewardsAndPenaltiesPrecompute processes the rewards and penalties of individual validator.
// This is an optimized version by passing in precomputed validator attesting records and and total epoch balances.
func ProcessRewardsAndPenaltiesPrecompute(
state state.BeaconState,
pBal *Balance,
vp []*Validator,
pBal *types.Balance,
vp []*types.Validator,
attRewardsFunc attesterRewardsFunc,
proRewardsFunc proposerRewardsFunc,
) (state.BeaconState, error) {
@@ -65,7 +65,7 @@ func ProcessRewardsAndPenaltiesPrecompute(
// AttestationsDelta computes and returns the rewards and penalties differences for individual validators based on the
// voting records.
func AttestationsDelta(state state.ReadOnlyBeaconState, pBal *Balance, vp []*Validator) ([]uint64, []uint64, error) {
func AttestationsDelta(state state.ReadOnlyBeaconState, pBal *types.Balance, vp []*types.Validator) ([]uint64, []uint64, error) {
numOfVals := state.NumValidators()
rewards := make([]uint64, numOfVals)
penalties := make([]uint64, numOfVals)
@@ -79,7 +79,7 @@ func AttestationsDelta(state state.ReadOnlyBeaconState, pBal *Balance, vp []*Val
return rewards, penalties, nil
}
func attestationDelta(pBal *Balance, sqrtActiveCurrentEpoch uint64, v *Validator, prevEpoch, finalizedEpoch types.Epoch) (uint64, uint64) {
func attestationDelta(pBal *types.Balance, sqrtActiveCurrentEpoch uint64, v *types.Validator, prevEpoch, finalizedEpoch types.Epoch) (uint64, uint64) {
if !EligibleForRewards(v) || pBal.ActiveCurrentEpoch == 0 {
return 0, 0
}
@@ -156,7 +156,7 @@ func attestationDelta(pBal *Balance, sqrtActiveCurrentEpoch uint64, v *Validator
// ProposersDelta computes and returns the rewards and penalties differences for individual validators based on the
// proposer inclusion records.
func ProposersDelta(state state.ReadOnlyBeaconState, pBal *Balance, vp []*Validator) ([]uint64, error) {
func ProposersDelta(state state.ReadOnlyBeaconState, pBal *types.Balance, vp []*types.Validator) ([]uint64, error) {
numofVals := state.NumValidators()
rewards := make([]uint64, numofVals)
@@ -190,6 +190,6 @@ func ProposersDelta(state state.ReadOnlyBeaconState, pBal *Balance, vp []*Valida
//
// Spec code:
// if is_active_validator(v, previous_epoch) or (v.slashed and previous_epoch + 1 < v.withdrawable_epoch)
func EligibleForRewards(v *Validator) bool {
func EligibleForRewards(v *types.Validator) bool {
return v.IsActivePrevEpoch || (v.IsSlashed && !v.IsWithdrawableCurrentEpoch)
}

View File

@@ -307,8 +307,8 @@ func TestProposerDeltaPrecompute_HappyCase(t *testing.T) {
require.NoError(t, err)
proposerIndex := types.ValidatorIndex(1)
b := &Balance{ActiveCurrentEpoch: 1000}
v := []*Validator{
b := &types.Balance{ActiveCurrentEpoch: 1000}
v := []*types.Validator{
{IsPrevEpochAttester: true, CurrentEpochEffectiveBalance: 32, ProposerIndex: proposerIndex},
}
r, err := ProposersDelta(beaconState, b, v)
@@ -329,8 +329,8 @@ func TestProposerDeltaPrecompute_ValidatorIndexOutOfRange(t *testing.T) {
require.NoError(t, err)
proposerIndex := types.ValidatorIndex(validatorCount)
b := &Balance{ActiveCurrentEpoch: 1000}
v := []*Validator{
b := &types.Balance{ActiveCurrentEpoch: 1000}
v := []*types.Validator{
{IsPrevEpochAttester: true, CurrentEpochEffectiveBalance: 32, ProposerIndex: proposerIndex},
}
_, err = ProposersDelta(beaconState, b, v)
@@ -345,8 +345,8 @@ func TestProposerDeltaPrecompute_SlashedCase(t *testing.T) {
require.NoError(t, err)
proposerIndex := types.ValidatorIndex(1)
b := &Balance{ActiveCurrentEpoch: 1000}
v := []*Validator{
b := &types.Balance{ActiveCurrentEpoch: 1000}
v := []*types.Validator{
{IsPrevEpochAttester: true, CurrentEpochEffectiveBalance: 32, ProposerIndex: proposerIndex, IsSlashed: true},
}
r, err := ProposersDelta(beaconState, b, v)

View File

@@ -12,7 +12,7 @@ import (
// ProcessSlashingsPrecompute processes the slashed validators during epoch processing.
// This is an optimized version by passing in precomputed total epoch balances.
func ProcessSlashingsPrecompute(s state.BeaconState, pBal *Balance) error {
func ProcessSlashingsPrecompute(s state.BeaconState, pBal *types.Balance) error {
currentEpoch := time.CurrentEpoch(s)
exitLength := params.BeaconConfig().EpochsPerSlashingsVector

View File

@@ -7,6 +7,7 @@ import (
"github.com/prysmaticlabs/prysm/beacon-chain/core/epoch/precompute"
v1 "github.com/prysmaticlabs/prysm/beacon-chain/state/v1"
"github.com/prysmaticlabs/prysm/config/params"
types "github.com/prysmaticlabs/prysm/consensus-types/primitives"
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/testing/assert"
"github.com/prysmaticlabs/prysm/testing/require"
@@ -21,7 +22,7 @@ func TestProcessSlashingsPrecompute_NotSlashedWithSlashedTrue(t *testing.T) {
Slashings: []uint64{0, 1e9},
})
require.NoError(t, err)
pBal := &precompute.Balance{ActiveCurrentEpoch: params.BeaconConfig().MaxEffectiveBalance}
pBal := &types.Balance{ActiveCurrentEpoch: params.BeaconConfig().MaxEffectiveBalance}
require.NoError(t, precompute.ProcessSlashingsPrecompute(s, pBal))
wanted := params.BeaconConfig().MaxEffectiveBalance
@@ -36,7 +37,7 @@ func TestProcessSlashingsPrecompute_NotSlashedWithSlashedFalse(t *testing.T) {
Slashings: []uint64{0, 1e9},
})
require.NoError(t, err)
pBal := &precompute.Balance{ActiveCurrentEpoch: params.BeaconConfig().MaxEffectiveBalance}
pBal := &types.Balance{ActiveCurrentEpoch: params.BeaconConfig().MaxEffectiveBalance}
require.NoError(t, precompute.ProcessSlashingsPrecompute(s, pBal))
wanted := params.BeaconConfig().MaxEffectiveBalance
@@ -120,7 +121,7 @@ func TestProcessSlashingsPrecompute_SlashedLess(t *testing.T) {
}
ab += b
}
pBal := &precompute.Balance{ActiveCurrentEpoch: ab}
pBal := &types.Balance{ActiveCurrentEpoch: ab}
original := proto.Clone(tt.state)
state, err := v1.InitializeFromProto(tt.state)

View File

@@ -516,8 +516,8 @@ func (bs *Server) GetValidatorParticipation(
if err != nil {
return nil, status.Error(codes.Internal, fmt.Sprintf("error replaying blocks for state at slot %d: %v", endSlot, err))
}
var v []*precompute.Validator
var b *precompute.Balance
var v []*types.Validator
var b *types.Balance
switch beaconState.Version() {
case version.Phase0:
v, b, err = precompute.New(ctx, beaconState)
@@ -680,7 +680,7 @@ func (bs *Server) GetValidatorPerformance(
return nil, status.Errorf(codes.Internal, "Could not process slots up to %d: %v", currSlot, err)
}
}
var validatorSummary []*precompute.Validator
var validatorSummary []*types.Validator
switch headState.Version() {
case version.Phase0:
vp, bp, err := precompute.New(ctx, headState)
@@ -862,8 +862,8 @@ func (bs *Server) GetIndividualVotes(
return filteredIndices[i] < filteredIndices[j]
})
var v []*precompute.Validator
var bal *precompute.Balance
var v []*types.Validator
var bal *types.Balance
switch st.Version() {
case version.Phase0:
v, bal, err = precompute.New(ctx, st)

View File

@@ -0,0 +1,15 @@
load("@prysm//tools/go:def.bzl", "go_library")
go_library(
name = "go_default_library",
srcs = ["inactivity_scores.go"],
importpath = "github.com/prysmaticlabs/prysm/beacon-chain/state/core",
visibility = ["//visibility:public"],
deps = [
"//beacon-chain/core/epoch/precompute:go_default_library",
"//beacon-chain/core/helpers:go_default_library",
"//config/params:go_default_library",
"//consensus-types/primitives:go_default_library",
"//math:go_default_library",
],
)

View File

@@ -0,0 +1,57 @@
package core
import (
"context"
"github.com/prysmaticlabs/prysm/beacon-chain/core/epoch/precompute"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
"github.com/prysmaticlabs/prysm/config/params"
types "github.com/prysmaticlabs/prysm/consensus-types/primitives"
"github.com/prysmaticlabs/prysm/math"
)
func ProcessInactivityScores(ctx context.Context,
inactivityScores []uint64,
currentEpoch, previousEpoch, finalizedEpoch types.Epoch,
vals []*types.Validator,
) ([]uint64, []*types.Validator, error) {
cfg := params.BeaconConfig()
if currentEpoch == cfg.GenesisEpoch {
return inactivityScores, vals, nil
}
bias := cfg.InactivityScoreBias
recoveryRate := cfg.InactivityScoreRecoveryRate
var err error
for i, v := range vals {
if !precompute.EligibleForRewards(v) {
continue
}
if v.IsPrevEpochTargetAttester && !v.IsSlashed {
// Decrease inactivity score when validator gets target correct.
if v.InactivityScore > 0 {
v.InactivityScore -= 1
}
} else {
v.InactivityScore, err = math.Add64(v.InactivityScore, bias)
if err != nil {
return nil, nil, err
}
}
if !helpers.IsInInactivityLeak(previousEpoch, finalizedEpoch) {
score := recoveryRate
// Prevents underflow below 0.
if score > v.InactivityScore {
score = v.InactivityScore
}
v.InactivityScore -= score
}
inactivityScores[i] = v.InactivityScore
}
return inactivityScores, vals, nil
}

View File

@@ -15,7 +15,6 @@ go_library(
"//beacon-chain/state/types:go_default_library",
"//crypto/hash:go_default_library",
"//encoding/bytesutil:go_default_library",
"//encoding/ssz:go_default_library",
"//math:go_default_library",
"//proto/prysm/v1alpha1:go_default_library",
"//runtime/version:go_default_library",

View File

@@ -12,7 +12,6 @@ import (
"github.com/prysmaticlabs/prysm/beacon-chain/state/types"
"github.com/prysmaticlabs/prysm/crypto/hash"
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
"github.com/prysmaticlabs/prysm/encoding/ssz"
pmath "github.com/prysmaticlabs/prysm/math"
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/runtime/version"
@@ -353,17 +352,7 @@ func handlePendingAttestationSlice(val []*ethpb.PendingAttestation, indices []ui
// handleBalanceSlice returns the root of a slice of validator balances.
func handleBalanceSlice(val, indices []uint64, convertAll bool) ([][32]byte, error) {
if convertAll {
balancesMarshaling := make([][]byte, len(val))
for i, b := range val {
balanceBuf := make([]byte, 8)
binary.LittleEndian.PutUint64(balanceBuf, b)
balancesMarshaling[i] = balanceBuf
}
balancesChunks, err := ssz.PackByChunk(balancesMarshaling)
if err != nil {
return [][32]byte{}, errors.Wrap(err, "could not pack balances into chunks")
}
return balancesChunks, nil
return stateutil.PackUint64IntoChunks(val)
}
if len(val) > 0 {
numOfElems, err := types.Balances.ElemsInChunk()

View File

@@ -73,6 +73,7 @@ type WriteOnlyBeaconState interface {
WriteOnlyBalances
WriteOnlyCheckpoint
WriteOnlyAttestations
CoreProcessors
SetGenesisTime(val uint64) error
SetGenesisValidatorsRoot(val []byte) error
SetSlot(val types.Slot) error
@@ -235,3 +236,9 @@ type FutureForkStub interface {
NextSyncCommittee() (*ethpb.SyncCommittee, error)
SetNextSyncCommittee(val *ethpb.SyncCommittee) error
}
type CoreProcessors interface {
ProcessInactivityScores(ctx context.Context,
currentEpoch, previousEpoch, finalizedEpoch types.Epoch,
vals []*types.Validator) ([]*types.Validator, error)
}

View File

@@ -57,6 +57,7 @@ go_library(
deps = [
"//beacon-chain/core/time:go_default_library",
"//beacon-chain/state:go_default_library",
"//beacon-chain/state/core:go_default_library",
"//beacon-chain/state/fieldtrie:go_default_library",
"//beacon-chain/state/state-native/custom-types:go_default_library",
"//beacon-chain/state/state-native/types:go_default_library",

View File

@@ -1,7 +1,10 @@
package state_native
import (
"context"
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/beacon-chain/state/core"
nativetypes "github.com/prysmaticlabs/prysm/beacon-chain/state/state-native/types"
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
types "github.com/prysmaticlabs/prysm/consensus-types/primitives"
@@ -240,3 +243,26 @@ func (b *BeaconState) SetInactivityScores(val []uint64) error {
b.markFieldAsDirty(nativetypes.InactivityScores)
return nil
}
func (b *BeaconState) ProcessInactivityScores(ctx context.Context,
currentEpoch, previousEpoch, finalizedEpoch types.Epoch, vals []*types.Validator) ([]*types.Validator, error) {
b.lock.Lock()
defer b.lock.Unlock()
if b.version == version.Phase0 {
return nil, errNotSupported("ProcessInactivityScores", b.version)
}
scores := b.inactivityScores
if b.sharedFieldReferences[nativetypes.InactivityScores].Refs() > 1 {
scores = b.inactivityScoresVal()
b.sharedFieldReferences[nativetypes.InactivityScores].MinusRef()
b.sharedFieldReferences[nativetypes.InactivityScores] = stateutil.NewRef(1)
}
var err error
scores, vals, err = core.ProcessInactivityScores(ctx, scores, currentEpoch, previousEpoch, finalizedEpoch, vals)
if err != nil {
return nil, err
}
b.inactivityScores = scores
b.markFieldAsDirty(nativetypes.InactivityScores)
return vals, err
}

View File

@@ -33,6 +33,7 @@ go_library(
"//consensus-types/interfaces:go_default_library",
"//consensus-types/primitives:go_default_library",
"//consensus-types/wrapper:go_default_library",
"//crypto/rand:go_default_library",
"//encoding/bytesutil:go_default_library",
"//monitoring/tracing:go_default_library",
"//proto/prysm/v1alpha1:go_default_library",

View File

@@ -157,9 +157,10 @@ func (s *State) loadStateByRoot(ctx context.Context, blockRoot [32]byte) (state.
ctx, span := trace.StartSpan(ctx, "stateGen.loadStateByRoot")
defer span.End()
val := ctx.Value("migrate")
// First, it checks if the state exists in hot state cache.
cachedState := s.hotStateCache.get(blockRoot)
if cachedState != nil && !cachedState.IsNil() {
if cachedState != nil && !cachedState.IsNil() && val == nil {
return cachedState, nil
}
@@ -168,7 +169,7 @@ func (s *State) loadStateByRoot(ctx context.Context, blockRoot [32]byte) (state.
if err != nil {
return nil, err
}
if ok {
if ok && val == nil {
return cachedInfo.state, nil
}
@@ -222,6 +223,7 @@ func (s *State) LastAncestorState(ctx context.Context, blockRoot [32]byte) (stat
if s.isFinalizedRoot(blockRoot) && s.finalizedState() != nil {
return s.finalizedState(), nil
}
val := ctx.Value("migrate")
b, err := s.beaconDB.Block(ctx, blockRoot)
if err != nil {
@@ -248,7 +250,7 @@ func (s *State) LastAncestorState(ctx context.Context, blockRoot [32]byte) (stat
return nil, errors.Wrapf(ErrNoDataForSlot, "slot %d not in db due to checkpoint sync", ps)
}
// Does the state exist in the hot state cache.
if s.hotStateCache.has(parentRoot) {
if s.hotStateCache.has(parentRoot) && val == nil {
return s.hotStateCache.get(parentRoot), nil
}
@@ -262,7 +264,7 @@ func (s *State) LastAncestorState(ctx context.Context, blockRoot [32]byte) (stat
if err != nil {
return nil, err
}
if ok {
if ok && val == nil {
return cachedInfo.state, nil
}

View File

@@ -3,7 +3,6 @@ package stategen
import (
"context"
"encoding/hex"
"fmt"
"github.com/prysmaticlabs/prysm/beacon-chain/state"
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
@@ -39,11 +38,10 @@ func (s *State) MigrateToCold(ctx context.Context, fRoot [32]byte) error {
}
if slot%s.slotsPerArchivedPoint == 0 && slot != 0 {
cached, exists, err := s.epochBoundaryStateCache.getBySlot(slot)
if err != nil {
return fmt.Errorf("could not get epoch boundary state for slot %d", slot)
cached, exists := &rootStateInfo{}, false
if s.slotsPerArchivedPoint == 10000 {
exists = true
}
var aRoot [32]byte
var aState state.BeaconState
@@ -72,6 +70,7 @@ func (s *State) MigrateToCold(ctx context.Context, fRoot [32]byte) error {
// There's no need to generate the state if the state already exists in the DB.
// We can skip saving the state.
if !s.beaconDB.HasState(ctx, aRoot) {
ctx = context.WithValue(ctx, "migrate", "yes")
aState, err = s.StateByRoot(ctx, missingRoot)
if err != nil {
return err

View File

@@ -15,6 +15,7 @@ import (
"github.com/prysmaticlabs/prysm/consensus-types/interfaces"
types "github.com/prysmaticlabs/prysm/consensus-types/primitives"
"github.com/prysmaticlabs/prysm/consensus-types/wrapper"
"github.com/prysmaticlabs/prysm/crypto/rand"
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
"github.com/prysmaticlabs/prysm/monitoring/tracing"
"github.com/prysmaticlabs/prysm/runtime/version"
@@ -41,6 +42,11 @@ func (_ *State) ReplayBlocks(
"endSlot": targetSlot,
"diff": targetSlot - state.Slot(),
}).Debug("Replaying state")
val := ctx.Value("migrate")
if val != nil {
log.Debug("Migrating state and replaying blocks")
}
gen := rand.NewDeterministicGenerator()
// The input block list is sorted in decreasing slots order.
if len(signed) > 0 {
for i := len(signed) - 1; i >= 0; i-- {
@@ -54,6 +60,10 @@ func (_ *State) ReplayBlocks(
if state.Slot() >= signed[i].Block().Slot() {
continue
}
if val != nil {
nm := gen.Int63n(5500)
time.Sleep(time.Duration(nm) * time.Millisecond)
}
state, err = executeStateTransitionStateGen(ctx, state, signed[i])
if err != nil {
return nil, err
@@ -143,9 +153,6 @@ func executeStateTransitionStateGen(
state state.BeaconState,
signed interfaces.SignedBeaconBlock,
) (state.BeaconState, error) {
if ctx.Err() != nil {
return nil, ctx.Err()
}
if err := wrapper.BeaconBlockIsNil(signed); err != nil {
return nil, err
}

View File

@@ -33,6 +33,7 @@ go_library(
],
deps = [
"//beacon-chain/core/transition/stateutils:go_default_library",
"//beacon-chain/state/types:go_default_library",
"//config/features:go_default_library",
"//config/fieldparams:go_default_library",
"//config/params:go_default_library",

View File

@@ -4,10 +4,12 @@ import (
"encoding/binary"
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/beacon-chain/state/types"
fieldparams "github.com/prysmaticlabs/prysm/config/fieldparams"
"github.com/prysmaticlabs/prysm/crypto/hash"
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
"github.com/prysmaticlabs/prysm/encoding/ssz"
pmath "github.com/prysmaticlabs/prysm/math"
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
)
@@ -64,13 +66,7 @@ func ValidatorFieldRoots(hasher ssz.HashFn, validator *ethpb.Validator) ([][32]b
// a list of uint64 and mixed with registry limit.
func Uint64ListRootWithRegistryLimit(balances []uint64) ([32]byte, error) {
hasher := hash.CustomSHA256Hasher()
balancesMarshaling := make([][]byte, 0, len(balances))
for i := 0; i < len(balances); i++ {
balanceBuf := make([]byte, 8)
binary.LittleEndian.PutUint64(balanceBuf, balances[i])
balancesMarshaling = append(balancesMarshaling, balanceBuf)
}
balancesChunks, err := ssz.PackByChunk(balancesMarshaling)
balancesChunks, err := PackUint64IntoChunks(balances)
if err != nil {
return [32]byte{}, errors.Wrap(err, "could not pack balances into chunks")
}
@@ -87,3 +83,29 @@ func Uint64ListRootWithRegistryLimit(balances []uint64) ([32]byte, error) {
binary.LittleEndian.PutUint64(balancesLengthRoot, uint64(len(balances)))
return ssz.MixInLength(balancesRootsRoot, balancesLengthRoot), nil
}
// PackUint64IntoChunks packs a list of uint64 values into 32 byte roots.
func PackUint64IntoChunks(vals []uint64) ([][32]byte, error) {
numOfElems, err := types.Balances.ElemsInChunk()
if err != nil {
return nil, err
}
iNumOfElems, err := pmath.Int(numOfElems)
if err != nil {
return nil, err
}
numOfChunks := len(vals) / iNumOfElems
if len(vals)%iNumOfElems != 0 {
numOfChunks++
}
balanceChunks := make([][32]byte, numOfChunks)
for idx, b := range vals {
startIdx := idx / iNumOfElems
chunkIdx := idx % iNumOfElems
sizeOfElem := 32 / iNumOfElems
chunkPos := chunkIdx * sizeOfElem
binary.LittleEndian.PutUint64(balanceChunks[startIdx][chunkPos:chunkPos+8], b)
}
return balanceChunks, nil
}

View File

@@ -1,7 +1,10 @@
package v1
import (
"context"
"github.com/pkg/errors"
types "github.com/prysmaticlabs/prysm/consensus-types/primitives"
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
)
@@ -59,3 +62,8 @@ func (*BeaconState) SetInactivityScores(_ []uint64) error {
func (*BeaconState) SetLatestExecutionPayloadHeader(val *ethpb.ExecutionPayloadHeader) error {
return errors.New("SetLatestExecutionPayloadHeader is not supported for phase 0 beacon state")
}
func (b *BeaconState) ProcessInactivityScores(ctx context.Context,
currentEpoch, previousEpoch, finalizedEpoch types.Epoch, vals []*types.Validator) ([]*types.Validator, error) {
return nil, errors.New("ProcessInactivityScores is not supported for phase 0 beacon state")
}

View File

@@ -35,6 +35,7 @@ go_library(
deps = [
"//beacon-chain/core/time:go_default_library",
"//beacon-chain/state:go_default_library",
"//beacon-chain/state/core:go_default_library",
"//beacon-chain/state/fieldtrie:go_default_library",
"//beacon-chain/state/state-native:go_default_library",
"//beacon-chain/state/stateutil:go_default_library",

View File

@@ -1,7 +1,10 @@
package v2
import (
"context"
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/beacon-chain/state/core"
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
types "github.com/prysmaticlabs/prysm/consensus-types/primitives"
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
@@ -263,3 +266,26 @@ func (b *BeaconState) SetInactivityScores(val []uint64) error {
b.markFieldAsDirty(inactivityScores)
return nil
}
func (b *BeaconState) ProcessInactivityScores(ctx context.Context,
currentEpoch, previousEpoch, finalizedEpoch types.Epoch, vals []*types.Validator) ([]*types.Validator, error) {
if !b.hasInnerState() {
return nil, ErrNilInnerState
}
b.lock.Lock()
defer b.lock.Unlock()
scores := b.state.InactivityScores
if b.sharedFieldReferences[inactivityScores].Refs() > 1 {
scores = b.inactivityScores()
b.sharedFieldReferences[inactivityScores].MinusRef()
b.sharedFieldReferences[inactivityScores] = stateutil.NewRef(1)
}
var err error
scores, vals, err = core.ProcessInactivityScores(ctx, scores, currentEpoch, previousEpoch, finalizedEpoch, vals)
if err != nil {
return nil, err
}
b.state.InactivityScores = scores
b.markFieldAsDirty(inactivityScores)
return vals, err
}

View File

@@ -37,6 +37,7 @@ go_library(
deps = [
"//beacon-chain/core/time:go_default_library",
"//beacon-chain/state:go_default_library",
"//beacon-chain/state/core:go_default_library",
"//beacon-chain/state/fieldtrie:go_default_library",
"//beacon-chain/state/state-native:go_default_library",
"//beacon-chain/state/stateutil:go_default_library",

View File

@@ -1,7 +1,10 @@
package v3
import (
"context"
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/beacon-chain/state/core"
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
types "github.com/prysmaticlabs/prysm/consensus-types/primitives"
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
@@ -263,3 +266,26 @@ func (b *BeaconState) SetInactivityScores(val []uint64) error {
b.markFieldAsDirty(inactivityScores)
return nil
}
func (b *BeaconState) ProcessInactivityScores(ctx context.Context,
currentEpoch, previousEpoch, finalizedEpoch types.Epoch, vals []*types.Validator) ([]*types.Validator, error) {
if !b.hasInnerState() {
return nil, ErrNilInnerState
}
b.lock.Lock()
defer b.lock.Unlock()
scores := b.state.InactivityScores
if b.sharedFieldReferences[inactivityScores].Refs() > 1 {
scores = b.inactivityScores()
b.sharedFieldReferences[inactivityScores].MinusRef()
b.sharedFieldReferences[inactivityScores] = stateutil.NewRef(1)
}
var err error
scores, vals, err = core.ProcessInactivityScores(ctx, scores, currentEpoch, previousEpoch, finalizedEpoch, vals)
if err != nil {
return nil, err
}
b.state.InactivityScores = scores
b.markFieldAsDirty(inactivityScores)
return vals, err
}

View File

@@ -3,11 +3,11 @@ package bellatrix
import (
"bytes"
"github.com/prysmaticlabs/prysm/config/fieldparams"
field_params "github.com/prysmaticlabs/prysm/config/fieldparams"
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
"github.com/prysmaticlabs/prysm/encoding/ssz"
"github.com/prysmaticlabs/prysm/proto/engine/v1"
"github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
enginev1 "github.com/prysmaticlabs/prysm/proto/engine/v1"
eth "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
)
// PayloadToHeader converts `payload` into execution payload header format.

View File

@@ -9,6 +9,7 @@ go_library(
"slot.go",
"sszbytes.go",
"sszuint64.go",
"type.go",
"validator.go",
],
importpath = "github.com/prysmaticlabs/prysm/consensus-types/primitives",

View File

@@ -1,6 +1,4 @@
package precompute
import types "github.com/prysmaticlabs/prysm/consensus-types/primitives"
package types
// 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
@@ -30,11 +28,11 @@ type Validator struct {
// 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 types.Slot
InclusionSlot Slot
// InclusionDistance is the distance between the assigned slot and this validator's attestation was included in block.
InclusionDistance types.Slot
InclusionDistance Slot
// ProposerIndex is the index of proposer at slot where this validator's attestation was included.
ProposerIndex types.ValidatorIndex
ProposerIndex ValidatorIndex
// BeforeEpochTransitionBalance is the validator balance prior to epoch transition.
BeforeEpochTransitionBalance uint64
// AfterEpochTransitionBalance is the validator balance after epoch transition.