prysm-pulse/slasher/db/kv/spanner_new.go

51 lines
1.3 KiB
Go
Raw Normal View History

package kv
import (
"context"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
bolt "go.etcd.io/bbolt"
"go.opencensus.io/trace"
)
// EpochSpans accepts epoch and returns the corresponding spans byte array
// for slashing detection.
// Returns span byte array, and error in case of db error.
// returns empty byte array if no entry for this epoch exists in db.
func (db *Store) EpochSpans(ctx context.Context, epoch uint64) (EpochStore, error) {
ctx, span := trace.StartSpan(ctx, "slasherDB.EpochSpans")
defer span.End()
var err error
var spans []byte
err = db.view(func(tx *bolt.Tx) error {
b := tx.Bucket(validatorsMinMaxSpanBucketNew)
if b == nil {
return nil
}
spans = b.Get(bytesutil.Bytes8(epoch))
return nil
})
if spans == nil {
spans = []byte{}
}
return spans, err
}
// SaveEpochSpans accepts a epoch and span byte array and writes it to disk.
func (db *Store) SaveEpochSpans(ctx context.Context, epoch uint64, es EpochStore) error {
ctx, span := trace.StartSpan(ctx, "slasherDB.SaveEpochSpans")
defer span.End()
if len(es)%spannerEncodedLength != 0 {
return ErrWrongSize
}
return db.update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucketIfNotExists(validatorsMinMaxSpanBucketNew)
if err != nil {
return err
}
return b.Put(bytesutil.Bytes8(epoch), es)
})
}