extra comment on cache key usage

This commit is contained in:
rigelrozanski 2018-09-02 15:42:25 -04:00
parent f29fdcafdd
commit 2c66ba0bd4
1 changed files with 5 additions and 1 deletions

View File

@ -18,9 +18,10 @@ import (
// live chain should, however we require the slashing to be fast as noone pays gas for it. // live chain should, however we require the slashing to be fast as noone pays gas for it.
type cachedValidator struct { type cachedValidator struct {
val types.Validator val types.Validator
marshalled string marshalled string // marshalled amino bytes for the validator object (not operator address)
} }
// validatorCache-key: validator amino bytes
var validatorCache = make(map[string]cachedValidator, 500) var validatorCache = make(map[string]cachedValidator, 500)
var validatorCacheList = list.New() var validatorCacheList = list.New()
@ -31,6 +32,7 @@ func (k Keeper) GetValidator(ctx sdk.Context, addr sdk.ValAddress) (validator ty
if value == nil { if value == nil {
return validator, false return validator, false
} }
// If these amino encoded bytes are in the cache, return the cached validator // If these amino encoded bytes are in the cache, return the cached validator
strValue := string(value) strValue := string(value)
if val, ok := validatorCache[strValue]; ok { if val, ok := validatorCache[strValue]; ok {
@ -39,11 +41,13 @@ func (k Keeper) GetValidator(ctx sdk.Context, addr sdk.ValAddress) (validator ty
valToReturn.Operator = addr valToReturn.Operator = addr
return valToReturn, true return valToReturn, true
} }
// amino bytes weren't found in cache, so amino unmarshal and add it to the cache // amino bytes weren't found in cache, so amino unmarshal and add it to the cache
validator = types.MustUnmarshalValidator(k.cdc, addr, value) validator = types.MustUnmarshalValidator(k.cdc, addr, value)
cachedVal := cachedValidator{validator, strValue} cachedVal := cachedValidator{validator, strValue}
validatorCache[strValue] = cachedValidator{validator, strValue} validatorCache[strValue] = cachedValidator{validator, strValue}
validatorCacheList.PushBack(cachedVal) validatorCacheList.PushBack(cachedVal)
// if the cache is too big, pop off the last element from it // if the cache is too big, pop off the last element from it
if validatorCacheList.Len() > 500 { if validatorCacheList.Len() > 500 {
valToRemove := validatorCacheList.Remove(validatorCacheList.Front()).(cachedValidator) valToRemove := validatorCacheList.Remove(validatorCacheList.Front()).(cachedValidator)