Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix state gen migration #5858

Merged
merged 8 commits into from
May 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions beacon-chain/state/stategen/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (s *State) MigrateToCold(ctx context.Context, finalizedSlot uint64, finaliz
// Verify migration is sensible. The new finalized point must increase the current split slot, and
// on an epoch boundary for hot state summary scheme to work.
currentSplitSlot := s.splitInfo.slot
if currentSplitSlot == 0 || currentSplitSlot > finalizedSlot {
if currentSplitSlot > finalizedSlot {
return nil
}

Expand Down Expand Up @@ -74,7 +74,8 @@ func (s *State) MigrateToCold(ctx context.Context, finalizedSlot uint64, finaliz
// Do not delete the current finalized state in case user wants to
// switch back to old state service, deleting the recent finalized state
// could cause issue switching back.
if s.beaconDB.HasState(ctx, r) && r != finalizedRoot {
lastArchivedIndexRoot := s.beaconDB.LastArchivedIndexRoot(ctx)
if s.beaconDB.HasState(ctx, r) && r != lastArchivedIndexRoot && r != finalizedRoot {
if err := s.beaconDB.DeleteState(ctx, r); err != nil {
// For whatever reason if node is unable to delete a state due to
// state is finalized, it is more reasonable to continue than to exit.
Expand Down
68 changes: 68 additions & 0 deletions beacon-chain/state/stategen/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,71 @@ func TestMigrateToCold_MigrationCompletes(t *testing.T) {
testutil.AssertLogsContain(t, hook, "Deleted state during migration")
testutil.AssertLogsContain(t, hook, "Set hot and cold state split point")
}

func TestMigrateToCold_CantDeleteCurrentArchivedIndex(t *testing.T) {
ctx := context.Background()
db := testDB.SetupDB(t)

service := New(db, cache.NewStateSummaryCache())
service.splitInfo.slot = 1
service.slotsPerArchivedPoint = 2

beaconState, _ := testutil.DeterministicGenesisState(t, 32)
if err := beaconState.SetSlot(params.BeaconConfig().SlotsPerEpoch); err != nil {
t.Fatal(err)
}
b := &ethpb.SignedBeaconBlock{
Block: &ethpb.BeaconBlock{Slot: 2},
}
if err := service.beaconDB.SaveBlock(ctx, b); err != nil {
t.Fatal(err)
}
bRoot, err := stateutil.BlockRoot(b.Block)
if err != nil {
t.Fatal(err)
}
if err := service.beaconDB.SaveStateSummary(ctx, &pb.StateSummary{Root: bRoot[:], Slot: 2}); err != nil {
t.Fatal(err)
}
if err := service.beaconDB.SaveState(ctx, beaconState, bRoot); err != nil {
t.Fatal(err)
}

newBeaconState, _ := testutil.DeterministicGenesisState(t, 32)
if err := newBeaconState.SetSlot(3); err != nil {
t.Fatal(err)
}
b = &ethpb.SignedBeaconBlock{
Block: &ethpb.BeaconBlock{Slot: 3},
}
if err := service.beaconDB.SaveBlock(ctx, b); err != nil {
t.Fatal(err)
}
bRoot, err = stateutil.BlockRoot(b.Block)
if err != nil {
t.Fatal(err)
}
if err := service.beaconDB.SaveStateSummary(ctx, &pb.StateSummary{Root: bRoot[:], Slot: 3}); err != nil {
t.Fatal(err)
}
if err := service.beaconDB.SaveState(ctx, newBeaconState, bRoot); err != nil {
t.Fatal(err)
}
if err := service.beaconDB.SaveArchivedPointRoot(ctx, bRoot, 1); err != nil {
t.Fatal(err)
}
if err := service.beaconDB.SaveLastArchivedIndex(ctx, 1); err != nil {
t.Fatal(err)
}

if err := service.MigrateToCold(ctx, beaconState.Slot(), [32]byte{}); err != nil {
t.Fatal(err)
}

if !service.beaconDB.HasArchivedPoint(ctx, 1) {
t.Error("Did not preserve archived point")
}
if !service.beaconDB.HasState(ctx, bRoot) {
t.Error("State should not be deleted")
}
}