2020-03-02 07:55:38 +00:00
|
|
|
package kv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-03-31 23:54:24 +00:00
|
|
|
"encoding/binary"
|
2020-03-02 07:55:38 +00:00
|
|
|
|
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SaveArchivedPointRoot saves an archived point root to the DB. This is used for cold state management.
|
|
|
|
func (k *Store) SaveArchivedPointRoot(ctx context.Context, blockRoot [32]byte, index uint64) error {
|
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.SaveArchivedPointRoot")
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
return k.db.Update(func(tx *bolt.Tx) error {
|
|
|
|
bucket := tx.Bucket(archivedIndexRootBucket)
|
2020-04-11 19:47:22 +00:00
|
|
|
return bucket.Put(bytesutil.Uint64ToBytes(index), blockRoot[:])
|
2020-03-02 07:55:38 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-16 19:07:07 +00:00
|
|
|
// SaveLastArchivedIndex to the db.
|
|
|
|
func (k *Store) SaveLastArchivedIndex(ctx context.Context, index uint64) error {
|
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.SaveHeadBlockRoot")
|
|
|
|
defer span.End()
|
|
|
|
return k.db.Update(func(tx *bolt.Tx) error {
|
|
|
|
bucket := tx.Bucket(archivedIndexRootBucket)
|
2020-04-11 19:47:22 +00:00
|
|
|
return bucket.Put(lastArchivedIndexKey, bytesutil.Uint64ToBytes(index))
|
2020-03-16 19:07:07 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-31 23:54:24 +00:00
|
|
|
// LastArchivedIndex from the db.
|
|
|
|
func (k *Store) LastArchivedIndex(ctx context.Context) (uint64, error) {
|
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.LastArchivedIndex")
|
|
|
|
defer span.End()
|
|
|
|
var index uint64
|
|
|
|
err := k.db.Update(func(tx *bolt.Tx) error {
|
|
|
|
bucket := tx.Bucket(archivedIndexRootBucket)
|
|
|
|
b := bucket.Get(lastArchivedIndexKey)
|
2020-04-11 20:54:19 +00:00
|
|
|
if b == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2020-03-31 23:54:24 +00:00
|
|
|
index = binary.LittleEndian.Uint64(b)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return index, err
|
|
|
|
}
|
|
|
|
|
2020-03-16 19:07:07 +00:00
|
|
|
// LastArchivedIndexRoot from the db.
|
|
|
|
func (k *Store) LastArchivedIndexRoot(ctx context.Context) [32]byte {
|
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.LastArchivedIndexRoot")
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
var blockRoot []byte
|
2020-04-13 04:11:09 +00:00
|
|
|
if err := k.db.View(func(tx *bolt.Tx) error {
|
2020-03-16 19:07:07 +00:00
|
|
|
bucket := tx.Bucket(archivedIndexRootBucket)
|
|
|
|
lastArchivedIndex := bucket.Get(lastArchivedIndexKey)
|
|
|
|
if lastArchivedIndex == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
blockRoot = bucket.Get(lastArchivedIndex)
|
|
|
|
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.
|
|
|
|
func (k *Store) ArchivedPointRoot(ctx context.Context, index uint64) [32]byte {
|
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.ArchivePointRoot")
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
var blockRoot []byte
|
2020-04-13 04:11:09 +00:00
|
|
|
if err := k.db.View(func(tx *bolt.Tx) error {
|
2020-03-02 07:55:38 +00:00
|
|
|
bucket := tx.Bucket(archivedIndexRootBucket)
|
2020-04-11 19:47:22 +00:00
|
|
|
blockRoot = bucket.Get(bytesutil.Uint64ToBytes(index))
|
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.
|
|
|
|
func (k *Store) HasArchivedPoint(ctx context.Context, index uint64) bool {
|
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.HasArchivedPoint")
|
|
|
|
defer span.End()
|
|
|
|
var exists bool
|
2020-04-13 04:11:09 +00:00
|
|
|
if err := k.db.View(func(tx *bolt.Tx) error {
|
2020-03-02 07:55:38 +00:00
|
|
|
iBucket := tx.Bucket(archivedIndexRootBucket)
|
2020-04-11 19:47:22 +00:00
|
|
|
exists = iBucket.Get(bytesutil.Uint64ToBytes(index)) != 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
|
|
|
|
}
|