Skip to content

Commit

Permalink
fix(cwerrors,callback): Added handlers to update module params (#214)
Browse files Browse the repository at this point in the history
Co-authored-by: rockstarRhino <“[email protected]”>
  • Loading branch information
2 people authored and omritoptix committed Dec 27, 2024
1 parent 9aab5e6 commit ec6debf
Show file tree
Hide file tree
Showing 15 changed files with 1,032 additions and 81 deletions.
2 changes: 2 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,7 @@ func NewRollapp(
keys[callbackTypes.StoreKey],
&app.WasmKeeper,
app.BankKeeper,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)

app.CWErrorsKeeper = cwerrorsKeeper.NewKeeper(
Expand All @@ -627,6 +628,7 @@ func NewRollapp(
tkeys[cwerrorsTypes.TStoreKey],
&app.WasmKeeper,
app.BankKeeper,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)

app.TimeUpgradeKeeper = timeupgradekeeper.NewKeeper(
Expand Down
19 changes: 18 additions & 1 deletion proto/rollapp/callback/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ option go_package = "github.com/dymensionxyz/rollapp-wasm/x/callback/types";
import "gogoproto/gogo.proto";
import "cosmos/base/v1beta1/coin.proto";
import "cosmos/msg/v1/msg.proto";
import "cosmos_proto/cosmos.proto";
import "rollapp/callback/v1/callback.proto";

// Msg defines the module messaging service.
Expand All @@ -15,6 +16,9 @@ service Msg {

// CancelCallback defines a message for cancelling an existing callback
rpc CancelCallback(MsgCancelCallback) returns (MsgCancelCallbackResponse);

// UpdateParams is used for updating module params.
rpc UpdateParams(MsgUpdateParams) returns (MsgUpdateParamsResponse);
}

// MsgRequestCallback is the Msg/RequestCallback request type.
Expand Down Expand Up @@ -54,4 +58,17 @@ message MsgCancelCallback{
message MsgCancelCallbackResponse {
// refund is the amount of fees being refunded due to the cancellation of the callback
cosmos.base.v1beta1.Coin refund = 1 [ (gogoproto.nullable) = false ];
}
}

// MsgUpdateParams is the Msg/UpdateParams request type.
message MsgUpdateParams {
option (cosmos.msg.v1.signer) = "authority";

// authority is the address that controls the module (defaults to x/gov unless overwritten).
string authority = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];
// NOTE: All parameters must be supplied.
Params params = 2 [(gogoproto.nullable) = false];
}

// MsgUpdateParamsResponse defines the response structure for executing a MsgUpdateParams message.
message MsgUpdateParamsResponse {}
19 changes: 19 additions & 0 deletions proto/rollapp/cwerrors/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package rollapp.cwerrors.v1;
import "gogoproto/gogo.proto";
import "cosmos/msg/v1/msg.proto";
import "cosmos/base/v1beta1/coin.proto";
import "rollapp/cwerrors/v1/params.proto";
import "cosmos_proto/cosmos.proto";

option go_package = "github.com/dymensionxyz/rollapp-wasm/x/cwerrors/types";

Expand All @@ -13,6 +15,10 @@ service Msg {
// sudo callback on errors
rpc SubscribeToError(MsgSubscribeToError)
returns (MsgSubscribeToErrorResponse);

// UpdateParams is used for updating module params.
rpc UpdateParams(MsgUpdateParams)
returns (MsgUpdateParamsResponse);
}

// MsgSubscribeToError is the Msg/SubscribeToError request type.
Expand All @@ -35,3 +41,16 @@ message MsgSubscribeToErrorResponse {
// valid
int64 subscription_valid_till = 1;
}

// MsgUpdateParams is the Msg/UpdateParams request type.
message MsgUpdateParams {
option (cosmos.msg.v1.signer) = "authority";

// authority is the address that controls the module (defaults to x/gov unless overwritten).
string authority = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];
// NOTE: All parameters must be supplied.
Params params = 2 [(gogoproto.nullable) = false];
}

// MsgUpdateParamsResponse defines the response structure for executing a MsgUpdateParams message.
message MsgUpdateParamsResponse {}
22 changes: 12 additions & 10 deletions x/callback/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@ package keeper

import (
"cosmossdk.io/collections"
"github.com/tendermint/tendermint/libs/log"
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
authTypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/tendermint/tendermint/libs/log"

"github.com/dymensionxyz/rollapp-wasm/internal/collcompat"
"github.com/dymensionxyz/rollapp-wasm/x/callback/types"
)

// Keeper provides module state operations.
type Keeper struct {
cdc codec.Codec
storeKey storetypes.StoreKey
wasmKeeper types.WasmKeeperExpected
bankKeeper types.BankKeeperExpected
authority string // authority is the x/gov module account
cdc codec.Codec
storeKey storetypes.StoreKey
wasmKeeper types.WasmKeeperExpected
bankKeeper types.BankKeeperExpected

Schema collections.Schema

Expand All @@ -28,13 +29,14 @@ type Keeper struct {
}

// NewKeeper creates a new Keeper instance.
func NewKeeper(cdc codec.Codec, storeKey storetypes.StoreKey, wk types.WasmKeeperExpected, bk types.BankKeeperExpected) Keeper {
func NewKeeper(cdc codec.Codec, storeKey storetypes.StoreKey, wk types.WasmKeeperExpected, bk types.BankKeeperExpected, authority string) Keeper {
sb := collections.NewSchemaBuilder(collcompat.NewKVStoreService(storeKey))
k := Keeper{
cdc: cdc,
storeKey: storeKey,
wasmKeeper: wk,
bankKeeper: bk,
cdc: cdc,
storeKey: storeKey,
wasmKeeper: wk,
bankKeeper: bk,
authority: authority,
Params: collections.NewItem(
sb,
types.ParamsKeyPrefix,
Expand Down
20 changes: 20 additions & 0 deletions x/callback/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"

sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

Expand Down Expand Up @@ -128,3 +129,22 @@ func (s MsgServer) RequestCallback(c context.Context, request *types.MsgRequestC

return &types.MsgRequestCallbackResponse{}, nil
}

func (s MsgServer) UpdateParams(goCtx context.Context, msg *types.MsgUpdateParams) (*types.MsgUpdateParamsResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

if s.keeper.authority != msg.Authority {
return nil, errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "invalid authority; expected %s, got %s", s.keeper.authority, msg.Authority)
}

err := msg.ValidateBasic()
if err != nil {
return nil, err
}

err = s.keeper.SetParams(ctx, msg.Params)
if err != nil {
return nil, err
}
return &types.MsgUpdateParamsResponse{}, nil
}
2 changes: 2 additions & 0 deletions x/callback/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ import (
func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
cdc.RegisterConcrete(&MsgRequestCallback{}, "callback/MsgRequestCallback", nil)
cdc.RegisterConcrete(&MsgCancelCallback{}, "callback/MsgCancelCallback", nil)
cdc.RegisterConcrete(&MsgUpdateParams{}, "callback/MsgUpdateParams", nil)
}

// RegisterInterfaces registers interfaces types with the interface registry.
func RegisterInterfaces(registry types.InterfaceRegistry) {
registry.RegisterImplementations((*sdk.Msg)(nil),
&MsgRequestCallback{},
&MsgCancelCallback{},
&MsgUpdateParams{},
)

msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc)
Expand Down
2 changes: 2 additions & 0 deletions x/callback/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const (
StoreKey = ModuleName
// QuerierRoute is the querier route for the module.
QuerierRoute = ModuleName

RouterKey = ModuleName
)

var (
Expand Down
38 changes: 38 additions & 0 deletions x/callback/types/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@ package types

import (
errorsmod "cosmossdk.io/errors"
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkErrors "github.com/cosmos/cosmos-sdk/types/errors"
)

const (
TypeMsgUpdateParams = "update_params"
)

var (
_ sdk.Msg = &MsgRequestCallback{}
_ sdk.Msg = &MsgCancelCallback{}
_ sdk.Msg = &MsgUpdateParams{}
)

// NewMsgRequestCallback creates a new MsgRequestCallback instance.
Expand Down Expand Up @@ -80,3 +86,35 @@ func (m MsgCancelCallback) ValidateBasic() error {

return nil
}

// GetSigners implements types.Msg.
func (m *MsgUpdateParams) GetSigners() []sdk.AccAddress {
addr, _ := sdk.AccAddressFromBech32(m.Authority)
return []sdk.AccAddress{addr}
}

// ValidateBasic implements types.Msg.
func (m *MsgUpdateParams) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(m.Authority); err != nil {
return fmt.Errorf("invalid authority address: %w", err)
}

if err := m.Params.Validate(); err != nil {
return err
}

return nil
}

func (m *MsgUpdateParams) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(m)
return sdk.MustSortJSON(bz)
}

func (m *MsgUpdateParams) Route() string {
return RouterKey
}

func (m *MsgUpdateParams) Type() string {
return TypeMsgUpdateParams
}
Loading

0 comments on commit ec6debf

Please sign in to comment.