prysm-pulse/beacon-chain/state/getters_test.go
Victor Farazdagi a069738c20
ETH2 Types: Slot (#8408)
* update shared/params

* update eth2-types deps

* update protobufs

* update shared/*

* fix testutil/state

* update beacon-chain/state

* update beacon-chain/db

* update tests

* fix test

* update beacon-chain/core

* update beacon-chain/blockchain

* update beacon-chain/cache

* beacon-chain/forkchoice

* update beacon-chain/operations

* update beacon-chain/p2p

* update beacon-chain/rpc

* update sync/initial-sync

* update deps

* update deps

* go fmt

* update beacon-chain/sync

* update endtoend/

* bazel build //beacon-chain - works w/o issues

* update slasher code

* udpate tools/

* update validator/

* update fastssz

* fix build

* fix test building

* update tests

* update ethereumapis deps

* fix tests

* update state/stategen

* fix build

* fix test

* add FarFutureSlot

* go imports

* Radek's suggestions

* Ivan's suggestions

* type conversions

* Nishant's suggestions

* add more tests to rpc_send_request

* fix test

* clean up

* fix conflicts

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
Co-authored-by: nisdas <nishdas93@gmail.com>
2021-02-16 07:45:34 +00:00

118 lines
3.4 KiB
Go

package state
import (
"runtime/debug"
"sync"
"testing"
types "github.com/prysmaticlabs/eth2-types"
eth "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
)
func TestBeaconState_SlotDataRace(t *testing.T) {
headState, err := InitializeFromProto(&pb.BeaconState{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.GenesisUnixTime()
_ = st.GenesisValidatorRoot()
_ = st.Slot()
_ = st.Fork()
_ = st.LatestBlockHeader()
_ = st.ParentRoot()
_ = 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([48]byte{})
_ = st.validatorIndexMap()
_ = 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()
_ = st.PreviousEpochAttestations()
_ = st.CurrentEpochAttestations()
_ = st.JustificationBits()
_ = st.PreviousJustifiedCheckpoint()
_ = st.CurrentJustifiedCheckpoint()
_ = st.FinalizedCheckpoint()
}
func TestReadOnlyValidator_NoPanic(t *testing.T) {
v := &ReadOnlyValidator{}
assert.Equal(t, false, v.Slashed(), "Expected not slashed")
assert.Equal(t, (*eth.Validator)(nil), v.CopyValidator(), "Expected nil result")
}
func TestReadOnlyValidator_ActivationEligibilityEpochNoPanic(t *testing.T) {
v := &ReadOnlyValidator{}
assert.Equal(t, types.Epoch(0), v.ActivationEligibilityEpoch(), "Expected 0 and not panic")
}
func TestBeaconState_MatchCurrentJustifiedCheckpt(t *testing.T) {
c1 := &eth.Checkpoint{Epoch: 1}
c2 := &eth.Checkpoint{Epoch: 2}
state, err := InitializeFromProto(&pb.BeaconState{CurrentJustifiedCheckpoint: c1})
require.NoError(t, err)
require.Equal(t, true, state.MatchCurrentJustifiedCheckpoint(c1))
require.Equal(t, false, state.MatchCurrentJustifiedCheckpoint(c2))
require.Equal(t, false, state.MatchPreviousJustifiedCheckpoint(c1))
require.Equal(t, false, state.MatchPreviousJustifiedCheckpoint(c2))
state.state = nil
require.Equal(t, false, state.MatchCurrentJustifiedCheckpoint(c1))
}
func TestBeaconState_MatchPreviousJustifiedCheckpt(t *testing.T) {
c1 := &eth.Checkpoint{Epoch: 1}
c2 := &eth.Checkpoint{Epoch: 2}
state, err := InitializeFromProto(&pb.BeaconState{PreviousJustifiedCheckpoint: c1})
require.NoError(t, err)
require.Equal(t, false, state.MatchCurrentJustifiedCheckpoint(c1))
require.Equal(t, false, state.MatchCurrentJustifiedCheckpoint(c2))
require.Equal(t, true, state.MatchPreviousJustifiedCheckpoint(c1))
require.Equal(t, false, state.MatchPreviousJustifiedCheckpoint(c2))
state.state = nil
require.Equal(t, false, state.MatchPreviousJustifiedCheckpoint(c1))
}