prysm-pulse/beacon-chain/state/stategen/cold_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

143 lines
3.7 KiB
Go

package stategen
import (
"context"
"reflect"
"strings"
"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"
)
func TestSaveColdState_NonArchivedPoint(t *testing.T) {
ctx := context.Background()
db, _ := testDB.SetupDB(t)
service := New(db, cache.NewStateSummaryCache())
service.slotsPerArchivedPoint = 2
beaconState, _ := testutil.DeterministicGenesisState(t, 32)
if err := beaconState.SetSlot(1); err != nil {
t.Fatal(err)
}
if err := service.saveColdState(ctx, [32]byte{}, beaconState); err != nil {
t.Error(err)
}
}
func TestSaveColdState_CanSave(t *testing.T) {
ctx := context.Background()
db, _ := testDB.SetupDB(t)
service := New(db, cache.NewStateSummaryCache())
service.slotsPerArchivedPoint = 1
beaconState, _ := testutil.DeterministicGenesisState(t, 32)
if err := beaconState.SetSlot(1); err != nil {
t.Fatal(err)
}
r := [32]byte{'a'}
if err := service.saveColdState(ctx, r, beaconState); err != nil {
t.Fatal(err)
}
if !service.beaconDB.HasArchivedPoint(ctx, 1) {
t.Error("Did not save cold state")
}
if service.beaconDB.ArchivedPointRoot(ctx, 1) != r {
t.Error("Did not get wanted root")
}
}
func TestLoadColdStateByRoot_NoStateSummary(t *testing.T) {
ctx := context.Background()
db, _ := testDB.SetupDB(t)
service := New(db, cache.NewStateSummaryCache())
if _, err := service.loadColdStateByRoot(ctx, [32]byte{'a'}); !strings.Contains(err.Error(), errUnknownStateSummary.Error()) {
t.Fatal("Did not get correct error")
}
}
func TestLoadStateByRoot_CanGet(t *testing.T) {
ctx := context.Background()
db, _ := testDB.SetupDB(t)
service := New(db, cache.NewStateSummaryCache())
service.slotsPerArchivedPoint = 1
beaconState, _ := testutil.DeterministicGenesisState(t, 32)
blk := &ethpb.SignedBeaconBlock{Block: &ethpb.BeaconBlock{}}
blkRoot, err := stateutil.BlockRoot(blk.Block)
if err != nil {
t.Fatal(err)
}
if err := service.beaconDB.SaveArchivedPointRoot(ctx, blkRoot, 0); err != nil {
t.Fatal(err)
}
if err := service.beaconDB.SaveGenesisBlockRoot(ctx, blkRoot); err != nil {
t.Fatal(err)
}
if service.beaconDB.SaveBlock(ctx, blk) != nil {
t.Fatal(err)
}
if err := service.beaconDB.SaveState(ctx, beaconState, blkRoot); err != nil {
t.Fatal(err)
}
if err := service.beaconDB.SaveStateSummary(ctx, &pb.StateSummary{
Root: blkRoot[:],
Slot: 100,
}); err != nil {
t.Fatal(err)
}
loadedState, err := service.StateByRoot(ctx, blkRoot)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(loadedState.InnerStateUnsafe(), beaconState.InnerStateUnsafe()) {
t.Error("Did not correctly save state")
}
}
func TestLoadColdStateBySlot_CanGet(t *testing.T) {
ctx := context.Background()
db, _ := testDB.SetupDB(t)
service := New(db, cache.NewStateSummaryCache())
beaconState, _ := testutil.DeterministicGenesisState(t, 32)
blk := &ethpb.SignedBeaconBlock{Block: &ethpb.BeaconBlock{}}
blkRoot, err := stateutil.BlockRoot(blk.Block)
if err != nil {
t.Fatal(err)
}
if err := service.beaconDB.SaveArchivedPointRoot(ctx, blkRoot, 0); err != nil {
t.Fatal(err)
}
if err := service.beaconDB.SaveGenesisBlockRoot(ctx, blkRoot); err != nil {
t.Fatal(err)
}
if service.beaconDB.SaveBlock(ctx, blk) != nil {
t.Fatal(err)
}
if err := service.beaconDB.SaveState(ctx, beaconState, blkRoot); err != nil {
t.Fatal(err)
}
loadedState, err := service.loadColdStateBySlot(ctx, 200)
if err != nil {
t.Fatal(err)
}
if loadedState.Slot() != 200 {
t.Error("Did not correctly save state")
}
}