ActiveValidatorCount to use cache (#6254)

* Add helper to prevent zero hashes

* Test

* Add ActiveIndicesCount getter for committee cache

* ActiveIndicesCount test

* ActiveValidatorCount to use cache

* Update cache on miss

* Update tests
This commit is contained in:
terence tsao
2020-06-14 15:24:49 -07:00
committed by GitHub
parent f4e9e2f49c
commit a9070ad725
6 changed files with 71 additions and 0 deletions

View File

@@ -169,6 +169,30 @@ func (c *CommitteeCache) ActiveIndices(seed [32]byte) ([]uint64, error) {
return item.SortedIndices, nil
}
// ActiveIndicesCount returns the active indices count of a given seed stored in cache.
func (c *CommitteeCache) ActiveIndicesCount(seed [32]byte) (int, error) {
c.lock.RLock()
defer c.lock.RUnlock()
obj, exists, err := c.CommitteeCache.GetByKey(key(seed))
if err != nil {
return 0, err
}
if exists {
CommitteeCacheHit.Inc()
} else {
CommitteeCacheMiss.Inc()
return 0, nil
}
item, ok := obj.(*Committees)
if !ok {
return 0, ErrNotCommittee
}
return len(item.SortedIndices), nil
}
// ProposerIndices returns the proposer indices of a given seed.
func (c *CommitteeCache) ProposerIndices(seed [32]byte) ([]uint64, error) {
c.lock.RLock()