2020-03-02 07:55:38 +00:00
|
|
|
package kv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-08-16 12:20:13 +00:00
|
|
|
types "github.com/prysmaticlabs/prysm/v3/consensus-types/primitives"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/encoding/bytesutil"
|
2020-03-24 20:00:54 +00:00
|
|
|
bolt "go.etcd.io/bbolt"
|
2020-03-02 07:55:38 +00:00
|
|
|
"go.opencensus.io/trace"
|
|
|
|
)
|
|
|
|
|
2020-07-18 18:05:04 +00:00
|
|
|
// LastArchivedSlot from the db.
|
2021-02-16 07:45:34 +00:00
|
|
|
func (s *Store) LastArchivedSlot(ctx context.Context) (types.Slot, error) {
|
2022-07-14 17:00:33 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.LastArchivedSlot")
|
2020-03-31 23:54:24 +00:00
|
|
|
defer span.End()
|
2021-02-16 07:45:34 +00:00
|
|
|
var index types.Slot
|
2020-10-12 08:11:05 +00:00
|
|
|
err := s.db.View(func(tx *bolt.Tx) error {
|
2020-07-18 18:05:04 +00:00
|
|
|
bkt := tx.Bucket(stateSlotIndicesBucket)
|
|
|
|
b, _ := bkt.Cursor().Last()
|
2021-02-16 07:45:34 +00:00
|
|
|
index = bytesutil.BytesToSlotBigEndian(b)
|
2020-03-31 23:54:24 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return index, err
|
|
|
|
}
|
|
|
|
|
2020-07-18 18:05:04 +00:00
|
|
|
// LastArchivedRoot from the db.
|
2020-10-12 08:11:05 +00:00
|
|
|
func (s *Store) LastArchivedRoot(ctx context.Context) [32]byte {
|
2022-07-14 17:00:33 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.LastArchivedRoot")
|
2020-03-16 19:07:07 +00:00
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
var blockRoot []byte
|
2020-10-12 08:11:05 +00:00
|
|
|
if err := s.db.View(func(tx *bolt.Tx) error {
|
2020-07-18 18:05:04 +00:00
|
|
|
bkt := tx.Bucket(stateSlotIndicesBucket)
|
|
|
|
_, blockRoot = bkt.Cursor().Last()
|
2020-03-16 19:07:07 +00:00
|
|
|
return nil
|
2020-04-13 04:11:09 +00:00
|
|
|
}); err != nil { // This view never returns an error, but we'll handle anyway for sanity.
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-03-16 19:07:07 +00:00
|
|
|
|
|
|
|
return bytesutil.ToBytes32(blockRoot)
|
|
|
|
}
|
|
|
|
|
2020-03-02 07:55:38 +00:00
|
|
|
// ArchivedPointRoot returns the block root of an archived point from the DB.
|
|
|
|
// This is essential for cold state management and to restore a cold state.
|
2021-02-16 07:45:34 +00:00
|
|
|
func (s *Store) ArchivedPointRoot(ctx context.Context, slot types.Slot) [32]byte {
|
2022-07-14 17:00:33 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.ArchivedPointRoot")
|
2020-03-02 07:55:38 +00:00
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
var blockRoot []byte
|
2020-10-12 08:11:05 +00:00
|
|
|
if err := s.db.View(func(tx *bolt.Tx) error {
|
2020-07-18 18:05:04 +00:00
|
|
|
bucket := tx.Bucket(stateSlotIndicesBucket)
|
2021-02-16 07:45:34 +00:00
|
|
|
blockRoot = bucket.Get(bytesutil.SlotToBytesBigEndian(slot))
|
2020-03-02 07:55:38 +00:00
|
|
|
return nil
|
2020-04-13 04:11:09 +00:00
|
|
|
}); err != nil { // This view never returns an error, but we'll handle anyway for sanity.
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-03-02 07:55:38 +00:00
|
|
|
|
|
|
|
return bytesutil.ToBytes32(blockRoot)
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasArchivedPoint returns true if an archived point exists in DB.
|
2021-02-16 07:45:34 +00:00
|
|
|
func (s *Store) HasArchivedPoint(ctx context.Context, slot types.Slot) bool {
|
2022-07-14 17:00:33 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.HasArchivedPoint")
|
2020-03-02 07:55:38 +00:00
|
|
|
defer span.End()
|
|
|
|
var exists bool
|
2020-10-12 08:11:05 +00:00
|
|
|
if err := s.db.View(func(tx *bolt.Tx) error {
|
2020-07-18 18:05:04 +00:00
|
|
|
iBucket := tx.Bucket(stateSlotIndicesBucket)
|
2021-02-16 07:45:34 +00:00
|
|
|
exists = iBucket.Get(bytesutil.SlotToBytesBigEndian(slot)) != nil
|
2020-03-02 07:55:38 +00:00
|
|
|
return nil
|
2020-04-13 04:11:09 +00:00
|
|
|
}); err != nil { // This view never returns an error, but we'll handle anyway for sanity.
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-03-02 07:55:38 +00:00
|
|
|
return exists
|
|
|
|
}
|