Move validator cache to the keeper in stake

This commit is contained in:
Jack Zampolin 2018-12-10 14:14:52 -08:00
parent c7646d2caf
commit 97efff8af6
2 changed files with 25 additions and 24 deletions

View File

@ -1,6 +1,8 @@
package keeper
import (
"container/list"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
@ -11,12 +13,14 @@ import (
// keeper of the stake store
type Keeper struct {
storeKey sdk.StoreKey
storeTKey sdk.StoreKey
cdc *codec.Codec
bankKeeper bank.Keeper
hooks sdk.StakingHooks
paramstore params.Subspace
storeKey sdk.StoreKey
storeTKey sdk.StoreKey
cdc *codec.Codec
bankKeeper bank.Keeper
hooks sdk.StakingHooks
paramstore params.Subspace
validatorCache map[string]cachedValidator
validatorCacheList *list.List
// codespace
codespace sdk.CodespaceType
@ -24,13 +28,15 @@ type Keeper struct {
func NewKeeper(cdc *codec.Codec, key, tkey sdk.StoreKey, ck bank.Keeper, paramstore params.Subspace, codespace sdk.CodespaceType) Keeper {
keeper := Keeper{
storeKey: key,
storeTKey: tkey,
cdc: cdc,
bankKeeper: ck,
paramstore: paramstore.WithTypeTable(ParamTypeTable()),
hooks: nil,
codespace: codespace,
storeKey: key,
storeTKey: tkey,
cdc: cdc,
bankKeeper: ck,
paramstore: paramstore.WithTypeTable(ParamTypeTable()),
hooks: nil,
validatorCache: make(map[string]cachedValidator, 500),
validatorCacheList: list.New(),
codespace: codespace,
}
return keeper
}

View File

@ -2,7 +2,6 @@ package keeper
import (
"bytes"
"container/list"
"fmt"
"time"
@ -19,10 +18,6 @@ type cachedValidator struct {
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 validatorCacheList = list.New()
// get a single validator
func (k Keeper) GetValidator(ctx sdk.Context, addr sdk.ValAddress) (validator types.Validator, found bool) {
store := ctx.KVStore(k.storeKey)
@ -33,7 +28,7 @@ func (k Keeper) GetValidator(ctx sdk.Context, addr sdk.ValAddress) (validator ty
// If these amino encoded bytes are in the cache, return the cached validator
strValue := string(value)
if val, ok := validatorCache[strValue]; ok {
if val, ok := k.validatorCache[strValue]; ok {
valToReturn := val.val
// Doesn't mutate the cache's value
valToReturn.OperatorAddr = addr
@ -43,13 +38,13 @@ func (k Keeper) GetValidator(ctx sdk.Context, addr sdk.ValAddress) (validator ty
// amino bytes weren't found in cache, so amino unmarshal and add it to the cache
validator = types.MustUnmarshalValidator(k.cdc, addr, value)
cachedVal := cachedValidator{validator, strValue}
validatorCache[strValue] = cachedValidator{validator, strValue}
validatorCacheList.PushBack(cachedVal)
k.validatorCache[strValue] = cachedValidator{validator, strValue}
k.validatorCacheList.PushBack(cachedVal)
// if the cache is too big, pop off the last element from it
if validatorCacheList.Len() > 500 {
valToRemove := validatorCacheList.Remove(validatorCacheList.Front()).(cachedValidator)
delete(validatorCache, valToRemove.marshalled)
if k.validatorCacheList.Len() > 500 {
valToRemove := k.validatorCacheList.Remove(k.validatorCacheList.Front()).(cachedValidator)
delete(k.validatorCache, valToRemove.marshalled)
}
validator = types.MustUnmarshalValidator(k.cdc, addr, value)