2020-03-04 22:09:21 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
2020-06-23 22:06:47 +00:00
|
|
|
"sync"
|
|
|
|
|
2020-03-04 22:09:21 +00:00
|
|
|
lru "github.com/hashicorp/golang-lru"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
|
|
|
stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// hotStateCacheSize defines the max number of hot state this can cache.
|
2020-09-24 18:10:02 +00:00
|
|
|
hotStateCacheSize = 32
|
2020-03-04 22:09:21 +00:00
|
|
|
// Metrics
|
|
|
|
hotStateCacheHit = promauto.NewCounter(prometheus.CounterOpts{
|
|
|
|
Name: "hot_state_cache_hit",
|
|
|
|
Help: "The total number of cache hits on the hot state cache.",
|
|
|
|
})
|
|
|
|
hotStateCacheMiss = promauto.NewCounter(prometheus.CounterOpts{
|
|
|
|
Name: "hot_state_cache_miss",
|
|
|
|
Help: "The total number of cache misses on the hot state cache.",
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
// HotStateCache is used to store the processed beacon state after finalized check point..
|
|
|
|
type HotStateCache struct {
|
|
|
|
cache *lru.Cache
|
2020-06-23 22:06:47 +00:00
|
|
|
lock sync.RWMutex
|
2020-03-04 22:09:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewHotStateCache initializes the map and underlying cache.
|
|
|
|
func NewHotStateCache() *HotStateCache {
|
|
|
|
cache, err := lru.New(hotStateCacheSize)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return &HotStateCache{
|
|
|
|
cache: cache,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns a cached response via input block root, if any.
|
|
|
|
// The response is copied by default.
|
|
|
|
func (c *HotStateCache) Get(root [32]byte) *stateTrie.BeaconState {
|
2020-06-23 22:06:47 +00:00
|
|
|
c.lock.RLock()
|
|
|
|
defer c.lock.RUnlock()
|
2020-03-04 22:09:21 +00:00
|
|
|
item, exists := c.cache.Get(root)
|
|
|
|
|
|
|
|
if exists && item != nil {
|
|
|
|
hotStateCacheHit.Inc()
|
|
|
|
return item.(*stateTrie.BeaconState).Copy()
|
|
|
|
}
|
|
|
|
hotStateCacheMiss.Inc()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-08 19:02:48 +00:00
|
|
|
// GetWithoutCopy returns a non-copied cached response via input block root.
|
|
|
|
func (c *HotStateCache) GetWithoutCopy(root [32]byte) *stateTrie.BeaconState {
|
2020-06-23 22:06:47 +00:00
|
|
|
c.lock.RLock()
|
|
|
|
defer c.lock.RUnlock()
|
2020-05-08 19:02:48 +00:00
|
|
|
item, exists := c.cache.Get(root)
|
|
|
|
if exists && item != nil {
|
|
|
|
hotStateCacheHit.Inc()
|
|
|
|
return item.(*stateTrie.BeaconState)
|
|
|
|
}
|
|
|
|
hotStateCacheMiss.Inc()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-04 22:09:21 +00:00
|
|
|
// Put the response in the cache.
|
|
|
|
func (c *HotStateCache) Put(root [32]byte, state *stateTrie.BeaconState) {
|
2020-06-23 22:06:47 +00:00
|
|
|
c.lock.Lock()
|
|
|
|
defer c.lock.Unlock()
|
2020-03-04 22:09:21 +00:00
|
|
|
c.cache.Add(root, state)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Has returns true if the key exists in the cache.
|
|
|
|
func (c *HotStateCache) Has(root [32]byte) bool {
|
2020-06-23 22:06:47 +00:00
|
|
|
c.lock.RLock()
|
|
|
|
defer c.lock.RUnlock()
|
2020-03-04 22:09:21 +00:00
|
|
|
return c.cache.Contains(root)
|
|
|
|
}
|
2020-05-08 19:02:48 +00:00
|
|
|
|
|
|
|
// Delete deletes the key exists in the cache.
|
|
|
|
func (c *HotStateCache) Delete(root [32]byte) bool {
|
2020-06-23 22:06:47 +00:00
|
|
|
c.lock.Lock()
|
|
|
|
defer c.lock.Unlock()
|
2020-05-08 19:02:48 +00:00
|
|
|
return c.cache.Remove(root)
|
|
|
|
}
|