prysm-pulse/beacon-chain/state/stategen/setter_test.go

105 lines
2.8 KiB
Go
Raw Normal View History

package stategen
import (
"context"
"testing"
//"github.com/gogo/protobuf/proto"
"github.com/prysmaticlabs/prysm/beacon-chain/cache"
testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/testutil"
logTest "github.com/sirupsen/logrus/hooks/test"
)
func TestSaveState_ColdStateCanBeSaved(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)
// This goes to cold section.
slot := uint64(1)
if err := beaconState.SetSlot(slot); err != nil {
t.Fatal(err)
}
service.finalizedInfo.slot = slot + 1
r := [32]byte{'a'}
if err := service.SaveState(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")
}
testutil.AssertLogsContain(t, hook, "Saved full state on archived point")
}
func TestSaveState_HotStateCanBeSaved(t *testing.T) {
ctx := context.Background()
db, _ := testDB.SetupDB(t)
service := New(db, cache.NewStateSummaryCache())
service.slotsPerArchivedPoint = 1
beaconState, _ := testutil.DeterministicGenesisState(t, 32)
// This goes to hot section, verify it can save on epoch boundary.
if err := beaconState.SetSlot(params.BeaconConfig().SlotsPerEpoch); err != nil {
t.Fatal(err)
}
r := [32]byte{'a'}
if err := service.SaveState(ctx, r, beaconState); err != nil {
t.Fatal(err)
}
// Should save both state and state summary.
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
_, ok, err := service.epochBoundaryStateCache.getByRoot(r)
if err != nil {
t.Fatal(err)
}
if !ok {
t.Error("Should have saved the state")
}
if !service.stateSummaryCache.Has(r) {
t.Error("Should have saved the state summary")
}
}
func TestSaveState_HotStateCached(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)
if err := beaconState.SetSlot(params.BeaconConfig().SlotsPerEpoch); err != nil {
t.Fatal(err)
}
// Cache the state prior.
r := [32]byte{'a'}
service.hotStateCache.Put(r, beaconState)
if err := service.SaveState(ctx, r, beaconState); err != nil {
t.Fatal(err)
}
// Should not save the state and state summary.
if service.beaconDB.HasState(ctx, r) {
t.Error("Should not have saved the state")
}
if service.beaconDB.HasStateSummary(ctx, r) {
t.Error("Should have saved the state summary")
}
testutil.AssertLogsDoNotContain(t, hook, "Saved full state on epoch boundary")
}