Skip to content

Commit

Permalink
refactor: tax module start using KVStoreService
Browse files Browse the repository at this point in the history
  • Loading branch information
ivivanov authored and ivivanov committed Aug 6, 2024
1 parent c911342 commit 3bdf437
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 87 deletions.
24 changes: 12 additions & 12 deletions app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
evidencekeeper "cosmossdk.io/x/evidence/keeper"
evidencetypes "cosmossdk.io/x/evidence/types"
"cosmossdk.io/x/feegrant"
feegrantkeeper "cosmossdk.io/x/feegrant/keeper"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/address"
Expand Down Expand Up @@ -60,20 +62,19 @@ import (
ibcexported "github.com/cosmos/ibc-go/v8/modules/core/exported"
ibckeeper "github.com/cosmos/ibc-go/v8/modules/core/keeper"

"github.com/CosmWasm/wasmd/x/wasm"
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"

"github.com/Nolus-Protocol/nolus-core/wasmbinding"
mintkeeper "github.com/Nolus-Protocol/nolus-core/x/mint/keeper"
minttypes "github.com/Nolus-Protocol/nolus-core/x/mint/types"
taxmodulekeeper "github.com/Nolus-Protocol/nolus-core/x/tax/keeper"
taxmoduletypes "github.com/Nolus-Protocol/nolus-core/x/tax/types"
taxkeeper "github.com/Nolus-Protocol/nolus-core/x/tax/keeper"
taxtypes "github.com/Nolus-Protocol/nolus-core/x/tax/types"
"github.com/Nolus-Protocol/nolus-core/x/vestings"
vestingskeeper "github.com/Nolus-Protocol/nolus-core/x/vestings/keeper"
vestingstypes "github.com/Nolus-Protocol/nolus-core/x/vestings/types"

"github.com/CosmWasm/wasmd/x/wasm"
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"

feegrantkeeper "cosmossdk.io/x/feegrant/keeper"
"github.com/neutron-org/neutron/v4/x/contractmanager"
contractmanagermodulekeeper "github.com/neutron-org/neutron/v4/x/contractmanager/keeper"
contractmanagermoduletypes "github.com/neutron-org/neutron/v4/x/contractmanager/types"
Expand Down Expand Up @@ -126,7 +127,7 @@ type AppKeepers struct {
ScopedICAHostKeeper capabilitykeeper.ScopedKeeper

MintKeeper *mintkeeper.Keeper
TaxKeeper *taxmodulekeeper.Keeper
TaxKeeper *taxkeeper.Keeper
VestingsKeeper *vestingskeeper.Keeper

InterchainTxsKeeper *interchaintxskeeper.Keeper
Expand Down Expand Up @@ -482,10 +483,9 @@ func (appKeepers *AppKeepers) NewAppKeepers(
// Set legacy router for backwards compatibility with gov v1beta1
appKeepers.GovKeeper.SetLegacyRouter(govRouter)

taxKeeper := taxmodulekeeper.NewKeeper(
taxKeeper := taxkeeper.NewKeeper(
appCodec,
appKeepers.keys[taxmoduletypes.StoreKey],
appKeepers.keys[taxmoduletypes.MemStoreKey],
runtime.NewKVStoreService(appKeepers.keys[taxtypes.StoreKey]),
appKeepers.WasmKeeper,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)
Expand Down Expand Up @@ -543,7 +543,7 @@ func initParamsKeeper(
appCodec codec.BinaryCodec, legacyAmino *codec.LegacyAmino, key, tkey storetypes.StoreKey,
) *paramskeeper.Keeper {
paramsKeeper := paramskeeper.NewKeeper(appCodec, legacyAmino, key, tkey)
paramsKeeper.Subspace(taxmoduletypes.ModuleName).WithKeyTable(taxmoduletypes.ParamKeyTable())
paramsKeeper.Subspace(taxtypes.ModuleName).WithKeyTable(taxtypes.ParamKeyTable())
paramsKeeper.Subspace(authtypes.ModuleName).WithKeyTable(authtypes.ParamKeyTable()) //nolint:staticcheck
paramsKeeper.Subspace(banktypes.ModuleName).WithKeyTable(banktypes.ParamKeyTable()) //nolint:staticcheck
paramsKeeper.Subspace(stakingtypes.ModuleName).WithKeyTable(stakingtypes.ParamKeyTable()) //nolint:staticcheck
Expand Down
26 changes: 14 additions & 12 deletions testutil/keeper/tax.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,38 @@ package keeper
import (
"testing"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"

protov2 "google.golang.org/protobuf/proto"

"cosmossdk.io/log"
"cosmossdk.io/store"
metrics "cosmossdk.io/store/metrics"
sdktypes "cosmossdk.io/store/types"
storetypes "cosmossdk.io/store/types"
mock_types "github.com/Nolus-Protocol/nolus-core/testutil/mocks/tax/types"
"github.com/Nolus-Protocol/nolus-core/x/tax/keeper"
"github.com/Nolus-Protocol/nolus-core/x/tax/types"

tmproto "github.com/cometbft/cometbft/proto/tendermint/types"

dbm "github.com/cosmos/cosmos-db"

"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/runtime"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"

protov2 "google.golang.org/protobuf/proto"
mock_types "github.com/Nolus-Protocol/nolus-core/testutil/mocks/tax/types"
"github.com/Nolus-Protocol/nolus-core/x/tax/keeper"
"github.com/Nolus-Protocol/nolus-core/x/tax/types"
)

func TaxKeeper(t testing.TB, isCheckTx bool, gasPrices sdk.DecCoins) (*keeper.Keeper, sdk.Context, *mock_types.MockWasmKeeper) {
storeKey := storetypes.NewKVStoreKey(types.StoreKey)
memStoreKey := storetypes.NewMemoryStoreKey(types.MemStoreKey)

db := dbm.NewMemDB()
stateStore := store.NewCommitMultiStore(db, log.NewNopLogger(), metrics.NewNoOpMetrics())
stateStore.MountStoreWithDB(storeKey, sdktypes.StoreTypeIAVL, db)
stateStore.MountStoreWithDB(memStoreKey, sdktypes.StoreTypeMemory, nil)
stateStore.MountStoreWithDB(storeKey, storetypes.StoreTypeIAVL, db)
require.NoError(t, stateStore.LoadLatestVersion())

registry := codectypes.NewInterfaceRegistry()
Expand All @@ -42,8 +45,7 @@ func TaxKeeper(t testing.TB, isCheckTx bool, gasPrices sdk.DecCoins) (*keeper.Ke

k := keeper.NewKeeper(
cdc,
storeKey,
memStoreKey,
runtime.NewKVStoreService(storeKey),
mockWasmKeeper,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)
Expand Down
12 changes: 7 additions & 5 deletions x/tax/keeper/grpc_query_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@ package keeper
import (
"context"

"github.com/Nolus-Protocol/nolus-core/x/tax/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/Nolus-Protocol/nolus-core/x/tax/types"
)

func (k Keeper) Params(c context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
func (k Keeper) Params(ctx context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
ctx := sdk.UnwrapSDKContext(c)
params := k.GetParams(ctx)
sdkCtx := sdk.UnwrapSDKContext(ctx)
params := k.GetParams(sdkCtx)

return &types.QueryParamsResponse{Params: params}, nil
}
30 changes: 15 additions & 15 deletions x/tax/keeper/keeper.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,39 @@
package keeper

import (
"context"
"fmt"

"cosmossdk.io/core/store"
"cosmossdk.io/log"

sdktypes "cosmossdk.io/store/types"
"github.com/Nolus-Protocol/nolus-core/x/tax/types"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/Nolus-Protocol/nolus-core/x/tax/types"
)

type Keeper struct {
cdc codec.BinaryCodec
storeKey sdktypes.StoreKey
memKey sdktypes.StoreKey
cdc codec.BinaryCodec
storeService store.KVStoreService
wasmKeeper types.WasmKeeper

wasmKeeper types.WasmKeeper
// the address capable of executing a MsgUpdateParams message. Typically, this
// should be the x/gov module account.
authority string
}

func NewKeeper(
cdc codec.BinaryCodec,
storeKey,
memKey sdktypes.StoreKey,
storeService store.KVStoreService,
wasmKeeper types.WasmKeeper,
authority string,
) Keeper {
return Keeper{
cdc: cdc,
storeKey: storeKey,
memKey: memKey,
wasmKeeper: wasmKeeper,
authority: authority,
cdc: cdc,
storeService: storeService,
wasmKeeper: wasmKeeper,
authority: authority,
}
}

Expand All @@ -43,6 +42,7 @@ func (k Keeper) GetAuthority() string {
return k.authority
}

func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
func (k Keeper) Logger(ctx context.Context) log.Logger {
c := sdk.UnwrapSDKContext(ctx)
return c.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
}
2 changes: 1 addition & 1 deletion x/tax/keeper/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ func NewMigrator(k Keeper, ss exported.Subspace) Migrator {
// and managed by the x/params modules and stores them directly into the x/tax
// module state.
func (m Migrator) Migrate2to3(ctx sdk.Context) error {
return v3.Migrate(ctx, ctx.KVStore(m.keeper.storeKey), m.legacySubspace, m.keeper.cdc)
return v3.Migrate(ctx, m.keeper.storeService.OpenKVStore(ctx), m.legacySubspace, m.keeper.cdc)
}
4 changes: 1 addition & 3 deletions x/tax/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/Nolus-Protocol/nolus-core/x/tax/types"

sdk "github.com/cosmos/cosmos-sdk/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
)

Expand All @@ -24,7 +23,7 @@ func NewMsgServerImpl(k Keeper) types.MsgServer {
}
}

func (ms msgServer) UpdateParams(goCtx context.Context, req *types.MsgUpdateParams) (*types.MsgUpdateParamsResponse, error) {
func (ms msgServer) UpdateParams(ctx context.Context, req *types.MsgUpdateParams) (*types.MsgUpdateParamsResponse, error) {
if err := req.ValidateBasic(); err != nil {
return nil, err
}
Expand All @@ -33,7 +32,6 @@ func (ms msgServer) UpdateParams(goCtx context.Context, req *types.MsgUpdatePara
return nil, errors.Wrapf(govtypes.ErrInvalidSigner, "invalid authority; expected %s, got %s", ms.authority, req.Authority)
}

ctx := sdk.UnwrapSDKContext(goCtx)
if err := ms.SetParams(ctx, req.Params); err != nil {
return nil, err
}
Expand Down
66 changes: 38 additions & 28 deletions x/tax/keeper/params.go
Original file line number Diff line number Diff line change
@@ -1,70 +1,80 @@
package keeper

import (
"context"

"github.com/Nolus-Protocol/nolus-core/x/tax/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// GetParams get all parameters as types.Params.
func (k Keeper) GetParams(ctx sdk.Context) (p types.Params) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.ParamsKey)
if bz == nil {
func (k Keeper) GetParams(ctx context.Context) types.Params {
store := k.storeService.OpenKVStore(ctx)

var p types.Params
b, err := store.Get(types.ParamsKey)
if err != nil {
// TODO panic("error getting stored tax params")
return p
}

k.cdc.MustUnmarshal(bz, &p)
k.cdc.MustUnmarshal(b, &p)
return p
}

// SetParams set the params.
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) error {
func (k Keeper) SetParams(ctx context.Context, params types.Params) error {
if err := params.Validate(); err != nil {
return err
}

store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&params)
store.Set(types.ParamsKey, bz)
store := k.storeService.OpenKVStore(ctx)
b := k.cdc.MustMarshal(&params)

if err := store.Set(types.ParamsKey, b); err != nil {
return err
}

return nil
}

// FeeRate returns the fee rate.
func (k Keeper) FeeRate(ctx sdk.Context) (res int32) {
var p types.Params
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.ParamsKey)
if bz == nil {
func (k Keeper) FeeRate(ctx context.Context) int32 {
store := k.storeService.OpenKVStore(ctx)

b, err := store.Get(types.ParamsKey)
if err != nil {
return 0
}

k.cdc.MustUnmarshal(bz, &p)
var p types.Params
k.cdc.MustUnmarshal(b, &p)
return p.FeeRate
}

// ContractAddress returns the contract address.
func (k Keeper) ContractAddress(ctx sdk.Context) (res string) {
var p types.Params
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.ParamsKey)
if bz == nil {
func (k Keeper) ContractAddress(ctx context.Context) string {
store := k.storeService.OpenKVStore(ctx)

b, err := store.Get(types.ParamsKey)
if err != nil {
return ""
}

k.cdc.MustUnmarshal(bz, &p)
var p types.Params
k.cdc.MustUnmarshal(b, &p)
return p.ContractAddress
}

// BseDenom returns the base denom.
func (k Keeper) BaseDenom(ctx sdk.Context) (res string) {
var p types.Params
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.ParamsKey)
if bz == nil {
func (k Keeper) BaseDenom(ctx context.Context) string {
store := k.storeService.OpenKVStore(ctx)

b, err := store.Get(types.ParamsKey)
if err != nil {
return ""
}

k.cdc.MustUnmarshal(bz, &p)
var p types.Params
k.cdc.MustUnmarshal(b, &p)
return p.BaseDenom
}
1 change: 1 addition & 0 deletions x/tax/keeper/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func (s *KeeperTestSuite) TestParams() {

s.Run(tc.name, func() {
expected := s.app.TaxKeeper.GetParams(s.ctx)
// TODO expect panic if params are not set
err := s.app.TaxKeeper.SetParams(s.ctx, tc.input)
if tc.expectErr {
s.Require().Error(err)
Expand Down
12 changes: 8 additions & 4 deletions x/tax/migrations/v3/migrate.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package v3

import (
storetypes "cosmossdk.io/store/types"
"github.com/Nolus-Protocol/nolus-core/x/tax/exported"
"github.com/Nolus-Protocol/nolus-core/x/tax/types"
storetypes "cosmossdk.io/core/store"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/Nolus-Protocol/nolus-core/x/tax/exported"
"github.com/Nolus-Protocol/nolus-core/x/tax/types"
)

const (
Expand All @@ -32,7 +34,9 @@ func Migrate(
}

bz := cdc.MustMarshal(&currParams)
store.Set(ParamsKey, bz)
if err := store.Set(ParamsKey, bz); err != nil {
return err
}

return nil
}
Loading

0 comments on commit 3bdf437

Please sign in to comment.