2020-03-02 07:55:38 +00:00
|
|
|
package kv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/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.
|
2020-10-12 08:11:05 +00:00
|
|
|
func (s *Store) LastArchivedSlot(ctx context.Context) (uint64, error) {
|
2020-07-18 18:05:04 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.LastArchivedSlot")
|
2020-03-31 23:54:24 +00:00
|
|
|
defer span.End()
|
|
|
|
var index uint64
|
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()
|
|
|
|
index = bytesutil.BytesToUint64BigEndian(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 {
|
2020-07-18 18:05:04 +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.
|
2020-10-12 08:11:05 +00:00
|
|
|
func (s *Store) ArchivedPointRoot(ctx context.Context, slot uint64) [32]byte {
|
2020-06-23 02:22:22 +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)
|
|
|
|
blockRoot = bucket.Get(bytesutil.Uint64ToBytesBigEndian(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.
|
2020-10-12 08:11:05 +00:00
|
|
|
func (s *Store) HasArchivedPoint(ctx context.Context, slot uint64) bool {
|
2020-03-02 07:55:38 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.HasArchivedPoint")
|
|
|
|
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)
|
|
|
|
exists = iBucket.Get(bytesutil.Uint64ToBytesBigEndian(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
|
|
|
|
}
|