prysm-pulse/slasher/db/kv/spanner_new.go
Shay Zluf fd19fd10a9
New spanner db structure (#6061)
* new spanner db structure

* lint fixes

* go mod fix

* fix iface

* remove unused

* remove extra line

* change from db

* exported field

* exported field

* revert to original

* fix

* ivan feedback

* ivan feedback

* ivan feedback

* revert mod changes

* fix db impl

* gaz

* import fix

* Try to fix tests

* ivan feedback

* new epoch store

* added comment

* fix error

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
Co-authored-by: Ivan Martinez <ivanthegreatdev@gmail.com>
2020-06-02 10:41:21 -04:00

51 lines
1.3 KiB
Go

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)
})
}