mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-10 07:58:22 -05:00
Fix import shadowing of state package (#8191)
* update shadowed var name * update var names * remove unnecessary delta Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
This commit is contained in:
@@ -370,15 +370,15 @@ func (s *Service) handleEpochBoundary(ctx context.Context, postState *stateTrie.
|
||||
// This feeds in the block and block's attestations to fork choice store. It's allows fork choice store
|
||||
// to gain information on the most current chain.
|
||||
func (s *Service) insertBlockAndAttestationsToForkChoiceStore(ctx context.Context, blk *ethpb.BeaconBlock, root [32]byte,
|
||||
state *stateTrie.BeaconState) error {
|
||||
fCheckpoint := state.FinalizedCheckpoint()
|
||||
jCheckpoint := state.CurrentJustifiedCheckpoint()
|
||||
st *stateTrie.BeaconState) error {
|
||||
fCheckpoint := st.FinalizedCheckpoint()
|
||||
jCheckpoint := st.CurrentJustifiedCheckpoint()
|
||||
if err := s.insertBlockToForkChoiceStore(ctx, blk, root, fCheckpoint, jCheckpoint); err != nil {
|
||||
return err
|
||||
}
|
||||
// Feed in block's attestations to fork choice store.
|
||||
for _, a := range blk.Body.Attestations {
|
||||
committee, err := helpers.BeaconCommitteeFromState(state, a.Data.Slot, a.Data.CommitteeIndex)
|
||||
committee, err := helpers.BeaconCommitteeFromState(st, a.Data.Slot, a.Data.CommitteeIndex)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -405,7 +405,7 @@ func (s *Service) insertBlockToForkChoiceStore(ctx context.Context, blk *ethpb.B
|
||||
|
||||
// This saves post state info to DB or cache. This also saves post state info to fork choice store.
|
||||
// Post state info consists of processed block and state. Do not call this method unless the block and state are verified.
|
||||
func (s *Service) savePostStateInfo(ctx context.Context, r [32]byte, b *ethpb.SignedBeaconBlock, state *stateTrie.BeaconState, initSync bool) error {
|
||||
func (s *Service) savePostStateInfo(ctx context.Context, r [32]byte, b *ethpb.SignedBeaconBlock, st *stateTrie.BeaconState, initSync bool) error {
|
||||
ctx, span := trace.StartSpan(ctx, "blockChain.savePostStateInfo")
|
||||
defer span.End()
|
||||
if initSync {
|
||||
@@ -413,10 +413,10 @@ func (s *Service) savePostStateInfo(ctx context.Context, r [32]byte, b *ethpb.Si
|
||||
} else if err := s.beaconDB.SaveBlock(ctx, b); err != nil {
|
||||
return errors.Wrapf(err, "could not save block from slot %d", b.Block.Slot)
|
||||
}
|
||||
if err := s.stateGen.SaveState(ctx, r, state); err != nil {
|
||||
if err := s.stateGen.SaveState(ctx, r, st); err != nil {
|
||||
return errors.Wrap(err, "could not save state")
|
||||
}
|
||||
if err := s.insertBlockAndAttestationsToForkChoiceStore(ctx, b.Block, r, state); err != nil {
|
||||
if err := s.insertBlockAndAttestationsToForkChoiceStore(ctx, b.Block, r, st); err != nil {
|
||||
return errors.Wrapf(err, "could not insert block %d to fork choice store", b.Block.Slot)
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -430,8 +430,8 @@ func TestHasBlock_ForkChoiceAndDB(t *testing.T) {
|
||||
block := testutil.NewBeaconBlock()
|
||||
r, err := block.Block.HashTreeRoot()
|
||||
require.NoError(t, err)
|
||||
state := testutil.NewBeaconState()
|
||||
require.NoError(t, s.insertBlockAndAttestationsToForkChoiceStore(ctx, block.Block, r, state))
|
||||
beaconState := testutil.NewBeaconState()
|
||||
require.NoError(t, s.insertBlockAndAttestationsToForkChoiceStore(ctx, block.Block, r, beaconState))
|
||||
|
||||
assert.Equal(t, false, s.hasBlock(ctx, [32]byte{}), "Should not have block")
|
||||
assert.Equal(t, true, s.hasBlock(ctx, r), "Should have block")
|
||||
@@ -484,9 +484,9 @@ func BenchmarkHasBlockForkChoiceStore(b *testing.B) {
|
||||
r, err := block.Block.HashTreeRoot()
|
||||
require.NoError(b, err)
|
||||
bs := &pb.BeaconState{FinalizedCheckpoint: ðpb.Checkpoint{Root: make([]byte, 32)}, CurrentJustifiedCheckpoint: ðpb.Checkpoint{Root: make([]byte, 32)}}
|
||||
state, err := beaconstate.InitializeFromProto(bs)
|
||||
beaconState, err := beaconstate.InitializeFromProto(bs)
|
||||
require.NoError(b, err)
|
||||
require.NoError(b, s.insertBlockAndAttestationsToForkChoiceStore(ctx, block.Block, r, state))
|
||||
require.NoError(b, s.insertBlockAndAttestationsToForkChoiceStore(ctx, block.Block, r, beaconState))
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
|
||||
@@ -41,10 +41,10 @@ func TestUnslashedAttestingIndices_CanSortAndFilter(t *testing.T) {
|
||||
Validators: validators,
|
||||
RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector),
|
||||
}
|
||||
state, err := state.InitializeFromProto(base)
|
||||
beaconState, err := state.InitializeFromProto(base)
|
||||
require.NoError(t, err)
|
||||
|
||||
indices, err := epoch.UnslashedAttestingIndices(state, atts)
|
||||
indices, err := epoch.UnslashedAttestingIndices(beaconState, atts)
|
||||
require.NoError(t, err)
|
||||
for i := 0; i < len(indices)-1; i++ {
|
||||
if indices[i] >= indices[i+1] {
|
||||
@@ -54,10 +54,10 @@ func TestUnslashedAttestingIndices_CanSortAndFilter(t *testing.T) {
|
||||
|
||||
// Verify the slashed validator is filtered.
|
||||
slashedValidator := indices[0]
|
||||
validators = state.Validators()
|
||||
validators = beaconState.Validators()
|
||||
validators[slashedValidator].Slashed = true
|
||||
require.NoError(t, state.SetValidators(validators))
|
||||
indices, err = epoch.UnslashedAttestingIndices(state, atts)
|
||||
require.NoError(t, beaconState.SetValidators(validators))
|
||||
indices, err = epoch.UnslashedAttestingIndices(beaconState, atts)
|
||||
require.NoError(t, err)
|
||||
for i := 0; i < len(indices); i++ {
|
||||
assert.NotEqual(t, slashedValidator, indices[i], "Slashed validator %d is not filtered", slashedValidator)
|
||||
@@ -87,10 +87,10 @@ func TestUnslashedAttestingIndices_DuplicatedAttestations(t *testing.T) {
|
||||
Validators: validators,
|
||||
RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector),
|
||||
}
|
||||
state, err := state.InitializeFromProto(base)
|
||||
beaconState, err := state.InitializeFromProto(base)
|
||||
require.NoError(t, err)
|
||||
|
||||
indices, err := epoch.UnslashedAttestingIndices(state, atts)
|
||||
indices, err := epoch.UnslashedAttestingIndices(beaconState, atts)
|
||||
require.NoError(t, err)
|
||||
|
||||
for i := 0; i < len(indices)-1; i++ {
|
||||
@@ -133,10 +133,10 @@ func TestAttestingBalance_CorrectBalance(t *testing.T) {
|
||||
Validators: validators,
|
||||
Balances: balances,
|
||||
}
|
||||
state, err := state.InitializeFromProto(base)
|
||||
beaconState, err := state.InitializeFromProto(base)
|
||||
require.NoError(t, err)
|
||||
|
||||
balance, err := epoch.AttestingBalance(state, atts)
|
||||
balance, err := epoch.AttestingBalance(beaconState, atts)
|
||||
require.NoError(t, err)
|
||||
wanted := 256 * params.BeaconConfig().MaxEffectiveBalance
|
||||
assert.Equal(t, wanted, balance)
|
||||
@@ -159,9 +159,9 @@ func TestBaseReward_AccurateRewards(t *testing.T) {
|
||||
{ExitEpoch: params.BeaconConfig().FarFutureEpoch, EffectiveBalance: tt.b}},
|
||||
Balances: []uint64{tt.a},
|
||||
}
|
||||
state, err := state.InitializeFromProto(base)
|
||||
beaconState, err := state.InitializeFromProto(base)
|
||||
require.NoError(t, err)
|
||||
c, err := epoch.BaseReward(state, 0)
|
||||
c, err := epoch.BaseReward(beaconState, 0)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, tt.c, c, "epoch.BaseReward(%d)", tt.a)
|
||||
}
|
||||
@@ -310,9 +310,9 @@ func TestProcessRegistryUpdates_NoRotation(t *testing.T) {
|
||||
},
|
||||
FinalizedCheckpoint: ðpb.Checkpoint{Root: make([]byte, 32)},
|
||||
}
|
||||
state, err := state.InitializeFromProto(base)
|
||||
beaconState, err := state.InitializeFromProto(base)
|
||||
require.NoError(t, err)
|
||||
newState, err := epoch.ProcessRegistryUpdates(state)
|
||||
newState, err := epoch.ProcessRegistryUpdates(beaconState)
|
||||
require.NoError(t, err)
|
||||
for i, validator := range newState.Validators() {
|
||||
assert.Equal(t, params.BeaconConfig().MaxSeedLookahead, validator.ExitEpoch, "Could not update registry %d", i)
|
||||
@@ -333,10 +333,10 @@ func TestProcessRegistryUpdates_EligibleToActivate(t *testing.T) {
|
||||
ActivationEpoch: params.BeaconConfig().FarFutureEpoch,
|
||||
})
|
||||
}
|
||||
state, err := state.InitializeFromProto(base)
|
||||
beaconState, err := state.InitializeFromProto(base)
|
||||
require.NoError(t, err)
|
||||
currentEpoch := helpers.CurrentEpoch(state)
|
||||
newState, err := epoch.ProcessRegistryUpdates(state)
|
||||
currentEpoch := helpers.CurrentEpoch(beaconState)
|
||||
newState, err := epoch.ProcessRegistryUpdates(beaconState)
|
||||
require.NoError(t, err)
|
||||
for i, validator := range newState.Validators() {
|
||||
assert.Equal(t, currentEpoch+1, validator.ActivationEligibilityEpoch, "Could not update registry %d, unexpected activation eligibility epoch", i)
|
||||
@@ -362,9 +362,9 @@ func TestProcessRegistryUpdates_ActivationCompletes(t *testing.T) {
|
||||
},
|
||||
FinalizedCheckpoint: ðpb.Checkpoint{Root: make([]byte, 32)},
|
||||
}
|
||||
state, err := state.InitializeFromProto(base)
|
||||
beaconState, err := state.InitializeFromProto(base)
|
||||
require.NoError(t, err)
|
||||
newState, err := epoch.ProcessRegistryUpdates(state)
|
||||
newState, err := epoch.ProcessRegistryUpdates(beaconState)
|
||||
require.NoError(t, err)
|
||||
for i, validator := range newState.Validators() {
|
||||
assert.Equal(t, params.BeaconConfig().MaxSeedLookahead, validator.ExitEpoch, "Could not update registry %d, unexpected exit slot", i)
|
||||
@@ -386,9 +386,9 @@ func TestProcessRegistryUpdates_ValidatorsEjected(t *testing.T) {
|
||||
},
|
||||
FinalizedCheckpoint: ðpb.Checkpoint{Root: make([]byte, 32)},
|
||||
}
|
||||
state, err := state.InitializeFromProto(base)
|
||||
beaconState, err := state.InitializeFromProto(base)
|
||||
require.NoError(t, err)
|
||||
newState, err := epoch.ProcessRegistryUpdates(state)
|
||||
newState, err := epoch.ProcessRegistryUpdates(beaconState)
|
||||
require.NoError(t, err)
|
||||
for i, validator := range newState.Validators() {
|
||||
assert.Equal(t, params.BeaconConfig().MaxSeedLookahead+1, validator.ExitEpoch, "Could not update registry %d, unexpected exit slot", i)
|
||||
@@ -411,9 +411,9 @@ func TestProcessRegistryUpdates_CanExits(t *testing.T) {
|
||||
},
|
||||
FinalizedCheckpoint: ðpb.Checkpoint{Root: make([]byte, 32)},
|
||||
}
|
||||
state, err := state.InitializeFromProto(base)
|
||||
beaconState, err := state.InitializeFromProto(base)
|
||||
require.NoError(t, err)
|
||||
newState, err := epoch.ProcessRegistryUpdates(state)
|
||||
newState, err := epoch.ProcessRegistryUpdates(beaconState)
|
||||
require.NoError(t, err)
|
||||
for i, validator := range newState.Validators() {
|
||||
assert.Equal(t, exitEpoch, validator.ExitEpoch, "Could not update registry %d, unexpected exit slot", i)
|
||||
|
||||
@@ -33,24 +33,24 @@ func TestProcessRewardsAndPenaltiesPrecompute(t *testing.T) {
|
||||
}
|
||||
base.PreviousEpochAttestations = atts
|
||||
|
||||
state, err := state.InitializeFromProto(base)
|
||||
beaconState, err := state.InitializeFromProto(base)
|
||||
require.NoError(t, err)
|
||||
|
||||
vp, bp, err := New(context.Background(), state)
|
||||
vp, bp, err := New(context.Background(), beaconState)
|
||||
require.NoError(t, err)
|
||||
vp, bp, err = ProcessAttestations(context.Background(), state, vp, bp)
|
||||
vp, bp, err = ProcessAttestations(context.Background(), beaconState, vp, bp)
|
||||
require.NoError(t, err)
|
||||
|
||||
state, err = ProcessRewardsAndPenaltiesPrecompute(state, bp, vp)
|
||||
beaconState, err = ProcessRewardsAndPenaltiesPrecompute(beaconState, bp, vp)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Indices that voted everything except for head, lost a bit money
|
||||
wanted := uint64(31999810265)
|
||||
assert.Equal(t, wanted, state.Balances()[4], "Unexpected balance")
|
||||
assert.Equal(t, wanted, beaconState.Balances()[4], "Unexpected balance")
|
||||
|
||||
// Indices that did not vote, lost more money
|
||||
wanted = uint64(31999873505)
|
||||
assert.Equal(t, wanted, state.Balances()[0], "Unexpected balance")
|
||||
assert.Equal(t, wanted, beaconState.Balances()[0], "Unexpected balance")
|
||||
}
|
||||
|
||||
func TestAttestationDeltaPrecompute(t *testing.T) {
|
||||
@@ -75,34 +75,34 @@ func TestAttestationDeltaPrecompute(t *testing.T) {
|
||||
}
|
||||
}
|
||||
base.PreviousEpochAttestations = atts
|
||||
state, err := state.InitializeFromProto(base)
|
||||
beaconState, err := state.InitializeFromProto(base)
|
||||
require.NoError(t, err)
|
||||
slashedAttestedIndices := []uint64{1413}
|
||||
for _, i := range slashedAttestedIndices {
|
||||
vs := state.Validators()
|
||||
vs := beaconState.Validators()
|
||||
vs[i].Slashed = true
|
||||
require.Equal(t, nil, state.SetValidators(vs))
|
||||
require.Equal(t, nil, beaconState.SetValidators(vs))
|
||||
}
|
||||
|
||||
vp, bp, err := New(context.Background(), state)
|
||||
vp, bp, err := New(context.Background(), beaconState)
|
||||
require.NoError(t, err)
|
||||
vp, bp, err = ProcessAttestations(context.Background(), state, vp, bp)
|
||||
vp, bp, err = ProcessAttestations(context.Background(), beaconState, vp, bp)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Add some variances to target and head balances.
|
||||
// See: https://github.com/prysmaticlabs/prysm/issues/5593
|
||||
bp.PrevEpochTargetAttested = bp.PrevEpochTargetAttested / 2
|
||||
bp.PrevEpochHeadAttested = bp.PrevEpochHeadAttested * 2 / 3
|
||||
rewards, penalties, err := AttestationsDelta(state, bp, vp)
|
||||
rewards, penalties, err := AttestationsDelta(beaconState, bp, vp)
|
||||
require.NoError(t, err)
|
||||
attestedBalance, err := epoch.AttestingBalance(state, atts)
|
||||
attestedBalance, err := epoch.AttestingBalance(beaconState, atts)
|
||||
require.NoError(t, err)
|
||||
totalBalance, err := helpers.TotalActiveBalance(state)
|
||||
totalBalance, err := helpers.TotalActiveBalance(beaconState)
|
||||
require.NoError(t, err)
|
||||
|
||||
attestedIndices := []uint64{55, 1339, 1746, 1811, 1569}
|
||||
for _, i := range attestedIndices {
|
||||
base, err := epoch.BaseReward(state, i)
|
||||
base, err := epoch.BaseReward(beaconState, i)
|
||||
require.NoError(t, err, "Could not get base reward")
|
||||
|
||||
// Base rewards for getting source right
|
||||
@@ -119,7 +119,7 @@ func TestAttestationDeltaPrecompute(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, i := range slashedAttestedIndices {
|
||||
base, err := epoch.BaseReward(state, i)
|
||||
base, err := epoch.BaseReward(beaconState, i)
|
||||
assert.NoError(t, err, "Could not get base reward")
|
||||
assert.Equal(t, uint64(0), rewards[i], "Unexpected slashed indices reward balance")
|
||||
assert.Equal(t, 3*base, penalties[i], "Unexpected slashed indices penalty balance")
|
||||
@@ -127,7 +127,7 @@ func TestAttestationDeltaPrecompute(t *testing.T) {
|
||||
|
||||
nonAttestedIndices := []uint64{434, 677, 872, 791}
|
||||
for _, i := range nonAttestedIndices {
|
||||
base, err := epoch.BaseReward(state, i)
|
||||
base, err := epoch.BaseReward(beaconState, i)
|
||||
assert.NoError(t, err, "Could not get base reward")
|
||||
wanted := 3 * base
|
||||
// Since all these validators did not attest, they shouldn't get rewarded.
|
||||
@@ -159,17 +159,17 @@ func TestAttestationDeltas_ZeroEpoch(t *testing.T) {
|
||||
}
|
||||
}
|
||||
base.PreviousEpochAttestations = atts
|
||||
state, err := state.InitializeFromProto(base)
|
||||
beaconState, err := state.InitializeFromProto(base)
|
||||
require.NoError(t, err)
|
||||
|
||||
pVals, pBal, err := New(context.Background(), state)
|
||||
pVals, pBal, err := New(context.Background(), beaconState)
|
||||
assert.NoError(t, err)
|
||||
pVals, pBal, err = ProcessAttestations(context.Background(), state, pVals, pBal)
|
||||
pVals, pBal, err = ProcessAttestations(context.Background(), beaconState, pVals, pBal)
|
||||
require.NoError(t, err)
|
||||
|
||||
pBal.ActiveCurrentEpoch = 0 // Could cause a divide by zero panic.
|
||||
|
||||
_, _, err = AttestationsDelta(state, pBal, pVals)
|
||||
_, _, err = AttestationsDelta(beaconState, pBal, pVals)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
@@ -197,12 +197,12 @@ func TestAttestationDeltas_ZeroInclusionDelay(t *testing.T) {
|
||||
}
|
||||
}
|
||||
base.PreviousEpochAttestations = atts
|
||||
state, err := state.InitializeFromProto(base)
|
||||
beaconState, err := state.InitializeFromProto(base)
|
||||
require.NoError(t, err)
|
||||
|
||||
pVals, pBal, err := New(context.Background(), state)
|
||||
pVals, pBal, err := New(context.Background(), beaconState)
|
||||
require.NoError(t, err)
|
||||
_, _, err = ProcessAttestations(context.Background(), state, pVals, pBal)
|
||||
_, _, err = ProcessAttestations(context.Background(), beaconState, pVals, pBal)
|
||||
require.ErrorContains(t, "attestation with inclusion delay of 0", err)
|
||||
}
|
||||
|
||||
@@ -223,27 +223,27 @@ func TestProcessRewardsAndPenaltiesPrecompute_SlashedInactivePenalty(t *testing.
|
||||
}
|
||||
base.PreviousEpochAttestations = atts
|
||||
|
||||
state, err := state.InitializeFromProto(base)
|
||||
beaconState, err := state.InitializeFromProto(base)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, state.SetSlot(params.BeaconConfig().SlotsPerEpoch*10))
|
||||
require.NoError(t, beaconState.SetSlot(params.BeaconConfig().SlotsPerEpoch*10))
|
||||
|
||||
slashedAttestedIndices := []uint64{14, 37, 68, 77, 139}
|
||||
for _, i := range slashedAttestedIndices {
|
||||
vs := state.Validators()
|
||||
vs := beaconState.Validators()
|
||||
vs[i].Slashed = true
|
||||
require.NoError(t, state.SetValidators(vs))
|
||||
require.NoError(t, beaconState.SetValidators(vs))
|
||||
}
|
||||
|
||||
vp, bp, err := New(context.Background(), state)
|
||||
vp, bp, err := New(context.Background(), beaconState)
|
||||
require.NoError(t, err)
|
||||
vp, bp, err = ProcessAttestations(context.Background(), state, vp, bp)
|
||||
vp, bp, err = ProcessAttestations(context.Background(), beaconState, vp, bp)
|
||||
require.NoError(t, err)
|
||||
rewards, penalties, err := AttestationsDelta(state, bp, vp)
|
||||
rewards, penalties, err := AttestationsDelta(beaconState, bp, vp)
|
||||
require.NoError(t, err)
|
||||
|
||||
finalityDelay := helpers.PrevEpoch(state) - state.FinalizedCheckpointEpoch()
|
||||
finalityDelay := helpers.PrevEpoch(beaconState) - beaconState.FinalizedCheckpointEpoch()
|
||||
for _, i := range slashedAttestedIndices {
|
||||
base, err := epoch.BaseReward(state, i)
|
||||
base, err := epoch.BaseReward(beaconState, i)
|
||||
require.NoError(t, err, "Could not get base reward")
|
||||
penalty := 3 * base
|
||||
proposerReward := base / params.BeaconConfig().ProposerRewardQuotient
|
||||
@@ -297,7 +297,7 @@ func TestProposerDeltaPrecompute_HappyCase(t *testing.T) {
|
||||
e := params.BeaconConfig().SlotsPerEpoch
|
||||
validatorCount := uint64(10)
|
||||
base := buildState(e, validatorCount)
|
||||
state, err := state.InitializeFromProto(base)
|
||||
beaconState, err := state.InitializeFromProto(base)
|
||||
require.NoError(t, err)
|
||||
|
||||
proposerIndex := uint64(1)
|
||||
@@ -305,7 +305,7 @@ func TestProposerDeltaPrecompute_HappyCase(t *testing.T) {
|
||||
v := []*Validator{
|
||||
{IsPrevEpochAttester: true, CurrentEpochEffectiveBalance: 32, ProposerIndex: proposerIndex},
|
||||
}
|
||||
r, err := ProposersDelta(state, b, v)
|
||||
r, err := ProposersDelta(beaconState, b, v)
|
||||
require.NoError(t, err)
|
||||
|
||||
baseReward := v[0].CurrentEpochEffectiveBalance * params.BeaconConfig().BaseRewardFactor /
|
||||
@@ -319,7 +319,7 @@ func TestProposerDeltaPrecompute_ValidatorIndexOutOfRange(t *testing.T) {
|
||||
e := params.BeaconConfig().SlotsPerEpoch
|
||||
validatorCount := uint64(10)
|
||||
base := buildState(e, validatorCount)
|
||||
state, err := state.InitializeFromProto(base)
|
||||
beaconState, err := state.InitializeFromProto(base)
|
||||
require.NoError(t, err)
|
||||
|
||||
proposerIndex := validatorCount
|
||||
@@ -327,7 +327,7 @@ func TestProposerDeltaPrecompute_ValidatorIndexOutOfRange(t *testing.T) {
|
||||
v := []*Validator{
|
||||
{IsPrevEpochAttester: true, CurrentEpochEffectiveBalance: 32, ProposerIndex: proposerIndex},
|
||||
}
|
||||
_, err = ProposersDelta(state, b, v)
|
||||
_, err = ProposersDelta(beaconState, b, v)
|
||||
assert.ErrorContains(t, "proposer index out of range", err)
|
||||
}
|
||||
|
||||
@@ -335,7 +335,7 @@ func TestProposerDeltaPrecompute_SlashedCase(t *testing.T) {
|
||||
e := params.BeaconConfig().SlotsPerEpoch
|
||||
validatorCount := uint64(10)
|
||||
base := buildState(e, validatorCount)
|
||||
state, err := state.InitializeFromProto(base)
|
||||
beaconState, err := state.InitializeFromProto(base)
|
||||
require.NoError(t, err)
|
||||
|
||||
proposerIndex := uint64(1)
|
||||
@@ -343,7 +343,7 @@ func TestProposerDeltaPrecompute_SlashedCase(t *testing.T) {
|
||||
v := []*Validator{
|
||||
{IsPrevEpochAttester: true, CurrentEpochEffectiveBalance: 32, ProposerIndex: proposerIndex, IsSlashed: true},
|
||||
}
|
||||
r, err := ProposersDelta(state, b, v)
|
||||
r, err := ProposersDelta(beaconState, b, v)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, uint64(0), r[proposerIndex], "Unexpected proposer reward for slashed")
|
||||
}
|
||||
@@ -351,51 +351,51 @@ func TestProposerDeltaPrecompute_SlashedCase(t *testing.T) {
|
||||
func TestFinalityDelay(t *testing.T) {
|
||||
base := buildState(params.BeaconConfig().SlotsPerEpoch*10, 1)
|
||||
base.FinalizedCheckpoint = ðpb.Checkpoint{Epoch: 3}
|
||||
state, err := state.InitializeFromProto(base)
|
||||
beaconState, err := state.InitializeFromProto(base)
|
||||
require.NoError(t, err)
|
||||
prevEpoch := uint64(0)
|
||||
finalizedEpoch := uint64(0)
|
||||
// Set values for each test case
|
||||
setVal := func() {
|
||||
prevEpoch = helpers.PrevEpoch(state)
|
||||
finalizedEpoch = state.FinalizedCheckpointEpoch()
|
||||
prevEpoch = helpers.PrevEpoch(beaconState)
|
||||
finalizedEpoch = beaconState.FinalizedCheckpointEpoch()
|
||||
}
|
||||
setVal()
|
||||
d := finalityDelay(prevEpoch, finalizedEpoch)
|
||||
w := helpers.PrevEpoch(state) - state.FinalizedCheckpointEpoch()
|
||||
w := helpers.PrevEpoch(beaconState) - beaconState.FinalizedCheckpointEpoch()
|
||||
assert.Equal(t, w, d, "Did not get wanted finality delay")
|
||||
|
||||
require.NoError(t, state.SetFinalizedCheckpoint(ðpb.Checkpoint{Epoch: 4}))
|
||||
require.NoError(t, beaconState.SetFinalizedCheckpoint(ðpb.Checkpoint{Epoch: 4}))
|
||||
setVal()
|
||||
d = finalityDelay(prevEpoch, finalizedEpoch)
|
||||
w = helpers.PrevEpoch(state) - state.FinalizedCheckpointEpoch()
|
||||
w = helpers.PrevEpoch(beaconState) - beaconState.FinalizedCheckpointEpoch()
|
||||
assert.Equal(t, w, d, "Did not get wanted finality delay")
|
||||
|
||||
require.NoError(t, state.SetFinalizedCheckpoint(ðpb.Checkpoint{Epoch: 5}))
|
||||
require.NoError(t, beaconState.SetFinalizedCheckpoint(ðpb.Checkpoint{Epoch: 5}))
|
||||
setVal()
|
||||
d = finalityDelay(prevEpoch, finalizedEpoch)
|
||||
w = helpers.PrevEpoch(state) - state.FinalizedCheckpointEpoch()
|
||||
w = helpers.PrevEpoch(beaconState) - beaconState.FinalizedCheckpointEpoch()
|
||||
assert.Equal(t, w, d, "Did not get wanted finality delay")
|
||||
}
|
||||
|
||||
func TestIsInInactivityLeak(t *testing.T) {
|
||||
base := buildState(params.BeaconConfig().SlotsPerEpoch*10, 1)
|
||||
base.FinalizedCheckpoint = ðpb.Checkpoint{Epoch: 3}
|
||||
state, err := state.InitializeFromProto(base)
|
||||
beaconState, err := state.InitializeFromProto(base)
|
||||
require.NoError(t, err)
|
||||
prevEpoch := uint64(0)
|
||||
finalizedEpoch := uint64(0)
|
||||
// Set values for each test case
|
||||
setVal := func() {
|
||||
prevEpoch = helpers.PrevEpoch(state)
|
||||
finalizedEpoch = state.FinalizedCheckpointEpoch()
|
||||
prevEpoch = helpers.PrevEpoch(beaconState)
|
||||
finalizedEpoch = beaconState.FinalizedCheckpointEpoch()
|
||||
}
|
||||
setVal()
|
||||
assert.Equal(t, true, isInInactivityLeak(prevEpoch, finalizedEpoch), "Wanted inactivity leak true")
|
||||
require.NoError(t, state.SetFinalizedCheckpoint(ðpb.Checkpoint{Epoch: 4}))
|
||||
require.NoError(t, beaconState.SetFinalizedCheckpoint(ðpb.Checkpoint{Epoch: 4}))
|
||||
setVal()
|
||||
assert.Equal(t, true, isInInactivityLeak(prevEpoch, finalizedEpoch), "Wanted inactivity leak true")
|
||||
require.NoError(t, state.SetFinalizedCheckpoint(ðpb.Checkpoint{Epoch: 5}))
|
||||
require.NoError(t, beaconState.SetFinalizedCheckpoint(ðpb.Checkpoint{Epoch: 5}))
|
||||
setVal()
|
||||
assert.Equal(t, false, isInInactivityLeak(prevEpoch, finalizedEpoch), "Wanted inactivity leak false")
|
||||
}
|
||||
|
||||
@@ -25,15 +25,15 @@ func runJustificationAndFinalizationTests(t *testing.T, config string) {
|
||||
}
|
||||
}
|
||||
|
||||
func processJustificationAndFinalizationPrecomputeWrapper(t *testing.T, state *state.BeaconState) (*state.BeaconState, error) {
|
||||
func processJustificationAndFinalizationPrecomputeWrapper(t *testing.T, st *state.BeaconState) (*state.BeaconState, error) {
|
||||
ctx := context.Background()
|
||||
vp, bp, err := precompute.New(ctx, state)
|
||||
vp, bp, err := precompute.New(ctx, st)
|
||||
require.NoError(t, err)
|
||||
_, bp, err = precompute.ProcessAttestations(ctx, state, vp, bp)
|
||||
_, bp, err = precompute.ProcessAttestations(ctx, st, vp, bp)
|
||||
require.NoError(t, err)
|
||||
|
||||
state, err = precompute.ProcessJustificationAndFinalizationPreCompute(state, bp)
|
||||
st, err = precompute.ProcessJustificationAndFinalizationPreCompute(st, bp)
|
||||
require.NoError(t, err, "Could not process justification")
|
||||
|
||||
return state, nil
|
||||
return st, nil
|
||||
}
|
||||
|
||||
@@ -27,15 +27,15 @@ func runRewardsAndPenaltiesTests(t *testing.T, config string) {
|
||||
}
|
||||
}
|
||||
|
||||
func processRewardsAndPenaltiesPrecomputeWrapper(t *testing.T, state *state.BeaconState) (*state.BeaconState, error) {
|
||||
func processRewardsAndPenaltiesPrecomputeWrapper(t *testing.T, st *state.BeaconState) (*state.BeaconState, error) {
|
||||
ctx := context.Background()
|
||||
vp, bp, err := precompute.New(ctx, state)
|
||||
vp, bp, err := precompute.New(ctx, st)
|
||||
require.NoError(t, err)
|
||||
vp, bp, err = precompute.ProcessAttestations(ctx, state, vp, bp)
|
||||
vp, bp, err = precompute.ProcessAttestations(ctx, st, vp, bp)
|
||||
require.NoError(t, err)
|
||||
|
||||
state, err = precompute.ProcessRewardsAndPenaltiesPrecompute(state, bp, vp)
|
||||
st, err = precompute.ProcessRewardsAndPenaltiesPrecompute(st, bp, vp)
|
||||
require.NoError(t, err, "Could not process reward")
|
||||
|
||||
return state, nil
|
||||
return st, nil
|
||||
}
|
||||
|
||||
@@ -23,8 +23,8 @@ const DomainByteLength = 4
|
||||
var ErrSigFailedToVerify = errors.New("signature did not verify")
|
||||
|
||||
// ComputeDomainAndSign computes the domain and signing root and sign it using the passed in private key.
|
||||
func ComputeDomainAndSign(state *state.BeaconState, epoch uint64, obj interface{}, domain [4]byte, key bls.SecretKey) ([]byte, error) {
|
||||
d, err := Domain(state.Fork(), epoch, domain, state.GenesisValidatorRoot())
|
||||
func ComputeDomainAndSign(st *state.BeaconState, epoch uint64, obj interface{}, domain [4]byte, key bls.SecretKey) ([]byte, error) {
|
||||
d, err := Domain(st.Fork(), epoch, domain, st.GenesisValidatorRoot())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -73,12 +73,12 @@ func signingData(rootFunc func() ([32]byte, error), domain []byte) ([32]byte, er
|
||||
}
|
||||
|
||||
// ComputeDomainVerifySigningRoot computes domain and verifies signing root of an object given the beacon state, validator index and signature.
|
||||
func ComputeDomainVerifySigningRoot(state *state.BeaconState, index, epoch uint64, obj interface{}, domain [4]byte, sig []byte) error {
|
||||
v, err := state.ValidatorAtIndex(index)
|
||||
func ComputeDomainVerifySigningRoot(st *state.BeaconState, index, epoch uint64, obj interface{}, domain [4]byte, sig []byte) error {
|
||||
v, err := st.ValidatorAtIndex(index)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
d, err := Domain(state.Fork(), epoch, domain, state.GenesisValidatorRoot())
|
||||
d, err := Domain(st.Fork(), epoch, domain, st.GenesisValidatorRoot())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -127,8 +127,8 @@ func (e Exporter) SaveGenesisBlockRoot(ctx context.Context, blockRoot [32]byte)
|
||||
}
|
||||
|
||||
// SaveState -- passthrough.
|
||||
func (e Exporter) SaveState(ctx context.Context, state *state.BeaconState, blockRoot [32]byte) error {
|
||||
return e.db.SaveState(ctx, state, blockRoot)
|
||||
func (e Exporter) SaveState(ctx context.Context, st *state.BeaconState, blockRoot [32]byte) error {
|
||||
return e.db.SaveState(ctx, st, blockRoot)
|
||||
}
|
||||
|
||||
// SaveStateSummary -- passthrough.
|
||||
|
||||
@@ -516,16 +516,16 @@ func (bs *Server) GetValidatorParticipation(
|
||||
startSlot = uint64(i)
|
||||
}
|
||||
|
||||
state, err := bs.StateGen.StateBySlot(ctx, startSlot)
|
||||
beaconState, err := bs.StateGen.StateBySlot(ctx, startSlot)
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "Could not get state: %v", err)
|
||||
}
|
||||
|
||||
v, b, err := precompute.New(ctx, state)
|
||||
v, b, err := precompute.New(ctx, beaconState)
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "Could not set up pre compute instance: %v", err)
|
||||
}
|
||||
_, b, err = precompute.ProcessAttestations(ctx, state, v, b)
|
||||
_, b, err = precompute.ProcessAttestations(ctx, beaconState, v, b)
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "Could not pre compute attestations: %v", err)
|
||||
}
|
||||
|
||||
@@ -573,11 +573,11 @@ func (vs *Server) defaultEth1DataResponse(ctx context.Context, currentHeight *bi
|
||||
}
|
||||
|
||||
// This filters the input attestations to return a list of valid attestations to be packaged inside a beacon block.
|
||||
func (vs *Server) filterAttestationsForBlockInclusion(ctx context.Context, state *stateTrie.BeaconState, atts []*ethpb.Attestation) ([]*ethpb.Attestation, error) {
|
||||
func (vs *Server) filterAttestationsForBlockInclusion(ctx context.Context, st *stateTrie.BeaconState, atts []*ethpb.Attestation) ([]*ethpb.Attestation, error) {
|
||||
ctx, span := trace.StartSpan(ctx, "ProposerServer.filterAttestationsForBlockInclusion")
|
||||
defer span.End()
|
||||
|
||||
validAtts, invalidAtts := proposerAtts(atts).filter(ctx, state)
|
||||
validAtts, invalidAtts := proposerAtts(atts).filter(ctx, st)
|
||||
if err := vs.deleteAttsInPool(ctx, invalidAtts); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -14,11 +14,11 @@ import (
|
||||
)
|
||||
|
||||
// SaveState saves the state in the cache and/or DB.
|
||||
func (s *State) SaveState(ctx context.Context, root [32]byte, state *state.BeaconState) error {
|
||||
func (s *State) SaveState(ctx context.Context, root [32]byte, st *state.BeaconState) error {
|
||||
ctx, span := trace.StartSpan(ctx, "stateGen.SaveState")
|
||||
defer span.End()
|
||||
|
||||
return s.saveStateByRoot(ctx, root, state)
|
||||
return s.saveStateByRoot(ctx, root, st)
|
||||
}
|
||||
|
||||
// ForceCheckpoint initiates a cold state save of the given state. This method does not update the
|
||||
@@ -45,7 +45,7 @@ func (s *State) ForceCheckpoint(ctx context.Context, root []byte) error {
|
||||
// This saves a post beacon state. On the epoch boundary,
|
||||
// it saves a full state. On an intermediate slot, it saves a back pointer to the
|
||||
// nearest epoch boundary state.
|
||||
func (s *State) saveStateByRoot(ctx context.Context, blockRoot [32]byte, state *state.BeaconState) error {
|
||||
func (s *State) saveStateByRoot(ctx context.Context, blockRoot [32]byte, st *state.BeaconState) error {
|
||||
ctx, span := trace.StartSpan(ctx, "stateGen.saveStateByRoot")
|
||||
defer span.End()
|
||||
|
||||
@@ -53,15 +53,15 @@ func (s *State) saveStateByRoot(ctx context.Context, blockRoot [32]byte, state *
|
||||
duration := uint64(math.Max(float64(s.saveHotStateDB.duration), 1))
|
||||
|
||||
s.saveHotStateDB.lock.Lock()
|
||||
if s.saveHotStateDB.enabled && state.Slot()%duration == 0 {
|
||||
if err := s.beaconDB.SaveState(ctx, state, blockRoot); err != nil {
|
||||
if s.saveHotStateDB.enabled && st.Slot()%duration == 0 {
|
||||
if err := s.beaconDB.SaveState(ctx, st, blockRoot); err != nil {
|
||||
s.saveHotStateDB.lock.Unlock()
|
||||
return err
|
||||
}
|
||||
s.saveHotStateDB.savedStateRoots = append(s.saveHotStateDB.savedStateRoots, blockRoot)
|
||||
|
||||
log.WithFields(logrus.Fields{
|
||||
"slot": state.Slot(),
|
||||
"slot": st.Slot(),
|
||||
"totalHotStateSavedInDB": len(s.saveHotStateDB.savedStateRoots),
|
||||
}).Info("Saving hot state to DB")
|
||||
}
|
||||
@@ -73,22 +73,22 @@ func (s *State) saveStateByRoot(ctx context.Context, blockRoot [32]byte, state *
|
||||
}
|
||||
|
||||
// Only on an epoch boundary slot, saves epoch boundary state in epoch boundary root state cache.
|
||||
if helpers.IsEpochStart(state.Slot()) {
|
||||
if err := s.epochBoundaryStateCache.put(blockRoot, state); err != nil {
|
||||
if helpers.IsEpochStart(st.Slot()) {
|
||||
if err := s.epochBoundaryStateCache.put(blockRoot, st); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// On an intermediate slots, save state summary.
|
||||
if err := s.beaconDB.SaveStateSummary(ctx, &pb.StateSummary{
|
||||
Slot: state.Slot(),
|
||||
Slot: st.Slot(),
|
||||
Root: blockRoot[:],
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Store the copied state in the hot state cache.
|
||||
s.hotStateCache.put(blockRoot, state)
|
||||
s.hotStateCache.put(blockRoot, st)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user