prysm-pulse/shared/testutil/block_test.go
Ivan Martinez c1c48a8af5 Create Benchmarks Package for State Transition (#3688)
* Begin benchmarks file for block processing
* Complete block processing benchmarks
* Begin epoch benchmarks
* Write most of epoch benchmarks
* Start config
* Make cases for max conditions
* Begin work on benchmarking doc
* Update benchmark numbers
* Complete epoch benchmarks
* Minor changes
* Make createFullBlock function
* Clean up block benchmarks
* Begin fixing merge issues
* Start adding 4M benchmarks
* Almost finish epoch benchmarks
* Test blocks under real life conditions
* More progress on benchmarks
* Fixes
* Fix benchmark errors
* Begin fixing benchmarks
* More progress on tests
* Complete epoch benchmarks
* More progress on block benches
* Finish epoch benchmarks, get progress on block benchmarks
* Undo unneeded changes
* Fix
* Fix block benchmarks
* Complete block benchmarks
* Finish block benchmarks
* Complete benchmarks
* Increase block benchmarks to 65536
* Fix everything
* Reset configs after benchmarks
* Fix logging and suggestions
* Fix comments
* Fix benchmarks after merge
* Fix merge issues
* Add sanity tests for benchmark
* Make sanity check simpler
* Begin fixing after merge
* Add log
* Remove extra line
* Remove unneeded change
* Finally get block benchmarks to pass
* Begin fixing epoch test
* Finetuning constants
* Revert "Finetuning constants"

This reverts commit a872790d675154e1ce213285e76fb6e08ef21d79.
* Finetuning
* Merge branch 'master' of https://github.com/prysmaticlabs/prysm into new-benchmarks
* Add benches for helper functions
* Abstract block generation to testutil
* Create block generation util in testutil
* Gazelle
* Fix deps
* Merge branch 'master' of https://github.com/prysmaticlabs/prysm into block-util
* Fix imports
* Merge branch 'master' of https://github.com/prysmaticlabs/prysm into block-util
* Merge branch 'master' into block-util
* Change tests to use config and fix integer division
* Merge branch 'block-util' of https://github.com/0xKiwi/prysm into block-util
* Merge branch 'master' of https://github.com/prysmaticlabs/prysm into block-util
* Remove logs
* Fix build
* Merge branch 'master' of https://github.com/prysmaticlabs/prysm into new-benchmarks
* Merge branch 'master' into block-util
* Add test to ensure finalization occurs
* Add check for finalization
* Merge branch 'block-util' of https://github.com/0xKiwi/prysm into block-util
* Merge branch 'master' of https://github.com/prysmaticlabs/prysm into block-util
* Add comment for incrementing the state
* Fix test
* Fix test
* Merge branch 'master' into block-util
* Fix testutil use
* Fix tests
* Change var name
* Merge branch 'master' into block-util
* Merge branch 'master' of https://github.com/prysmaticlabs/prysm into new-benchmarks
* Merge branch 'block-util' of https://github.com/0xKiwi/prysm into new-benchmarks
* Begin cleaning benchmarks
* Get some numbers going
* Use state saved to disk
* Remove cruft
* Cleanup
* Merge branch 'master' of https://github.com/prysmaticlabs/prysm into new-benchmarks
* Fix merge arrows
* Set up block util and benchmarks for 128 attestations
* Use intended config for benchmark
* Add more benchmark functions
* Merge branch 'master' of https://github.com/prysmaticlabs/prysm into new-benchmarks
* Add benchmark epoch and modify block gen config to exclude signing
* Cleanup
* Merge branch 'master' of https://github.com/prysmaticlabs/prysm into new-benchmarks
* Merge branch 'master' of https://github.com/prysmaticlabs/prysm into new-benchmarks
* Begin unstaleling
* Merge branch 'master' of https://github.com/prysmaticlabs/prysm into new-benchmarks
* Update block gen util to v0.9 changes
* Prepare benchmarks to use marshalled files
* Cleanup block gen tool some more
* split up into file generation and benchmarking
* Remove logrus
* Merge branch 'master' into new-benchmarks
* Get benchmarks work, start work on process epoch benchmark
* Merge branch 'master' of https://github.com/prysmaticlabs/prysm into new-benchmarks
* Merge branch 'new-benchmarks' of https://github.com/0xKiwi/prysm into new-benchmarks
* All benchmarks working
* Merge branch 'master' of https://github.com/prysmaticlabs/prysm into new-benchmarks
* Fix after merge
* Cleanup
* Add bazel target
* Added TestBenchmarkExecuteStateTransition_WithCache
* Change tests to use SSZ and begin making binary
* Merge branch 'master' of https://github.com/prysmaticlabs/prysm into new-benchmarks
* Merge branch 'new-benchmarks' of https://github.com/0xKiwi/prysm into new-benchmarks
* bazel binary
* Fully change to binary
* Merge branch 'master' of https://github.com/prysmaticlabs/prysm into new-benchmarks
* Create go_binary to handle benchmark files
* Merge branch 'master' of https://github.com/prysmaticlabs/prysm into new-benchmarks
* Gofmt
* Remove genesis state from generated files
* Fix tests
* Gazelle
* Fix tests
* Merge branch 'master' of https://github.com/prysmaticlabs/prysm into new-benchmarks
* Fix block util
* Allow attestations to be in future for block util
* Fix inclusion delay issue
* Merge branch 'master' of https://github.com/prysmaticlabs/prysm into new-benchmarks
* Finally fix test
* Add README detailing usage and results
* Merge branch 'master' of https://github.com/prysmaticlabs/prysm into new-benchmarks
* Change test to run with bazel test
* Fix imports
* Merge branch 'master' into new-benchmarks
* Accidentally removed  config change
* Merge branch 'new-benchmarks' of https://github.com/0xKiwi/prysm into new-benchmarks
* Merge branch 'master' of https://github.com/prysmaticlabs/prysm into new-benchmarks
* Move to core/state/
* Update readme
* Gazelle
* Remove test for cached block
2019-11-26 18:09:57 +00:00

221 lines
7.3 KiB
Go

package testutil
import (
"context"
"testing"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
"github.com/prysmaticlabs/prysm/beacon-chain/core/state"
"github.com/prysmaticlabs/prysm/beacon-chain/core/state/stateutils"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/params"
)
func TestGenerateFullBlock_PassesStateTransition(t *testing.T) {
deposits, _, privs := SetupInitialDeposits(t, 128)
eth1Data := GenerateEth1Data(t, deposits)
beaconState, err := state.GenesisBeaconState(deposits, 0, eth1Data)
if err != nil {
t.Fatal(err)
}
conf := &BlockGenConfig{
MaxAttestations: 4,
Signatures: true,
}
block := GenerateFullBlock(t, beaconState, privs, conf, beaconState.Slot)
beaconState, err = state.ExecuteStateTransition(context.Background(), beaconState, block)
if err != nil {
t.Fatal(err)
}
}
func TestGenerateFullBlock_ThousandValidators(t *testing.T) {
helpers.ClearAllCaches()
params.OverrideBeaconConfig(params.MinimalSpecConfig())
defer params.OverrideBeaconConfig(params.MainnetConfig())
deposits, _, privs := SetupInitialDeposits(t, 1024)
eth1Data := GenerateEth1Data(t, deposits)
beaconState, err := state.GenesisBeaconState(deposits, 0, eth1Data)
if err != nil {
t.Fatal(err)
}
conf := &BlockGenConfig{
MaxAttestations: 16,
Signatures: true,
}
block := GenerateFullBlock(t, beaconState, privs, conf, beaconState.Slot)
beaconState, err = state.ExecuteStateTransition(context.Background(), beaconState, block)
if err != nil {
t.Fatal(err)
}
}
func TestGenerateFullBlock_Passes4Epochs(t *testing.T) {
helpers.ClearAllCaches()
// Changing to minimal config as this will process 4 epochs of blocks.
params.OverrideBeaconConfig(params.MinimalSpecConfig())
defer params.OverrideBeaconConfig(params.MainnetConfig())
deposits, _, privs := SetupInitialDeposits(t, 64)
eth1Data := GenerateEth1Data(t, deposits)
beaconState, err := state.GenesisBeaconState(deposits, 0, eth1Data)
if err != nil {
t.Fatal(err)
}
conf := &BlockGenConfig{
MaxAttestations: 2,
Signatures: true,
}
finalSlot := params.BeaconConfig().SlotsPerEpoch*4 + 3
for i := 0; i < int(finalSlot); i++ {
block := GenerateFullBlock(t, beaconState, privs, conf, beaconState.Slot)
beaconState, err = state.ExecuteStateTransition(context.Background(), beaconState, block)
if err != nil {
t.Fatal(err)
}
}
// Blocks are one slot ahead of beacon state.
if finalSlot != beaconState.Slot {
t.Fatalf("expected output slot to be %d, received %d", finalSlot, beaconState.Slot)
}
if beaconState.CurrentJustifiedCheckpoint.Epoch != 3 {
t.Fatalf("expected justified epoch to change to 3, received %d", beaconState.CurrentJustifiedCheckpoint.Epoch)
}
if beaconState.FinalizedCheckpoint.Epoch != 2 {
t.Fatalf("expected finalized epoch to change to 2, received %d", beaconState.CurrentJustifiedCheckpoint.Epoch)
}
}
func TestGenerateFullBlock_ValidProposerSlashings(t *testing.T) {
params.OverrideBeaconConfig(params.MinimalSpecConfig())
defer params.OverrideBeaconConfig(params.MainnetConfig())
deposits, _, privs := SetupInitialDeposits(t, 32)
eth1Data := GenerateEth1Data(t, deposits)
beaconState, err := state.GenesisBeaconState(deposits, 0, eth1Data)
if err != nil {
t.Fatal(err)
}
conf := &BlockGenConfig{
MaxProposerSlashings: 1,
Signatures: true,
}
block := GenerateFullBlock(t, beaconState, privs, conf, beaconState.Slot+1)
beaconState, err = state.ExecuteStateTransition(context.Background(), beaconState, block)
if err != nil {
t.Fatal(err)
}
slashableIndice := block.Body.ProposerSlashings[0].ProposerIndex
if !beaconState.Validators[slashableIndice].Slashed {
t.Fatal("expected validator to be slashed")
}
}
func TestGenerateFullBlock_ValidAttesterSlashings(t *testing.T) {
params.OverrideBeaconConfig(params.MinimalSpecConfig())
defer params.OverrideBeaconConfig(params.MainnetConfig())
deposits, _, privs := SetupInitialDeposits(t, 32)
eth1Data := GenerateEth1Data(t, deposits)
beaconState, err := state.GenesisBeaconState(deposits, 0, eth1Data)
if err != nil {
t.Fatal(err)
}
conf := &BlockGenConfig{
MaxAttesterSlashings: 1,
Signatures: true,
}
block := GenerateFullBlock(t, beaconState, privs, conf, beaconState.Slot)
beaconState, err = state.ExecuteStateTransition(context.Background(), beaconState, block)
if err != nil {
t.Fatal(err)
}
slashableIndices := block.Body.AttesterSlashings[0].Attestation_1.CustodyBit_0Indices
if !beaconState.Validators[slashableIndices[0]].Slashed {
t.Fatal("expected validator to be slashed")
}
}
func TestGenerateFullBlock_ValidAttestations(t *testing.T) {
params.OverrideBeaconConfig(params.MinimalSpecConfig())
defer params.OverrideBeaconConfig(params.MainnetConfig())
helpers.ClearAllCaches()
deposits, _, privs := SetupInitialDeposits(t, 256)
eth1Data := GenerateEth1Data(t, deposits)
beaconState, err := state.GenesisBeaconState(deposits, 0, eth1Data)
if err != nil {
t.Fatal(err)
}
conf := &BlockGenConfig{
MaxAttestations: 4,
Signatures: true,
}
block := GenerateFullBlock(t, beaconState, privs, conf, beaconState.Slot)
beaconState, err = state.ExecuteStateTransition(context.Background(), beaconState, block)
if err != nil {
t.Fatal(err)
}
if len(beaconState.CurrentEpochAttestations) != 4 {
t.Fatal("expected 4 attestations to be saved to the beacon state")
}
}
func TestGenerateFullBlock_ValidDeposits(t *testing.T) {
deposits, _, privs := SetupInitialDeposits(t, 256)
eth1Data := GenerateEth1Data(t, deposits)
beaconState, err := state.GenesisBeaconState(deposits, 0, eth1Data)
if err != nil {
t.Fatal(err)
}
deposits, _, privs = SetupInitialDeposits(t, 257)
eth1Data = GenerateEth1Data(t, deposits)
beaconState.Eth1Data = eth1Data
conf := &BlockGenConfig{
MaxDeposits: 1,
Signatures: true,
}
block := GenerateFullBlock(t, beaconState, privs, conf, beaconState.Slot)
beaconState, err = state.ExecuteStateTransition(context.Background(), beaconState, block)
if err != nil {
t.Fatal(err)
}
depositedPubkey := block.Body.Deposits[0].Data.PublicKey
valIndexMap := stateutils.ValidatorIndexMap(beaconState)
index := valIndexMap[bytesutil.ToBytes48(depositedPubkey)]
if beaconState.Validators[index].EffectiveBalance != params.BeaconConfig().MaxEffectiveBalance {
t.Fatalf(
"expected validator balance to be max effective balance, received %d",
beaconState.Validators[index].EffectiveBalance,
)
}
}
func TestGenerateFullBlock_ValidVoluntaryExits(t *testing.T) {
deposits, _, privs := SetupInitialDeposits(t, 256)
eth1Data := GenerateEth1Data(t, deposits)
beaconState, err := state.GenesisBeaconState(deposits, 0, eth1Data)
if err != nil {
t.Fatal(err)
}
// Moving the state 2048 epochs forward due to PERSISTENT_COMMITTEE_PERIOD.
beaconState.Slot = 3 + params.BeaconConfig().PersistentCommitteePeriod*params.BeaconConfig().SlotsPerEpoch
conf := &BlockGenConfig{
MaxVoluntaryExits: 1,
Signatures: true,
}
block := GenerateFullBlock(t, beaconState, privs, conf, beaconState.Slot)
beaconState, err = state.ExecuteStateTransition(context.Background(), beaconState, block)
if err != nil {
t.Fatal(err)
}
exitedIndex := block.Body.VoluntaryExits[0].ValidatorIndex
if beaconState.Validators[exitedIndex].ExitEpoch == params.BeaconConfig().FarFutureEpoch {
t.Fatal("expected exiting validator index to be marked as exiting")
}
}