2021-06-30 15:06:19 +00:00
|
|
|
package v1
|
2020-03-11 09:11:07 +00:00
|
|
|
|
|
|
|
import (
|
2020-05-04 10:15:33 +00:00
|
|
|
"runtime/debug"
|
2020-03-11 09:11:07 +00:00
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
2021-02-16 07:45:34 +00:00
|
|
|
types "github.com/prysmaticlabs/eth2-types"
|
2021-07-21 21:34:07 +00:00
|
|
|
eth "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
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"
|
2020-03-11 09:11:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBeaconState_SlotDataRace(t *testing.T) {
|
2021-07-29 21:45:17 +00:00
|
|
|
headState, err := InitializeFromProto(ðpb.BeaconState{Slot: 1})
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, err)
|
2020-03-11 09:11:07 +00:00
|
|
|
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(2)
|
|
|
|
go func() {
|
2021-02-16 07:45:34 +00:00
|
|
|
require.NoError(t, headState.SetSlot(0))
|
2020-03-11 09:11:07 +00:00
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
headState.Slot()
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}
|
2020-04-22 04:44:33 +00:00
|
|
|
|
|
|
|
func TestNilState_NoPanic(t *testing.T) {
|
|
|
|
var st *BeaconState
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
2020-05-04 10:15:33 +00:00
|
|
|
t.Errorf("Method panicked when it was not supposed to: %v\n%v\n", r, string(debug.Stack()))
|
2020-04-22 04:44:33 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
// retrieve elements from nil state
|
2020-05-04 10:15:33 +00:00
|
|
|
_ = st.GenesisTime()
|
2020-04-22 04:44:33 +00:00
|
|
|
_ = st.GenesisValidatorRoot()
|
2020-05-04 10:15:33 +00:00
|
|
|
_ = st.GenesisValidatorRoot()
|
|
|
|
_ = st.Slot()
|
|
|
|
_ = st.Fork()
|
|
|
|
_ = st.LatestBlockHeader()
|
|
|
|
_ = st.BlockRoots()
|
|
|
|
_, err := st.BlockRootAtIndex(0)
|
2020-10-01 18:53:36 +00:00
|
|
|
_ = err
|
2020-05-04 10:15:33 +00:00
|
|
|
_ = st.StateRoots()
|
|
|
|
_ = st.HistoricalRoots()
|
2020-04-22 04:44:33 +00:00
|
|
|
_ = st.Eth1Data()
|
2020-05-04 10:15:33 +00:00
|
|
|
_ = st.Eth1DataVotes()
|
|
|
|
_ = st.Eth1DepositIndex()
|
|
|
|
_, err = st.ValidatorAtIndex(0)
|
2020-10-01 18:53:36 +00:00
|
|
|
_ = err
|
2020-05-04 10:15:33 +00:00
|
|
|
_, err = st.ValidatorAtIndexReadOnly(0)
|
2020-10-01 18:53:36 +00:00
|
|
|
_ = err
|
2020-05-04 10:15:33 +00:00
|
|
|
_, _ = st.ValidatorIndexByPubkey([48]byte{})
|
|
|
|
_ = st.PubkeyAtIndex(0)
|
|
|
|
_ = st.NumValidators()
|
|
|
|
_ = st.Balances()
|
|
|
|
_, err = st.BalanceAtIndex(0)
|
2020-10-01 18:53:36 +00:00
|
|
|
_ = err
|
2020-05-04 10:15:33 +00:00
|
|
|
_ = st.BalancesLength()
|
|
|
|
_ = st.RandaoMixes()
|
|
|
|
_, err = st.RandaoMixAtIndex(0)
|
2020-10-01 18:53:36 +00:00
|
|
|
_ = err
|
2020-05-04 10:15:33 +00:00
|
|
|
_ = st.RandaoMixesLength()
|
|
|
|
_ = st.Slashings()
|
2021-03-26 18:15:03 +00:00
|
|
|
_, err = st.PreviousEpochAttestations()
|
|
|
|
require.NoError(t, err)
|
|
|
|
_, err = st.CurrentEpochAttestations()
|
|
|
|
require.NoError(t, err)
|
2020-05-04 10:15:33 +00:00
|
|
|
_ = st.JustificationBits()
|
|
|
|
_ = st.PreviousJustifiedCheckpoint()
|
|
|
|
_ = st.CurrentJustifiedCheckpoint()
|
|
|
|
_ = st.FinalizedCheckpoint()
|
2020-04-22 04:44:33 +00:00
|
|
|
}
|
2020-06-11 17:38:03 +00:00
|
|
|
|
2021-02-10 19:30:11 +00:00
|
|
|
func TestBeaconState_MatchCurrentJustifiedCheckpt(t *testing.T) {
|
|
|
|
c1 := ð.Checkpoint{Epoch: 1}
|
|
|
|
c2 := ð.Checkpoint{Epoch: 2}
|
2021-07-29 21:45:17 +00:00
|
|
|
beaconState, err := InitializeFromProto(ðpb.BeaconState{CurrentJustifiedCheckpoint: c1})
|
2021-02-10 19:30:11 +00:00
|
|
|
require.NoError(t, err)
|
2021-03-08 22:37:33 +00:00
|
|
|
require.Equal(t, true, beaconState.MatchCurrentJustifiedCheckpoint(c1))
|
|
|
|
require.Equal(t, false, beaconState.MatchCurrentJustifiedCheckpoint(c2))
|
|
|
|
require.Equal(t, false, beaconState.MatchPreviousJustifiedCheckpoint(c1))
|
|
|
|
require.Equal(t, false, beaconState.MatchPreviousJustifiedCheckpoint(c2))
|
|
|
|
beaconState.state = nil
|
|
|
|
require.Equal(t, false, beaconState.MatchCurrentJustifiedCheckpoint(c1))
|
2021-02-10 19:30:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBeaconState_MatchPreviousJustifiedCheckpt(t *testing.T) {
|
|
|
|
c1 := ð.Checkpoint{Epoch: 1}
|
|
|
|
c2 := ð.Checkpoint{Epoch: 2}
|
2021-07-29 21:45:17 +00:00
|
|
|
beaconState, err := InitializeFromProto(ðpb.BeaconState{PreviousJustifiedCheckpoint: c1})
|
2021-02-10 19:30:11 +00:00
|
|
|
require.NoError(t, err)
|
2021-03-08 22:37:33 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, false, beaconState.MatchCurrentJustifiedCheckpoint(c1))
|
|
|
|
require.Equal(t, false, beaconState.MatchCurrentJustifiedCheckpoint(c2))
|
|
|
|
require.Equal(t, true, beaconState.MatchPreviousJustifiedCheckpoint(c1))
|
|
|
|
require.Equal(t, false, beaconState.MatchPreviousJustifiedCheckpoint(c2))
|
|
|
|
beaconState.state = nil
|
|
|
|
require.Equal(t, false, beaconState.MatchPreviousJustifiedCheckpoint(c1))
|
2021-02-10 19:30:11 +00:00
|
|
|
}
|
2021-03-16 00:43:27 +00:00
|
|
|
|
|
|
|
func TestBeaconState_MarshalSSZ_NilState(t *testing.T) {
|
2021-07-29 21:45:17 +00:00
|
|
|
s, err := InitializeFromProto(ðpb.BeaconState{})
|
2021-03-16 00:43:27 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
s.state = nil
|
|
|
|
_, err = s.MarshalSSZ()
|
|
|
|
require.ErrorContains(t, "nil beacon state", err)
|
|
|
|
}
|
2021-05-05 08:28:35 +00:00
|
|
|
|
|
|
|
func TestBeaconState_ValidatorByPubkey(t *testing.T) {
|
|
|
|
keyCreator := func(input []byte) [48]byte {
|
|
|
|
nKey := [48]byte{}
|
|
|
|
copy(nKey[:1], input)
|
|
|
|
return nKey
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
modifyFunc func(b *BeaconState, k [48]byte)
|
|
|
|
exists bool
|
|
|
|
expectedIdx types.ValidatorIndex
|
|
|
|
largestIdxInSet types.ValidatorIndex
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "retrieve validator",
|
|
|
|
modifyFunc: func(b *BeaconState, key [48]byte) {
|
|
|
|
assert.NoError(t, b.AppendValidator(ð.Validator{PublicKey: key[:]}))
|
|
|
|
},
|
|
|
|
exists: true,
|
|
|
|
expectedIdx: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "retrieve validator with multiple validators from the start",
|
|
|
|
modifyFunc: func(b *BeaconState, key [48]byte) {
|
|
|
|
key1 := keyCreator([]byte{'C'})
|
|
|
|
key2 := keyCreator([]byte{'D'})
|
|
|
|
assert.NoError(t, b.AppendValidator(ð.Validator{PublicKey: key[:]}))
|
|
|
|
assert.NoError(t, b.AppendValidator(ð.Validator{PublicKey: key1[:]}))
|
|
|
|
assert.NoError(t, b.AppendValidator(ð.Validator{PublicKey: key2[:]}))
|
|
|
|
},
|
|
|
|
exists: true,
|
|
|
|
expectedIdx: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "retrieve validator with multiple validators",
|
|
|
|
modifyFunc: func(b *BeaconState, key [48]byte) {
|
|
|
|
key1 := keyCreator([]byte{'C'})
|
|
|
|
key2 := keyCreator([]byte{'D'})
|
|
|
|
assert.NoError(t, b.AppendValidator(ð.Validator{PublicKey: key1[:]}))
|
|
|
|
assert.NoError(t, b.AppendValidator(ð.Validator{PublicKey: key2[:]}))
|
|
|
|
assert.NoError(t, b.AppendValidator(ð.Validator{PublicKey: key[:]}))
|
|
|
|
},
|
|
|
|
exists: true,
|
|
|
|
expectedIdx: 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "retrieve validator with multiple validators from the start with shared state",
|
|
|
|
modifyFunc: func(b *BeaconState, key [48]byte) {
|
|
|
|
key1 := keyCreator([]byte{'C'})
|
|
|
|
key2 := keyCreator([]byte{'D'})
|
|
|
|
assert.NoError(t, b.AppendValidator(ð.Validator{PublicKey: key[:]}))
|
|
|
|
_ = b.Copy()
|
|
|
|
assert.NoError(t, b.AppendValidator(ð.Validator{PublicKey: key1[:]}))
|
|
|
|
assert.NoError(t, b.AppendValidator(ð.Validator{PublicKey: key2[:]}))
|
|
|
|
},
|
|
|
|
exists: true,
|
|
|
|
expectedIdx: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "retrieve validator with multiple validators with shared state",
|
|
|
|
modifyFunc: func(b *BeaconState, key [48]byte) {
|
|
|
|
key1 := keyCreator([]byte{'C'})
|
|
|
|
key2 := keyCreator([]byte{'D'})
|
|
|
|
assert.NoError(t, b.AppendValidator(ð.Validator{PublicKey: key1[:]}))
|
|
|
|
assert.NoError(t, b.AppendValidator(ð.Validator{PublicKey: key2[:]}))
|
|
|
|
n := b.Copy()
|
|
|
|
// Append to another state
|
|
|
|
assert.NoError(t, n.AppendValidator(ð.Validator{PublicKey: key[:]}))
|
|
|
|
|
|
|
|
},
|
|
|
|
exists: false,
|
|
|
|
expectedIdx: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "retrieve validator with multiple validators with shared state at boundary",
|
|
|
|
modifyFunc: func(b *BeaconState, key [48]byte) {
|
|
|
|
key1 := keyCreator([]byte{'C'})
|
|
|
|
assert.NoError(t, b.AppendValidator(ð.Validator{PublicKey: key1[:]}))
|
|
|
|
n := b.Copy()
|
|
|
|
// Append to another state
|
|
|
|
assert.NoError(t, n.AppendValidator(ð.Validator{PublicKey: key[:]}))
|
|
|
|
|
|
|
|
},
|
|
|
|
exists: false,
|
|
|
|
expectedIdx: 0,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2021-07-29 21:45:17 +00:00
|
|
|
s, err := InitializeFromProto(ðpb.BeaconState{})
|
2021-05-05 08:28:35 +00:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|