prysm-pulse/beacon-chain/blockchain/stategenerator/state_generator_test.go
shayzluf 6c1740eefc Add Caching to Tree Hashing Algorithm (#1929)
* added todo to hash file in ssz

* params and copy of block cache

* start hash cache

* Hash cache implementation

* fixed some comments

* fixed promatheus duplicate counter name

* removed TODO

* change to use special expiration cache

* function name fixes junk object generator

* naming changes

* gazzle fix

* added pruning last read data test

* fixed gometallinter errors

* fix benchmarks and no int64 not serializable

* move struct from test

* add feature flag

* fix merge issues

* add featureflag to beacon and validator

* featureflag init for tests

* added feature flag to all ssz dependent tests

* remove setter func

* replace k8s tweaked expiration cache to https://github.com/karlseguin/ccache

* remove else

* change request by preston

* added init featureflags to genesis_test

* Update shared/ssz/hash_cache.go

add dot

Co-Authored-By: shayzluf <thezluf@gmail.com>

* Update shared/ssz/hash_cache.go

Co-Authored-By: shayzluf <thezluf@gmail.com>

* Update shared/ssz/hash_cache.go

remove extra space

Co-Authored-By: shayzluf <thezluf@gmail.com>

* Update shared/params/config.go

add dot

Co-Authored-By: shayzluf <thezluf@gmail.com>

* Update shared/featureconfig/config.go

remove dot

Co-Authored-By: shayzluf <thezluf@gmail.com>

* Update shared/featureconfig/config.go

remove dot

Co-Authored-By: shayzluf <thezluf@gmail.com>

* remove powchain from prometheus hash cache name

* fixes fron change requests

* fix change requests

* remove faulty merge test

* gazelle fix

* fix fmt.sprintf

* remove debug binary

* fix gazelle
2019-04-24 13:39:02 +08:00

190 lines
6.8 KiB
Go

package stategenerator_test
import (
"context"
"strings"
"testing"
"github.com/gogo/protobuf/proto"
"github.com/prysmaticlabs/prysm/beacon-chain/blockchain/stategenerator"
"github.com/prysmaticlabs/prysm/beacon-chain/chaintest/backend"
"github.com/prysmaticlabs/prysm/beacon-chain/db"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/featureconfig"
"github.com/prysmaticlabs/prysm/shared/params"
)
func init() {
featureconfig.InitFeatureConfig(&featureconfig.FeatureFlagConfig{
CacheTreeHash: false,
})
}
func TestGenerateState_OK(t *testing.T) {
b, err := backend.NewSimulatedBackend()
if err != nil {
t.Fatalf("Could not create a new simulated backend %v", err)
}
privKeys, err := b.SetupBackend(100)
if err != nil {
t.Fatalf("Could not set up backend %v", err)
}
beaconDb := b.DB()
defer b.Shutdown()
defer db.TeardownDB(beaconDb)
ctx := context.Background()
slotLimit := uint64(30)
// Run the simulated chain for 30 slots, to get a state that we can save as finalized.
for i := uint64(0); i < slotLimit; i++ {
if err := b.GenerateBlockAndAdvanceChain(&backend.SimulatedObjects{}, privKeys); err != nil {
t.Fatalf("Could not generate block and transition state successfully %v for slot %d", err, b.State().Slot+1)
}
inMemBlocks := b.InMemoryBlocks()
if err := beaconDb.SaveBlock(inMemBlocks[len(inMemBlocks)-1]); err != nil {
t.Fatalf("Unable to save block %v", err)
}
if err := beaconDb.UpdateChainHead(ctx, inMemBlocks[len(inMemBlocks)-1], b.State()); err != nil {
t.Fatalf("Unable to save block %v", err)
}
if err := beaconDb.SaveFinalizedBlock(inMemBlocks[len(inMemBlocks)-1]); err != nil {
t.Fatalf("Unable to save finalized state: %v", err)
}
}
if err := beaconDb.SaveFinalizedState(b.State()); err != nil {
t.Fatalf("Unable to save finalized state: %v", err)
}
// Run the chain for another 30 slots so that we can have this at the current head.
for i := uint64(0); i < slotLimit; i++ {
if err := b.GenerateBlockAndAdvanceChain(&backend.SimulatedObjects{}, privKeys); err != nil {
t.Fatalf("Could not generate block and transition state successfully %v for slot %d", err, b.State().Slot+1)
}
inMemBlocks := b.InMemoryBlocks()
if err := beaconDb.SaveBlock(inMemBlocks[len(inMemBlocks)-1]); err != nil {
t.Fatalf("Unable to save block %v", err)
}
if err := beaconDb.UpdateChainHead(ctx, inMemBlocks[len(inMemBlocks)-1], b.State()); err != nil {
t.Fatalf("Unable to save block %v", err)
}
}
// Ran 30 slots to save finalized slot then ran another 30 slots.
slotToGenerateTill := params.BeaconConfig().GenesisSlot + slotLimit*2
newState, err := stategenerator.GenerateStateFromBlock(context.Background(), beaconDb, slotToGenerateTill)
if err != nil {
t.Fatalf("Unable to generate new state from previous finalized state %v", err)
}
if newState.Slot != b.State().Slot {
t.Fatalf("The generated state and the current state do not have the same slot, expected: %d but got %d",
b.State().Slot, newState.Slot)
}
if !proto.Equal(newState, b.State()) {
t.Error("Generated and saved states are unequal")
}
}
func TestGenerateState_WithNilBlocksOK(t *testing.T) {
b, err := backend.NewSimulatedBackend()
if err != nil {
t.Fatalf("Could not create a new simulated backend %v", err)
}
privKeys, err := b.SetupBackend(100)
if err != nil {
t.Fatalf("Could not set up backend %v", err)
}
beaconDb := b.DB()
defer b.Shutdown()
defer db.TeardownDB(beaconDb)
ctx := context.Background()
slotLimit := uint64(30)
// Run the simulated chain for 30 slots, to get a state that we can save as finalized.
for i := uint64(0); i < slotLimit; i++ {
if err := b.GenerateBlockAndAdvanceChain(&backend.SimulatedObjects{}, privKeys); err != nil {
t.Fatalf("Could not generate block and transition state successfully %v for slot %d", err, b.State().Slot+1)
}
inMemBlocks := b.InMemoryBlocks()
if err := beaconDb.SaveBlock(inMemBlocks[len(inMemBlocks)-1]); err != nil {
t.Fatalf("Unable to save block %v", err)
}
if err := beaconDb.UpdateChainHead(ctx, inMemBlocks[len(inMemBlocks)-1], b.State()); err != nil {
t.Fatalf("Unable to save block %v", err)
}
if err := beaconDb.SaveFinalizedBlock(inMemBlocks[len(inMemBlocks)-1]); err != nil {
t.Fatalf("Unable to save finalized state: %v", err)
}
}
if err := beaconDb.SaveFinalizedState(b.State()); err != nil {
t.Fatalf("Unable to save finalized state")
}
slotsWithNil := uint64(10)
// Run the chain for 10 slots with nil blocks.
for i := uint64(0); i < slotsWithNil; i++ {
if err := b.GenerateNilBlockAndAdvanceChain(); err != nil {
t.Fatalf("Could not generate block and transition state successfully %v for slot %d", err, b.State().Slot+1)
}
}
for i := uint64(0); i < slotLimit-slotsWithNil; i++ {
if err := b.GenerateBlockAndAdvanceChain(&backend.SimulatedObjects{}, privKeys); err != nil {
t.Fatalf("Could not generate block and transition state successfully %v for slot %d", err, b.State().Slot+1)
}
inMemBlocks := b.InMemoryBlocks()
if err := beaconDb.SaveBlock(inMemBlocks[len(inMemBlocks)-1]); err != nil {
t.Fatalf("Unable to save block %v", err)
}
if err := beaconDb.UpdateChainHead(ctx, inMemBlocks[len(inMemBlocks)-1], b.State()); err != nil {
t.Fatalf("Unable to save block %v", err)
}
}
// Ran 30 slots to save finalized slot then ran another 10 slots w/o blocks and 20 slots w/ blocks.
slotToGenerateTill := params.BeaconConfig().GenesisSlot + slotLimit*2
newState, err := stategenerator.GenerateStateFromBlock(context.Background(), beaconDb, slotToGenerateTill)
if err != nil {
t.Fatalf("Unable to generate new state from previous finalized state %v", err)
}
if newState.Slot != b.State().Slot {
t.Fatalf("The generated state and the current state do not have the same slot, expected: %d but got %d",
b.State().Slot, newState.Slot)
}
if !proto.Equal(newState, b.State()) {
t.Error("generated and saved states are unequal")
}
}
func TestGenerateState_NilLatestFinalizedBlock(t *testing.T) {
b, err := backend.NewSimulatedBackend()
if err != nil {
t.Fatalf("Could not create a new simulated backend %v", err)
}
beaconDB := b.DB()
defer b.Shutdown()
defer db.TeardownDB(beaconDB)
beaconState := &pb.BeaconState{
Slot: params.BeaconConfig().GenesisSlot + params.BeaconConfig().SlotsPerEpoch*4,
}
if err := beaconDB.SaveFinalizedState(beaconState); err != nil {
t.Fatalf("Unable to save finalized state")
}
if err := beaconDB.SaveHistoricalState(context.Background(), beaconState); err != nil {
t.Fatalf("Unable to save finalized state")
}
slot := params.BeaconConfig().GenesisSlot + 1 + params.BeaconConfig().SlotsPerEpoch*4
want := "latest head in state is nil"
if _, err := stategenerator.GenerateStateFromBlock(context.Background(), beaconDB, slot); !strings.Contains(err.Error(), want) {
t.Errorf("Expected %v, received %v", want, err)
}
}