mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-10 07:58:22 -05:00
Harden beacon state interface for Altair (#8673)
* State: clean up * Add error to return signature * Remove SetCurrentEpochAttestations and SetPreviousEpochAttestations * Fix tests Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
This commit is contained in:
@@ -25,7 +25,7 @@ func TestReportEpochMetrics_BadAttestation(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
h, err := testutil.NewBeaconState()
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, h.SetCurrentEpochAttestations([]*pb.PendingAttestation{{InclusionDelay: 0}}))
|
||||
require.NoError(t, h.AppendCurrentEpochAttestations(&pb.PendingAttestation{InclusionDelay: 0}))
|
||||
err = reportEpochMetrics(context.Background(), s, h)
|
||||
require.ErrorContains(t, "attestation with inclusion delay of 0", err)
|
||||
}
|
||||
@@ -36,7 +36,7 @@ func TestReportEpochMetrics_SlashedValidatorOutOfBound(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
v.Slashed = true
|
||||
require.NoError(t, h.UpdateValidatorAtIndex(0, v))
|
||||
require.NoError(t, h.SetCurrentEpochAttestations([]*pb.PendingAttestation{{InclusionDelay: 1, Data: testutil.HydrateAttestationData(ð.AttestationData{})}}))
|
||||
require.NoError(t, h.AppendCurrentEpochAttestations(&pb.PendingAttestation{InclusionDelay: 1, Data: testutil.HydrateAttestationData(ð.AttestationData{})}))
|
||||
err = reportEpochMetrics(context.Background(), h, h)
|
||||
require.ErrorContains(t, "slot 0 out of bounds", err)
|
||||
}
|
||||
|
||||
@@ -73,7 +73,7 @@ func TestVerifyAttestationNoVerifySignature_IncorrectSourceEpoch(t *testing.T) {
|
||||
ckp := beaconState.CurrentJustifiedCheckpoint()
|
||||
copy(ckp.Root, "hello-world")
|
||||
require.NoError(t, beaconState.SetCurrentJustifiedCheckpoint(ckp))
|
||||
require.NoError(t, beaconState.SetCurrentEpochAttestations([]*pb.PendingAttestation{}))
|
||||
require.NoError(t, beaconState.AppendCurrentEpochAttestations(&pb.PendingAttestation{}))
|
||||
|
||||
err = blocks.VerifyAttestationNoVerifySignature(context.TODO(), beaconState, att)
|
||||
assert.NotEqual(t, nil, err)
|
||||
|
||||
@@ -68,7 +68,7 @@ func TestProcessAttestations_NeitherCurrentNorPrevEpoch(t *testing.T) {
|
||||
pfc := beaconState.PreviousJustifiedCheckpoint()
|
||||
pfc.Root = []byte("hello-world")
|
||||
require.NoError(t, beaconState.SetPreviousJustifiedCheckpoint(pfc))
|
||||
require.NoError(t, beaconState.SetPreviousEpochAttestations([]*pb.PendingAttestation{}))
|
||||
require.NoError(t, beaconState.AppendPreviousEpochAttestations(&pb.PendingAttestation{}))
|
||||
|
||||
want := fmt.Sprintf(
|
||||
"expected target epoch (%d) to be the previous epoch (%d) or the current epoch (%d)",
|
||||
@@ -101,7 +101,7 @@ func TestProcessAttestations_CurrentEpochFFGDataMismatches(t *testing.T) {
|
||||
cfc := beaconState.CurrentJustifiedCheckpoint()
|
||||
cfc.Root = []byte("hello-world")
|
||||
require.NoError(t, beaconState.SetCurrentJustifiedCheckpoint(cfc))
|
||||
require.NoError(t, beaconState.SetCurrentEpochAttestations([]*pb.PendingAttestation{}))
|
||||
require.NoError(t, beaconState.AppendCurrentEpochAttestations(&pb.PendingAttestation{}))
|
||||
|
||||
want := "source check point not equal to current justified checkpoint"
|
||||
_, err := blocks.ProcessAttestations(context.Background(), beaconState, b)
|
||||
@@ -139,7 +139,7 @@ func TestProcessAttestations_PrevEpochFFGDataMismatches(t *testing.T) {
|
||||
pfc := beaconState.PreviousJustifiedCheckpoint()
|
||||
pfc.Root = []byte("hello-world")
|
||||
require.NoError(t, beaconState.SetPreviousJustifiedCheckpoint(pfc))
|
||||
require.NoError(t, beaconState.SetPreviousEpochAttestations([]*pb.PendingAttestation{}))
|
||||
require.NoError(t, beaconState.AppendPreviousEpochAttestations(&pb.PendingAttestation{}))
|
||||
|
||||
want := "source check point not equal to previous justified checkpoint"
|
||||
_, err = blocks.ProcessAttestations(context.Background(), beaconState, b)
|
||||
@@ -175,7 +175,7 @@ func TestProcessAttestations_InvalidAggregationBitsLength(t *testing.T) {
|
||||
cfc := beaconState.CurrentJustifiedCheckpoint()
|
||||
cfc.Root = []byte("hello-world")
|
||||
require.NoError(t, beaconState.SetCurrentJustifiedCheckpoint(cfc))
|
||||
require.NoError(t, beaconState.SetCurrentEpochAttestations([]*pb.PendingAttestation{}))
|
||||
require.NoError(t, beaconState.AppendCurrentEpochAttestations(&pb.PendingAttestation{}))
|
||||
|
||||
expected := "failed to verify aggregation bitfield: wanted participants bitfield length 3, got: 4"
|
||||
_, err = blocks.ProcessAttestations(context.Background(), beaconState, b)
|
||||
@@ -200,7 +200,7 @@ func TestProcessAttestations_OK(t *testing.T) {
|
||||
cfc := beaconState.CurrentJustifiedCheckpoint()
|
||||
cfc.Root = mockRoot[:]
|
||||
require.NoError(t, beaconState.SetCurrentJustifiedCheckpoint(cfc))
|
||||
require.NoError(t, beaconState.SetCurrentEpochAttestations([]*pb.PendingAttestation{}))
|
||||
require.NoError(t, beaconState.AppendCurrentEpochAttestations(&pb.PendingAttestation{}))
|
||||
|
||||
committee, err := helpers.BeaconCommitteeFromState(beaconState, att.Data.Slot, att.Data.CommitteeIndex)
|
||||
require.NoError(t, err)
|
||||
@@ -242,7 +242,7 @@ func TestProcessAggregatedAttestation_OverlappingBits(t *testing.T) {
|
||||
cfc := beaconState.CurrentJustifiedCheckpoint()
|
||||
cfc.Root = bytesutil.PadTo([]byte("hello-world"), 32)
|
||||
require.NoError(t, beaconState.SetCurrentJustifiedCheckpoint(cfc))
|
||||
require.NoError(t, beaconState.SetCurrentEpochAttestations([]*pb.PendingAttestation{}))
|
||||
require.NoError(t, beaconState.AppendCurrentEpochAttestations(&pb.PendingAttestation{}))
|
||||
|
||||
committee, err := helpers.BeaconCommitteeFromState(beaconState, att1.Data.Slot, att1.Data.CommitteeIndex)
|
||||
require.NoError(t, err)
|
||||
@@ -305,7 +305,7 @@ func TestProcessAggregatedAttestation_NoOverlappingBits(t *testing.T) {
|
||||
cfc := beaconState.CurrentJustifiedCheckpoint()
|
||||
cfc.Root = mockRoot[:]
|
||||
require.NoError(t, beaconState.SetCurrentJustifiedCheckpoint(cfc))
|
||||
require.NoError(t, beaconState.SetCurrentEpochAttestations([]*pb.PendingAttestation{}))
|
||||
require.NoError(t, beaconState.AppendCurrentEpochAttestations(&pb.PendingAttestation{}))
|
||||
|
||||
committee, err := helpers.BeaconCommitteeFromState(beaconState, att1.Data.Slot, att1.Data.CommitteeIndex)
|
||||
require.NoError(t, err)
|
||||
@@ -395,7 +395,7 @@ func TestProcessAttestationsNoVerify_OK(t *testing.T) {
|
||||
ckp := beaconState.CurrentJustifiedCheckpoint()
|
||||
copy(ckp.Root, "hello-world")
|
||||
require.NoError(t, beaconState.SetCurrentJustifiedCheckpoint(ckp))
|
||||
require.NoError(t, beaconState.SetCurrentEpochAttestations([]*pb.PendingAttestation{}))
|
||||
require.NoError(t, beaconState.AppendCurrentEpochAttestations(&pb.PendingAttestation{}))
|
||||
|
||||
_, err = blocks.ProcessAttestationNoVerifySignature(context.TODO(), beaconState, att)
|
||||
assert.NoError(t, err)
|
||||
@@ -426,7 +426,7 @@ func TestVerifyAttestationNoVerifySignature_OK(t *testing.T) {
|
||||
ckp := beaconState.CurrentJustifiedCheckpoint()
|
||||
copy(ckp.Root, "hello-world")
|
||||
require.NoError(t, beaconState.SetCurrentJustifiedCheckpoint(ckp))
|
||||
require.NoError(t, beaconState.SetCurrentEpochAttestations([]*pb.PendingAttestation{}))
|
||||
require.NoError(t, beaconState.AppendCurrentEpochAttestations(&pb.PendingAttestation{}))
|
||||
|
||||
err = blocks.VerifyAttestationNoVerifySignature(context.TODO(), beaconState, att)
|
||||
assert.NoError(t, err)
|
||||
@@ -452,7 +452,7 @@ func TestVerifyAttestationNoVerifySignature_BadAttIdx(t *testing.T) {
|
||||
ckp := beaconState.CurrentJustifiedCheckpoint()
|
||||
copy(ckp.Root, "hello-world")
|
||||
require.NoError(t, beaconState.SetCurrentJustifiedCheckpoint(ckp))
|
||||
require.NoError(t, beaconState.SetCurrentEpochAttestations([]*pb.PendingAttestation{}))
|
||||
require.NoError(t, beaconState.AppendCurrentEpochAttestations(&pb.PendingAttestation{}))
|
||||
err := blocks.VerifyAttestationNoVerifySignature(context.TODO(), beaconState, att)
|
||||
require.ErrorContains(t, "committee index 100 >= committee count 1", err)
|
||||
}
|
||||
|
||||
@@ -296,7 +296,9 @@ func TestProcessFinalUpdates_CanProcess(t *testing.T) {
|
||||
|
||||
// Verify historical root accumulator was appended.
|
||||
assert.Equal(t, 1, len(newS.HistoricalRoots()), "Unexpected slashed balance")
|
||||
assert.NotNil(t, newS.CurrentEpochAttestations(), "Nil value stored in current epoch attestations instead of empty slice")
|
||||
currAtt, err := newS.CurrentEpochAttestations()
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, currAtt, "Nil value stored in current epoch attestations instead of empty slice")
|
||||
}
|
||||
|
||||
func TestProcessRegistryUpdates_NoRotation(t *testing.T) {
|
||||
|
||||
@@ -29,7 +29,15 @@ func ProcessAttestations(
|
||||
v := &Validator{}
|
||||
var err error
|
||||
|
||||
for _, a := range append(state.PreviousEpochAttestations(), state.CurrentEpochAttestations()...) {
|
||||
prevAtt, err := state.PreviousEpochAttestations()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
curAtt, err := state.CurrentEpochAttestations()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
for _, a := range append(prevAtt, curAtt...) {
|
||||
if a.InclusionDelay == 0 {
|
||||
return nil, nil, errors.New("attestation with inclusion delay of 0")
|
||||
}
|
||||
|
||||
@@ -170,9 +170,9 @@ func TestProcessAttestations(t *testing.T) {
|
||||
require.NoError(t, beaconState.SetBlockRoots(br))
|
||||
att2.Data.Target.Root = newRt[:]
|
||||
att2.Data.BeaconBlockRoot = newRt[:]
|
||||
err := beaconState.SetPreviousEpochAttestations([]*pb.PendingAttestation{{Data: att1.Data, AggregationBits: bf, InclusionDelay: 1}})
|
||||
err := beaconState.AppendPreviousEpochAttestations(&pb.PendingAttestation{Data: att1.Data, AggregationBits: bf, InclusionDelay: 1})
|
||||
require.NoError(t, err)
|
||||
err = beaconState.SetCurrentEpochAttestations([]*pb.PendingAttestation{{Data: att2.Data, AggregationBits: bf, InclusionDelay: 1}})
|
||||
err = beaconState.AppendCurrentEpochAttestations(&pb.PendingAttestation{Data: att2.Data, AggregationBits: bf, InclusionDelay: 1})
|
||||
require.NoError(t, err)
|
||||
|
||||
pVals := make([]*precompute.Validator, validators)
|
||||
|
||||
@@ -73,8 +73,12 @@ func TestGenesisBeaconState_OK(t *testing.T) {
|
||||
|
||||
// Recent state checks.
|
||||
assert.DeepEqual(t, make([]uint64, params.BeaconConfig().EpochsPerSlashingsVector), newState.Slashings(), "Slashings was not correctly initialized")
|
||||
assert.DeepSSZEqual(t, []*pb.PendingAttestation{}, newState.CurrentEpochAttestations(), "CurrentEpochAttestations was not correctly initialized")
|
||||
assert.DeepSSZEqual(t, []*pb.PendingAttestation{}, newState.PreviousEpochAttestations(), "PreviousEpochAttestations was not correctly initialized")
|
||||
currAtt, err := newState.CurrentEpochAttestations()
|
||||
require.NoError(t, err)
|
||||
assert.DeepSSZEqual(t, []*pb.PendingAttestation{}, currAtt, "CurrentEpochAttestations was not correctly initialized")
|
||||
prevAtt, err := newState.CurrentEpochAttestations()
|
||||
require.NoError(t, err)
|
||||
assert.DeepSSZEqual(t, []*pb.PendingAttestation{}, prevAtt, "PreviousEpochAttestations was not correctly initialized")
|
||||
|
||||
zeroHash := params.BeaconConfig().ZeroHash[:]
|
||||
// History root checks.
|
||||
|
||||
@@ -293,7 +293,7 @@ func TestProcessBlock_IncorrectProcessExits(t *testing.T) {
|
||||
cp := beaconState.CurrentJustifiedCheckpoint()
|
||||
cp.Root = []byte("hello-world")
|
||||
require.NoError(t, beaconState.SetCurrentJustifiedCheckpoint(cp))
|
||||
require.NoError(t, beaconState.SetCurrentEpochAttestations([]*pb.PendingAttestation{}))
|
||||
require.NoError(t, beaconState.AppendCurrentEpochAttestations(&pb.PendingAttestation{}))
|
||||
_, err = state.VerifyOperationLengths(context.Background(), beaconState, block)
|
||||
wanted := "number of voluntary exits (17) in block body exceeds allowed threshold of 16"
|
||||
assert.ErrorContains(t, wanted, err)
|
||||
@@ -319,7 +319,7 @@ func createFullBlockWithOperations(t *testing.T) (iface.BeaconState,
|
||||
copy(mockRoot[:], "hello-world")
|
||||
cp.Root = mockRoot[:]
|
||||
require.NoError(t, beaconState.SetCurrentJustifiedCheckpoint(cp))
|
||||
require.NoError(t, beaconState.SetCurrentEpochAttestations([]*pb.PendingAttestation{}))
|
||||
require.NoError(t, beaconState.AppendCurrentEpochAttestations(&pb.PendingAttestation{}))
|
||||
|
||||
proposerSlashIdx := types.ValidatorIndex(3)
|
||||
slotsPerEpoch := params.BeaconConfig().SlotsPerEpoch
|
||||
|
||||
@@ -1516,8 +1516,8 @@ func TestServer_GetValidatorParticipation_CurrentAndPrevEpoch(t *testing.T) {
|
||||
require.NoError(t, headState.SetSlot(2*params.BeaconConfig().SlotsPerEpoch-1))
|
||||
require.NoError(t, headState.SetValidators(validators))
|
||||
require.NoError(t, headState.SetBalances(balances))
|
||||
require.NoError(t, headState.SetCurrentEpochAttestations(atts))
|
||||
require.NoError(t, headState.SetPreviousEpochAttestations(atts))
|
||||
require.NoError(t, headState.AppendCurrentEpochAttestations(atts[0]))
|
||||
require.NoError(t, headState.AppendPreviousEpochAttestations(atts[0]))
|
||||
|
||||
b := testutil.NewBeaconBlock()
|
||||
b.Block.Slot = 16
|
||||
@@ -1595,8 +1595,8 @@ func TestServer_GetValidatorParticipation_OrphanedUntilGenesis(t *testing.T) {
|
||||
require.NoError(t, headState.SetSlot(2*params.BeaconConfig().SlotsPerEpoch-1))
|
||||
require.NoError(t, headState.SetValidators(validators))
|
||||
require.NoError(t, headState.SetBalances(balances))
|
||||
require.NoError(t, headState.SetCurrentEpochAttestations(atts))
|
||||
require.NoError(t, headState.SetPreviousEpochAttestations(atts))
|
||||
require.NoError(t, headState.AppendCurrentEpochAttestations(atts[0]))
|
||||
require.NoError(t, headState.AppendPreviousEpochAttestations(atts[0]))
|
||||
|
||||
b := testutil.NewBeaconBlock()
|
||||
require.NoError(t, beaconDB.SaveBlock(ctx, b))
|
||||
@@ -1675,8 +1675,8 @@ func TestGetValidatorPerformance_OK(t *testing.T) {
|
||||
AggregationBits: bitfield.Bitlist{},
|
||||
InclusionDelay: 1,
|
||||
}
|
||||
require.NoError(t, headState.AppendPreviousEpochAttestations(atts[i]))
|
||||
}
|
||||
require.NoError(t, headState.SetPreviousEpochAttestations(atts))
|
||||
defaultBal := params.BeaconConfig().MaxEffectiveBalance
|
||||
extraBal := params.BeaconConfig().MaxEffectiveBalance + params.BeaconConfig().GweiPerEth
|
||||
balances := []uint64{defaultBal, extraBal, extraBal + params.BeaconConfig().GweiPerEth}
|
||||
@@ -2032,12 +2032,12 @@ func TestServer_GetIndividualVotes_Working(t *testing.T) {
|
||||
require.NoError(t, beaconState.SetBlockRoots(br))
|
||||
att2.Data.Target.Root = rt[:]
|
||||
att2.Data.BeaconBlockRoot = newRt[:]
|
||||
err = beaconState.SetPreviousEpochAttestations([]*pb.PendingAttestation{
|
||||
{Data: att1.Data, AggregationBits: bf, InclusionDelay: 1},
|
||||
err = beaconState.AppendPreviousEpochAttestations(&pb.PendingAttestation{
|
||||
Data: att1.Data, AggregationBits: bf, InclusionDelay: 1,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
err = beaconState.SetCurrentEpochAttestations([]*pb.PendingAttestation{
|
||||
{Data: att2.Data, AggregationBits: bf, InclusionDelay: 1},
|
||||
err = beaconState.AppendCurrentEpochAttestations(&pb.PendingAttestation{
|
||||
Data: att2.Data, AggregationBits: bf, InclusionDelay: 1,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
|
||||
@@ -134,8 +134,8 @@ type ReadOnlyEth1Data interface {
|
||||
|
||||
// ReadOnlyAttestations defines a struct which only has read access to attestations methods.
|
||||
type ReadOnlyAttestations interface {
|
||||
PreviousEpochAttestations() []*pbp2p.PendingAttestation
|
||||
CurrentEpochAttestations() []*pbp2p.PendingAttestation
|
||||
PreviousEpochAttestations() ([]*pbp2p.PendingAttestation, error)
|
||||
CurrentEpochAttestations() ([]*pbp2p.PendingAttestation, error)
|
||||
}
|
||||
|
||||
// WriteOnlyBlockRoots defines a struct which only has write access to block roots methods.
|
||||
@@ -189,8 +189,6 @@ type WriteOnlyCheckpoint interface {
|
||||
|
||||
// WriteOnlyAttestations defines a struct which only has write access to attestations methods.
|
||||
type WriteOnlyAttestations interface {
|
||||
SetPreviousEpochAttestations(val []*pbp2p.PendingAttestation) error
|
||||
SetCurrentEpochAttestations(val []*pbp2p.PendingAttestation) error
|
||||
AppendCurrentEpochAttestations(val *pbp2p.PendingAttestation) error
|
||||
AppendPreviousEpochAttestations(val *pbp2p.PendingAttestation) error
|
||||
RotateAttestations() error
|
||||
|
||||
@@ -781,18 +781,18 @@ func (b *BeaconState) slashings() []uint64 {
|
||||
}
|
||||
|
||||
// PreviousEpochAttestations corresponding to blocks on the beacon chain.
|
||||
func (b *BeaconState) PreviousEpochAttestations() []*pbp2p.PendingAttestation {
|
||||
func (b *BeaconState) PreviousEpochAttestations() ([]*pbp2p.PendingAttestation, error) {
|
||||
if !b.hasInnerState() {
|
||||
return nil
|
||||
return nil, nil
|
||||
}
|
||||
if b.state.PreviousEpochAttestations == nil {
|
||||
return nil
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
b.lock.RLock()
|
||||
defer b.lock.RUnlock()
|
||||
|
||||
return b.previousEpochAttestations()
|
||||
return b.previousEpochAttestations(), nil
|
||||
}
|
||||
|
||||
// previousEpochAttestations corresponding to blocks on the beacon chain.
|
||||
@@ -806,18 +806,18 @@ func (b *BeaconState) previousEpochAttestations() []*pbp2p.PendingAttestation {
|
||||
}
|
||||
|
||||
// CurrentEpochAttestations corresponding to blocks on the beacon chain.
|
||||
func (b *BeaconState) CurrentEpochAttestations() []*pbp2p.PendingAttestation {
|
||||
func (b *BeaconState) CurrentEpochAttestations() ([]*pbp2p.PendingAttestation, error) {
|
||||
if !b.hasInnerState() {
|
||||
return nil
|
||||
return nil, nil
|
||||
}
|
||||
if b.state.CurrentEpochAttestations == nil {
|
||||
return nil
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
b.lock.RLock()
|
||||
defer b.lock.RUnlock()
|
||||
|
||||
return b.currentEpochAttestations()
|
||||
return b.currentEpochAttestations(), nil
|
||||
}
|
||||
|
||||
// currentEpochAttestations corresponding to blocks on the beacon chain.
|
||||
|
||||
@@ -69,8 +69,10 @@ func TestNilState_NoPanic(t *testing.T) {
|
||||
_ = err
|
||||
_ = st.RandaoMixesLength()
|
||||
_ = st.Slashings()
|
||||
_ = st.PreviousEpochAttestations()
|
||||
_ = st.CurrentEpochAttestations()
|
||||
_, err = st.PreviousEpochAttestations()
|
||||
require.NoError(t, err)
|
||||
_, err = st.CurrentEpochAttestations()
|
||||
require.NoError(t, err)
|
||||
_ = st.JustificationBits()
|
||||
_ = st.PreviousJustifiedCheckpoint()
|
||||
_ = st.CurrentJustifiedCheckpoint()
|
||||
|
||||
@@ -191,10 +191,14 @@ func TestStateReferenceCopy_NoUnexpectedAttestationsMutation(t *testing.T) {
|
||||
{AggregationBits: bitfield.NewBitlist(1)},
|
||||
{AggregationBits: bitfield.NewBitlist(2)},
|
||||
}
|
||||
require.NoError(t, a.SetPreviousEpochAttestations(atts[:1]))
|
||||
require.NoError(t, a.SetCurrentEpochAttestations(atts[:1]))
|
||||
assert.Equal(t, 1, len(a.CurrentEpochAttestations()), "Unexpected number of attestations")
|
||||
assert.Equal(t, 1, len(a.PreviousEpochAttestations()), "Unexpected number of attestations")
|
||||
a.setPreviousEpochAttestations(atts[:1])
|
||||
a.setCurrentEpochAttestations(atts[:1])
|
||||
curAtt, err := a.CurrentEpochAttestations()
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 1, len(curAtt), "Unexpected number of attestations")
|
||||
preAtt, err := a.PreviousEpochAttestations()
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 1, len(preAtt), "Unexpected number of attestations")
|
||||
|
||||
// Copy, increases reference count.
|
||||
copied := a.Copy()
|
||||
@@ -226,8 +230,12 @@ func TestStateReferenceCopy_NoUnexpectedAttestationsMutation(t *testing.T) {
|
||||
// Extends state a attestations.
|
||||
require.NoError(t, a.AppendCurrentEpochAttestations(atts[1]))
|
||||
require.NoError(t, a.AppendPreviousEpochAttestations(atts[1]))
|
||||
assert.Equal(t, 2, len(a.CurrentEpochAttestations()), "Unexpected number of attestations")
|
||||
assert.Equal(t, 2, len(a.PreviousEpochAttestations()), "Unexpected number of attestations")
|
||||
curAtt, err = a.CurrentEpochAttestations()
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 2, len(curAtt), "Unexpected number of attestations")
|
||||
preAtt, err = a.PreviousEpochAttestations()
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 2, len(preAtt), "Unexpected number of attestations")
|
||||
assertAttFound(a.state.GetCurrentEpochAttestations(), 1)
|
||||
assertAttFound(a.state.GetPreviousEpochAttestations(), 1)
|
||||
assertAttFound(a.state.GetCurrentEpochAttestations(), 2)
|
||||
|
||||
@@ -496,19 +496,6 @@ func (b *BeaconState) UpdateSlashingsAtIndex(idx, val uint64) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetPreviousEpochAttestations for the beacon state. Updates the entire
|
||||
// list to a new value by overwriting the previous one.
|
||||
func (b *BeaconState) SetPreviousEpochAttestations(val []*pbp2p.PendingAttestation) error {
|
||||
if !b.hasInnerState() {
|
||||
return ErrNilInnerState
|
||||
}
|
||||
b.lock.Lock()
|
||||
defer b.lock.Unlock()
|
||||
|
||||
b.setPreviousEpochAttestations(val)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *BeaconState) setPreviousEpochAttestations(val []*pbp2p.PendingAttestation) {
|
||||
b.sharedFieldReferences[previousEpochAttestations].MinusRef()
|
||||
b.sharedFieldReferences[previousEpochAttestations] = stateutil.NewRef(1)
|
||||
@@ -518,19 +505,6 @@ func (b *BeaconState) setPreviousEpochAttestations(val []*pbp2p.PendingAttestati
|
||||
b.rebuildTrie[previousEpochAttestations] = true
|
||||
}
|
||||
|
||||
// SetCurrentEpochAttestations for the beacon state. Updates the entire
|
||||
// list to a new value by overwriting the previous one.
|
||||
func (b *BeaconState) SetCurrentEpochAttestations(val []*pbp2p.PendingAttestation) error {
|
||||
if !b.hasInnerState() {
|
||||
return ErrNilInnerState
|
||||
}
|
||||
b.lock.Lock()
|
||||
defer b.lock.Unlock()
|
||||
|
||||
b.setCurrentEpochAttestations(val)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *BeaconState) setCurrentEpochAttestations(val []*pbp2p.PendingAttestation) {
|
||||
b.sharedFieldReferences[currentEpochAttestations].MinusRef()
|
||||
b.sharedFieldReferences[currentEpochAttestations] = stateutil.NewRef(1)
|
||||
|
||||
@@ -18,6 +18,6 @@ func TestBeaconState_RotateAttestations(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
require.NoError(t, st.RotateAttestations())
|
||||
require.Equal(t, 0, len(st.CurrentEpochAttestations()))
|
||||
require.Equal(t, types.Slot(456), st.PreviousEpochAttestations()[0].Data.Slot)
|
||||
require.Equal(t, 0, len(st.currentEpochAttestations()))
|
||||
require.Equal(t, types.Slot(456), st.previousEpochAttestations()[0].Data.Slot)
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ func TestReplayBlocks_AllSkipSlots(t *testing.T) {
|
||||
copy(mockRoot[:], "hello-world")
|
||||
cp.Root = mockRoot[:]
|
||||
require.NoError(t, beaconState.SetCurrentJustifiedCheckpoint(cp))
|
||||
require.NoError(t, beaconState.SetCurrentEpochAttestations([]*pb.PendingAttestation{}))
|
||||
require.NoError(t, beaconState.AppendCurrentEpochAttestations(&pb.PendingAttestation{}))
|
||||
|
||||
service := New(beaconDB)
|
||||
targetSlot := params.BeaconConfig().SlotsPerEpoch - 1
|
||||
@@ -67,7 +67,7 @@ func TestReplayBlocks_SameSlot(t *testing.T) {
|
||||
copy(mockRoot[:], "hello-world")
|
||||
cp.Root = mockRoot[:]
|
||||
require.NoError(t, beaconState.SetCurrentJustifiedCheckpoint(cp))
|
||||
require.NoError(t, beaconState.SetCurrentEpochAttestations([]*pb.PendingAttestation{}))
|
||||
require.NoError(t, beaconState.AppendCurrentEpochAttestations(&pb.PendingAttestation{}))
|
||||
|
||||
service := New(beaconDB)
|
||||
targetSlot := beaconState.Slot()
|
||||
@@ -97,7 +97,7 @@ func TestReplayBlocks_LowerSlotBlock(t *testing.T) {
|
||||
copy(mockRoot[:], "hello-world")
|
||||
cp.Root = mockRoot[:]
|
||||
require.NoError(t, beaconState.SetCurrentJustifiedCheckpoint(cp))
|
||||
require.NoError(t, beaconState.SetCurrentEpochAttestations([]*pb.PendingAttestation{}))
|
||||
require.NoError(t, beaconState.AppendCurrentEpochAttestations(&pb.PendingAttestation{}))
|
||||
|
||||
service := New(beaconDB)
|
||||
targetSlot := beaconState.Slot()
|
||||
|
||||
@@ -119,7 +119,9 @@ func TestGenerateFullBlock_ValidAttestations(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
beaconState, err = state.ExecuteStateTransition(context.Background(), beaconState, block)
|
||||
require.NoError(t, err)
|
||||
if len(beaconState.CurrentEpochAttestations()) != 4 {
|
||||
atts, err := beaconState.CurrentEpochAttestations()
|
||||
require.NoError(t, err)
|
||||
if len(atts) != 4 {
|
||||
t.Fatal("expected 4 attestations to be saved to the beacon state")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user