Skip to content

Commit

Permalink
Check new state mgmt service is compatible with DB (#5231)
Browse files Browse the repository at this point in the history
  • Loading branch information
terencechain committed Mar 29, 2020
1 parent b40e6db commit c67b01e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
1 change: 1 addition & 0 deletions beacon-chain/db/kv/BUILD.bazel
Expand Up @@ -8,6 +8,7 @@ go_library(
"attestations.go",
"backup.go",
"blocks.go",
"check_state.go",
"checkpoint.go",
"deposit_contract.go",
"encoding.go",
Expand Down
33 changes: 33 additions & 0 deletions beacon-chain/db/kv/check_state.go
@@ -0,0 +1,33 @@
package kv

import (
"errors"

"github.com/prysmaticlabs/prysm/shared/featureconfig"
bolt "go.etcd.io/bbolt"
)

var historicalStateDeletedKey = []byte("historical-states-deleted")

func (kv *Store) ensureNewStateServiceCompatible() error {
if !featureconfig.Get().NewStateMgmt {
return kv.db.Update(func(tx *bolt.Tx) error {
bkt := tx.Bucket(newStateServiceCompatibleBucket)
return bkt.Put(historicalStateDeletedKey, []byte{0x01})
})
}

var historicalStateDeleted bool
kv.db.View(func(tx *bolt.Tx) error {
bkt := tx.Bucket(newStateServiceCompatibleBucket)
v := bkt.Get(historicalStateDeletedKey)
historicalStateDeleted = len(v) == 1 && v[0] == 0x01
return nil
})

if historicalStateDeleted {
return errors.New("historical states were pruned in db, do not run with flag --new-state-mgmt")
}

return nil
}
8 changes: 6 additions & 2 deletions beacon-chain/db/kv/kv.go
Expand Up @@ -110,13 +110,17 @@ func NewKVStore(dirPath string) (*Store, error) {
blockSlotIndicesBucket,
blockParentRootIndicesBucket,
finalizedBlockRootsIndexBucket,
// Migration bucket.
migrationBucket,
// New State Management service bucket.
newStateServiceCompatibleBucket,
)
}); err != nil {
return nil, err
}

if err := kv.ensureNewStateServiceCompatible(); err != nil {
return nil, err
}

err = prometheus.Register(createBoltCollector(kv.db))

return kv, err
Expand Down
4 changes: 2 additions & 2 deletions beacon-chain/db/kv/schema.go
Expand Up @@ -46,6 +46,6 @@ var (
savedBlockSlotsKey = []byte("saved-block-slots")
savedStateSlotsKey = []byte("saved-state-slots")

// Migration bucket.
migrationBucket = []byte("migrations")
// New state management service compatibility bucket.
newStateServiceCompatibleBucket = []byte("new-state-compatible")
)

0 comments on commit c67b01e

Please sign in to comment.