2019-12-04 19:20:33 +00:00
|
|
|
package stateutil_test
|
|
|
|
|
|
|
|
import (
|
2021-04-05 15:07:56 +00:00
|
|
|
"context"
|
2019-12-04 19:20:33 +00:00
|
|
|
"reflect"
|
2019-12-18 02:57:40 +00:00
|
|
|
"strconv"
|
2019-12-04 19:20:33 +00:00
|
|
|
"testing"
|
|
|
|
|
2019-12-18 02:57:40 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
2019-12-04 19:20:33 +00:00
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/interop"
|
2019-12-18 02:57:40 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
2020-07-18 07:56:48 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
2019-12-04 19:20:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestState_FieldCount(t *testing.T) {
|
2020-12-08 03:49:28 +00:00
|
|
|
count := params.BeaconConfig().BeaconStateFieldCount
|
2019-12-04 19:20:33 +00:00
|
|
|
typ := reflect.TypeOf(pb.BeaconState{})
|
|
|
|
numFields := 0
|
|
|
|
for i := 0; i < typ.NumField(); i++ {
|
2021-05-17 18:32:04 +00:00
|
|
|
if typ.Field(i).Name == "state" ||
|
|
|
|
typ.Field(i).Name == "sizeCache" ||
|
|
|
|
typ.Field(i).Name == "unknownFields" {
|
2019-12-04 19:20:33 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
numFields++
|
|
|
|
}
|
2020-07-18 07:56:48 +00:00
|
|
|
assert.Equal(t, count, numFields)
|
2019-12-04 19:20:33 +00:00
|
|
|
}
|
|
|
|
|
2020-01-16 21:40:09 +00:00
|
|
|
func BenchmarkHashTreeRoot_Generic_512(b *testing.B) {
|
2019-12-04 19:20:33 +00:00
|
|
|
b.StopTimer()
|
|
|
|
genesisState := setupGenesisState(b, 512)
|
|
|
|
b.StartTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2020-08-27 18:13:32 +00:00
|
|
|
_, err := genesisState.HashTreeRoot()
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(b, err)
|
2019-12-04 19:20:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-16 21:40:09 +00:00
|
|
|
func BenchmarkHashTreeRoot_Generic_16384(b *testing.B) {
|
2019-12-18 02:57:40 +00:00
|
|
|
b.StopTimer()
|
|
|
|
genesisState := setupGenesisState(b, 16384)
|
|
|
|
b.StartTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2020-08-27 18:13:32 +00:00
|
|
|
_, err := genesisState.HashTreeRoot()
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(b, err)
|
2019-12-18 02:57:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-16 21:40:09 +00:00
|
|
|
func BenchmarkHashTreeRoot_Generic_300000(b *testing.B) {
|
2019-12-18 02:57:40 +00:00
|
|
|
b.StopTimer()
|
|
|
|
genesisState := setupGenesisState(b, 300000)
|
|
|
|
b.StartTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2020-08-27 18:13:32 +00:00
|
|
|
_, err := genesisState.HashTreeRoot()
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(b, err)
|
2019-12-18 02:57:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-04 19:20:33 +00:00
|
|
|
func setupGenesisState(tb testing.TB, count uint64) *pb.BeaconState {
|
2021-04-05 15:07:56 +00:00
|
|
|
genesisState, _, err := interop.GenerateGenesisState(context.Background(), 0, 1)
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(tb, err, "Could not generate genesis beacon state")
|
2019-12-18 02:57:40 +00:00
|
|
|
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, ðpb.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)
|
|
|
|
}
|
2019-12-04 19:20:33 +00:00
|
|
|
return genesisState
|
|
|
|
}
|