prysm-pulse/slasher/db/kv/spanner_new.go
Ivan Martinez ec6309a928
Cleanup flat spans implementation (#6150)
* Flat spanner improvements

* Fix tests, add more to ES type

* Move types to detection/types

* Fix

* Fix comments

* Fixes

* Use SlotTickerWithOffset for StreamIndexedAttestations (#5999)

* Change streamindexed to slot ticker with offset
* Make 2/3rds
* Add test
* Merge branch 'master' of github.com:prysmaticlabs/prysm into ticker-offset
* Add check for offset
* Merge branch 'master' of github.com:prysmaticlabs/prysm into ticker-offset
* Fix test
* Merge refs/heads/master into ticker-offset

* Fix long running E2E after v0.12 changes (#6008)

* Fix deposits in long run e2e

* Fix participation

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>

* Small fixes for slasher  (#6021)

* Small fixes for slasher

* Remove useless log

* Fix test

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>

* Add voluntary exit processing to E2E (#6016)

* Add voluntary exit to E2E

* Fix long urnning e2e

* Fix for comments

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>

* Change slashing pool to use spec code  (#6030)

* Change slashing pool to use more spec code

* Fix test

* Undo unneeded changes

* Make sure to catch regression

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>

* Fix long-running E2E (#6047)

* Fixes for long running E2E
* Merge branch 'master' of github.com:prysmaticlabs/prysm into fix-e2e
* Move metrics check up
* Merge refs/heads/master into fix-e2e

* E2E Improvements (#6091)

* Some fixes
* Merge branch 'master' into e2e-fixes
* Add another small delay
* Merge branch 'e2e-fixes' of github.com:prysmaticlabs/prysm into e2e-fixes
* Remove genesis test, make normal e2e run longer
* Gaz
* more fixes
* Merge branch 'master' into e2e-fixes
* Merge refs/heads/master into e2e-fixes
* Fix comment
* Merge refs/heads/master into e2e-fixes

* Begin changing tests

* Start work on tests

* Redo tests for EpochStore

* Add test for highest index

* Fixes

* Gaz

* Fix

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
2020-06-11 14:17:44 -04:00

89 lines
2.8 KiB
Go

package kv
import (
"context"
"errors"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/slasher/detection/attestations/types"
log "github.com/sirupsen/logrus"
bolt "go.etcd.io/bbolt"
"go.opencensus.io/trace"
)
// This function defines a function which triggers upon a span map being
// evicted from the cache. It allows us to persist the span map by the epoch value
// to the database itself in the validatorsMinMaxSpanBucket.
func persistFlatSpanMapsOnEviction(db *Store) func(key interface{}, value interface{}) {
// We use a closure here so we can access the database itself
// on the eviction of a span map from the cache. The function has the signature
// required by the ristretto cache OnEvict method.
// See https://godoc.org/github.com/dgraph-io/ristretto#Config.
return func(key interface{}, value interface{}) {
log.Tracef("Evicting flat span map for epoch: %d", key)
err := db.update(func(tx *bolt.Tx) error {
epoch, keyOK := key.(uint64)
epochStore, valueOK := value.(*types.EpochStore)
if !keyOK || !valueOK {
return errors.New("could not cast key and value into needed types")
}
bucket := tx.Bucket(validatorsMinMaxSpanBucketNew)
if err := bucket.Put(bytesutil.Bytes8(epoch), epochStore.Bytes()); err != nil {
return err
}
epochSpansCacheEvictions.Inc()
return nil
})
if err != nil {
log.Errorf("Failed to save span map to db on cache eviction: %v", err)
}
}
}
// 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) (*types.EpochStore, error) {
ctx, span := trace.StartSpan(ctx, "slasherDB.EpochSpans")
defer span.End()
var copiedSpans []byte
err := db.view(func(tx *bolt.Tx) error {
b := tx.Bucket(validatorsMinMaxSpanBucketNew)
if b == nil {
return nil
}
spans := b.Get(bytesutil.Bytes8(epoch))
copiedSpans = make([]byte, len(spans))
copy(copiedSpans, spans)
return nil
})
if err != nil {
return &types.EpochStore{}, err
}
if copiedSpans == nil {
copiedSpans = []byte{}
}
return types.NewEpochStore(copiedSpans)
}
// SaveEpochSpans accepts a epoch and span byte array and writes it to disk.
func (db *Store) SaveEpochSpans(ctx context.Context, epoch uint64, es *types.EpochStore) error {
ctx, span := trace.StartSpan(ctx, "slasherDB.SaveEpochSpans")
defer span.End()
if len(es.Bytes())%int(types.SpannerEncodedLength) != 0 {
return types.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.Bytes())
})
}