Remove prune state (#4680)

* Rm prune state
* Merge branch 'master' into rm-prune-state
This commit is contained in:
terence tsao 2020-01-28 19:53:57 -08:00 committed by GitHub
parent ade61717a4
commit a22c97739e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 0 additions and 77 deletions

View File

@ -14,7 +14,6 @@ go_library(
"kv.go", "kv.go",
"operations.go", "operations.go",
"powchain.go", "powchain.go",
"prune_states.go",
"schema.go", "schema.go",
"slashings.go", "slashings.go",
"state.go", "state.go",

View File

@ -1,7 +1,6 @@
package kv package kv
import ( import (
"context"
"os" "os"
"path" "path"
"time" "time"
@ -112,10 +111,6 @@ func NewKVStore(dirPath string) (*Store, error) {
return nil, err return nil, err
} }
if err := kv.pruneStates(context.TODO()); err != nil {
return nil, err
}
err = prometheus.Register(createBoltCollector(kv.db)) err = prometheus.Register(createBoltCollector(kv.db))
return kv, err return kv, err

View File

@ -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
}