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
|
|
|
"testing"
|
|
|
|
|
2022-02-08 09:30:06 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state"
|
2022-02-11 06:48:29 +00:00
|
|
|
testtmpl "github.com/prysmaticlabs/prysm/beacon-chain/state/testing"
|
2022-01-06 17:33:08 +00:00
|
|
|
fieldparams "github.com/prysmaticlabs/prysm/config/fieldparams"
|
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/require"
|
2020-03-11 09:11:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBeaconState_SlotDataRace(t *testing.T) {
|
2022-02-11 06:48:29 +00:00
|
|
|
testtmpl.VerifyBeaconState_SlotDataRace(t, func() (state.BeaconState, error) {
|
|
|
|
return InitializeFromProto(ðpb.BeaconState{Slot: 1})
|
|
|
|
})
|
2020-03-11 09:11:07 +00:00
|
|
|
}
|
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
|
2022-01-06 17:33:08 +00:00
|
|
|
_, _ = st.ValidatorIndexByPubkey([fieldparams.BLSPubkeyLength]byte{})
|
2020-05-04 10:15:33 +00:00
|
|
|
_ = 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) {
|
2022-02-11 06:48:29 +00:00
|
|
|
testtmpl.VerifyBeaconState_MatchCurrentJustifiedCheckpt(
|
|
|
|
t,
|
|
|
|
func(cp *ethpb.Checkpoint) (state.BeaconState, error) {
|
|
|
|
return InitializeFromProto(ðpb.BeaconState{CurrentJustifiedCheckpoint: cp})
|
|
|
|
},
|
|
|
|
func(i state.BeaconState) {
|
|
|
|
s, ok := i.(*BeaconState)
|
|
|
|
if !ok {
|
|
|
|
panic("error in type assertion in test template")
|
|
|
|
}
|
|
|
|
s.state = nil
|
|
|
|
},
|
|
|
|
)
|
2021-02-10 19:30:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBeaconState_MatchPreviousJustifiedCheckpt(t *testing.T) {
|
2022-02-11 06:48:29 +00:00
|
|
|
testtmpl.VerifyBeaconState_MatchPreviousJustifiedCheckpt(
|
|
|
|
t,
|
|
|
|
func(cp *ethpb.Checkpoint) (state.BeaconState, error) {
|
|
|
|
return InitializeFromProto(ðpb.BeaconState{PreviousJustifiedCheckpoint: cp})
|
2021-05-05 08:28:35 +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-05-05 08:28:35 +00:00
|
|
|
},
|
2022-02-11 06:48:29 +00:00
|
|
|
)
|
|
|
|
}
|
2021-05-05 08:28:35 +00:00
|
|
|
|
2022-02-11 06:48:29 +00:00
|
|
|
func TestBeaconState_MarshalSSZ_NilState(t *testing.T) {
|
|
|
|
testtmpl.VerifyBeaconState_MarshalSSZ_NilState(
|
|
|
|
t,
|
|
|
|
func() (state.BeaconState, error) {
|
|
|
|
return InitializeFromProto(ðpb.BeaconState{})
|
2021-05-05 08:28:35 +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-05-05 08:28:35 +00:00
|
|
|
},
|
2022-02-11 06:48:29 +00:00
|
|
|
)
|
|
|
|
}
|
2021-05-05 08:28:35 +00:00
|
|
|
|
2022-02-11 06:48:29 +00:00
|
|
|
func TestBeaconState_ValidatorByPubkey(t *testing.T) {
|
|
|
|
testtmpl.VerifyBeaconState_ValidatorByPubkey(t, func() (state.BeaconState, error) {
|
|
|
|
return InitializeFromProto(ðpb.BeaconState{})
|
|
|
|
})
|
2021-05-05 08:28:35 +00:00
|
|
|
}
|