prysm-pulse/beacon-chain/state/stategen/migrate_test.go
terence tsao def7b602e3
Hot states use no DB (#6488)
* Add cache to service struct
* Update hot getters/setters to use cache
* Update migration
* Update other services to adapt
* Fix initial sync get state
* Update getter related tests
* Update hot related tests
* Update migrate related tests
* New awesome tests for migration
* Clean up rest of the tests
* Merge refs/heads/master into hot-state-no-db
* Fix block chain head tests
* Fix block chain processor tests
* Fixed RPC tests
* Update cold getter and test
* Merge branch 'hot-state-no-db' of github.com:prysmaticlabs/prysm into hot-state-no-db
* Fix sync tests
* Short cut if state is already in DB
* Remove uneeded saves
* Update beacon-chain/state/stategen/hot_test.go

Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>
* Update beacon-chain/state/stategen/getter_test.go

Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>
* Update beacon-chain/state/stategen/getter_test.go

Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>
* Update beacon-chain/state/stategen/service.go

Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>
* Update beacon-chain/state/stategen/setter_test.go

Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>
* Preston's feedback
* Merge branch 'hot-state-no-db' of github.com:prysmaticlabs/prysm into hot-state-no-db
* Return a copy of cache states
* Remove hot state caches check in StateByRoot
* Merge branch 'hot-state-no-db' of github.com:prysmaticlabs/prysm into hot-state-no-db
* Merge refs/heads/master into hot-state-no-db
* Raul's feedback
* Merge branch 'hot-state-no-db' of github.com:prysmaticlabs/prysm into hot-state-no-db
2020-07-06 17:22:12 +00:00

142 lines
3.8 KiB
Go

package stategen
import (
"context"
"reflect"
"testing"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/beacon-chain/cache"
testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/testutil"
logTest "github.com/sirupsen/logrus/hooks/test"
)
func TestMigrateToCold_CanSaveFinalizedInfo(t *testing.T) {
ctx := context.Background()
db, _ := testDB.SetupDB(t)
service := New(db, cache.NewStateSummaryCache())
beaconState, _ := testutil.DeterministicGenesisState(t, 32)
r := [32]byte{'a'}
if err := service.epochBoundaryStateCache.put(r, beaconState); err != nil {
t.Fatal(err)
}
if err := service.MigrateToCold(ctx, 1, r); err != nil {
t.Fatal(err)
}
wanted := &finalizedInfo{state: beaconState, root: r, slot: 1}
if !reflect.DeepEqual(wanted, service.finalizedInfo) {
t.Error("Incorrect finalized info")
}
}
func TestMigrateToCold_HappyPath(t *testing.T) {
hook := logTest.NewGlobal()
ctx := context.Background()
db, _ := testDB.SetupDB(t)
service := New(db, cache.NewStateSummaryCache())
service.slotsPerArchivedPoint = 1
beaconState, _ := testutil.DeterministicGenesisState(t, 32)
stateSlot := uint64(1)
if err := beaconState.SetSlot(stateSlot); err != nil {
t.Fatal(err)
}
fRoot := [32]byte{'a'}
if err := service.epochBoundaryStateCache.put(fRoot, beaconState); err != nil {
t.Fatal(err)
}
fSlot := uint64(2)
if err := service.MigrateToCold(ctx, fSlot, fRoot); err != nil {
t.Fatal(err)
}
gotState, err := service.beaconDB.State(ctx, fRoot)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(gotState.InnerStateUnsafe(), beaconState.InnerStateUnsafe()) {
t.Error("Did not save state")
}
gotRoot := service.beaconDB.ArchivedPointRoot(ctx, stateSlot/service.slotsPerArchivedPoint)
if gotRoot != fRoot {
t.Error("Did not save archived root")
}
lastIndex, err := service.beaconDB.LastArchivedIndex(ctx)
if err != nil {
t.Fatal(err)
}
if lastIndex != 1 {
t.Error("Did not save last archived index")
}
testutil.AssertLogsContain(t, hook, "Saved state in DB")
}
func TestMigrateToCold_RegeneratePath(t *testing.T) {
hook := logTest.NewGlobal()
ctx := context.Background()
db, _ := testDB.SetupDB(t)
service := New(db, cache.NewStateSummaryCache())
service.slotsPerArchivedPoint = 1
beaconState, _ := testutil.DeterministicGenesisState(t, 32)
stateSlot := uint64(1)
if err := beaconState.SetSlot(stateSlot); err != nil {
t.Fatal(err)
}
blk := &ethpb.SignedBeaconBlock{Block: &ethpb.BeaconBlock{}}
fRoot, err := stateutil.BlockRoot(blk.Block)
if err != nil {
t.Fatal(err)
}
if err := service.beaconDB.SaveBlock(ctx, blk); err != nil {
t.Fatal(err)
}
if err := service.beaconDB.SaveGenesisBlockRoot(ctx, fRoot); err != nil {
t.Fatal(err)
}
if err := service.beaconDB.SaveStateSummary(ctx, &pb.StateSummary{
Slot: 1,
Root: fRoot[:],
}); err != nil {
t.Fatal(err)
}
service.finalizedInfo = &finalizedInfo{
slot: 1,
root: fRoot,
state: beaconState,
}
fSlot := uint64(2)
if err := service.MigrateToCold(ctx, fSlot, fRoot); err != nil {
t.Fatal(err)
}
gotState, err := service.beaconDB.State(ctx, fRoot)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(gotState.InnerStateUnsafe(), beaconState.InnerStateUnsafe()) {
t.Error("Did not save state")
}
gotRoot := service.beaconDB.ArchivedPointRoot(ctx, stateSlot/service.slotsPerArchivedPoint)
if gotRoot != fRoot {
t.Error("Did not save archived root")
}
lastIndex, err := service.beaconDB.LastArchivedIndex(ctx)
if err != nil {
t.Fatal(err)
}
if lastIndex != 1 {
t.Error("Did not save last archived index")
}
testutil.AssertLogsContain(t, hook, "Saved state in DB")
}