Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(x/bank/v2): migrate to handlers #21659

Merged
merged 6 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions x/bank/proto/buf.gen.gogo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ plugins:
- name: gocosmos
out: ..
opt: plugins=grpc,Mgoogle/protobuf/any.proto=github.com/cosmos/gogoproto/types/any
- name: grpc-gateway
out: ..
opt: logtostderr=true,allow_colon_final_segments=true
# - name: grpc-gateway ## TODO find a way to exclude v2
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tracked by #21683

# out: ..
# opt: logtostderr=true,allow_colon_final_segments=true
11 changes: 0 additions & 11 deletions x/bank/proto/cosmos/bank/v2/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,11 @@ syntax = "proto3";
package cosmos.bank.v2;

import "gogoproto/gogo.proto";
import "google/api/annotations.proto";
import "cosmos/query/v1/query.proto";
import "amino/amino.proto";
import "cosmos/bank/v2/bank.proto";

option go_package = "cosmossdk.io/x/bank/v2/types";

// Query defines the gRPC querier service.
service Query {
// Params queries the parameters of x/bank module.
rpc Params(QueryParamsRequest) returns (QueryParamsResponse) {
option (cosmos.query.v1.module_query_safe) = true;
option (google.api.http).get = "/cosmos/bank/v2/params";
}
}

// QueryParamsRequest defines the request type for querying x/bank/v2 parameters.
message QueryParamsRequest {}

Expand Down
9 changes: 0 additions & 9 deletions x/bank/proto/cosmos/bank/v2/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,6 @@ import "amino/amino.proto";

option go_package = "cosmossdk.io/x/bank/v2/types";

// Msg defines the bank Msg service.
service Msg {
option (cosmos.msg.v1.service) = true;

// UpdateParams defines a governance operation for updating the x/bank/v2 module parameters.
// The authority is defined in the keeper.
rpc UpdateParams(MsgUpdateParams) returns (MsgUpdateParamsResponse) {}
}

