2020-12-16 16:56:21 +00:00
|
|
|
package kv
|
2020-03-30 22:10:45 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
2022-08-16 12:20:13 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/prysm/v3/proto/prysm/v1alpha1"
|
2020-03-30 22:10:45 +00:00
|
|
|
)
|
|
|
|
|
2020-12-16 16:56:21 +00:00
|
|
|
const stateSummaryCachePruneCount = 128
|
|
|
|
|
|
|
|
// stateSummaryCache caches state summary object.
|
|
|
|
type stateSummaryCache struct {
|
2021-07-29 21:45:17 +00:00
|
|
|
initSyncStateSummaries map[[32]byte]*ethpb.StateSummary
|
2020-03-30 22:10:45 +00:00
|
|
|
initSyncStateSummariesLock sync.RWMutex
|
|
|
|
}
|
|
|
|
|
2020-12-16 16:56:21 +00:00
|
|
|
// newStateSummaryCache creates a new state summary cache.
|
|
|
|
func newStateSummaryCache() *stateSummaryCache {
|
|
|
|
return &stateSummaryCache{
|
2021-07-29 21:45:17 +00:00
|
|
|
initSyncStateSummaries: make(map[[32]byte]*ethpb.StateSummary),
|
2020-03-30 22:10:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-16 16:56:21 +00:00
|
|
|
// put saves a state summary to the initial sync state summaries cache.
|
2021-07-29 21:45:17 +00:00
|
|
|
func (c *stateSummaryCache) put(r [32]byte, b *ethpb.StateSummary) {
|
2021-01-19 13:21:32 +00:00
|
|
|
c.initSyncStateSummariesLock.Lock()
|
|
|
|
defer c.initSyncStateSummariesLock.Unlock()
|
|
|
|
c.initSyncStateSummaries[r] = b
|
2020-03-30 22:10:45 +00:00
|
|
|
}
|
|
|
|
|
2020-12-16 16:56:21 +00:00
|
|
|
// has checks if a state summary exists in the initial sync state summaries cache using the root
|
2020-03-30 22:10:45 +00:00
|
|
|
// of the block.
|
2021-01-19 13:21:32 +00:00
|
|
|
func (c *stateSummaryCache) has(r [32]byte) bool {
|
|
|
|
c.initSyncStateSummariesLock.RLock()
|
|
|
|
defer c.initSyncStateSummariesLock.RUnlock()
|
|
|
|
_, ok := c.initSyncStateSummaries[r]
|
2020-03-30 22:10:45 +00:00
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2022-04-06 21:24:00 +00:00
|
|
|
// delete state summary in cache.
|
|
|
|
func (c *stateSummaryCache) delete(r [32]byte) {
|
|
|
|
c.initSyncStateSummariesLock.Lock()
|
|
|
|
defer c.initSyncStateSummariesLock.Unlock()
|
|
|
|
delete(c.initSyncStateSummaries, r)
|
|
|
|
}
|
|
|
|
|
2020-12-16 16:56:21 +00:00
|
|
|
// get retrieves a state summary from the initial sync state summaries cache using the root of
|
2020-03-30 22:10:45 +00:00
|
|
|
// the block.
|
2021-07-29 21:45:17 +00:00
|
|
|
func (c *stateSummaryCache) get(r [32]byte) *ethpb.StateSummary {
|
2021-01-19 13:21:32 +00:00
|
|
|
c.initSyncStateSummariesLock.RLock()
|
|
|
|
defer c.initSyncStateSummariesLock.RUnlock()
|
|
|
|
b := c.initSyncStateSummaries[r]
|
2020-03-30 22:10:45 +00:00
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2020-12-16 16:56:21 +00:00
|
|
|
// len retrieves the state summary count from the state summaries cache.
|
2021-01-19 13:21:32 +00:00
|
|
|
func (c *stateSummaryCache) len() int {
|
|
|
|
c.initSyncStateSummariesLock.RLock()
|
|
|
|
defer c.initSyncStateSummariesLock.RUnlock()
|
|
|
|
return len(c.initSyncStateSummaries)
|
2020-12-16 16:56:21 +00:00
|
|
|
}
|
|
|
|
|
2020-03-30 22:10:45 +00:00
|
|
|
// GetAll retrieves all the beacon state summaries from the initial sync state summaries cache, the returned
|
|
|
|
// state summaries are unordered.
|
2021-07-29 21:45:17 +00:00
|
|
|
func (c *stateSummaryCache) getAll() []*ethpb.StateSummary {
|
2021-01-19 13:21:32 +00:00
|
|
|
c.initSyncStateSummariesLock.RLock()
|
|
|
|
defer c.initSyncStateSummariesLock.RUnlock()
|
2020-03-30 22:10:45 +00:00
|
|
|
|
2021-07-29 21:45:17 +00:00
|
|
|
summaries := make([]*ethpb.StateSummary, 0, len(c.initSyncStateSummaries))
|
2021-01-19 13:21:32 +00:00
|
|
|
for _, b := range c.initSyncStateSummaries {
|
2020-10-26 21:17:07 +00:00
|
|
|
summaries = append(summaries, b)
|
2020-03-30 22:10:45 +00:00
|
|
|
}
|
2020-10-26 21:17:07 +00:00
|
|
|
return summaries
|
2020-03-30 22:10:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Clear clears out the initial sync state summaries cache.
|
2021-01-19 13:21:32 +00:00
|
|
|
func (c *stateSummaryCache) clear() {
|
|
|
|
c.initSyncStateSummariesLock.Lock()
|
|
|
|
defer c.initSyncStateSummariesLock.Unlock()
|
2021-07-29 21:45:17 +00:00
|
|
|
c.initSyncStateSummaries = make(map[[32]byte]*ethpb.StateSummary)
|
2020-03-30 22:10:45 +00:00
|
|
|
}
|