diff --git a/x/wasm/keeper/keeper.go b/x/wasm/keeper/keeper.go index 31684a90a3..deb1a68cc3 100644 --- a/x/wasm/keeper/keeper.go +++ b/x/wasm/keeper/keeper.go @@ -12,18 +12,17 @@ import ( "strings" "time" + "cosmossdk.io/collections" corestoretypes "cosmossdk.io/core/store" - - "github.com/cosmos/cosmos-sdk/runtime" - errorsmod "cosmossdk.io/errors" - storetypes "cosmossdk.io/store/types" - "cosmossdk.io/log" "cosmossdk.io/store/prefix" + storetypes "cosmossdk.io/store/types" + wasmvm "github.com/CosmWasm/wasmvm" wasmvmtypes "github.com/CosmWasm/wasmvm/types" "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/runtime" "github.com/cosmos/cosmos-sdk/telemetry" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" @@ -107,6 +106,7 @@ type Keeper struct { maxQueryStackSize uint32 acceptedAccountTypes map[reflect.Type]struct{} accountPruner AccountPruner + params collections.Item[types.Params] // the address capable of executing a MsgUpdateParams message. Typically, this // should be the x/gov module account. authority string @@ -122,31 +122,16 @@ func (k Keeper) getInstantiateAccessConfig(ctx sdk.Context) types.AccessType { // GetParams returns the total set of wasm parameters. func (k Keeper) GetParams(ctx sdk.Context) types.Params { - var params types.Params - bz, err := k.storeService.OpenKVStore(ctx).Get(types.ParamsKey) + p, err := k.params.Get(ctx) if err != nil { panic(err) } - if bz == nil { - return params - } - - k.cdc.MustUnmarshal(bz, ¶ms) - return params + return p } // SetParams sets all wasm parameters. func (k Keeper) SetParams(ctx sdk.Context, ps types.Params) error { - if err := ps.ValidateBasic(); err != nil { - return err - } - - store := k.storeService.OpenKVStore(ctx) - bz, err := k.cdc.Marshal(&ps) - if err != nil { - return err - } - return store.Set(types.ParamsKey, bz) + return k.params.Set(ctx, ps) } // GetAuthority returns the x/wasm module's authority. diff --git a/x/wasm/keeper/keeper_cgo.go b/x/wasm/keeper/keeper_cgo.go index 41dee0f32d..ecd5605b10 100644 --- a/x/wasm/keeper/keeper_cgo.go +++ b/x/wasm/keeper/keeper_cgo.go @@ -5,6 +5,8 @@ package keeper import ( "path/filepath" + "cosmossdk.io/collections" + corestoretypes "cosmossdk.io/core/store" wasmvm "github.com/CosmWasm/wasmvm" @@ -40,6 +42,7 @@ func NewKeeper( panic(err) } + sb := collections.NewSchemaBuilder(storeService) keeper := &Keeper{ storeService: storeService, cdc: cdc, @@ -54,6 +57,7 @@ func NewKeeper( gasRegister: NewDefaultWasmGasRegister(), maxQueryStackSize: types.DefaultMaxQueryStackSize, acceptedAccountTypes: defaultAcceptedAccountTypes, + params: collections.NewItem(sb, types.ParamsKey, "params", codec.CollValue[types.Params](cdc)), authority: authority, } keeper.wasmVMQueryHandler = DefaultQueryPlugins(bankKeeper, stakingKeeper, distrKeeper, channelKeeper, keeper) diff --git a/x/wasm/keeper/options_test.go b/x/wasm/keeper/options_test.go index e6d055e915..a7b0e99185 100644 --- a/x/wasm/keeper/options_test.go +++ b/x/wasm/keeper/options_test.go @@ -4,8 +4,9 @@ import ( "reflect" "testing" + storetypes "cosmossdk.io/store/types" wasmvm "github.com/CosmWasm/wasmvm" - + "github.com/cosmos/cosmos-sdk/runtime" authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" vestingtypes "github.com/cosmos/cosmos-sdk/x/auth/vesting/types" @@ -19,6 +20,9 @@ import ( ) func TestConstructorOptions(t *testing.T) { + storeKey := storetypes.NewKVStoreKey(types.StoreKey) + codec := MakeEncodingConfig(t).Marshaler + specs := map[string]struct { srcOpt Option verify func(*testing.T, Keeper) @@ -113,7 +117,8 @@ func TestConstructorOptions(t *testing.T) { } for name, spec := range specs { t.Run(name, func(t *testing.T) { - k := NewKeeper(nil, nil, authkeeper.AccountKeeper{}, &bankkeeper.BaseKeeper{}, stakingkeeper.Keeper{}, nil, nil, nil, nil, nil, nil, nil, nil, "tempDir", types.DefaultWasmConfig(), AvailableCapabilities, "", spec.srcOpt) + k := NewKeeper(codec, runtime.NewKVStoreService(storeKey), authkeeper.AccountKeeper{}, &bankkeeper.BaseKeeper{}, stakingkeeper.Keeper{}, nil, nil, nil, nil, nil, nil, nil, nil, "tempDir", types.DefaultWasmConfig(), AvailableCapabilities, "", spec.srcOpt) + spec.verify(t, k) }) }