diff --git a/CHANGELOG.md b/CHANGELOG.md index ee6868aac..c2e5209b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,7 @@ FEATURES * [stake] Seperation of fee distribution to a new module * [stake] Creation of a validator/delegation generics in `/types` * [stake] Helper Description of the store in x/stake/store.md +* [stake] removed use of caches in the stake keeper BUG FIXES diff --git a/x/stake/genesis.go b/x/stake/genesis.go index 505f0c204..5b7354653 100644 --- a/x/stake/genesis.go +++ b/x/stake/genesis.go @@ -30,7 +30,7 @@ func DefaultGenesisState() GenesisState { // InitGenesis - store genesis parameters func InitGenesis(ctx sdk.Context, k Keeper, data GenesisState) { k.setPool(ctx, data.Pool) - k.setParams(ctx, data.Params) + k.setNewParams(ctx, data.Params) for _, validator := range data.Validators { k.setValidator(ctx, validator) } diff --git a/x/stake/keeper.go b/x/stake/keeper.go index b76f3ddb3..fe9f184d9 100644 --- a/x/stake/keeper.go +++ b/x/stake/keeper.go @@ -15,10 +15,6 @@ type Keeper struct { cdc *wire.Codec coinKeeper bank.Keeper - // caches - pool Pool - params Params - // codespace codespace sdk.CodespaceType } @@ -461,12 +457,11 @@ func (k Keeper) removeDelegation(ctx sdk.Context, bond Delegation) { //_______________________________________________________________________ // load/save the global staking params -func (k Keeper) GetParams(ctx sdk.Context) (params Params) { - // check if cached before anything - if !k.params.equal(Params{}) { - return k.params - } +func (k Keeper) GetParams(ctx sdk.Context) Params { store := ctx.KVStore(k.storeKey) + return k.getParams(store) +} +func (k Keeper) getParams(store sdk.KVStore) (params Params) { b := store.Get(ParamKey) if b == nil { panic("Stored params should not have been nil") @@ -475,17 +470,24 @@ func (k Keeper) GetParams(ctx sdk.Context) (params Params) { k.cdc.MustUnmarshalBinary(b, ¶ms) return } -func (k Keeper) setParams(ctx sdk.Context, params Params) { + +func (k Keeper) setNewParams(ctx sdk.Context, params Params) { store := ctx.KVStore(k.storeKey) b := k.cdc.MustMarshalBinary(params) store.Set(ParamKey, b) +} + +func (k Keeper) setParams(ctx sdk.Context, params Params) { + store := ctx.KVStore(k.storeKey) + exParams := k.getParams(store) // if max validator count changes, must recalculate validator set - if k.params.MaxValidators != params.MaxValidators { + if exParams.MaxValidators != params.MaxValidators { pool := k.GetPool(ctx) k.updateBondedValidators(ctx, store, pool, nil) } - k.params = params // update the cache + b := k.cdc.MustMarshalBinary(params) + store.Set(ParamKey, b) } //_______________________________________________________________________ @@ -496,10 +498,6 @@ func (k Keeper) GetPool(ctx sdk.Context) (pool Pool) { return k.getPool(store) } func (k Keeper) getPool(store sdk.KVStore) (pool Pool) { - // check if cached before anything - if !k.pool.equal(Pool{}) { - return k.pool - } b := store.Get(PoolKey) if b == nil { panic("Stored pool should not have been nil") @@ -512,7 +510,6 @@ func (k Keeper) setPool(ctx sdk.Context, p Pool) { store := ctx.KVStore(k.storeKey) b := k.cdc.MustMarshalBinary(p) store.Set(PoolKey, b) - k.pool = Pool{} //clear the cache } //__________________________________________________________________________ diff --git a/x/stake/test_common.go b/x/stake/test_common.go index 8173d00b1..6d2f514b9 100644 --- a/x/stake/test_common.go +++ b/x/stake/test_common.go @@ -103,7 +103,7 @@ func createTestInput(t *testing.T, isCheckTx bool, initCoins int64) (sdk.Context ck := bank.NewKeeper(accountMapper) keeper := NewKeeper(cdc, keyStake, ck, DefaultCodespace) keeper.setPool(ctx, initialPool()) - keeper.setParams(ctx, defaultParams()) + keeper.setNewParams(ctx, defaultParams()) // fill all the addresses with some coins for _, addr := range addrs {