2019-08-11 00:50:10 +00:00
|
|
|
package kv
|
|
|
|
|
|
|
|
import (
|
2019-10-24 05:39:41 +00:00
|
|
|
"bytes"
|
2019-08-11 00:50:10 +00:00
|
|
|
"context"
|
2020-03-24 21:51:24 +00:00
|
|
|
"math"
|
2019-08-11 00:50:10 +00:00
|
|
|
|
2019-08-13 22:33:31 +00:00
|
|
|
"github.com/pkg/errors"
|
2019-11-27 05:08:18 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
2020-03-24 21:51:24 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/db/filters"
|
2020-01-31 20:57:01 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state"
|
2019-08-11 00:50:10 +00:00
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
2020-02-19 11:36:37 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
2020-03-16 19:07:07 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/featureconfig"
|
2020-03-24 20:00:54 +00:00
|
|
|
bolt "go.etcd.io/bbolt"
|
2019-08-20 19:35:48 +00:00
|
|
|
"go.opencensus.io/trace"
|
2019-08-11 00:50:10 +00:00
|
|
|
)
|
|
|
|
|
2019-08-13 22:33:31 +00:00
|
|
|
// State returns the saved state using block's signing root,
|
|
|
|
// this particular block was used to generate the state.
|
2020-01-31 20:57:01 +00:00
|
|
|
func (k *Store) State(ctx context.Context, blockRoot [32]byte) (*state.BeaconState, error) {
|
2019-08-20 19:35:48 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.State")
|
|
|
|
defer span.End()
|
2019-08-13 22:33:31 +00:00
|
|
|
var s *pb.BeaconState
|
|
|
|
err := k.db.View(func(tx *bolt.Tx) error {
|
|
|
|
bucket := tx.Bucket(stateBucket)
|
|
|
|
enc := bucket.Get(blockRoot[:])
|
|
|
|
if enc == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
s, err = createState(enc)
|
|
|
|
return err
|
|
|
|
})
|
2020-01-31 20:57:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if s == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return state.InitializeFromProtoUnsafe(s)
|
2019-08-11 00:50:10 +00:00
|
|
|
}
|
|
|
|
|
2019-08-13 22:33:31 +00:00
|
|
|
// HeadState returns the latest canonical state in beacon chain.
|
2020-01-31 20:57:01 +00:00
|
|
|
func (k *Store) HeadState(ctx context.Context) (*state.BeaconState, error) {
|
2019-08-20 19:35:48 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.HeadState")
|
|
|
|
defer span.End()
|
2019-08-13 22:33:31 +00:00
|
|
|
var s *pb.BeaconState
|
|
|
|
err := k.db.View(func(tx *bolt.Tx) error {
|
|
|
|
// Retrieve head block's signing root from blocks bucket,
|
|
|
|
// to look up what the head state is.
|
|
|
|
bucket := tx.Bucket(blocksBucket)
|
|
|
|
headBlkRoot := bucket.Get(headBlockRootKey)
|
|
|
|
|
|
|
|
bucket = tx.Bucket(stateBucket)
|
|
|
|
enc := bucket.Get(headBlkRoot)
|
|
|
|
if enc == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
s, err = createState(enc)
|
|
|
|
return err
|
|
|
|
})
|
2020-01-31 20:57:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if s == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2019-09-04 03:03:09 +00:00
|
|
|
span.AddAttributes(trace.BoolAttribute("exists", s != nil))
|
|
|
|
if s != nil {
|
|
|
|
span.AddAttributes(trace.Int64Attribute("slot", int64(s.Slot)))
|
|
|
|
}
|
2020-01-31 20:57:01 +00:00
|
|
|
return state.InitializeFromProtoUnsafe(s)
|
2019-08-11 00:50:10 +00:00
|
|
|
}
|
|
|
|
|
2019-08-29 22:32:35 +00:00
|
|
|
// GenesisState returns the genesis state in beacon chain.
|
2020-01-31 20:57:01 +00:00
|
|
|
func (k *Store) GenesisState(ctx context.Context) (*state.BeaconState, error) {
|
2019-08-29 22:32:35 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.GenesisState")
|
|
|
|
defer span.End()
|
|
|
|
var s *pb.BeaconState
|
|
|
|
err := k.db.View(func(tx *bolt.Tx) error {
|
|
|
|
// Retrieve genesis block's signing root from blocks bucket,
|
|
|
|
// to look up what the genesis state is.
|
|
|
|
bucket := tx.Bucket(blocksBucket)
|
|
|
|
genesisBlockRoot := bucket.Get(genesisBlockRootKey)
|
|
|
|
|
|
|
|
bucket = tx.Bucket(stateBucket)
|
|
|
|
enc := bucket.Get(genesisBlockRoot)
|
|
|
|
if enc == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
s, err = createState(enc)
|
|
|
|
return err
|
|
|
|
})
|
2020-01-31 20:57:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if s == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return state.InitializeFromProtoUnsafe(s)
|
2019-08-29 22:32:35 +00:00
|
|
|
}
|
|
|
|
|
2019-08-13 22:33:31 +00:00
|
|
|
// SaveState stores a state to the db using block's signing root which was used to generate the state.
|
2020-01-31 20:57:01 +00:00
|
|
|
func (k *Store) SaveState(ctx context.Context, state *state.BeaconState, blockRoot [32]byte) error {
|
2019-08-20 19:35:48 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.SaveState")
|
|
|
|
defer span.End()
|
2020-02-05 19:52:14 +00:00
|
|
|
if state == nil {
|
|
|
|
return errors.New("nil state")
|
|
|
|
}
|
2020-01-31 20:57:01 +00:00
|
|
|
enc, err := encode(state.InnerStateUnsafe())
|
2019-08-13 22:33:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return k.db.Update(func(tx *bolt.Tx) error {
|
|
|
|
bucket := tx.Bucket(stateBucket)
|
2020-03-24 21:51:24 +00:00
|
|
|
if err := bucket.Put(blockRoot[:], enc); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return k.setStateSlotBitField(ctx, tx, state.Slot())
|
2019-08-13 22:33:31 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-02-18 21:10:54 +00:00
|
|
|
// SaveStates stores multiple states to the db using the provided corresponding roots.
|
|
|
|
func (k *Store) SaveStates(ctx context.Context, states []*state.BeaconState, blockRoots [][32]byte) error {
|
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.SaveStates")
|
|
|
|
defer span.End()
|
|
|
|
if states == nil {
|
|
|
|
return errors.New("nil state")
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
multipleEncs := make([][]byte, len(states))
|
|
|
|
for i, st := range states {
|
|
|
|
multipleEncs[i], err = encode(st.InnerStateUnsafe())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return k.db.Update(func(tx *bolt.Tx) error {
|
|
|
|
bucket := tx.Bucket(stateBucket)
|
|
|
|
for i, rt := range blockRoots {
|
2020-03-24 21:51:24 +00:00
|
|
|
if err := k.setStateSlotBitField(ctx, tx, states[i].Slot()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-18 21:10:54 +00:00
|
|
|
err = bucket.Put(rt[:], multipleEncs[i])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-01-10 14:25:43 +00:00
|
|
|
// HasState checks if a state by root exists in the db.
|
|
|
|
func (k *Store) HasState(ctx context.Context, blockRoot [32]byte) bool {
|
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.HasState")
|
|
|
|
defer span.End()
|
|
|
|
var exists bool
|
|
|
|
// #nosec G104. Always returns nil.
|
|
|
|
k.db.View(func(tx *bolt.Tx) error {
|
|
|
|
bucket := tx.Bucket(stateBucket)
|
|
|
|
exists = bucket.Get(blockRoot[:]) != nil
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return exists
|
|
|
|
}
|
|
|
|
|
2019-10-22 18:13:41 +00:00
|
|
|
// DeleteState by block root.
|
|
|
|
func (k *Store) DeleteState(ctx context.Context, blockRoot [32]byte) error {
|
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.DeleteState")
|
|
|
|
defer span.End()
|
2019-10-24 05:39:41 +00:00
|
|
|
|
2020-02-05 21:01:56 +00:00
|
|
|
return k.db.Update(func(tx *bolt.Tx) error {
|
2019-10-24 05:39:41 +00:00
|
|
|
bkt := tx.Bucket(blocksBucket)
|
|
|
|
genesisBlockRoot := bkt.Get(genesisBlockRootKey)
|
|
|
|
|
|
|
|
bkt = tx.Bucket(checkpointBucket)
|
|
|
|
enc := bkt.Get(finalizedCheckpointKey)
|
|
|
|
checkpoint := ðpb.Checkpoint{}
|
|
|
|
if enc == nil {
|
|
|
|
checkpoint = ðpb.Checkpoint{Root: genesisBlockRoot}
|
2019-11-27 06:32:56 +00:00
|
|
|
} else if err := decode(enc, checkpoint); err != nil {
|
|
|
|
return err
|
2019-10-24 05:39:41 +00:00
|
|
|
}
|
|
|
|
|
2019-12-09 07:35:18 +00:00
|
|
|
bkt = tx.Bucket(blocksBucket)
|
|
|
|
headBlkRoot := bkt.Get(headBlockRootKey)
|
|
|
|
|
2020-03-16 19:07:07 +00:00
|
|
|
if featureconfig.Get().NewStateMgmt {
|
|
|
|
if tx.Bucket(stateSummaryBucket).Get(blockRoot[:]) == nil {
|
|
|
|
return errors.New("cannot delete state without state summary")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Safe guard against deleting genesis, finalized, head state.
|
|
|
|
if bytes.Equal(blockRoot[:], checkpoint.Root) || bytes.Equal(blockRoot[:], genesisBlockRoot) || bytes.Equal(blockRoot[:], headBlkRoot) {
|
|
|
|
return errors.New("cannot delete genesis, finalized, or head state")
|
|
|
|
}
|
2019-10-24 05:39:41 +00:00
|
|
|
}
|
|
|
|
|
2020-03-24 21:51:24 +00:00
|
|
|
slot, err := slotByBlockRoot(ctx, tx, blockRoot[:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := k.clearStateSlotBitField(ctx, tx, slot); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-24 05:39:41 +00:00
|
|
|
bkt = tx.Bucket(stateBucket)
|
2019-10-22 18:13:41 +00:00
|
|
|
return bkt.Delete(blockRoot[:])
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteStates by block roots.
|
2020-02-19 21:10:56 +00:00
|
|
|
//
|
|
|
|
// Note: bkt.Delete(key) uses a binary search to find the item in the database. Iterating with a
|
|
|
|
// cursor is faster when there are a large set of keys to delete. This method is O(n) deletion where
|
|
|
|
// n is the number of keys in the database. The alternative of calling bkt.Delete on each key to
|
|
|
|
// delete would be O(m*log(n)) which would be much slower given a large set of keys to delete.
|
2019-10-22 18:13:41 +00:00
|
|
|
func (k *Store) DeleteStates(ctx context.Context, blockRoots [][32]byte) error {
|
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.DeleteStates")
|
|
|
|
defer span.End()
|
2019-11-27 06:32:56 +00:00
|
|
|
|
2020-02-19 11:36:37 +00:00
|
|
|
rootMap := make(map[[32]byte]bool)
|
|
|
|
for _, blockRoot := range blockRoots {
|
|
|
|
rootMap[blockRoot] = true
|
|
|
|
}
|
|
|
|
|
2020-02-05 21:01:56 +00:00
|
|
|
return k.db.Update(func(tx *bolt.Tx) error {
|
2019-11-27 06:32:56 +00:00
|
|
|
bkt := tx.Bucket(blocksBucket)
|
|
|
|
genesisBlockRoot := bkt.Get(genesisBlockRootKey)
|
|
|
|
|
|
|
|
bkt = tx.Bucket(checkpointBucket)
|
|
|
|
enc := bkt.Get(finalizedCheckpointKey)
|
|
|
|
checkpoint := ðpb.Checkpoint{}
|
|
|
|
if enc == nil {
|
|
|
|
checkpoint = ðpb.Checkpoint{Root: genesisBlockRoot}
|
|
|
|
} else if err := decode(enc, checkpoint); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-24 21:51:24 +00:00
|
|
|
blockBkt := tx.Bucket(blocksBucket)
|
|
|
|
headBlkRoot := blockBkt.Get(headBlockRootKey)
|
2020-02-19 11:36:37 +00:00
|
|
|
bkt = tx.Bucket(stateBucket)
|
|
|
|
c := bkt.Cursor()
|
|
|
|
|
|
|
|
for blockRoot, _ := c.First(); blockRoot != nil; blockRoot, _ = c.Next() {
|
|
|
|
if rootMap[bytesutil.ToBytes32(blockRoot)] {
|
2020-03-16 19:07:07 +00:00
|
|
|
if featureconfig.Get().NewStateMgmt {
|
|
|
|
if tx.Bucket(stateSummaryBucket).Get(blockRoot[:]) == nil {
|
|
|
|
return errors.New("cannot delete state without state summary")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Safe guard against deleting genesis, finalized, head state.
|
|
|
|
if bytes.Equal(blockRoot[:], checkpoint.Root) || bytes.Equal(blockRoot[:], genesisBlockRoot) || bytes.Equal(blockRoot[:], headBlkRoot) {
|
|
|
|
return errors.New("cannot delete genesis, finalized, or head state")
|
|
|
|
}
|
2020-02-19 11:36:37 +00:00
|
|
|
}
|
2020-03-24 21:51:24 +00:00
|
|
|
|
|
|
|
slot, err := slotByBlockRoot(ctx, tx, blockRoot)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := k.clearStateSlotBitField(ctx, tx, slot); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-19 11:36:37 +00:00
|
|
|
if err := c.Delete(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-27 06:32:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2019-10-22 18:13:41 +00:00
|
|
|
}
|
|
|
|
|
2019-08-13 22:33:31 +00:00
|
|
|
// creates state from marshaled proto state bytes.
|
|
|
|
func createState(enc []byte) (*pb.BeaconState, error) {
|
|
|
|
protoState := &pb.BeaconState{}
|
2019-11-27 06:32:56 +00:00
|
|
|
err := decode(enc, protoState)
|
2019-08-13 22:33:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to unmarshal encoding")
|
|
|
|
}
|
|
|
|
return protoState, nil
|
2019-08-11 00:50:10 +00:00
|
|
|
}
|
2020-03-24 21:51:24 +00:00
|
|
|
|
|
|
|
// slotByBlockRoot retrieves the corresponding slot of the input block root.
|
|
|
|
func slotByBlockRoot(ctx context.Context, tx *bolt.Tx, blockRoot []byte) (uint64, error) {
|
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.slotByBlockRoot")
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
if featureconfig.Get().NewStateMgmt {
|
|
|
|
bkt := tx.Bucket(stateSummaryBucket)
|
|
|
|
enc := bkt.Get(blockRoot)
|
|
|
|
if enc == nil {
|
|
|
|
return 0, errors.New("state summary enc can't be nil")
|
|
|
|
}
|
|
|
|
stateSummary := &pb.StateSummary{}
|
|
|
|
if err := decode(enc, stateSummary); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return stateSummary.Slot, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
bkt := tx.Bucket(stateBucket)
|
|
|
|
enc := bkt.Get(blockRoot)
|
|
|
|
if enc == nil {
|
|
|
|
return 0, errors.New("state enc can't be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
s, err := createState(enc)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if s == nil {
|
|
|
|
return 0, errors.New("state can't be nil")
|
|
|
|
}
|
|
|
|
return s.Slot, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// HighestSlotStates returns the states with the highest slot from the db.
|
|
|
|
// Ideally there should just be one state per slot, but given validator
|
|
|
|
// can double propose, a single slot could have multiple block roots and
|
|
|
|
// reuslts states. This returns a list of states.
|
|
|
|
func (k *Store) HighestSlotStates(ctx context.Context) ([]*state.BeaconState, error) {
|
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.HighestSlotState")
|
|
|
|
defer span.End()
|
|
|
|
var states []*state.BeaconState
|
|
|
|
err := k.db.View(func(tx *bolt.Tx) error {
|
|
|
|
slotBkt := tx.Bucket(slotsHasObjectBucket)
|
|
|
|
savedSlots := slotBkt.Get(savedStateSlotsKey)
|
|
|
|
highestIndex, err := bytesutil.HighestBitIndex(savedSlots)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-03-26 15:37:40 +00:00
|
|
|
states, err = k.statesAtSlotBitfieldIndex(ctx, tx, highestIndex)
|
2020-03-24 21:51:24 +00:00
|
|
|
|
2020-03-26 15:37:40 +00:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-03-24 21:51:24 +00:00
|
|
|
|
2020-03-26 15:37:40 +00:00
|
|
|
if len(states) == 0 {
|
|
|
|
return nil, errors.New("could not get one state")
|
|
|
|
}
|
2020-03-24 21:51:24 +00:00
|
|
|
|
2020-03-26 15:37:40 +00:00
|
|
|
return states, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// HighestSlotStatesBelow returns the states with the highest slot below the input slot
|
|
|
|
// from the db. Ideally there should just be one state per slot, but given validator
|
|
|
|
// can double propose, a single slot could have multiple block roots and
|
|
|
|
// reuslts states. This returns a list of states.
|
|
|
|
func (k *Store) HighestSlotStatesBelow(ctx context.Context, slot uint64) ([]*state.BeaconState, error) {
|
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.HighestSlotStatesBelow")
|
|
|
|
defer span.End()
|
|
|
|
var states []*state.BeaconState
|
|
|
|
err := k.db.View(func(tx *bolt.Tx) error {
|
|
|
|
slotBkt := tx.Bucket(slotsHasObjectBucket)
|
|
|
|
savedSlots := slotBkt.Get(savedStateSlotsKey)
|
|
|
|
highestIndex, err := bytesutil.HighestBitIndexAt(savedSlots, int(slot))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-03-24 21:51:24 +00:00
|
|
|
}
|
2020-03-26 15:37:40 +00:00
|
|
|
states, err = k.statesAtSlotBitfieldIndex(ctx, tx, highestIndex)
|
2020-03-24 21:51:24 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(states) == 0 {
|
|
|
|
return nil, errors.New("could not get one state")
|
|
|
|
}
|
|
|
|
|
|
|
|
return states, nil
|
|
|
|
}
|
|
|
|
|
2020-03-26 15:37:40 +00:00
|
|
|
// statesAtSlotBitfieldIndex retrieves the states in DB given the input index. The index represents
|
|
|
|
// the position of the slot bitfield the saved state maps to.
|
|
|
|
func (k *Store) statesAtSlotBitfieldIndex(ctx context.Context, tx *bolt.Tx, index int) ([]*state.BeaconState, error) {
|
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.statesAtSlotBitfieldIndex")
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
highestSlot := index - 1
|
|
|
|
highestSlot = int(math.Max(0, float64(highestSlot)))
|
|
|
|
f := filters.NewFilter().SetStartSlot(uint64(highestSlot)).SetEndSlot(uint64(highestSlot))
|
|
|
|
|
|
|
|
keys, err := getBlockRootsByFilter(ctx, tx, f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(keys) == 0 {
|
|
|
|
return nil, errors.New("could not get one block root to get state")
|
|
|
|
}
|
|
|
|
|
|
|
|
stateBkt := tx.Bucket(stateBucket)
|
|
|
|
states := make([]*state.BeaconState, 0, len(keys))
|
|
|
|
for i := range keys {
|
|
|
|
enc := stateBkt.Get(keys[i][:])
|
|
|
|
if enc == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
pbState, err := createState(enc)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s, err := state.InitializeFromProtoUnsafe(pbState)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
states = append(states, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
return states, err
|
|
|
|
}
|
|
|
|
|
2020-03-24 21:51:24 +00:00
|
|
|
// setStateSlotBitField sets the state slot bit in DB.
|
|
|
|
// This helps to track which slot has a saved state in db.
|
|
|
|
func (k *Store) setStateSlotBitField(ctx context.Context, tx *bolt.Tx, slot uint64) error {
|
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.setStateSlotBitField")
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
k.stateSlotBitLock.Lock()
|
|
|
|
defer k.stateSlotBitLock.Unlock()
|
|
|
|
|
|
|
|
bucket := tx.Bucket(slotsHasObjectBucket)
|
|
|
|
slotBitfields := bucket.Get(savedStateSlotsKey)
|
|
|
|
|
|
|
|
// Copy is needed to avoid unsafe pointer conversions.
|
|
|
|
// See: https://github.com/etcd-io/bbolt/pull/201
|
|
|
|
tmp := make([]byte, len(slotBitfields))
|
|
|
|
copy(tmp, slotBitfields)
|
|
|
|
slotBitfields = bytesutil.SetBit(tmp, int(slot))
|
|
|
|
return bucket.Put(savedStateSlotsKey, slotBitfields)
|
|
|
|
}
|
|
|
|
|
|
|
|
// clearStateSlotBitField clears the state slot bit in DB.
|
|
|
|
// This helps to track which slot has a saved state in db.
|
|
|
|
func (k *Store) clearStateSlotBitField(ctx context.Context, tx *bolt.Tx, slot uint64) error {
|
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.clearStateSlotBitField")
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
k.stateSlotBitLock.Lock()
|
|
|
|
defer k.stateSlotBitLock.Unlock()
|
|
|
|
|
|
|
|
bucket := tx.Bucket(slotsHasObjectBucket)
|
|
|
|
slotBitfields := bucket.Get(savedStateSlotsKey)
|
|
|
|
|
|
|
|
// Copy is needed to avoid unsafe pointer conversions.
|
|
|
|
// See: https://github.com/etcd-io/bbolt/pull/201
|
|
|
|
tmp := make([]byte, len(slotBitfields))
|
|
|
|
copy(tmp, slotBitfields)
|
|
|
|
slotBitfields = bytesutil.ClearBit(tmp, int(slot))
|
|
|
|
return bucket.Put(savedStateSlotsKey, slotBitfields)
|
|
|
|
}
|