2020-03-02 04:27:56 +00:00
|
|
|
package kv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
2020-03-24 20:00:54 +00:00
|
|
|
bolt "go.etcd.io/bbolt"
|
2020-03-02 04:27:56 +00:00
|
|
|
"go.opencensus.io/trace"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SaveStateSummary saves a state summary object to the DB.
|
2020-10-12 08:11:05 +00:00
|
|
|
func (s *Store) SaveStateSummary(ctx context.Context, summary *pb.StateSummary) error {
|
2020-03-02 04:27:56 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.SaveStateSummary")
|
|
|
|
defer span.End()
|
|
|
|
|
2020-10-12 08:11:05 +00:00
|
|
|
return s.SaveStateSummaries(ctx, []*pb.StateSummary{summary})
|
2020-03-02 04:27:56 +00:00
|
|
|
}
|
|
|
|
|
2020-03-30 22:10:45 +00:00
|
|
|
// SaveStateSummaries saves state summary objects to the DB.
|
2020-10-12 08:11:05 +00:00
|
|
|
func (s *Store) SaveStateSummaries(ctx context.Context, summaries []*pb.StateSummary) error {
|
2020-03-30 22:10:45 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.SaveStateSummaries")
|
|
|
|
defer span.End()
|
|
|
|
|
2020-10-12 08:11:05 +00:00
|
|
|
return s.db.Update(func(tx *bolt.Tx) error {
|
2020-03-30 22:10:45 +00:00
|
|
|
bucket := tx.Bucket(stateSummaryBucket)
|
|
|
|
for _, summary := range summaries {
|
2020-07-01 04:00:49 +00:00
|
|
|
enc, err := encode(ctx, summary)
|
2020-03-30 22:10:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := bucket.Put(summary.Root, enc); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-02 04:27:56 +00:00
|
|
|
// StateSummary returns the state summary object from the db using input block root.
|
2020-10-12 08:11:05 +00:00
|
|
|
func (s *Store) StateSummary(ctx context.Context, blockRoot [32]byte) (*pb.StateSummary, error) {
|
2020-03-02 04:27:56 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.StateSummary")
|
|
|
|
defer span.End()
|
2020-10-12 08:11:05 +00:00
|
|
|
enc, err := s.stateSummaryBytes(ctx, blockRoot)
|
2020-07-07 16:29:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(enc) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
summary := &pb.StateSummary{}
|
|
|
|
if err := decode(ctx, enc, summary); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return summary, nil
|
2020-03-02 04:27:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HasStateSummary returns true if a state summary exists in DB.
|
2020-10-12 08:11:05 +00:00
|
|
|
func (s *Store) HasStateSummary(ctx context.Context, blockRoot [32]byte) bool {
|
2020-03-02 04:27:56 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.HasStateSummary")
|
|
|
|
defer span.End()
|
2020-10-12 08:11:05 +00:00
|
|
|
enc, err := s.stateSummaryBytes(ctx, blockRoot)
|
2020-07-07 16:29:25 +00:00
|
|
|
if err != nil {
|
2020-04-13 04:11:09 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
2020-07-07 16:29:25 +00:00
|
|
|
return len(enc) > 0
|
|
|
|
}
|
|
|
|
|
2020-10-12 08:11:05 +00:00
|
|
|
func (s *Store) stateSummaryBytes(ctx context.Context, blockRoot [32]byte) ([]byte, error) {
|
2020-07-07 16:29:25 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.stateSummaryBytes")
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
var enc []byte
|
2020-10-12 08:11:05 +00:00
|
|
|
err := s.db.View(func(tx *bolt.Tx) error {
|
2020-07-07 16:29:25 +00:00
|
|
|
bucket := tx.Bucket(stateSummaryBucket)
|
|
|
|
enc = bucket.Get(blockRoot[:])
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return enc, err
|
2020-03-02 04:27:56 +00:00
|
|
|
}
|