2020-03-14 16:31:21 +00:00
|
|
|
package stategen
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2020-03-27 20:28:38 +00:00
|
|
|
//"github.com/gogo/protobuf/proto"
|
2020-03-30 22:10:45 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/cache"
|
2020-03-14 16:31:21 +00:00
|
|
|
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()
|
2020-06-23 20:40:55 +00:00
|
|
|
db, _ := testDB.SetupDB(t)
|
2020-03-14 16:31:21 +00:00
|
|
|
|
2020-03-30 22:10:45 +00:00
|
|
|
service := New(db, cache.NewStateSummaryCache())
|
2020-03-14 16:31:21 +00:00
|
|
|
service.slotsPerArchivedPoint = 1
|
|
|
|
beaconState, _ := testutil.DeterministicGenesisState(t, 32)
|
|
|
|
|
|
|
|
// This goes to cold section.
|
|
|
|
slot := uint64(1)
|
2020-04-14 16:41:09 +00:00
|
|
|
if err := beaconState.SetSlot(slot); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-07-03 06:33:58 +00:00
|
|
|
service.finalizedInfo.slot = slot + 1
|
2020-03-14 16:31:21 +00:00
|
|
|
|
|
|
|
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()
|
2020-06-23 20:40:55 +00:00
|
|
|
db, _ := testDB.SetupDB(t)
|
2020-03-14 16:31:21 +00:00
|
|
|
|
2020-03-30 22:10:45 +00:00
|
|
|
service := New(db, cache.NewStateSummaryCache())
|
2020-03-14 16:31:21 +00:00
|
|
|
service.slotsPerArchivedPoint = 1
|
|
|
|
beaconState, _ := testutil.DeterministicGenesisState(t, 32)
|
|
|
|
// This goes to hot section, verify it can save on epoch boundary.
|
2020-04-14 16:41:09 +00:00
|
|
|
if err := beaconState.SetSlot(params.BeaconConfig().SlotsPerEpoch); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-03-14 16:31:21 +00:00
|
|
|
|
|
|
|
r := [32]byte{'a'}
|
|
|
|
if err := service.SaveState(ctx, r, beaconState); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should save both state and state summary.
|
2020-07-06 17:22:12 +00:00
|
|
|
_, ok, err := service.epochBoundaryStateCache.getByRoot(r)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !ok {
|
2020-03-14 16:31:21 +00:00
|
|
|
t.Error("Should have saved the state")
|
|
|
|
}
|
2020-03-30 22:10:45 +00:00
|
|
|
if !service.stateSummaryCache.Has(r) {
|
2020-03-14 16:31:21 +00:00
|
|
|
t.Error("Should have saved the state summary")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSaveState_HotStateCached(t *testing.T) {
|
|
|
|
hook := logTest.NewGlobal()
|
|
|
|
ctx := context.Background()
|
2020-06-23 20:40:55 +00:00
|
|
|
db, _ := testDB.SetupDB(t)
|
2020-03-14 16:31:21 +00:00
|
|
|
|
2020-03-30 22:10:45 +00:00
|
|
|
service := New(db, cache.NewStateSummaryCache())
|
2020-03-14 16:31:21 +00:00
|
|
|
service.slotsPerArchivedPoint = 1
|
|
|
|
beaconState, _ := testutil.DeterministicGenesisState(t, 32)
|
2020-04-14 16:41:09 +00:00
|
|
|
if err := beaconState.SetSlot(params.BeaconConfig().SlotsPerEpoch); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-03-14 16:31:21 +00:00
|
|
|
|
|
|
|
// 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")
|
|
|
|
}
|