2020-06-14 02:51:54 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
lru "github.com/hashicorp/golang-lru"
|
2021-02-22 23:20:57 +00:00
|
|
|
types "github.com/prysmaticlabs/eth2-types"
|
2021-09-02 10:36:54 +00:00
|
|
|
lruwrpr "github.com/prysmaticlabs/prysm/shared/lru"
|
2021-02-09 10:05:22 +00:00
|
|
|
slashertypes "github.com/prysmaticlabs/prysm/slasher/detection/attestations/types"
|
2020-06-14 02:51:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// EpochFlatSpansCache is used to store the spans needed on a per-epoch basis for slashing detection.
|
|
|
|
type EpochFlatSpansCache struct {
|
|
|
|
cache *lru.Cache
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewEpochFlatSpansCache initializes the underlying cache with the given size and on evict function.
|
|
|
|
func NewEpochFlatSpansCache(size int, onEvicted func(key interface{}, value interface{})) (*EpochFlatSpansCache, error) {
|
|
|
|
if size != 0 {
|
|
|
|
epochSpansCacheSize = size
|
|
|
|
}
|
2021-09-02 10:36:54 +00:00
|
|
|
return &EpochFlatSpansCache{cache: lruwrpr.NewWithEvict(epochSpansCacheSize, onEvicted)}, nil
|
2020-06-14 02:51:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns an ok bool and the cached value for the requested epoch key, if any.
|
2021-02-09 10:05:22 +00:00
|
|
|
func (c *EpochFlatSpansCache) Get(epoch types.Epoch) (*slashertypes.EpochStore, bool) {
|
2020-06-14 02:51:54 +00:00
|
|
|
item, exists := c.cache.Get(epoch)
|
|
|
|
if exists && item != nil {
|
|
|
|
epochSpansCacheHit.Inc()
|
2021-02-09 10:05:22 +00:00
|
|
|
return item.(*slashertypes.EpochStore), true
|
2020-06-14 02:51:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
epochSpansCacheMiss.Inc()
|
2021-02-09 10:05:22 +00:00
|
|
|
return &slashertypes.EpochStore{}, false
|
2020-06-14 02:51:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the response in the cache.
|
2021-02-09 10:05:22 +00:00
|
|
|
func (c *EpochFlatSpansCache) Set(epoch types.Epoch, epochSpans *slashertypes.EpochStore) {
|
2020-06-14 02:51:54 +00:00
|
|
|
_ = c.cache.Add(epoch, epochSpans)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete removes an epoch from the cache and returns if it existed or not.
|
|
|
|
// Performs the onEviction function before removal.
|
2021-02-09 10:05:22 +00:00
|
|
|
func (c *EpochFlatSpansCache) Delete(epoch types.Epoch) bool {
|
2020-06-14 02:51:54 +00:00
|
|
|
return c.cache.Remove(epoch)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PruneOldest removes the oldest key from the span cache, calling its OnEvict function.
|
|
|
|
func (c *EpochFlatSpansCache) PruneOldest() uint64 {
|
|
|
|
if c.cache.Len() == epochSpansCacheSize {
|
|
|
|
epoch, _, _ := c.cache.RemoveOldest()
|
|
|
|
return epoch.(uint64)
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Has returns true if the key exists in the cache.
|
2021-02-09 10:05:22 +00:00
|
|
|
func (c *EpochFlatSpansCache) Has(epoch types.Epoch) bool {
|
2020-06-14 02:51:54 +00:00
|
|
|
return c.cache.Contains(epoch)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Purge removes all keys from the SpanCache and evicts all current data.
|
|
|
|
func (c *EpochFlatSpansCache) Purge() {
|
|
|
|
log.Info("Saving all cached data to DB, please wait for completion.")
|
|
|
|
c.cache.Purge()
|
|
|
|
}
|
2020-07-07 02:57:40 +00:00
|
|
|
|
|
|
|
// Length returns the number of cached items.
|
|
|
|
func (c *EpochFlatSpansCache) Length() int {
|
|
|
|
return c.cache.Len()
|
|
|
|
}
|