2020-05-31 06:44:34 +00:00
|
|
|
package state_test
|
2019-11-26 18:09:57 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2020-03-24 14:16:07 +00:00
|
|
|
"github.com/gogo/protobuf/proto"
|
2019-11-26 18:09:57 +00:00
|
|
|
"github.com/prysmaticlabs/go-ssz"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
|
2020-03-24 14:16:07 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/state"
|
2020-01-31 20:57:01 +00:00
|
|
|
beaconstate "github.com/prysmaticlabs/prysm/beacon-chain/state"
|
2020-03-24 14:16:07 +00:00
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
2020-01-17 17:25:35 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/benchutil"
|
2019-11-26 18:09:57 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
2020-08-10 10:34:33 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
2019-11-26 18:09:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var runAmount = 25
|
|
|
|
|
2020-06-09 22:40:48 +00:00
|
|
|
func TestExecuteStateTransition_FullBlock(t *testing.T) {
|
2020-01-17 17:25:35 +00:00
|
|
|
benchutil.SetBenchmarkConfig()
|
|
|
|
beaconState, err := benchutil.PreGenState1Epoch()
|
2020-08-10 10:34:33 +00:00
|
|
|
require.NoError(t, err)
|
2020-01-17 17:25:35 +00:00
|
|
|
block, err := benchutil.PreGenFullBlock()
|
2020-08-10 10:34:33 +00:00
|
|
|
require.NoError(t, err)
|
2019-11-26 18:09:57 +00:00
|
|
|
|
2020-04-14 20:27:03 +00:00
|
|
|
oldSlot := beaconState.Slot()
|
|
|
|
beaconState, err = state.ExecuteStateTransition(context.Background(), beaconState, block)
|
2020-08-10 10:34:33 +00:00
|
|
|
require.NoError(t, err, "Failed to process block, benchmarks will fail")
|
|
|
|
require.NotEqual(t, oldSlot, beaconState.Slot(), "Expected slots to be different")
|
2019-11-26 18:09:57 +00:00
|
|
|
}
|
|
|
|
|
2019-12-11 00:14:33 +00:00
|
|
|
func BenchmarkExecuteStateTransition_FullBlock(b *testing.B) {
|
2020-01-17 17:25:35 +00:00
|
|
|
benchutil.SetBenchmarkConfig()
|
|
|
|
beaconState, err := benchutil.PreGenState1Epoch()
|
2020-08-10 10:34:33 +00:00
|
|
|
require.NoError(b, err)
|
2019-11-26 18:09:57 +00:00
|
|
|
cleanStates := clonedStates(beaconState)
|
2020-01-17 17:25:35 +00:00
|
|
|
block, err := benchutil.PreGenFullBlock()
|
2020-08-10 10:34:33 +00:00
|
|
|
require.NoError(b, err)
|
2019-11-26 18:09:57 +00:00
|
|
|
|
|
|
|
b.N = runAmount
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2020-08-10 10:34:33 +00:00
|
|
|
_, err := state.ExecuteStateTransition(context.Background(), cleanStates[i], block)
|
|
|
|
require.NoError(b, err)
|
2019-11-26 18:09:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkExecuteStateTransition_WithCache(b *testing.B) {
|
2020-01-17 17:25:35 +00:00
|
|
|
benchutil.SetBenchmarkConfig()
|
2019-11-26 18:09:57 +00:00
|
|
|
|
2020-01-17 17:25:35 +00:00
|
|
|
beaconState, err := benchutil.PreGenState1Epoch()
|
2020-08-10 10:34:33 +00:00
|
|
|
require.NoError(b, err)
|
2019-11-26 18:09:57 +00:00
|
|
|
cleanStates := clonedStates(beaconState)
|
2020-01-17 17:25:35 +00:00
|
|
|
block, err := benchutil.PreGenFullBlock()
|
2020-08-10 10:34:33 +00:00
|
|
|
require.NoError(b, err)
|
2019-11-26 18:09:57 +00:00
|
|
|
|
|
|
|
// We have to reset slot back to last epoch to hydrate cache. Since
|
|
|
|
// some attestations in block are from previous epoch
|
2020-01-31 20:57:01 +00:00
|
|
|
currentSlot := beaconState.Slot()
|
2020-08-10 10:34:33 +00:00
|
|
|
require.NoError(b, beaconState.SetSlot(beaconState.Slot()-params.BeaconConfig().SlotsPerEpoch))
|
|
|
|
require.NoError(b, helpers.UpdateCommitteeCache(beaconState, helpers.CurrentEpoch(beaconState)))
|
|
|
|
require.NoError(b, beaconState.SetSlot(currentSlot))
|
2019-12-02 20:54:57 +00:00
|
|
|
// Run the state transition once to populate the cache.
|
2020-08-10 10:34:33 +00:00
|
|
|
_, err = state.ExecuteStateTransition(context.Background(), beaconState, block)
|
|
|
|
require.NoError(b, err, "Failed to process block, benchmarks will fail")
|
2019-12-02 20:54:57 +00:00
|
|
|
|
2019-11-26 18:09:57 +00:00
|
|
|
b.N = runAmount
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2020-08-10 10:34:33 +00:00
|
|
|
_, err := state.ExecuteStateTransition(context.Background(), cleanStates[i], block)
|
|
|
|
require.NoError(b, err, "Failed to process block, benchmarks will fail")
|
2019-11-26 18:09:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkProcessEpoch_2FullEpochs(b *testing.B) {
|
2020-01-17 17:25:35 +00:00
|
|
|
benchutil.SetBenchmarkConfig()
|
|
|
|
beaconState, err := benchutil.PreGenState2FullEpochs()
|
2020-08-10 10:34:33 +00:00
|
|
|
require.NoError(b, err)
|
2019-11-26 18:09:57 +00:00
|
|
|
|
2019-12-02 20:54:57 +00:00
|
|
|
// We have to reset slot back to last epoch to hydrate cache. Since
|
|
|
|
// some attestations in block are from previous epoch
|
2020-01-31 20:57:01 +00:00
|
|
|
currentSlot := beaconState.Slot()
|
2020-08-10 10:34:33 +00:00
|
|
|
require.NoError(b, beaconState.SetSlot(beaconState.Slot()-params.BeaconConfig().SlotsPerEpoch))
|
|
|
|
require.NoError(b, helpers.UpdateCommitteeCache(beaconState, helpers.CurrentEpoch(beaconState)))
|
|
|
|
require.NoError(b, beaconState.SetSlot(currentSlot))
|
2019-12-02 20:54:57 +00:00
|
|
|
|
2019-11-26 18:09:57 +00:00
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2019-12-10 23:32:11 +00:00
|
|
|
// ProcessEpochPrecompute is the optimized version of process epoch. It's enabled by default
|
|
|
|
// at run time.
|
2020-08-10 10:34:33 +00:00
|
|
|
_, err := state.ProcessEpochPrecompute(context.Background(), beaconState.Copy())
|
|
|
|
require.NoError(b, err)
|
2019-11-26 18:09:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkHashTreeRoot_FullState(b *testing.B) {
|
2020-01-17 17:25:35 +00:00
|
|
|
beaconState, err := benchutil.PreGenState2FullEpochs()
|
2020-08-10 10:34:33 +00:00
|
|
|
require.NoError(b, err)
|
2019-11-26 18:09:57 +00:00
|
|
|
|
|
|
|
b.N = 50
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
if _, err := ssz.HashTreeRoot(beaconState); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-11 00:14:33 +00:00
|
|
|
func BenchmarkHashTreeRootState_FullState(b *testing.B) {
|
2020-01-17 17:25:35 +00:00
|
|
|
beaconState, err := benchutil.PreGenState2FullEpochs()
|
2020-08-10 10:34:33 +00:00
|
|
|
require.NoError(b, err)
|
2019-12-11 00:14:33 +00:00
|
|
|
|
2020-05-31 06:44:34 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2019-12-11 00:14:33 +00:00
|
|
|
// Hydrate the HashTreeRootState cache.
|
2020-08-10 10:34:33 +00:00
|
|
|
_, err = beaconState.HashTreeRoot(ctx)
|
|
|
|
require.NoError(b, err)
|
2019-12-11 00:14:33 +00:00
|
|
|
|
|
|
|
b.N = 50
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2020-08-10 10:34:33 +00:00
|
|
|
_, err := beaconState.HashTreeRoot(ctx)
|
|
|
|
require.NoError(b, err)
|
2019-12-11 00:14:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-24 14:16:07 +00:00
|
|
|
func BenchmarkMarshalState_FullState(b *testing.B) {
|
|
|
|
beaconState, err := benchutil.PreGenState2FullEpochs()
|
2020-08-10 10:34:33 +00:00
|
|
|
require.NoError(b, err)
|
2020-03-24 14:16:07 +00:00
|
|
|
natState := beaconState.InnerStateUnsafe()
|
|
|
|
|
|
|
|
b.Run("Proto_Marshal", func(b *testing.B) {
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.N = 1000
|
|
|
|
for i := 0; i < b.N; i++ {
|
2020-08-10 10:34:33 +00:00
|
|
|
_, err := proto.Marshal(natState)
|
|
|
|
require.NoError(b, err)
|
2020-03-24 14:16:07 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
b.Run("Fast_SSZ_Marshal", func(b *testing.B) {
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.N = 1000
|
|
|
|
for i := 0; i < b.N; i++ {
|
2020-08-10 10:34:33 +00:00
|
|
|
_, err := natState.MarshalSSZ()
|
|
|
|
require.NoError(b, err)
|
2020-03-24 14:16:07 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkUnmarshalState_FullState(b *testing.B) {
|
|
|
|
beaconState, err := benchutil.PreGenState2FullEpochs()
|
2020-08-10 10:34:33 +00:00
|
|
|
require.NoError(b, err)
|
2020-03-24 14:16:07 +00:00
|
|
|
natState := beaconState.InnerStateUnsafe()
|
|
|
|
protoObject, err := proto.Marshal(natState)
|
2020-08-10 10:34:33 +00:00
|
|
|
require.NoError(b, err)
|
2020-03-24 14:16:07 +00:00
|
|
|
sszObject, err := natState.MarshalSSZ()
|
2020-08-10 10:34:33 +00:00
|
|
|
require.NoError(b, err)
|
2020-03-24 14:16:07 +00:00
|
|
|
|
|
|
|
b.Run("Proto_Unmarshal", func(b *testing.B) {
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.N = 1000
|
|
|
|
for i := 0; i < b.N; i++ {
|
2020-08-10 10:34:33 +00:00
|
|
|
require.NoError(b, proto.Unmarshal(protoObject, &pb.BeaconState{}))
|
2020-03-24 14:16:07 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
b.Run("Fast_SSZ_Unmarshal", func(b *testing.B) {
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.N = 1000
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
sszState := &pb.BeaconState{}
|
2020-08-10 10:34:33 +00:00
|
|
|
require.NoError(b, sszState.UnmarshalSSZ(sszObject))
|
2020-03-24 14:16:07 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-01-31 20:57:01 +00:00
|
|
|
func clonedStates(beaconState *beaconstate.BeaconState) []*beaconstate.BeaconState {
|
|
|
|
clonedStates := make([]*beaconstate.BeaconState, runAmount)
|
2019-11-26 18:09:57 +00:00
|
|
|
for i := 0; i < runAmount; i++ {
|
2020-02-06 03:26:15 +00:00
|
|
|
clonedStates[i] = beaconState.Copy()
|
2019-11-26 18:09:57 +00:00
|
|
|
}
|
|
|
|
return clonedStates
|
|
|
|
}
|