2021-06-30 15:06:19 +00:00
|
|
|
package v1
|
2021-03-25 22:05:15 +00:00
|
|
|
|
|
|
|
import (
|
2021-04-13 19:12:56 +00:00
|
|
|
"context"
|
2021-03-25 22:05:15 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
types "github.com/prysmaticlabs/eth2-types"
|
2021-08-09 21:54:24 +00:00
|
|
|
stateTypes "github.com/prysmaticlabs/prysm/beacon-chain/state/types"
|
2021-09-21 19:59:25 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/config/params"
|
2021-07-29 21:45:17 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
2021-09-23 18:53:46 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/testing/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/testing/require"
|
2021-03-25 22:05:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBeaconState_RotateAttestations(t *testing.T) {
|
2021-07-29 21:45:17 +00:00
|
|
|
st, err := InitializeFromProto(ðpb.BeaconState{
|
2021-03-25 22:05:15 +00:00
|
|
|
Slot: 1,
|
2021-12-07 17:52:39 +00:00
|
|
|
CurrentEpochAttestations: []*ethpb.PendingAttestation{{Data: ðpb.AttestationData{Slot: 456}}},
|
|
|
|
PreviousEpochAttestations: []*ethpb.PendingAttestation{{Data: ðpb.AttestationData{Slot: 123}}},
|
2021-03-25 22:05:15 +00:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.NoError(t, st.RotateAttestations())
|
2022-02-08 09:30:06 +00:00
|
|
|
s, ok := st.(*BeaconState)
|
|
|
|
require.Equal(t, true, ok)
|
|
|
|
require.Equal(t, 0, len(s.currentEpochAttestations()))
|
|
|
|
require.Equal(t, types.Slot(456), s.previousEpochAttestations()[0].Data.Slot)
|
2021-03-25 22:05:15 +00:00
|
|
|
}
|
2021-04-13 19:12:56 +00:00
|
|
|
|
|
|
|
func TestAppendBeyondIndicesLimit(t *testing.T) {
|
|
|
|
zeroHash := params.BeaconConfig().ZeroHash
|
|
|
|
mockblockRoots := make([][]byte, params.BeaconConfig().SlotsPerHistoricalRoot)
|
|
|
|
for i := 0; i < len(mockblockRoots); i++ {
|
|
|
|
mockblockRoots[i] = zeroHash[:]
|
|
|
|
}
|
|
|
|
|
|
|
|
mockstateRoots := make([][]byte, params.BeaconConfig().SlotsPerHistoricalRoot)
|
|
|
|
for i := 0; i < len(mockstateRoots); i++ {
|
|
|
|
mockstateRoots[i] = zeroHash[:]
|
|
|
|
}
|
|
|
|
mockrandaoMixes := make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector)
|
|
|
|
for i := 0; i < len(mockrandaoMixes); i++ {
|
|
|
|
mockrandaoMixes[i] = zeroHash[:]
|
|
|
|
}
|
2022-02-08 09:30:06 +00:00
|
|
|
newState, err := InitializeFromProto(ðpb.BeaconState{
|
2021-04-13 19:12:56 +00:00
|
|
|
Slot: 1,
|
2021-12-07 17:52:39 +00:00
|
|
|
CurrentEpochAttestations: []*ethpb.PendingAttestation{{Data: ðpb.AttestationData{Slot: 456}}},
|
|
|
|
PreviousEpochAttestations: []*ethpb.PendingAttestation{{Data: ðpb.AttestationData{Slot: 123}}},
|
|
|
|
Validators: []*ethpb.Validator{},
|
|
|
|
Eth1Data: ðpb.Eth1Data{},
|
2021-04-13 19:12:56 +00:00
|
|
|
BlockRoots: mockblockRoots,
|
|
|
|
StateRoots: mockstateRoots,
|
|
|
|
RandaoMixes: mockrandaoMixes,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2022-02-08 09:30:06 +00:00
|
|
|
_, err = newState.HashTreeRoot(context.Background())
|
2021-04-13 19:12:56 +00:00
|
|
|
require.NoError(t, err)
|
2022-02-08 09:30:06 +00:00
|
|
|
st, ok := newState.(*BeaconState)
|
|
|
|
require.Equal(t, true, ok)
|
2021-08-09 21:54:24 +00:00
|
|
|
for i := stateTypes.FieldIndex(0); i < stateTypes.FieldIndex(params.BeaconConfig().BeaconStateFieldCount); i++ {
|
2021-04-13 19:12:56 +00:00
|
|
|
st.dirtyFields[i] = true
|
|
|
|
}
|
|
|
|
_, err = st.HashTreeRoot(context.Background())
|
|
|
|
require.NoError(t, err)
|
|
|
|
for i := 0; i < 10; i++ {
|
2021-12-07 17:52:39 +00:00
|
|
|
assert.NoError(t, st.AppendValidator(ðpb.Validator{}))
|
2021-04-13 19:12:56 +00:00
|
|
|
}
|
|
|
|
assert.Equal(t, false, st.rebuildTrie[validators])
|
2021-05-13 06:29:14 +00:00
|
|
|
assert.NotEqual(t, len(st.dirtyIndices[validators]), 0)
|
2021-04-13 19:12:56 +00:00
|
|
|
|
|
|
|
for i := 0; i < indicesLimit; i++ {
|
2021-12-07 17:52:39 +00:00
|
|
|
assert.NoError(t, st.AppendValidator(ðpb.Validator{}))
|
2021-04-13 19:12:56 +00:00
|
|
|
}
|
|
|
|
assert.Equal(t, true, st.rebuildTrie[validators])
|
2021-05-13 06:29:14 +00:00
|
|
|
assert.Equal(t, len(st.dirtyIndices[validators]), 0)
|
2021-04-13 19:12:56 +00:00
|
|
|
}
|