2019-08-11 00:50:10 +00:00
|
|
|
package kv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2019-08-13 22:33:31 +00:00
|
|
|
"github.com/boltdb/bolt"
|
|
|
|
"github.com/gogo/protobuf/proto"
|
|
|
|
"github.com/pkg/errors"
|
2019-08-11 00:50:10 +00:00
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
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.
|
|
|
|
func (k *Store) State(ctx context.Context, blockRoot [32]byte) (*pb.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
|
|
|
|
})
|
|
|
|
return s, err
|
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.
|
2019-08-11 00:50:10 +00:00
|
|
|
func (k *Store) HeadState(ctx context.Context) (*pb.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
|
|
|
|
})
|
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)))
|
|
|
|
}
|
2019-08-13 22:33:31 +00:00
|
|
|
return s, err
|
2019-08-11 00:50:10 +00:00
|
|
|
}
|
|
|
|
|
2019-08-29 22:32:35 +00:00
|
|
|
// GenesisState returns the genesis state in beacon chain.
|
|
|
|
func (k *Store) GenesisState(ctx context.Context) (*pb.BeaconState, error) {
|
|
|
|
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
|
|
|
|
})
|
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
|
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.
|
2019-08-11 00:50:10 +00:00
|
|
|
func (k *Store) SaveState(ctx context.Context, state *pb.BeaconState, blockRoot [32]byte) error {
|
2019-08-20 19:35:48 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.SaveState")
|
|
|
|
defer span.End()
|
2019-08-13 22:33:31 +00:00
|
|
|
enc, err := proto.Marshal(state)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return k.db.Update(func(tx *bolt.Tx) error {
|
|
|
|
bucket := tx.Bucket(stateBucket)
|
|
|
|
return bucket.Put(blockRoot[:], enc)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// creates state from marshaled proto state bytes.
|
|
|
|
func createState(enc []byte) (*pb.BeaconState, error) {
|
|
|
|
protoState := &pb.BeaconState{}
|
|
|
|
err := proto.Unmarshal(enc, protoState)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to unmarshal encoding")
|
|
|
|
}
|
|
|
|
return protoState, nil
|
2019-08-11 00:50:10 +00:00
|
|
|
}
|