mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-23 03:51:29 +00:00
a04b7c2e4f
* WIP - slasher highest attestation start * fixed previous * highest source and target * highest attestation cache * cleanup * persist + fixes * PR fixes and cleanup * slashing proto * highest att. api * cleanup + tests * increased highest att. cache to 300K * removed highest att. api (for a separate PR) * fixed linting * bazel build fix * highest att. kv test * slasher highest att. test + purge + fix on eviction persist performance * cleanup + linting * linting + test fixes * bazel gazelle run * PR fixes * run goimports * go mod tidy * ineffectual assignment fix * run gazelle * bazel gazelle run * test fixes * linter fix * Apply suggestions from code review Co-authored-by: Shay Zluf <thezluf@gmail.com> * goimports run * cache tests * A bunch of small fixes * gazelle fix + gofmt * merge fixes * kv ordering fix * small typos and text fixes * capital letter fix Co-authored-by: Shay Zluf <thezluf@gmail.com>
74 lines
2.2 KiB
Go
74 lines
2.2 KiB
Go
package cache
|
|
|
|
import (
|
|
lru "github.com/hashicorp/golang-lru"
|
|
slashpb "github.com/prysmaticlabs/prysm/proto/slashing"
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
var (
|
|
// highestAttCacheSize defines the max number of sets of highest attestation in cache.
|
|
highestAttCacheSize = 3000
|
|
)
|
|
|
|
// HighestAttestationCache is used to store per validator id highest attestation in cache.
|
|
type HighestAttestationCache struct {
|
|
cache *lru.Cache
|
|
}
|
|
|
|
// NewHighestAttestationCache initializes the cache.
|
|
func NewHighestAttestationCache(size int, onEvicted func(key interface{}, value interface{})) (*HighestAttestationCache, error) {
|
|
if size != 0 {
|
|
highestAttCacheSize = size
|
|
}
|
|
cache, err := lru.NewWithEvict(highestAttCacheSize, onEvicted)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &HighestAttestationCache{cache: cache}, nil
|
|
}
|
|
|
|
// Get returns an ok bool and the cached value for the requested validator id key, if any.
|
|
func (c *HighestAttestationCache) Get(setKey uint64) (map[uint64]*slashpb.HighestAttestation, bool) {
|
|
item, exists := c.cache.Get(setKey)
|
|
if exists && item != nil {
|
|
return item.(map[uint64]*slashpb.HighestAttestation), true
|
|
}
|
|
return nil, false
|
|
}
|
|
|
|
// Set the response in the cache.
|
|
func (c *HighestAttestationCache) Set(setKey uint64, highest *slashpb.HighestAttestation) {
|
|
set, ok := c.Get(setKey)
|
|
if ok {
|
|
set[highest.ValidatorId] = highest
|
|
} else {
|
|
set = map[uint64]*slashpb.HighestAttestation{
|
|
highest.ValidatorId: highest,
|
|
}
|
|
c.cache.Add(setKey, set)
|
|
}
|
|
}
|
|
|
|
// Delete removes a validator id from the cache and returns if it existed or not.
|
|
// Performs the onEviction function before removal.
|
|
func (c *HighestAttestationCache) Delete(setKey uint64) bool {
|
|
return c.cache.Remove(setKey)
|
|
}
|
|
|
|
// Has returns true if the key exists in the cache.
|
|
func (c *HighestAttestationCache) Has(setKey uint64) bool {
|
|
return c.cache.Contains(setKey)
|
|
}
|
|
|
|
// Clear removes all keys from the ValidatorCache.
|
|
func (c *HighestAttestationCache) Clear() {
|
|
c.cache.Purge()
|
|
}
|
|
|
|
// Purge removes all keys from cache and evicts all current data.
|
|
func (c *HighestAttestationCache) Purge() {
|
|
log.Info("Saving all highest attestation cache data to DB, please wait for completion.")
|
|
c.cache.Purge()
|
|
}
|