2020-03-31 23:54:24 +00:00
|
|
|
package kv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-06-06 21:47:10 +00:00
|
|
|
"fmt"
|
2020-03-31 23:54:24 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2020-06-06 21:47:10 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
2020-04-20 20:19:53 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2020-03-31 23:54:24 +00:00
|
|
|
bolt "go.etcd.io/bbolt"
|
2020-06-23 13:51:00 +00:00
|
|
|
"go.opencensus.io/trace"
|
2020-03-31 23:54:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var historicalStateDeletedKey = []byte("historical-states-deleted")
|
2020-06-06 21:47:10 +00:00
|
|
|
var archivedSlotsPerPointKey = []byte("slots-per-archived-point")
|
2020-03-31 23:54:24 +00:00
|
|
|
|
2020-04-15 00:38:52 +00:00
|
|
|
// HistoricalStatesDeleted verifies historical states exist in DB.
|
|
|
|
func (kv *Store) HistoricalStatesDeleted(ctx context.Context) error {
|
2020-06-23 13:51:00 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.HistoricalStatesDeleted")
|
|
|
|
defer span.End()
|
2020-03-31 23:54:24 +00:00
|
|
|
|
2020-06-06 21:47:10 +00:00
|
|
|
if err := kv.verifySlotsPerArchivePoint(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-31 23:54:24 +00:00
|
|
|
var historicalStateDeleted bool
|
2020-04-13 04:11:09 +00:00
|
|
|
if err := kv.db.View(func(tx *bolt.Tx) error {
|
2020-03-31 23:54:24 +00:00
|
|
|
bkt := tx.Bucket(newStateServiceCompatibleBucket)
|
|
|
|
v := bkt.Get(historicalStateDeletedKey)
|
|
|
|
historicalStateDeleted = len(v) == 1 && v[0] == 0x01
|
|
|
|
return nil
|
2020-04-13 04:11:09 +00:00
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-03-31 23:54:24 +00:00
|
|
|
|
|
|
|
if historicalStateDeleted {
|
2020-05-27 17:35:54 +00:00
|
|
|
log.Warn("Regenerating and saving historical states. This may take a while. Skip this with --skip-regen-historical-states")
|
2020-03-31 23:54:24 +00:00
|
|
|
if err := kv.regenHistoricalStates(ctx); err != nil {
|
|
|
|
return errors.Wrap(err, "could not regenerate historical states, please retry")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return kv.db.Update(func(tx *bolt.Tx) error {
|
|
|
|
bkt := tx.Bucket(newStateServiceCompatibleBucket)
|
|
|
|
return bkt.Put(historicalStateDeletedKey, []byte{0x00})
|
|
|
|
})
|
|
|
|
}
|
2020-06-06 21:47:10 +00:00
|
|
|
|
|
|
|
// This verifies the slots per archived point has not been altered since it's used.
|
|
|
|
// The node does not allow slots per archived point to alter once it's in operation.
|
|
|
|
func (kv *Store) verifySlotsPerArchivePoint() error {
|
|
|
|
return kv.db.Update(func(tx *bolt.Tx) error {
|
|
|
|
bkt := tx.Bucket(newStateServiceCompatibleBucket)
|
|
|
|
v := bkt.Get(archivedSlotsPerPointKey)
|
|
|
|
if v == nil {
|
|
|
|
if err := bkt.Put(archivedSlotsPerPointKey, bytesutil.Bytes8(params.BeaconConfig().SlotsPerArchivedPoint)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
slotsPerPoint := bytesutil.FromBytes8(v)
|
|
|
|
if slotsPerPoint != params.BeaconConfig().SlotsPerArchivedPoint {
|
|
|
|
return fmt.Errorf("could not update --slots-per-archive-point after it has been set. Please continue to use %d, or resync from genesis using %d",
|
|
|
|
slotsPerPoint, params.BeaconConfig().SlotsPerArchivedPoint)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|