prysm-pulse/beacon-chain/state/stateutil/state_root_test.go
Victor Farazdagi bbde2a6820
Assert no side effects occur on test (#5714)
* add reset funcs
* init -> TestMain for beacon config params
* fixes build file
* core/epoch spectests updated
* core/blocks spectests updated
* fix the rest of spectests
* SetupTestConfigCleanup
* revert copy() api
* rever all cases of copy()
* fixes spectests
* updates fork_test
* config_test update
* fixes spectest/config_test
* Merge refs/heads/master into assert-no-side-effects-occur-in-tests
* Merge branch 'master' into assert-no-side-effects-occur-in-tests
* Merge refs/heads/master into assert-no-side-effects-occur-in-tests
* Merge refs/heads/master into assert-no-side-effects-occur-in-tests
* Merge refs/heads/master into assert-no-side-effects-occur-in-tests
* Merge refs/heads/master into assert-no-side-effects-occur-in-tests
* Merge refs/heads/master into assert-no-side-effects-occur-in-tests
* Merge refs/heads/master into assert-no-side-effects-occur-in-tests
* Merge refs/heads/master into assert-no-side-effects-occur-in-tests
* Merge branch 'master' into assert-no-side-effects-occur-in-tests
* Merge refs/heads/master into assert-no-side-effects-occur-in-tests
* Merge refs/heads/master into assert-no-side-effects-occur-in-tests
2020-05-05 08:39:38 +00:00

122 lines
3.1 KiB
Go

package stateutil_test
import (
"reflect"
"strconv"
"strings"
"testing"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/go-ssz"
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/interop"
"github.com/prysmaticlabs/prysm/shared/params"
)
func TestState_FieldCount(t *testing.T) {
count := 21
typ := reflect.TypeOf(pb.BeaconState{})
numFields := 0
for i := 0; i < typ.NumField(); i++ {
if strings.HasPrefix(typ.Field(i).Name, "XXX_") {
continue
}
numFields++
}
if numFields != count {
t.Errorf("Expected state to have %d fields, received %d", count, numFields)
}
}
func BenchmarkHashTreeRootState_Custom_512(b *testing.B) {
b.StopTimer()
genesisState := setupGenesisState(b, 512)
b.StartTimer()
for i := 0; i < b.N; i++ {
if _, err := stateutil.HashTreeRootState(genesisState); err != nil {
b.Fatal(err)
}
}
}
func BenchmarkHashTreeRootState_Custom_16384(b *testing.B) {
b.StopTimer()
genesisState := setupGenesisState(b, 16384)
b.StartTimer()
for i := 0; i < b.N; i++ {
if _, err := stateutil.HashTreeRootState(genesisState); err != nil {
b.Fatal(err)
}
}
}
func BenchmarkHashTreeRootState_Custom_300000(b *testing.B) {
b.StopTimer()
genesisState := setupGenesisState(b, 300000)
b.StartTimer()
for i := 0; i < b.N; i++ {
if _, err := stateutil.HashTreeRootState(genesisState); err != nil {
b.Fatal(err)
}
}
}
func BenchmarkHashTreeRoot_Generic_512(b *testing.B) {
b.StopTimer()
genesisState := setupGenesisState(b, 512)
b.StartTimer()
for i := 0; i < b.N; i++ {
if _, err := ssz.HashTreeRoot(genesisState); err != nil {
b.Fatal(err)
}
}
}
func BenchmarkHashTreeRoot_Generic_16384(b *testing.B) {
b.StopTimer()
genesisState := setupGenesisState(b, 16384)
b.StartTimer()
for i := 0; i < b.N; i++ {
if _, err := ssz.HashTreeRoot(genesisState); err != nil {
b.Fatal(err)
}
}
}
func BenchmarkHashTreeRoot_Generic_300000(b *testing.B) {
b.StopTimer()
genesisState := setupGenesisState(b, 300000)
b.StartTimer()
for i := 0; i < b.N; i++ {
if _, err := ssz.HashTreeRoot(genesisState); err != nil {
b.Fatal(err)
}
}
}
func setupGenesisState(tb testing.TB, count uint64) *pb.BeaconState {
genesisState, _, err := interop.GenerateGenesisState(0, 1)
if err != nil {
tb.Fatalf("Could not generate genesis beacon state: %v", err)
}
for i := uint64(1); i < count; i++ {
someRoot := [32]byte{}
someKey := [48]byte{}
copy(someRoot[:], strconv.Itoa(int(i)))
copy(someKey[:], strconv.Itoa(int(i)))
genesisState.Validators = append(genesisState.Validators, &ethpb.Validator{
PublicKey: someKey[:],
WithdrawalCredentials: someRoot[:],
EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance,
Slashed: false,
ActivationEligibilityEpoch: 1,
ActivationEpoch: 1,
ExitEpoch: 1,
WithdrawableEpoch: 1,
})
genesisState.Balances = append(genesisState.Balances, params.BeaconConfig().MaxEffectiveBalance)
}
return genesisState
}