From e0a720ad83857e22f5204ab3061984f2094617ac Mon Sep 17 00:00:00 2001 From: Pino' Surace Date: Thu, 9 Mar 2023 11:05:26 +0100 Subject: [PATCH] Implement UpdateParams msg --- app/app.go | 4 ++-- x/wasm/keeper/keeper_test.go | 2 +- x/wasm/keeper/msg_server.go | 45 ++++++++++++++++++++++++++--------- x/wasm/keeper/options_test.go | 3 +-- x/wasm/keeper/test_common.go | 2 +- x/wasm/module.go | 2 +- x/wasm/module_test.go | 3 ++- x/wasm/types/tx.go | 24 +++++++++++++++++++ 8 files changed, 66 insertions(+), 19 deletions(-) diff --git a/app/app.go b/app/app.go index 1ecfbd578b..91bd3c6ffd 100644 --- a/app/app.go +++ b/app/app.go @@ -592,7 +592,6 @@ func NewWasmApp( app.WasmKeeper = wasm.NewKeeper( appCodec, keys[wasm.StoreKey], - app.GetSubspace(wasm.ModuleName), app.AccountKeeper, app.BankKeeper, app.StakingKeeper, @@ -606,6 +605,7 @@ func NewWasmApp( wasmDir, wasmConfig, availableCapabilities, + authtypes.NewModuleAddress(govtypes.ModuleName).String(), wasmOpts..., ) @@ -681,7 +681,7 @@ func NewWasmApp( groupmodule.NewAppModule(appCodec, app.GroupKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry), nftmodule.NewAppModule(appCodec, app.NFTKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry), consensus.NewAppModule(appCodec, app.ConsensusParamsKeeper), - wasm.NewAppModule(appCodec, &app.WasmKeeper, app.StakingKeeper, app.AccountKeeper, app.BankKeeper, app.MsgServiceRouter()), + wasm.NewAppModule(appCodec, &app.WasmKeeper, app.StakingKeeper, app.AccountKeeper, app.BankKeeper, app.MsgServiceRouter(), app.GetSubspace(paramstypes.ModuleName)), ibc.NewAppModule(app.IBCKeeper), transfer.NewAppModule(app.TransferKeeper), ibcfee.NewAppModule(app.IBCFeeKeeper), diff --git a/x/wasm/keeper/keeper_test.go b/x/wasm/keeper/keeper_test.go index 24c80c9239..518e5051a3 100644 --- a/x/wasm/keeper/keeper_test.go +++ b/x/wasm/keeper/keeper_test.go @@ -748,7 +748,7 @@ func TestInstantiateWithContractFactoryChildQueriesParent(t *testing.T) { // overwrite wasmvm in router router := baseapp.NewMsgServiceRouter() router.SetInterfaceRegistry(keepers.EncodingConfig.InterfaceRegistry) - types.RegisterMsgServer(router, NewMsgServerImpl(NewDefaultPermissionKeeper(keeper))) + types.RegisterMsgServer(router, NewMsgServerImpl(NewDefaultPermissionKeeper(keeper), *keeper)) keeper.messenger = NewDefaultMessageHandler(router, nil, nil, nil, keepers.EncodingConfig.Marshaler, nil) // overwrite wasmvm in response handler keeper.wasmVMResponseHandler = NewDefaultWasmVMContractResponseHandler(NewMessageDispatcher(keeper.messenger, keeper)) diff --git a/x/wasm/keeper/msg_server.go b/x/wasm/keeper/msg_server.go index d0383db699..f9f1de9edd 100644 --- a/x/wasm/keeper/msg_server.go +++ b/x/wasm/keeper/msg_server.go @@ -12,11 +12,12 @@ import ( var _ types.MsgServer = msgServer{} type msgServer struct { - keeper types.ContractOpsKeeper + contractKeeper types.ContractOpsKeeper + wasmKeeper Keeper } -func NewMsgServerImpl(k types.ContractOpsKeeper) types.MsgServer { - return &msgServer{keeper: k} +func NewMsgServerImpl(ck types.ContractOpsKeeper, wk Keeper) types.MsgServer { + return &msgServer{contractKeeper: ck, wasmKeeper: wk} } func (m msgServer) StoreCode(goCtx context.Context, msg *types.MsgStoreCode) (*types.MsgStoreCodeResponse, error) { @@ -35,7 +36,7 @@ func (m msgServer) StoreCode(goCtx context.Context, msg *types.MsgStoreCode) (*t sdk.NewAttribute(sdk.AttributeKeySender, msg.Sender), )) - codeID, checksum, err := m.keeper.Create(ctx, senderAddr, msg.WASMByteCode, msg.InstantiatePermission) + codeID, checksum, err := m.contractKeeper.Create(ctx, senderAddr, msg.WASMByteCode, msg.InstantiatePermission) if err != nil { return nil, err } @@ -70,7 +71,7 @@ func (m msgServer) InstantiateContract(goCtx context.Context, msg *types.MsgInst sdk.NewAttribute(sdk.AttributeKeySender, msg.Sender), )) - contractAddr, data, err := m.keeper.Instantiate(ctx, msg.CodeID, senderAddr, adminAddr, msg.Msg, msg.Label, msg.Funds) + contractAddr, data, err := m.contractKeeper.Instantiate(ctx, msg.CodeID, senderAddr, adminAddr, msg.Msg, msg.Label, msg.Funds) if err != nil { return nil, err } @@ -104,7 +105,7 @@ func (m msgServer) InstantiateContract2(goCtx context.Context, msg *types.MsgIns sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName), sdk.NewAttribute(sdk.AttributeKeySender, msg.Sender), )) - contractAddr, data, err := m.keeper.Instantiate2(ctx, msg.CodeID, senderAddr, adminAddr, msg.Msg, msg.Label, msg.Funds, msg.Salt, msg.FixMsg) + contractAddr, data, err := m.contractKeeper.Instantiate2(ctx, msg.CodeID, senderAddr, adminAddr, msg.Msg, msg.Label, msg.Funds, msg.Salt, msg.FixMsg) if err != nil { return nil, err } @@ -136,7 +137,7 @@ func (m msgServer) ExecuteContract(goCtx context.Context, msg *types.MsgExecuteC sdk.NewAttribute(sdk.AttributeKeySender, msg.Sender), )) - data, err := m.keeper.Execute(ctx, contractAddr, senderAddr, msg.Msg, msg.Funds) + data, err := m.contractKeeper.Execute(ctx, contractAddr, senderAddr, msg.Msg, msg.Funds) if err != nil { return nil, err } @@ -167,7 +168,7 @@ func (m msgServer) MigrateContract(goCtx context.Context, msg *types.MsgMigrateC sdk.NewAttribute(sdk.AttributeKeySender, msg.Sender), )) - data, err := m.keeper.Migrate(ctx, contractAddr, senderAddr, msg.CodeID, msg.Msg) + data, err := m.contractKeeper.Migrate(ctx, contractAddr, senderAddr, msg.CodeID, msg.Msg) if err != nil { return nil, err } @@ -202,7 +203,7 @@ func (m msgServer) UpdateAdmin(goCtx context.Context, msg *types.MsgUpdateAdmin) sdk.NewAttribute(sdk.AttributeKeySender, msg.Sender), )) - if err := m.keeper.UpdateContractAdmin(ctx, contractAddr, senderAddr, newAdminAddr); err != nil { + if err := m.contractKeeper.UpdateContractAdmin(ctx, contractAddr, senderAddr, newAdminAddr); err != nil { return nil, err } @@ -230,7 +231,7 @@ func (m msgServer) ClearAdmin(goCtx context.Context, msg *types.MsgClearAdmin) ( sdk.NewAttribute(sdk.AttributeKeySender, msg.Sender), )) - if err := m.keeper.ClearContractAdmin(ctx, contractAddr, senderAddr); err != nil { + if err := m.contractKeeper.ClearContractAdmin(ctx, contractAddr, senderAddr); err != nil { return nil, err } @@ -243,7 +244,7 @@ func (m msgServer) UpdateInstantiateConfig(goCtx context.Context, msg *types.Msg } ctx := sdk.UnwrapSDKContext(goCtx) - if err := m.keeper.SetAccessConfig(ctx, msg.CodeID, sdk.AccAddress(msg.Sender), *msg.NewInstantiatePermission); err != nil { + if err := m.contractKeeper.SetAccessConfig(ctx, msg.CodeID, sdk.AccAddress(msg.Sender), *msg.NewInstantiatePermission); err != nil { return nil, err } ctx.EventManager().EmitEvent(sdk.NewEvent( @@ -254,3 +255,25 @@ func (m msgServer) UpdateInstantiateConfig(goCtx context.Context, msg *types.Msg return &types.MsgUpdateInstantiateConfigResponse{}, nil } + +func (m msgServer) UpdateParams(goCtx context.Context, req *types.MsgUpdateParams) (*types.MsgUpdateParamsResponse, error) { + authority := m.wasmKeeper.authority + if authority != req.Authority { + return nil, sdkerrors.Wrapf(types.ErrInvalid, "invalid authority; expected %s, got %s", authority, req.Authority) + } + + ctx := sdk.UnwrapSDKContext(goCtx) + if err := m.wasmKeeper.SetParams(ctx, req.Params); err != nil { + return nil, err + } + + ctx.EventManager().EmitEvent( + sdk.NewEvent( + sdk.EventTypeMessage, + sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName), + sdk.NewAttribute(sdk.AttributeKeySender, req.Authority), + ), + ) + + return &types.MsgUpdateParamsResponse{}, nil +} diff --git a/x/wasm/keeper/options_test.go b/x/wasm/keeper/options_test.go index 0486d34827..903e0615ff 100644 --- a/x/wasm/keeper/options_test.go +++ b/x/wasm/keeper/options_test.go @@ -8,7 +8,6 @@ import ( authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" vestingtypes "github.com/cosmos/cosmos-sdk/x/auth/vesting/types" bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" - paramtypes "github.com/cosmos/cosmos-sdk/x/params/types" stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -103,7 +102,7 @@ func TestConstructorOptions(t *testing.T) { } for name, spec := range specs { t.Run(name, func(t *testing.T) { - k := NewKeeper(nil, nil, paramtypes.NewSubspace(nil, nil, nil, nil, ""), authkeeper.AccountKeeper{}, &bankkeeper.BaseKeeper{}, stakingkeeper.Keeper{}, nil, nil, nil, nil, nil, nil, nil, "tempDir", types.DefaultWasmConfig(), AvailableCapabilities, spec.srcOpt) + k := NewKeeper(nil, nil, authkeeper.AccountKeeper{}, &bankkeeper.BaseKeeper{}, stakingkeeper.Keeper{}, nil, nil, nil, nil, nil, nil, nil, "tempDir", types.DefaultWasmConfig(), AvailableCapabilities, "", spec.srcOpt) spec.verify(t, k) }) } diff --git a/x/wasm/keeper/test_common.go b/x/wasm/keeper/test_common.go index 8b136be360..950b486406 100644 --- a/x/wasm/keeper/test_common.go +++ b/x/wasm/keeper/test_common.go @@ -423,7 +423,7 @@ func createTestInput( gov.NewAppModule(appCodec, govKeeper, accountKeeper, bankKeeper, subspace(govtypes.ModuleName)), ) am.RegisterServices(module.NewConfigurator(appCodec, msgRouter, querier)) - types.RegisterMsgServer(msgRouter, NewMsgServerImpl(NewDefaultPermissionKeeper(keeper))) + types.RegisterMsgServer(msgRouter, NewMsgServerImpl(NewDefaultPermissionKeeper(keeper), keeper)) types.RegisterQueryServer(querier, NewGrpcQuerier(appCodec, keys[types.ModuleName], keeper, keeper.queryGasLimit)) keepers := TestKeepers{ diff --git a/x/wasm/module.go b/x/wasm/module.go index bafb156133..a50d473d1f 100644 --- a/x/wasm/module.go +++ b/x/wasm/module.go @@ -149,7 +149,7 @@ func (am AppModule) IsAppModule() { // marker func (AppModule) ConsensusVersion() uint64 { return 3 } func (am AppModule) RegisterServices(cfg module.Configurator) { - types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(keeper.NewDefaultPermissionKeeper(am.keeper))) + types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(keeper.NewDefaultPermissionKeeper(am.keeper), *am.keeper)) types.RegisterQueryServer(cfg.QueryServer(), NewQuerier(am.keeper)) m := keeper.NewMigrator(*am.keeper, am.legacySubspace) diff --git a/x/wasm/module_test.go b/x/wasm/module_test.go index d6e17da693..28541fee60 100644 --- a/x/wasm/module_test.go +++ b/x/wasm/module_test.go @@ -20,6 +20,7 @@ import ( "github.com/cosmos/cosmos-sdk/types/module" authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" + paramstypes "github.com/cosmos/cosmos-sdk/x/params/types" stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -51,7 +52,7 @@ func setupTest(t *testing.T) testData { queryRouter.SetInterfaceRegistry(encConf.InterfaceRegistry) serviceRouter.SetInterfaceRegistry(encConf.InterfaceRegistry) data := testData{ - module: NewAppModule(encConf.Marshaler, keepers.WasmKeeper, keepers.StakingKeeper, keepers.AccountKeeper, keepers.BankKeeper, nil), + module: NewAppModule(encConf.Marshaler, keepers.WasmKeeper, keepers.StakingKeeper, keepers.AccountKeeper, keepers.BankKeeper, nil, paramstypes.Subspace{}), ctx: ctx, acctKeeper: keepers.AccountKeeper, keeper: *keepers.WasmKeeper, diff --git a/x/wasm/types/tx.go b/x/wasm/types/tx.go index 045f5341e7..e552ad9c0d 100644 --- a/x/wasm/types/tx.go +++ b/x/wasm/types/tx.go @@ -445,3 +445,27 @@ func (msg MsgUpdateInstantiateConfig) GetSigners() []sdk.AccAddress { } return []sdk.AccAddress{senderAddr} } + +func (msg MsgUpdateParams) Route() string { + return RouterKey +} + +func (msg MsgUpdateParams) Type() string { + return "update-params" +} + +func (msg MsgUpdateParams) GetSigners() []sdk.AccAddress { + authority, err := sdk.AccAddressFromBech32(msg.Authority) + if err != nil { // should never happen as valid basic rejects invalid addresses + panic(err.Error()) + } + return []sdk.AccAddress{authority} +} + +func (msg MsgUpdateParams) GetSignBytes() []byte { + return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg)) +} + +func (msg MsgUpdateParams) ValidateBasic() error { + return msg.Params.ValidateBasic() +}