Move validator cache to the keeper in stake (#3075)
This commit is contained in:
commit
582ca8e8b9
|
@ -1,6 +1,8 @@
|
||||||
package keeper
|
package keeper
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"container/list"
|
||||||
|
|
||||||
"github.com/cosmos/cosmos-sdk/codec"
|
"github.com/cosmos/cosmos-sdk/codec"
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
|
|
||||||
|
@ -9,14 +11,18 @@ import (
|
||||||
"github.com/cosmos/cosmos-sdk/x/stake/types"
|
"github.com/cosmos/cosmos-sdk/x/stake/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const aminoCacheSize = 500
|
||||||
|
|
||||||
// keeper of the stake store
|
// keeper of the stake store
|
||||||
type Keeper struct {
|
type Keeper struct {
|
||||||
storeKey sdk.StoreKey
|
storeKey sdk.StoreKey
|
||||||
storeTKey sdk.StoreKey
|
storeTKey sdk.StoreKey
|
||||||
cdc *codec.Codec
|
cdc *codec.Codec
|
||||||
bankKeeper bank.Keeper
|
bankKeeper bank.Keeper
|
||||||
hooks sdk.StakingHooks
|
hooks sdk.StakingHooks
|
||||||
paramstore params.Subspace
|
paramstore params.Subspace
|
||||||
|
validatorCache map[string]cachedValidator
|
||||||
|
validatorCacheList *list.List
|
||||||
|
|
||||||
// codespace
|
// codespace
|
||||||
codespace sdk.CodespaceType
|
codespace sdk.CodespaceType
|
||||||
|
@ -24,13 +30,15 @@ type Keeper struct {
|
||||||
|
|
||||||
func NewKeeper(cdc *codec.Codec, key, tkey sdk.StoreKey, ck bank.Keeper, paramstore params.Subspace, codespace sdk.CodespaceType) Keeper {
|
func NewKeeper(cdc *codec.Codec, key, tkey sdk.StoreKey, ck bank.Keeper, paramstore params.Subspace, codespace sdk.CodespaceType) Keeper {
|
||||||
keeper := Keeper{
|
keeper := Keeper{
|
||||||
storeKey: key,
|
storeKey: key,
|
||||||
storeTKey: tkey,
|
storeTKey: tkey,
|
||||||
cdc: cdc,
|
cdc: cdc,
|
||||||
bankKeeper: ck,
|
bankKeeper: ck,
|
||||||
paramstore: paramstore.WithTypeTable(ParamTypeTable()),
|
paramstore: paramstore.WithTypeTable(ParamTypeTable()),
|
||||||
hooks: nil,
|
hooks: nil,
|
||||||
codespace: codespace,
|
validatorCache: make(map[string]cachedValidator, aminoCacheSize),
|
||||||
|
validatorCacheList: list.New(),
|
||||||
|
codespace: codespace,
|
||||||
}
|
}
|
||||||
return keeper
|
return keeper
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,6 @@ package keeper
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"container/list"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -19,10 +18,6 @@ type cachedValidator struct {
|
||||||
marshalled string // marshalled amino bytes for the validator object (not operator address)
|
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
|
// get a single validator
|
||||||
func (k Keeper) GetValidator(ctx sdk.Context, addr sdk.ValAddress) (validator types.Validator, found bool) {
|
func (k Keeper) GetValidator(ctx sdk.Context, addr sdk.ValAddress) (validator types.Validator, found bool) {
|
||||||
store := ctx.KVStore(k.storeKey)
|
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
|
// 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 := k.validatorCache[strValue]; ok {
|
||||||
valToReturn := val.val
|
valToReturn := val.val
|
||||||
// Doesn't mutate the cache's value
|
// Doesn't mutate the cache's value
|
||||||
valToReturn.OperatorAddr = addr
|
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
|
// 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}
|
k.validatorCache[strValue] = cachedValidator{validator, strValue}
|
||||||
validatorCacheList.PushBack(cachedVal)
|
k.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 k.validatorCacheList.Len() > aminoCacheSize {
|
||||||
valToRemove := validatorCacheList.Remove(validatorCacheList.Front()).(cachedValidator)
|
valToRemove := k.validatorCacheList.Remove(k.validatorCacheList.Front()).(cachedValidator)
|
||||||
delete(validatorCache, valToRemove.marshalled)
|
delete(k.validatorCache, valToRemove.marshalled)
|
||||||
}
|
}
|
||||||
|
|
||||||
validator = types.MustUnmarshalValidator(k.cdc, addr, value)
|
validator = types.MustUnmarshalValidator(k.cdc, addr, value)
|
||||||
|
|
Loading…
Reference in New Issue