mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-25 12:57:18 +00:00
8455656597
* return interface from testing/util * remove usages of v1 * return interface from InitializeFromProto * return interface from InitializeFromProto * fix test * fix interface visibility * more fixes * use InitializeFromProtoUnsafe in testing/util * return early error from mock * v2 * fix tests * remove unnecessary assertion * use struct in nil state test * Revert "Auxiliary commit to revert individual files from 6bb528c2c5df2446ad18450009f63f44318d41a9" This reverts commit 7d70238a301209f6dbfc8ff1d81b16e33b0bd67d. * use struct in sync committee test * v3 * use InitializeFromProtoUnsafe in mock * use version information * Revert "Auxiliary commit to revert individual files from 6bb528c2c5df2446ad18450009f63f44318d41a9" This reverts commit 5d5e6f2884d21caec7530c16ad2a0d0d27c44aa1. * revert changes to ClearPreGenesisData * fix build error * remove error from PreGenesisState * bzl
193 lines
5.7 KiB
Go
193 lines
5.7 KiB
Go
package v2
|
|
|
|
import (
|
|
"runtime/debug"
|
|
"sync"
|
|
"testing"
|
|
|
|
types "github.com/prysmaticlabs/eth2-types"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state"
|
|
fieldparams "github.com/prysmaticlabs/prysm/config/fieldparams"
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
|
"github.com/prysmaticlabs/prysm/testing/assert"
|
|
"github.com/prysmaticlabs/prysm/testing/require"
|
|
)
|
|
|
|
func TestBeaconState_SlotDataRace(t *testing.T) {
|
|
headState, err := InitializeFromProto(ðpb.BeaconStateAltair{Slot: 1})
|
|
require.NoError(t, err)
|
|
|
|
wg := sync.WaitGroup{}
|
|
wg.Add(2)
|
|
go func() {
|
|
require.NoError(t, headState.SetSlot(0))
|
|
wg.Done()
|
|
}()
|
|
go func() {
|
|
headState.Slot()
|
|
wg.Done()
|
|
}()
|
|
|
|
wg.Wait()
|
|
}
|
|
|
|
func TestNilState_NoPanic(t *testing.T) {
|
|
var st *BeaconState
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
t.Errorf("Method panicked when it was not supposed to: %v\n%v\n", r, string(debug.Stack()))
|
|
}
|
|
}()
|
|
// retrieve elements from nil state
|
|
_ = st.GenesisTime()
|
|
_ = st.GenesisValidatorRoot()
|
|
_ = st.GenesisValidatorRoot()
|
|
_ = st.Slot()
|
|
_ = st.Fork()
|
|
_ = st.LatestBlockHeader()
|
|
_ = st.BlockRoots()
|
|
_, err := st.BlockRootAtIndex(0)
|
|
_ = err
|
|
_ = st.StateRoots()
|
|
_ = st.HistoricalRoots()
|
|
_ = st.Eth1Data()
|
|
_ = st.Eth1DataVotes()
|
|
_ = st.Eth1DepositIndex()
|
|
_, err = st.ValidatorAtIndex(0)
|
|
_ = err
|
|
_, err = st.ValidatorAtIndexReadOnly(0)
|
|
_ = err
|
|
_, _ = st.ValidatorIndexByPubkey([fieldparams.BLSPubkeyLength]byte{})
|
|
_ = st.PubkeyAtIndex(0)
|
|
_ = st.NumValidators()
|
|
_ = st.Balances()
|
|
_, err = st.BalanceAtIndex(0)
|
|
_ = err
|
|
_ = st.BalancesLength()
|
|
_ = st.RandaoMixes()
|
|
_, err = st.RandaoMixAtIndex(0)
|
|
_ = err
|
|
_ = st.RandaoMixesLength()
|
|
_ = st.Slashings()
|
|
_, err = st.CurrentEpochParticipation()
|
|
_ = err
|
|
_, err = st.PreviousEpochParticipation()
|
|
_ = err
|
|
_ = st.JustificationBits()
|
|
_ = st.PreviousJustifiedCheckpoint()
|
|
_ = st.CurrentJustifiedCheckpoint()
|
|
_ = st.FinalizedCheckpoint()
|
|
_, err = st.CurrentEpochParticipation()
|
|
_ = err
|
|
_, err = st.PreviousEpochParticipation()
|
|
_ = err
|
|
_, err = st.InactivityScores()
|
|
_ = err
|
|
_, err = st.CurrentSyncCommittee()
|
|
_ = err
|
|
_, err = st.NextSyncCommittee()
|
|
_ = err
|
|
}
|
|
|
|
func TestBeaconState_ValidatorByPubkey(t *testing.T) {
|
|
keyCreator := func(input []byte) [fieldparams.BLSPubkeyLength]byte {
|
|
nKey := [fieldparams.BLSPubkeyLength]byte{}
|
|
copy(nKey[:1], input)
|
|
return nKey
|
|
}
|
|
|
|
tests := []struct {
|
|
name string
|
|
modifyFunc func(b state.BeaconStateAltair, k [fieldparams.BLSPubkeyLength]byte)
|
|
exists bool
|
|
expectedIdx types.ValidatorIndex
|
|
largestIdxInSet types.ValidatorIndex
|
|
}{
|
|
{
|
|
name: "retrieve validator",
|
|
modifyFunc: func(b state.BeaconStateAltair, key [fieldparams.BLSPubkeyLength]byte) {
|
|
assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key[:]}))
|
|
},
|
|
exists: true,
|
|
expectedIdx: 0,
|
|
},
|
|
{
|
|
name: "retrieve validator with multiple validators from the start",
|
|
modifyFunc: func(b state.BeaconStateAltair, key [fieldparams.BLSPubkeyLength]byte) {
|
|
key1 := keyCreator([]byte{'C'})
|
|
key2 := keyCreator([]byte{'D'})
|
|
assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key[:]}))
|
|
assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key1[:]}))
|
|
assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key2[:]}))
|
|
},
|
|
exists: true,
|
|
expectedIdx: 0,
|
|
},
|
|
{
|
|
name: "retrieve validator with multiple validators",
|
|
modifyFunc: func(b state.BeaconStateAltair, key [fieldparams.BLSPubkeyLength]byte) {
|
|
key1 := keyCreator([]byte{'C'})
|
|
key2 := keyCreator([]byte{'D'})
|
|
assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key1[:]}))
|
|
assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key2[:]}))
|
|
assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key[:]}))
|
|
},
|
|
exists: true,
|
|
expectedIdx: 2,
|
|
},
|
|
{
|
|
name: "retrieve validator with multiple validators from the start with shared state",
|
|
modifyFunc: func(b state.BeaconStateAltair, key [fieldparams.BLSPubkeyLength]byte) {
|
|
key1 := keyCreator([]byte{'C'})
|
|
key2 := keyCreator([]byte{'D'})
|
|
assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key[:]}))
|
|
_ = b.Copy()
|
|
assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key1[:]}))
|
|
assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key2[:]}))
|
|
},
|
|
exists: true,
|
|
expectedIdx: 0,
|
|
},
|
|
{
|
|
name: "retrieve validator with multiple validators with shared state",
|
|
modifyFunc: func(b state.BeaconStateAltair, key [fieldparams.BLSPubkeyLength]byte) {
|
|
key1 := keyCreator([]byte{'C'})
|
|
key2 := keyCreator([]byte{'D'})
|
|
assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key1[:]}))
|
|
assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key2[:]}))
|
|
n := b.Copy()
|
|
// Append to another state
|
|
assert.NoError(t, n.AppendValidator(ðpb.Validator{PublicKey: key[:]}))
|
|
|
|
},
|
|
exists: false,
|
|
expectedIdx: 0,
|
|
},
|
|
{
|
|
name: "retrieve validator with multiple validators with shared state at boundary",
|
|
modifyFunc: func(b state.BeaconStateAltair, key [fieldparams.BLSPubkeyLength]byte) {
|
|
key1 := keyCreator([]byte{'C'})
|
|
assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key1[:]}))
|
|
n := b.Copy()
|
|
// Append to another state
|
|
assert.NoError(t, n.AppendValidator(ðpb.Validator{PublicKey: key[:]}))
|
|
|
|
},
|
|
exists: false,
|
|
expectedIdx: 0,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
s, err := InitializeFromProto(ðpb.BeaconStateAltair{})
|
|
require.NoError(t, err)
|
|
nKey := keyCreator([]byte{'A'})
|
|
tt.modifyFunc(s, nKey)
|
|
idx, ok := s.ValidatorIndexByPubkey(nKey)
|
|
assert.Equal(t, tt.exists, ok)
|
|
assert.Equal(t, tt.expectedIdx, idx)
|
|
})
|
|
}
|
|
}
|