Skip to content

Commit

Permalink
Implement UpdateParams msg
Browse files Browse the repository at this point in the history
  • Loading branch information
pinosu committed Mar 9, 2023
1 parent a364c2e commit e0a720a
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 19 deletions.
4 changes: 2 additions & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,6 @@ func NewWasmApp(
app.WasmKeeper = wasm.NewKeeper(
appCodec,
keys[wasm.StoreKey],
app.GetSubspace(wasm.ModuleName),
app.AccountKeeper,
app.BankKeeper,
app.StakingKeeper,
Expand All @@ -606,6 +605,7 @@ func NewWasmApp(
wasmDir,
wasmConfig,
availableCapabilities,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
wasmOpts...,
)

Expand Down Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion x/wasm/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
45 changes: 34 additions & 11 deletions x/wasm/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand All @@ -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(
Expand All @@ -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
}
3 changes: 1 addition & 2 deletions x/wasm/keeper/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
})
}
Expand Down
2 changes: 1 addition & 1 deletion x/wasm/keeper/test_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion x/wasm/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion x/wasm/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down
24 changes: 24 additions & 0 deletions x/wasm/types/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

0 comments on commit e0a720a

Please sign in to comment.