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

Guard against deleting genesis and finalized state in DB #3842

Merged
merged 8 commits into from
Oct 24, 2019
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions beacon-chain/blockchain/forkchoice/process_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,18 @@ func (s *Store) OnBlock(ctx context.Context, b *ethpb.BeaconBlock) error {
if postState.FinalizedCheckpoint.Epoch > s.finalizedCheckpt.Epoch {
s.clearSeenAtts()
helpers.ClearAllCaches()
if err := s.db.SaveFinalizedCheckpoint(ctx, postState.FinalizedCheckpoint); err != nil {
return errors.Wrap(err, "could not save finalized checkpoint")
}

startSlot := helpers.StartSlot(s.finalizedCheckpt.Epoch + 1)
endSlot := helpers.StartSlot(postState.FinalizedCheckpoint.Epoch+1) - 1 // Inclusive
if err := s.rmStatesBySlots(ctx, startSlot, endSlot); err != nil {
if err := s.rmStatesSinceLastFinalized(ctx, startSlot, endSlot); err != nil {
return errors.Wrapf(err, "could not delete states prior to finalized check point, range: %d, %d",
startSlot, endSlot+params.BeaconConfig().SlotsPerEpoch)
}

s.finalizedCheckpt = postState.FinalizedCheckpoint
if err := s.db.SaveFinalizedCheckpoint(ctx, postState.FinalizedCheckpoint); err != nil {
return errors.Wrap(err, "could not save finalized checkpoint")
}
}

// Update validator indices in database as needed.
Expand Down Expand Up @@ -186,7 +187,7 @@ func (s *Store) OnBlockNoVerifyStateTransition(ctx context.Context, b *ethpb.Bea
helpers.ClearAllCaches()
startSlot := helpers.StartSlot(s.finalizedCheckpt.Epoch + 1)
endSlot := helpers.StartSlot(postState.FinalizedCheckpoint.Epoch+1) - 1 // Inclusive
if err := s.rmStatesBySlots(ctx, startSlot, endSlot); err != nil {
if err := s.rmStatesSinceLastFinalized(ctx, startSlot, endSlot); err != nil {
return errors.Wrapf(err, "could not delete states prior to finalized check point, range: %d, %d",
startSlot, endSlot+params.BeaconConfig().SlotsPerEpoch)
}
Expand Down Expand Up @@ -376,14 +377,14 @@ func (s *Store) clearSeenAtts() {
s.seenAtts = make(map[[32]byte]bool)
}

// rmStatesBySlots deletes the states in db in between the range of slots.
func (s *Store) rmStatesBySlots(ctx context.Context, startSlot uint64, endSlot uint64) error {
// rmStatesSinceLastFinalized deletes the states in db since last finalized check point.
func (s *Store) rmStatesSinceLastFinalized(ctx context.Context, startSlot uint64, endSlot uint64) error {
ctx, span := trace.StartSpan(ctx, "forkchoice.rmStatesBySlots")
defer span.End()

// Do not remove genesis state.
prestonvanloon marked this conversation as resolved.
Show resolved Hide resolved
if startSlot == 0 {
startSlot = 1
// Do not remove genesis state or finalized state at epoch boundary.
if startSlot%params.BeaconConfig().SlotsPerEpoch == 0 {
startSlot++
}

filter := filters.NewFilter().SetStartSlot(startSlot).SetEndSlot(endSlot)
Expand Down
15 changes: 9 additions & 6 deletions beacon-chain/blockchain/forkchoice/process_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ func TestStore_SavesNewBlockAttestations(t *testing.T) {
}
}

func TestRemoveStateBySlots(t *testing.T) {
func TestRemoveStateSinceLastFinalized(t *testing.T) {
ctx := context.Background()
db := testDB.SetupDB(t)
defer testDB.TeardownDB(t, db)
Expand Down Expand Up @@ -309,7 +309,7 @@ func TestRemoveStateBySlots(t *testing.T) {
// New finalized epoch: 1
finalizedEpoch := uint64(1)
endSlot := helpers.StartSlot(finalizedEpoch+1) - 1 // Inclusive
if err := store.rmStatesBySlots(ctx, 0, endSlot); err != nil {
if err := store.rmStatesSinceLastFinalized(ctx, 0, endSlot); err != nil {
t.Fatal(err)
}
for _, r := range blockRoots {
Expand All @@ -326,17 +326,20 @@ func TestRemoveStateBySlots(t *testing.T) {
// New finalized epoch: 5
newFinalizedEpoch := uint64(5)
endSlot = helpers.StartSlot(newFinalizedEpoch+1) - 1 // Inclusive
if err := store.rmStatesBySlots(ctx, helpers.StartSlot(finalizedEpoch+1), endSlot); err != nil {
if err := store.rmStatesSinceLastFinalized(ctx, helpers.StartSlot(finalizedEpoch+1), endSlot); err != nil {
t.Fatal(err)
}
for _, r := range blockRoots {
s, err := store.db.State(ctx, r)
if err != nil {
t.Fatal(err)
}
// Also verifies genesis state didnt get deleted
if s != nil && s.Slot != 0 && s.Slot < endSlot {
t.Errorf("State with slot %d should not be in DB", s.Slot)
// Also verifies boundary state didnt get deleted
if s != nil {
isBoundary := s.Slot%params.BeaconConfig().SlotsPerEpoch == 0
if !isBoundary && s.Slot < endSlot {
t.Errorf("State with slot %d should not be in DB", s.Slot)
}
}
}
}
22 changes: 21 additions & 1 deletion beacon-chain/db/kv/state.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kv

import (
"bytes"
"context"
"fmt"
"strings"
Expand All @@ -10,6 +11,7 @@ import (
"github.com/gogo/protobuf/proto"
"github.com/pkg/errors"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
"go.opencensus.io/trace"
)

Expand Down Expand Up @@ -104,8 +106,26 @@ func (k *Store) SaveState(ctx context.Context, state *pb.BeaconState, blockRoot
func (k *Store) DeleteState(ctx context.Context, blockRoot [32]byte) error {
ctx, span := trace.StartSpan(ctx, "BeaconDB.DeleteState")
defer span.End()

return k.db.Batch(func(tx *bolt.Tx) error {
bkt := tx.Bucket(stateBucket)
bkt := tx.Bucket(blocksBucket)
genesisBlockRoot := bkt.Get(genesisBlockRootKey)

bkt = tx.Bucket(checkpointBucket)
enc := bkt.Get(finalizedCheckpointKey)
checkpoint := &ethpb.Checkpoint{}
if enc == nil {
checkpoint = &ethpb.Checkpoint{Root: genesisBlockRoot}
} else {
proto.Unmarshal(enc, checkpoint)
}

// Safe guard against deleting genesis or finalized state.
if bytes.Equal(blockRoot[:], checkpoint.Root) || bytes.Equal(blockRoot[:], genesisBlockRoot) {
return errors.New("could not delete genesis or finalized state")
}

bkt = tx.Bucket(stateBucket)
return bkt.Delete(blockRoot[:])
})
}
Expand Down
39 changes: 39 additions & 0 deletions beacon-chain/db/kv/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,42 @@ func TestStore_StatesBatchDelete(t *testing.T) {
}
}
}

func TestStore_DeleteGenesisState(t *testing.T) {
db := setupDB(t)
defer teardownDB(t, db)
ctx := context.Background()

genesisBlockRoot := [32]byte{'A'}
if err := db.SaveGenesisBlockRoot(ctx, genesisBlockRoot); err != nil {
t.Fatal(err)
}
genesisState := &pb.BeaconState{Slot: 100}
if err := db.SaveState(ctx, genesisState, genesisBlockRoot); err != nil {
t.Fatal(err)
}
wantedErr := "could not delete genesis or finalized state"
if err := db.DeleteState(ctx, genesisBlockRoot); err.Error() != wantedErr {
t.Error("Did not receive wanted error")
}
}

func TestStore_DeleteFinalizedState(t *testing.T) {
db := setupDB(t)
defer teardownDB(t, db)
ctx := context.Background()

finalizedBlockRoot := [32]byte{'A'}
finalizedCheckpoint := &ethpb.Checkpoint{Root: finalizedBlockRoot[:]}
if err := db.SaveFinalizedCheckpoint(ctx, finalizedCheckpoint); err != nil {
t.Fatal(err)
}
finalizedState := &pb.BeaconState{Slot: 100}
if err := db.SaveState(ctx, finalizedState, finalizedBlockRoot); err != nil {
t.Fatal(err)
}
wantedErr := "could not delete genesis or finalized state"
if err := db.DeleteState(ctx, finalizedBlockRoot); err.Error() != wantedErr {
t.Error("Did not receive wanted error")
}
}
1 change: 0 additions & 1 deletion tools/interop/export-genesis/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/prysmaticlabs/prysm/beacon-chain/db"
)


// A basic tool to extract genesis.ssz from existing beaconchain.db.
// ex:
// bazel run //tools/interop/export-genesis:export-genesis -- /tmp/data/beaconchaindata /tmp/genesis.ssz
Expand Down