-
Notifications
You must be signed in to change notification settings - Fork 919
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* flat span cache * Plug cache into kv * Add comments * Fix * Add doc.go * Gaz Co-authored-by: Ivan Martinez <ivanthegreatdev@gmail.com> Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
- Loading branch information
1 parent
c29cccf
commit 4c66edf
Showing
6 changed files
with
79 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Package cache contains critical caches necessary for the runtime | ||
// of the slasher service, such as a cache for maintaining validator history on a per epoch basis | ||
package cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package cache | ||
|
||
import ( | ||
lru "github.com/hashicorp/golang-lru" | ||
"github.com/prysmaticlabs/prysm/slasher/detection/attestations/types" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
// 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 | ||
} | ||
cache, err := lru.NewWithEvict(epochSpansCacheSize, onEvicted) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &EpochFlatSpansCache{cache: cache}, nil | ||
} | ||
|
||
// Get returns an ok bool and the cached value for the requested epoch key, if any. | ||
func (c *EpochFlatSpansCache) Get(epoch uint64) (*types.EpochStore, bool) { | ||
item, exists := c.cache.Get(epoch) | ||
if exists && item != nil { | ||
epochSpansCacheHit.Inc() | ||
return item.(*types.EpochStore), true | ||
} | ||
|
||
epochSpansCacheMiss.Inc() | ||
return &types.EpochStore{}, false | ||
} | ||
|
||
// Set the response in the cache. | ||
func (c *EpochFlatSpansCache) Set(epoch uint64, epochSpans *types.EpochStore) { | ||
_ = 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. | ||
func (c *EpochFlatSpansCache) Delete(epoch uint64) bool { | ||
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. | ||
func (c *EpochFlatSpansCache) Has(epoch uint64) bool { | ||
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters