2019-08-27 22:01:27 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
2020-08-16 14:36:37 +00:00
|
|
|
lru "github.com/hashicorp/golang-lru"
|
2019-08-27 22:01:27 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
2022-08-16 12:20:13 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v3/beacon-chain/state"
|
|
|
|
lruwrpr "github.com/prysmaticlabs/prysm/v3/cache/lru"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/crypto/hash"
|
|
|
|
ethpb "github.com/prysmaticlabs/prysm/v3/proto/prysm/v1alpha1"
|
2019-08-27 22:01:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// maxCheckpointStateSize defines the max number of entries check point to state cache can contain.
|
2020-02-10 18:14:31 +00:00
|
|
|
// Choosing 10 to account for multiple forks, this allows 5 forks per epoch boundary with 2 epochs
|
|
|
|
// window to accept attestation based on latest spec.
|
2020-08-16 14:36:37 +00:00
|
|
|
maxCheckpointStateSize = 10
|
2019-08-27 22:01:27 +00:00
|
|
|
|
|
|
|
// Metrics.
|
|
|
|
checkpointStateMiss = promauto.NewCounter(prometheus.CounterOpts{
|
2020-08-16 14:36:37 +00:00
|
|
|
Name: "check_point_state_cache_miss",
|
2019-08-27 22:01:27 +00:00
|
|
|
Help: "The number of check point state requests that aren't present in the cache.",
|
|
|
|
})
|
|
|
|
checkpointStateHit = promauto.NewCounter(prometheus.CounterOpts{
|
|
|
|
Name: "check_point_state_cache_hit",
|
|
|
|
Help: "The number of check point state requests that are present in the cache.",
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
// CheckpointStateCache is a struct with 1 queue for looking up state by checkpoint.
|
|
|
|
type CheckpointStateCache struct {
|
2020-11-03 21:18:15 +00:00
|
|
|
cache *lru.Cache
|
2019-08-27 22:01:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewCheckpointStateCache creates a new checkpoint state cache for storing/accessing processed state.
|
|
|
|
func NewCheckpointStateCache() *CheckpointStateCache {
|
|
|
|
return &CheckpointStateCache{
|
2021-09-02 10:36:54 +00:00
|
|
|
cache: lruwrpr.New(maxCheckpointStateSize),
|
2019-08-27 22:01:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// StateByCheckpoint fetches state by checkpoint. Returns true with a
|
|
|
|
// reference to the CheckpointState info, if exists. Otherwise returns false, nil.
|
2021-07-23 16:11:21 +00:00
|
|
|
func (c *CheckpointStateCache) StateByCheckpoint(cp *ethpb.Checkpoint) (state.BeaconState, error) {
|
2021-09-15 22:55:11 +00:00
|
|
|
h, err := hash.HashProto(cp)
|
2019-08-27 22:01:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-11-03 21:18:15 +00:00
|
|
|
item, exists := c.cache.Get(h)
|
2019-08-27 22:01:27 +00:00
|
|
|
|
2020-08-16 14:36:37 +00:00
|
|
|
if exists && item != nil {
|
2019-08-27 22:01:27 +00:00
|
|
|
checkpointStateHit.Inc()
|
2020-08-16 14:36:37 +00:00
|
|
|
// Copy here is unnecessary since the return will only be used to verify attestation signature.
|
2021-07-23 16:11:21 +00:00
|
|
|
return item.(state.BeaconState), nil
|
2019-08-27 22:01:27 +00:00
|
|
|
}
|
|
|
|
|
2020-08-16 14:36:37 +00:00
|
|
|
checkpointStateMiss.Inc()
|
|
|
|
return nil, nil
|
2019-08-27 22:01:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddCheckpointState adds CheckpointState object to the cache. This method also trims the least
|
|
|
|
// recently added CheckpointState object if the cache size has ready the max cache size limit.
|
2021-07-23 16:11:21 +00:00
|
|
|
func (c *CheckpointStateCache) AddCheckpointState(cp *ethpb.Checkpoint, s state.ReadOnlyBeaconState) error {
|
2021-09-15 22:55:11 +00:00
|
|
|
h, err := hash.HashProto(cp)
|
2020-08-16 14:36:37 +00:00
|
|
|
if err != nil {
|
2019-08-27 22:01:27 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-11-03 21:18:15 +00:00
|
|
|
c.cache.Add(h, s)
|
2019-08-27 22:01:27 +00:00
|
|
|
return nil
|
|
|
|
}
|