2020-03-06 23:06:01 +00:00
|
|
|
package stategen
|
|
|
|
|
|
|
|
import (
|
2020-03-15 16:47:49 +00:00
|
|
|
"context"
|
2020-03-06 23:06:01 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-03-15 16:47:49 +00:00
|
|
|
"github.com/gogo/protobuf/proto"
|
2020-03-30 22:10:45 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/cache"
|
2020-03-15 16:47:49 +00:00
|
|
|
testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
|
2020-03-06 23:06:01 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
2020-03-15 16:47:49 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
2020-03-06 23:06:01 +00:00
|
|
|
)
|
|
|
|
|
2020-03-15 16:47:49 +00:00
|
|
|
func TestResume(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
db := testDB.SetupDB(t)
|
|
|
|
defer testDB.TeardownDB(t, db)
|
|
|
|
|
2020-03-30 22:10:45 +00:00
|
|
|
service := New(db, cache.NewStateSummaryCache())
|
2020-03-15 16:47:49 +00:00
|
|
|
root := [32]byte{'A'}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
if err := service.beaconDB.SaveState(ctx, beaconState, root); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := service.beaconDB.SaveArchivedPointRoot(ctx, root, 1); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := service.beaconDB.SaveLastArchivedIndex(ctx, 1); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-03-15 16:47:49 +00:00
|
|
|
|
2020-03-27 20:28:38 +00:00
|
|
|
resumeState, err := service.Resume(ctx)
|
2020-03-15 16:47:49 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !proto.Equal(beaconState.InnerStateUnsafe(), resumeState.InnerStateUnsafe()) {
|
|
|
|
t.Error("Diff saved state")
|
|
|
|
}
|
2020-03-27 20:28:38 +00:00
|
|
|
if service.splitInfo.slot != params.BeaconConfig().SlotsPerEpoch {
|
|
|
|
t.Errorf("Did not get watned slot")
|
2020-03-15 16:47:49 +00:00
|
|
|
}
|
2020-03-27 20:28:38 +00:00
|
|
|
if root != service.splitInfo.root {
|
|
|
|
t.Errorf("Did not get wanted root")
|
2020-03-15 16:47:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestVerifySlotsPerArchivePoint(t *testing.T) {
|
2020-03-06 23:06:01 +00:00
|
|
|
type tc struct {
|
|
|
|
input uint64
|
|
|
|
result bool
|
|
|
|
}
|
|
|
|
tests := []tc{
|
|
|
|
{0, false},
|
|
|
|
{1, false},
|
|
|
|
{params.BeaconConfig().SlotsPerEpoch, true},
|
|
|
|
{params.BeaconConfig().SlotsPerEpoch + 1, false},
|
|
|
|
{params.BeaconConfig().SlotsPerHistoricalRoot, true},
|
|
|
|
{params.BeaconConfig().SlotsPerHistoricalRoot + 1, false},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
if got := verifySlotsPerArchivePoint(tt.input); got != tt.result {
|
|
|
|
t.Errorf("verifySlotsPerArchivePoint(%d) = %v, want %v", tt.input, got, tt.result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|