Move validator cache to the keeper in stake (#3075)
This commit is contained in:
commit
582ca8e8b9
|
@ -1,6 +1,8 @@
|
|||
package keeper
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
|
||||
|
@ -9,6 +11,8 @@ import (
|
|||
"github.com/cosmos/cosmos-sdk/x/stake/types"
|
||||
)
|
||||
|
||||
const aminoCacheSize = 500
|
||||
|
||||
// keeper of the stake store
|
||||
type Keeper struct {
|
||||
storeKey sdk.StoreKey
|
||||
|
@ -17,6 +21,8 @@ type Keeper struct {
|
|||
bankKeeper bank.Keeper
|
||||
hooks sdk.StakingHooks
|
||||
paramstore params.Subspace
|
||||
validatorCache map[string]cachedValidator
|
||||
validatorCacheList *list.List
|
||||
|
||||
// codespace
|
||||
codespace sdk.CodespaceType
|
||||
|
@ -30,6 +36,8 @@ func NewKeeper(cdc *codec.Codec, key, tkey sdk.StoreKey, ck bank.Keeper, paramst
|
|||
bankKeeper: ck,
|
||||
paramstore: paramstore.WithTypeTable(ParamTypeTable()),
|
||||
hooks: nil,
|
||||
validatorCache: make(map[string]cachedValidator, aminoCacheSize),
|
||||
validatorCacheList: list.New(),
|
||||
codespace: codespace,
|
||||
}
|
||||
return keeper
|
||||
|
|
|
@ -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() > aminoCacheSize {
|
||||
valToRemove := k.validatorCacheList.Remove(k.validatorCacheList.Front()).(cachedValidator)
|
||||
delete(k.validatorCache, valToRemove.marshalled)
|
||||
}
|
||||
|
||||
validator = types.MustUnmarshalValidator(k.cdc, addr, value)
|
||||
|
|
Loading…
Reference in New Issue