2020-03-12 00:38:30 +00:00
|
|
|
package stategen
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-03-31 16:23:39 +00:00
|
|
|
"strings"
|
2020-03-12 00:38:30 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
2020-03-30 22:10:45 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/cache"
|
2020-03-12 00:38:30 +00:00
|
|
|
testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
|
2020-05-05 04:30:24 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
|
2020-03-12 21:04:24 +00:00
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
2020-03-12 00:38:30 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
|
|
|
)
|
|
|
|
|
2020-03-12 12:58:06 +00:00
|
|
|
func TestSaveColdState_NonArchivedPoint(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
db := testDB.SetupDB(t)
|
|
|
|
|
2020-03-30 22:10:45 +00:00
|
|
|
service := New(db, cache.NewStateSummaryCache())
|
2020-03-12 12:58:06 +00:00
|
|
|
service.slotsPerArchivedPoint = 2
|
|
|
|
beaconState, _ := testutil.DeterministicGenesisState(t, 32)
|
2020-04-14 16:41:09 +00:00
|
|
|
if err := beaconState.SetSlot(1); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-03-12 12:58:06 +00:00
|
|
|
|
2020-04-21 18:18:55 +00:00
|
|
|
if err := service.saveColdState(ctx, [32]byte{}, beaconState); err != nil {
|
|
|
|
t.Error(err)
|
2020-03-12 12:58:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-31 16:23:39 +00:00
|
|
|
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)
|
2020-04-14 16:41:09 +00:00
|
|
|
if err := beaconState.SetSlot(1); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-03-31 16:23:39 +00:00
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-13 16:32:02 +00:00
|
|
|
func TestLoadColdStateByRoot_CanGet(t *testing.T) {
|
2020-03-31 16:23:39 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
db := testDB.SetupDB(t)
|
|
|
|
|
|
|
|
service := New(db, cache.NewStateSummaryCache())
|
|
|
|
service.slotsPerArchivedPoint = 1
|
|
|
|
|
|
|
|
beaconState, _ := testutil.DeterministicGenesisState(t, 32)
|
|
|
|
blk := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}}
|
2020-05-05 04:30:24 +00:00
|
|
|
blkRoot, err := stateutil.BlockRoot(blk.Block)
|
2020-04-14 16:41:09 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-05-06 20:25:37 +00:00
|
|
|
if err := service.beaconDB.SaveArchivedPointRoot(ctx, blkRoot, 0); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-04-14 16:41:09 +00:00
|
|
|
if err := service.beaconDB.SaveGenesisBlockRoot(ctx, blkRoot); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-03-31 16:23:39 +00:00
|
|
|
if err := service.beaconDB.SaveState(ctx, beaconState, blkRoot); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := service.beaconDB.SaveStateSummary(ctx, &pb.StateSummary{
|
|
|
|
Root: blkRoot[:],
|
2020-04-13 16:32:02 +00:00
|
|
|
Slot: 100,
|
2020-03-31 16:23:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
loadedState, err := service.loadColdStateByRoot(ctx, blkRoot)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-04-13 16:32:02 +00:00
|
|
|
if loadedState.Slot() != 100 {
|
2020-03-31 16:23:39 +00:00
|
|
|
t.Error("Did not correctly save state")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-13 16:32:02 +00:00
|
|
|
func TestLoadColdStateBySlot_CanGet(t *testing.T) {
|
2020-03-31 16:23:39 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
db := testDB.SetupDB(t)
|
|
|
|
|
|
|
|
service := New(db, cache.NewStateSummaryCache())
|
|
|
|
|
|
|
|
beaconState, _ := testutil.DeterministicGenesisState(t, 32)
|
|
|
|
blk := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}}
|
2020-05-05 04:30:24 +00:00
|
|
|
blkRoot, err := stateutil.BlockRoot(blk.Block)
|
2020-04-14 16:41:09 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-05-06 20:25:37 +00:00
|
|
|
if err := service.beaconDB.SaveArchivedPointRoot(ctx, blkRoot, 0); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-04-14 16:41:09 +00:00
|
|
|
if err := service.beaconDB.SaveGenesisBlockRoot(ctx, blkRoot); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-03-31 16:23:39 +00:00
|
|
|
if err := service.beaconDB.SaveState(ctx, beaconState, blkRoot); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-04-13 16:32:02 +00:00
|
|
|
loadedState, err := service.loadColdStateBySlot(ctx, 200)
|
2020-03-31 16:23:39 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-04-13 16:32:02 +00:00
|
|
|
if loadedState.Slot() != 200 {
|
2020-03-31 16:23:39 +00:00
|
|
|
t.Error("Did not correctly save state")
|
|
|
|
}
|
|
|
|
}
|