2020-03-02 04:27:56 +00:00
|
|
|
package kv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2023-03-17 18:52:56 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v4/encoding/bytesutil"
|
|
|
|
ethpb "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1"
|
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.
|
2021-07-29 21:45:17 +00:00
|
|
|
func (s *Store) SaveStateSummary(ctx context.Context, summary *ethpb.StateSummary) error {
|
2020-03-02 04:27:56 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.SaveStateSummary")
|
|
|
|
defer span.End()
|
|
|
|
|
2021-07-29 21:45:17 +00:00
|
|
|
return s.SaveStateSummaries(ctx, []*ethpb.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.
|
2021-07-29 21:45:17 +00:00
|
|
|
func (s *Store) SaveStateSummaries(ctx context.Context, summaries []*ethpb.StateSummary) error {
|
2020-03-30 22:10:45 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.SaveStateSummaries")
|
|
|
|
defer span.End()
|
|
|
|
|
2020-12-16 16:56:21 +00:00
|
|
|
// When we reach the state summary cache prune count,
|
|
|
|
// dump the cached state summaries to the DB.
|
|
|
|
if s.stateSummaryCache.len() >= stateSummaryCachePruneCount {
|
|
|
|
if err := s.saveCachedStateSummariesDB(ctx); err != nil {
|
|
|
|
return err
|
2020-03-30 22:10:45 +00:00
|
|
|
}
|
2020-12-16 16:56:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, ss := range summaries {
|
|
|
|
s.stateSummaryCache.put(bytesutil.ToBytes32(ss.Root), ss)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-03-30 22:10:45 +00:00
|
|
|
}
|
|
|
|
|
2020-03-02 04:27:56 +00:00
|
|
|
// StateSummary returns the state summary object from the db using input block root.
|
2021-07-29 21:45:17 +00:00
|
|
|
func (s *Store) StateSummary(ctx context.Context, blockRoot [32]byte) (*ethpb.StateSummary, error) {
|
2020-03-02 04:27:56 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.StateSummary")
|
|
|
|
defer span.End()
|
2020-12-16 16:56:21 +00:00
|
|
|
|
|
|
|
if s.stateSummaryCache.has(blockRoot) {
|
|
|
|
return s.stateSummaryCache.get(blockRoot), nil
|
|
|
|
}
|
2021-08-19 21:42:30 +00:00
|
|
|
var enc []byte
|
|
|
|
if err := s.db.View(func(tx *bolt.Tx) error {
|
|
|
|
enc = tx.Bucket(stateSummaryBucket).Get(blockRoot[:])
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
2020-07-07 16:29:25 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(enc) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2021-07-29 21:45:17 +00:00
|
|
|
summary := ðpb.StateSummary{}
|
2020-07-07 16:29:25 +00:00
|
|
|
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 {
|
2022-07-14 17:00:33 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.HasStateSummary")
|
2020-10-28 06:07:14 +00:00
|
|
|
defer span.End()
|
2020-12-16 16:56:21 +00:00
|
|
|
|
2022-12-08 19:32:04 +00:00
|
|
|
if s.stateSummaryCache.has(blockRoot) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-08-19 21:42:30 +00:00
|
|
|
var hasSummary bool
|
|
|
|
if err := s.db.View(func(tx *bolt.Tx) error {
|
2022-12-08 19:32:04 +00:00
|
|
|
enc := tx.Bucket(stateSummaryBucket).Get(blockRoot[:])
|
|
|
|
hasSummary = len(enc) > 0
|
2021-08-19 21:42:30 +00:00
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return false
|
2020-12-16 16:56:21 +00:00
|
|
|
}
|
2021-08-19 21:42:30 +00:00
|
|
|
return hasSummary
|
|
|
|
}
|
2020-12-16 16:56:21 +00:00
|
|
|
|
|
|
|
// This saves all cached state summary objects to DB, and clears up the cache.
|
|
|
|
func (s *Store) saveCachedStateSummariesDB(ctx context.Context) error {
|
|
|
|
summaries := s.stateSummaryCache.getAll()
|
|
|
|
encs := make([][]byte, len(summaries))
|
|
|
|
for i, s := range summaries {
|
|
|
|
enc, err := encode(ctx, s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
encs[i] = enc
|
|
|
|
}
|
|
|
|
if err := s.db.Update(func(tx *bolt.Tx) error {
|
|
|
|
bucket := tx.Bucket(stateSummaryBucket)
|
|
|
|
for i, s := range summaries {
|
|
|
|
if err := bucket.Put(s.Root, encs[i]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.stateSummaryCache.clear()
|
|
|
|
return nil
|
|
|
|
}
|
2022-04-06 21:24:00 +00:00
|
|
|
|
|
|
|
// deleteStateSummary deletes a state summary object from the db using input block root.
|
|
|
|
func (s *Store) deleteStateSummary(blockRoot [32]byte) error {
|
|
|
|
s.stateSummaryCache.delete(blockRoot)
|
|
|
|
return s.db.Update(func(tx *bolt.Tx) error {
|
|
|
|
bucket := tx.Bucket(stateSummaryBucket)
|
|
|
|
return bucket.Delete(blockRoot[:])
|
|
|
|
})
|
|
|
|
}
|