// MsgUpdateParams is the Msg/UpdateParams request type.
message MsgUpdateParams {
option (cosmos.msg.v1.signer) = "authority";
Expand Down
58 changes: 27 additions & 31 deletions x/bank/v2/autocli.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,36 @@
package bankv2

import (
"fmt"

autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
"cosmossdk.io/x/bank/v2/types"

"github.com/cosmos/cosmos-sdk/version"
)

// AutoCLIOptions implements the autocli.HasAutoCLIConfig interface.
func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions {
return &autocliv1.ModuleOptions{
Query: &autocliv1.ServiceCommandDescriptor{
Service: types.Query_serviceDesc.ServiceName,
RpcCommandOptions: []*autocliv1.RpcCommandOptions{
{
RpcMethod: "Params",
Use: "params",
Short: "Query current bank/v2 parameters",
},
},
},
Tx: &autocliv1.ServiceCommandDescriptor{
Service: types.Msg_serviceDesc.ServiceName,
EnhanceCustomCommand: true,
RpcCommandOptions: []*autocliv1.RpcCommandOptions{
{
RpcMethod: "UpdateParams",
Use: "update-params-proposal <params>",
Short: "Submit a proposal to update bank module params. Note: the entire params must be provided.",
Example: fmt.Sprintf(`%s tx bank update-params-proposal '{ }'`, version.AppName),
PositionalArgs: []*autocliv1.PositionalArgDescriptor{{ProtoField: "params"}},
GovProposal: true,
},
},
},
}
return nil // Disable AutoCLI until https://github.com/cosmos/cosmos-sdk/issues/21682 is resolved.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tracked by #21682

// return &autocliv1.ModuleOptions{
// Query: &autocliv1.ServiceCommandDescriptor{
// Service: "cosmos.bank.v2.Query",
// RpcCommandOptions: []*autocliv1.RpcCommandOptions{
// {
// RpcMethod: "Params",
// Use: "params",
// Short: "Query current bank/v2 parameters",
// },
// },
// },
// Tx: &autocliv1.ServiceCommandDescriptor{
// Service: "cosmos.bank.v2.Msg",
// EnhanceCustomCommand: true,
// RpcCommandOptions: []*autocliv1.RpcCommandOptions{
// {
// RpcMethod: "UpdateParams",
// Use: "update-params-proposal <params>",
// Short: "Submit a proposal to update bank module params. Note: the entire params must be provided.",
// Example: fmt.Sprintf(`%s tx bank update-params-proposal '{ }'`, version.AppName),
// PositionalArgs: []*autocliv1.PositionalArgDescriptor{{ProtoField: "params"}},
// GovProposal: true,
// },
// },
// },
// }
}
60 changes: 60 additions & 0 deletions x/bank/v2/keeper/handlers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package keeper

import (
"bytes"
"context"
"errors"
"fmt"

"cosmossdk.io/x/bank/v2/types"
)

type handlers struct {
*Keeper
}

// NewHandlers creates a new bank/v2 handlers
func NewHandlers(k *Keeper) handlers {
return handlers{k}
}

// UpdateParams updates the parameters of the bank/v2 module.
func (h handlers) MsgUpdateParams(ctx context.Context, msg *types.MsgUpdateParams) (*types.MsgUpdateParamsResponse, error) {
authorityBytes, err := h.addressCodec.StringToBytes(msg.Authority)
if err != nil {
return nil, err
}

if !bytes.Equal(h.authority, authorityBytes) {
expectedAuthority, err := h.addressCodec.BytesToString(h.authority)
if err != nil {
return nil, err
}

return nil, fmt.Errorf("invalid authority; expected %s, got %s", expectedAuthority, msg.Authority)
}

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

if err := h.params.Set(ctx, msg.Params); err != nil {
return nil, err
}

return &types.MsgUpdateParamsResponse{}, nil
}

// QueryParams queries the parameters of the bank/v2 module.
func (h handlers) QueryParams(ctx context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
if req == nil {
return nil, errors.New("empty request")
}

params, err := h.params.Get(ctx)
if err != nil {
return nil, err
}

return &types.QueryParamsResponse{Params: params}, nil
}
47 changes: 0 additions & 47 deletions x/bank/v2/keeper/msg_server.go

This file was deleted.

33 changes: 0 additions & 33 deletions x/bank/v2/keeper/query_server.go

This file was deleted.

45 changes: 36 additions & 9 deletions x/bank/v2/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package bankv2
import (
"context"
"encoding/json"
"errors"
"fmt"

"google.golang.org/grpc"
gogoproto "github.com/cosmos/gogoproto/proto"

appmodulev2 "cosmossdk.io/core/appmodule/v2"
"cosmossdk.io/core/registry"
Expand All @@ -22,6 +23,8 @@ var (
_ appmodulev2.AppModule = AppModule{}
_ appmodulev2.HasGenesis = AppModule{}
_ appmodulev2.HasRegisterInterfaces = AppModule{}
_ appmodulev2.HasQueryHandlers = AppModule{}
_ appmodulev2.HasMsgHandlers = AppModule{}
)

// AppModule implements an application module for the bank module.
Expand Down Expand Up @@ -53,14 +56,6 @@ func (AppModule) RegisterInterfaces(registrar registry.InterfaceRegistrar) {
types.RegisterInterfaces(registrar)
}

// RegisterServices registers module services.
func (am AppModule) RegisterServices(registrar grpc.ServiceRegistrar) error {
types.RegisterMsgServer(registrar, keeper.NewMsgServer(am.keeper))
types.RegisterQueryServer(registrar, keeper.NewQuerier(am.keeper))

return nil
}

// DefaultGenesis returns default genesis state as raw bytes for the bank module.
func (am AppModule) DefaultGenesis() json.RawMessage {
return am.cdc.MustMarshalJSON(types.DefaultGenesisState())
Expand Down Expand Up @@ -95,3 +90,35 @@ func (am AppModule) ExportGenesis(ctx context.Context) (json.RawMessage, error)

return am.cdc.MarshalJSON(gs)
}

// RegisterMsgHandlers registers the message handlers for the bank module.
func (am AppModule) RegisterMsgHandlers(router appmodulev2.MsgRouter) {
handlers := keeper.NewHandlers(am.keeper)

var errs error
if err := appmodulev2.RegisterHandler(
router, gogoproto.MessageName(&types.MsgUpdateParams{}), handlers.MsgUpdateParams,
); err != nil {
errs = errors.Join(errs, err)
}

if errs != nil {
panic(errs)
}
}

// RegisterQueryHandlers registers the query handlers for the bank module.
func (am AppModule) RegisterQueryHandlers(router appmodulev2.QueryRouter) {
handlers := keeper.NewHandlers(am.keeper)

var errs error
if err := appmodulev2.RegisterHandler(
router, gogoproto.MessageName(&types.QueryParamsRequest{}), handlers.QueryParams,
); err != nil {
errs = errors.Join(errs, err)
}

if errs != nil {
panic(errs)
}
}
4 changes: 0 additions & 4 deletions x/bank/v2/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@ package types
import (
"cosmossdk.io/core/registry"
"cosmossdk.io/core/transaction"

"github.com/cosmos/cosmos-sdk/types/msgservice"
)

func RegisterInterfaces(registrar registry.InterfaceRegistrar) {
registrar.RegisterImplementations((*transaction.Msg)(nil),
&MsgUpdateParams{},
)

msgservice.RegisterMsgServiceDesc(registrar, &_Msg_serviceDesc)
}
Loading
Loading