diff --git a/beacon-chain/db/kv/BUILD.bazel b/beacon-chain/db/kv/BUILD.bazel index 93d279f31..dc79fe082 100644 --- a/beacon-chain/db/kv/BUILD.bazel +++ b/beacon-chain/db/kv/BUILD.bazel @@ -14,7 +14,6 @@ go_library( "kv.go", "operations.go", "powchain.go", - "prune_states.go", "schema.go", "slashings.go", "state.go", diff --git a/beacon-chain/db/kv/kv.go b/beacon-chain/db/kv/kv.go index 6777b76ad..461e8411b 100644 --- a/beacon-chain/db/kv/kv.go +++ b/beacon-chain/db/kv/kv.go @@ -1,7 +1,6 @@ package kv import ( - "context" "os" "path" "time" @@ -112,10 +111,6 @@ func NewKVStore(dirPath string) (*Store, error) { return nil, err } - if err := kv.pruneStates(context.TODO()); err != nil { - return nil, err - } - err = prometheus.Register(createBoltCollector(kv.db)) return kv, err diff --git a/beacon-chain/db/kv/prune_states.go b/beacon-chain/db/kv/prune_states.go deleted file mode 100644 index deabb9e19..000000000 --- a/beacon-chain/db/kv/prune_states.go +++ /dev/null @@ -1,71 +0,0 @@ -package kv - -import ( - "bytes" - "context" - - "github.com/boltdb/bolt" - "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" - "github.com/prysmaticlabs/prysm/beacon-chain/db/filters" - "github.com/sirupsen/logrus" -) - -var pruneStatesKey = []byte("prune-states") - -func (k *Store) pruneStates(ctx context.Context) error { - var pruned bool - - k.db.View(func(tx *bolt.Tx) error { - bkt := tx.Bucket(migrationBucket) - v := bkt.Get(pruneStatesKey) - pruned = len(v) == 1 && v[0] == 0x01 - return nil - }) - - if pruned { - return nil - } - - log := logrus.WithField("prefix", "kv") - log.Info("Pruning states before last finalized check point. This might take a while...") - - roots, err := k.rootsToPrune(ctx) - if err != nil { - return err - } - - if err := k.DeleteStates(ctx, roots); err != nil { - return err - } - - return k.db.Update(func(tx *bolt.Tx) error { - bkt := tx.Bucket(migrationBucket) - return bkt.Put(pruneStatesKey, []byte{0x01}) - }) -} - -// This retrieves the key roots needed to prune states -// * Get last finalized check point -// * Rewind end slot until it's not finalized root -// * return roots between slot 1 and end slot -func (k *Store) rootsToPrune(ctx context.Context) ([][32]byte, error) { - cp, err := k.FinalizedCheckpoint(ctx) - if err != nil { - return nil, err - } - f := filters.NewFilter().SetStartSlot(1).SetEndSlot(helpers.StartSlot(cp.Epoch)) - roots, err := k.BlockRoots(ctx, f) - if err != nil { - return nil, err - } - // Ensure we don't delete finalized root - i := 0 - if len(roots) > 1 { - i = len(roots) - 1 - for bytes.Equal(roots[i][:], cp.Root) { - i-- - } - } - - return roots[:i], nil -}