2021-07-26 20:55:30 +00:00
|
|
|
package v2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"runtime/debug"
|
|
|
|
"testing"
|
2021-07-27 15:47:03 +00:00
|
|
|
|
2022-08-16 12:20:13 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v3/beacon-chain/state"
|
|
|
|
testtmpl "github.com/prysmaticlabs/prysm/v3/beacon-chain/state/testing"
|
|
|
|
fieldparams "github.com/prysmaticlabs/prysm/v3/config/fieldparams"
|
|
|
|
ethpb "github.com/prysmaticlabs/prysm/v3/proto/prysm/v1alpha1"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/testing/require"
|
2021-07-26 20:55:30 +00:00
|
|
|
)
|
|
|
|
|
2021-07-27 15:47:03 +00:00
|
|
|
func TestBeaconState_SlotDataRace(t *testing.T) {
|
2022-03-15 18:32:09 +00:00
|
|
|
testtmpl.VerifyBeaconStateSlotDataRace(t, func() (state.BeaconState, error) {
|
2022-02-11 06:48:29 +00:00
|
|
|
return InitializeFromProto(ðpb.BeaconStateAltair{Slot: 1})
|
|
|
|
})
|
2021-07-27 15:47:03 +00:00
|
|
|
}
|
|
|
|
|
2021-07-26 20:55:30 +00:00
|
|
|
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()
|
2022-02-14 13:34:38 +00:00
|
|
|
_ = st.GenesisValidatorsRoot()
|
|
|
|
_ = st.GenesisValidatorsRoot()
|
2021-07-26 20:55:30 +00:00
|
|
|
_ = 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
|
2022-01-06 17:33:08 +00:00
|
|
|
_, _ = st.ValidatorIndexByPubkey([fieldparams.BLSPubkeyLength]byte{})
|
2021-07-26 20:55:30 +00:00
|
|
|
_ = st.PubkeyAtIndex(0)
|
|
|
|
_ = st.NumValidators()
|
|
|
|
_ = st.Balances()
|
|
|
|
_, err = st.BalanceAtIndex(0)
|
|
|
|
_ = err
|
|
|
|
_ = st.BalancesLength()
|
|
|
|
_ = st.RandaoMixes()
|
|
|
|
_, err = st.RandaoMixAtIndex(0)
|
2022-05-17 09:38:35 +00:00
|
|
|
require.ErrorIs(t, ErrNilInnerState, err)
|
2021-07-26 20:55:30 +00:00
|
|
|
_ = st.RandaoMixesLength()
|
|
|
|
_ = st.Slashings()
|
|
|
|
_, err = st.CurrentEpochParticipation()
|
2022-05-17 09:38:35 +00:00
|
|
|
require.ErrorIs(t, ErrNilInnerState, err)
|
2021-07-26 20:55:30 +00:00
|
|
|
_, err = st.PreviousEpochParticipation()
|
2022-05-17 09:38:35 +00:00
|
|
|
require.ErrorIs(t, ErrNilInnerState, err)
|
2021-07-26 20:55:30 +00:00
|
|
|
_ = st.JustificationBits()
|
2022-05-09 13:02:34 +00:00
|
|
|
_ = err
|
2021-07-26 20:55:30 +00:00
|
|
|
_ = st.PreviousJustifiedCheckpoint()
|
|
|
|
_ = st.CurrentJustifiedCheckpoint()
|
|
|
|
_ = st.FinalizedCheckpoint()
|
|
|
|
_, err = st.CurrentEpochParticipation()
|
2022-05-17 09:38:35 +00:00
|
|
|
require.ErrorIs(t, ErrNilInnerState, err)
|
2021-07-26 20:55:30 +00:00
|
|
|
_, err = st.PreviousEpochParticipation()
|
2022-05-17 09:38:35 +00:00
|
|
|
require.ErrorIs(t, ErrNilInnerState, err)
|
2021-07-26 20:55:30 +00:00
|
|
|
_, err = st.InactivityScores()
|
|
|
|
_ = err
|
|
|
|
_, err = st.CurrentSyncCommittee()
|
2022-05-17 09:38:35 +00:00
|
|
|
require.ErrorIs(t, ErrNilInnerState, err)
|
2021-07-26 20:55:30 +00:00
|
|
|
_, err = st.NextSyncCommittee()
|
2022-05-17 09:38:35 +00:00
|
|
|
require.ErrorIs(t, ErrNilInnerState, err)
|
2022-05-27 16:38:00 +00:00
|
|
|
_, _, _, err = st.UnrealizedCheckpointBalances()
|
|
|
|
require.ErrorIs(t, ErrNilInnerState, err)
|
2021-07-26 20:55:30 +00:00
|
|
|
}
|
2021-08-19 19:33:05 +00:00
|
|
|
|
2022-02-11 06:48:29 +00:00
|
|
|
func TestBeaconState_MatchCurrentJustifiedCheckpt(t *testing.T) {
|
2022-03-15 18:32:09 +00:00
|
|
|
testtmpl.VerifyBeaconStateMatchCurrentJustifiedCheckpt(
|
2022-02-11 06:48:29 +00:00
|
|
|
t,
|
|
|
|
func(cp *ethpb.Checkpoint) (state.BeaconState, error) {
|
|
|
|
return InitializeFromProto(ðpb.BeaconStateAltair{CurrentJustifiedCheckpoint: cp})
|
2021-08-19 19:33:05 +00:00
|
|
|
},
|
2022-02-11 06:48:29 +00:00
|
|
|
func(i state.BeaconState) {
|
|
|
|
s, ok := i.(*BeaconState)
|
|
|
|
if !ok {
|
|
|
|
panic("error in type assertion in test template")
|
|
|
|
}
|
|
|
|
s.state = nil
|
2021-08-19 19:33:05 +00:00
|
|
|
},
|
2022-02-11 06:48:29 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBeaconState_MatchPreviousJustifiedCheckpt(t *testing.T) {
|
2022-03-15 18:32:09 +00:00
|
|
|
testtmpl.VerifyBeaconStateMatchPreviousJustifiedCheckpt(
|
2022-02-11 06:48:29 +00:00
|
|
|
t,
|
|
|
|
func(cp *ethpb.Checkpoint) (state.BeaconState, error) {
|
|
|
|
return InitializeFromProto(ðpb.BeaconStateAltair{PreviousJustifiedCheckpoint: cp})
|
2021-08-19 19:33:05 +00:00
|
|
|
},
|
2022-02-11 06:48:29 +00:00
|
|
|
func(i state.BeaconState) {
|
|
|
|
s, ok := i.(*BeaconState)
|
|
|
|
if !ok {
|
|
|
|
panic("error in type assertion in test template")
|
|
|
|
}
|
|
|
|
s.state = nil
|
2021-08-19 19:33:05 +00:00
|
|
|
},
|
2022-02-11 06:48:29 +00:00
|
|
|
)
|
|
|
|
}
|
2021-08-19 19:33:05 +00:00
|
|
|
|
2022-02-11 06:48:29 +00:00
|
|
|
func TestBeaconState_MarshalSSZ_NilState(t *testing.T) {
|
2022-03-15 18:32:09 +00:00
|
|
|
testtmpl.VerifyBeaconStateMarshalSSZNilState(
|
2022-02-11 06:48:29 +00:00
|
|
|
t,
|
|
|
|
func() (state.BeaconState, error) {
|
|
|
|
return InitializeFromProto(ðpb.BeaconStateAltair{})
|
2021-08-19 19:33:05 +00:00
|
|
|
},
|
2022-02-11 06:48:29 +00:00
|
|
|
func(i state.BeaconState) {
|
|
|
|
s, ok := i.(*BeaconState)
|
|
|
|
if !ok {
|
|
|
|
panic("error in type assertion in test template")
|
|
|
|
}
|
|
|
|
s.state = nil
|
2021-08-19 19:33:05 +00:00
|
|
|
},
|
2022-02-11 06:48:29 +00:00
|
|
|
)
|
|
|
|
}
|
2021-08-19 19:33:05 +00:00
|
|
|
|
2022-02-11 06:48:29 +00:00
|
|
|
func TestBeaconState_ValidatorByPubkey(t *testing.T) {
|
2022-03-15 18:32:09 +00:00
|
|
|
testtmpl.VerifyBeaconStateValidatorByPubkey(t, func() (state.BeaconState, error) {
|
2022-02-11 06:48:29 +00:00
|
|
|
return InitializeFromProto(ðpb.BeaconStateAltair{})
|
|
|
|
})
|
2021-08-19 19:33:05 +00:00
|
|
|
}
|