Skip to content

Commit

Permalink
Use collections lib for params
Browse files Browse the repository at this point in the history
  • Loading branch information
alpe committed Jun 22, 2023
1 parent abf3ff9 commit 52d4260
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 25 deletions.
31 changes: 8 additions & 23 deletions x/wasm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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, &params)
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.
Expand Down
4 changes: 4 additions & 0 deletions x/wasm/keeper/keeper_cgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package keeper
import (
"path/filepath"

"cosmossdk.io/collections"

corestoretypes "cosmossdk.io/core/store"

wasmvm "github.com/CosmWasm/wasmvm"
Expand Down Expand Up @@ -40,6 +42,7 @@ func NewKeeper(
panic(err)
}

sb := collections.NewSchemaBuilder(storeService)
keeper := &Keeper{
storeService: storeService,
cdc: cdc,
Expand All @@ -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)
Expand Down
9 changes: 7 additions & 2 deletions x/wasm/keeper/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down Expand Up @@ -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)
})
}
Expand Down

0 comments on commit 52d4260

Please sign in to comment.