Skip to content

Commit

Permalink
feat(templates): remove handler.go (ignite#2771)
Browse files Browse the repository at this point in the history
* remove and modify

* remove modify functions

* update changelog

Co-authored-by: Lucas Btd <[email protected]>
  • Loading branch information
Alex Johnson and lumtis authored Aug 25, 2022
1 parent 7cf6fd8 commit ccfca78
Show file tree
Hide file tree
Showing 16 changed files with 7 additions and 271 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Changes

- Upgraded Cosmos SDK to v0.46.0 and IBC to v5 in CLI and scaffolding templates
- Removed `handler.go` from scaffolded module template

### Features

Expand Down
24 changes: 1 addition & 23 deletions docs/docs/guide/03-blog/00-build-blog.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ The `message` command has created and modified several files:
modify proto/blog/tx.proto
modify x/blog/client/cli/tx.go
create x/blog/client/cli/tx_create_post.go
modify x/blog/handler.go
create x/blog/keeper/msg_server_create_post.go
modify x/blog/module_simulation.go
create x/blog/simulation/create_post.go
Expand Down Expand Up @@ -107,27 +106,6 @@ service Msg {
}
```

Next, look at the `x/blog/handler.go` file. Ignite CLI has added a `case` to the `switch` statement inside the `NewHandler` function. This switch statement is responsible for routing messages and calling specific keeper methods based on the type of the message:

```go
func NewHandler(k keeper.Keeper) sdk.Handler {
//...
return func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) {
//...
switch msg := msg.(type) {
case *types.MsgCreatePost:
res, err := msgServer.CreatePost(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
//...
}
}
}
```

The `case *types.MsgCreatePost` statement handles messages of type `MsgCreatePost`, calls the `CreatePost` method, and returns back the response.

Every module has a handler function like this to process messages and call keeper methods.

## Define messages logic

In the newly scaffolded `x/blog/keeper/msg_server_create_post.go` file, you can see a placeholder implementation of the `CreatePost` function. Right now it does nothing and returns an empty response. For your blog chain, you want the contents of the message (title and body) to be written to the state as a new post.
Expand Down Expand Up @@ -314,7 +292,7 @@ func (k Keeper) AppendPost(ctx sdk.Context, post types.Post) uint64 {

By following these steps, you have implemented all of the code required to create new posts and store them on-chain. Now, when a transaction that contains a message of type `MsgCreatePost` is broadcast, the message is routed to your blog module.

- `x/blog/handler.go` calls `k.CreatePost` which in turn calls `AppendPost`
- `k.CreatePost` calls `AppendPost`
- `AppendPost` gets the number of posts from the store, adds a post using the count as an ID, increments the count, and returns the ID

Now that you have added the functionality to create posts and broadcast them to our chain, you can add querying.
Expand Down
4 changes: 1 addition & 3 deletions docs/docs/guide/03-blog/01-comment-blog.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ The `message` command has created and modified several files:
modify proto/blog/tx.proto
modify x/blog/client/cli/tx.go
create x/blog/client/cli/tx_create_comment.go
modify x/blog/handler.go
create x/blog/keeper/msg_server_create_comment.go
modify x/blog/module_simulation.go
create x/blog/simulation/create_comment.go
Expand Down Expand Up @@ -296,7 +295,7 @@ When you ran the `ignite scaffold list comment --no-message` command, these func

By following these steps, you have implemented all of the code required to create comments and store them on-chain. Now, when a transaction that contains a message of type `MsgCreateComment` is broadcast, the message is routed to your blog module.

- `x/blog/handler.go` calls `k.CreateComment` which in turn calls `AppendComment`.
- `k.CreateComment` calls `AppendComment`.
- `AppendComment` gets the number of comments from the store, adds a comment using the count as an ID, increments the count, and returns the ID.

## Create the delete-comment message
Expand All @@ -317,7 +316,6 @@ The `message` command has created and modified several files:
modify proto/blog/tx.proto
modify x/blog/client/cli/tx.go
create x/blog/client/cli/tx_delete_comment.go
modify x/blog/handler.go
create x/blog/keeper/msg_server_delete_comment.go
modify x/blog/module_simulation.go
create x/blog/simulation/delete_comment.go
Expand Down
5 changes: 0 additions & 5 deletions docs/docs/guide/04-nameservice/02-messages.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ The `ignite scaffold message buy-name name bid` command creates and modifies sev
modify proto/nameservice/tx.proto
modify x/nameservice/client/cli/tx.go
create x/nameservice/client/cli/tx_buy_name.go
modify x/nameservice/handler.go
create x/nameservice/keeper/msg_server_buy_name.go
modify x/nameservice/types/codec.go
create x/nameservice/types/message_buy_name.go
Expand Down Expand Up @@ -141,10 +140,6 @@ These are the changes for each one of these files:

Defines methods to satisfy the `Msg` interface.

- `x/nameservice/handler.go`

Registers the `MsgBuyName` message in the module message handler.

- `x/nameservice/keeper/msg_server_buy_name.go`

Defines the `BuyName` keeper method. You can notice that the message follows the `Msg` interface. The message `struct` contains all the information required when buying a name: `Name`, `Bid`, and `Creator`. This struct was added automatically.
Expand Down
6 changes: 0 additions & 6 deletions docs/docs/guide/05-scavenge/04-messages.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ create x/scavenge/client/cli/query_params.go
create x/scavenge/client/cli/tx.go
create x/scavenge/genesis.go
create x/scavenge/genesis_test.go
create x/scavenge/handler.go
create x/scavenge/keeper/grpc_query.go
create x/scavenge/keeper/grpc_query_params.go
create x/scavenge/keeper/grpc_query_params_test.go
Expand Down Expand Up @@ -75,10 +74,6 @@ The `scaffold message` command does all of these code updates for you:

* Defines methods to satisfy `Msg` interface

* `x/scavenge/handler.go`

* Registers the `MsgSubmitScavenge` message in the module message handler

* `x/scavenge/keeper/msg_server_submit_scavenge.go`

* Defines the `SubmitScavenge` keeper method
Expand Down Expand Up @@ -127,7 +122,6 @@ Because you're using the same `ignite scaffold message` command, the set of modi
modify proto/scavenge/tx.proto
modify x/scavenge/client/cli/tx.go
create x/scavenge/client/cli/tx_commit_solution.go
modify x/scavenge/handler.go
create x/scavenge/keeper/msg_server_commit_solution.go
modify x/scavenge/module_simulation.go
create x/scavenge/simulation/commit_solution.go
Expand Down
4 changes: 2 additions & 2 deletions docs/docs/guide/05-scavenge/06-handlers.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ sidebar_position: 6

# Handlers

For a message to reach a keeper, it has to go through a handler. A handler is where you can apply logic to allow or deny a message to succeed.
For a message to reach a keeper, it has to go through a message server handler. A handler is where you can apply logic to allow or deny a message to succeed.

* If you're familiar with the [Model-view-controller](https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller) (MVC) software architecture, the keeper is a bit like the model, and the handler is a bit like the controller.
* If you're familiar with [React](<https://en.wikipedia.org/wiki/React_(web_framework)>) or [Vue](https://en.wikipedia.org/wiki/Vue.js) architecture, the keeper is a bit like the reducer store and the handler is a bit like actions.

The module-wide message handler is defined in `x/scavenge/handler.go`. Three message types were automatically added to the handler:
Three message types were automatically added to the message server:

* `MsgSubmitScavenge`
* `MsgCommitSolution`
Expand Down
1 change: 0 additions & 1 deletion docs/docs/guide/08-interchange/04-creating-order-books.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ The `send-create-pair` command is used to create order books. This command:

- Creates and broadcasts a transaction with a message of type `SendCreatePair`.
- The message gets routed to the `dex` module.
- The message is processed by the message handler in `x/dex/handler.go`.
- Finally, a `SendCreatePair` keeper method is called.

You need the `send-create-pair` command to do the following:
Expand Down
24 changes: 0 additions & 24 deletions ignite/templates/ibc/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ func NewOracle(replacer placeholder.Replacer, opts *OracleOptions) (*genny.Gener
g.RunFn(moduleOracleModify(replacer, opts))
g.RunFn(protoQueryOracleModify(replacer, opts))
g.RunFn(protoTxOracleModify(replacer, opts))
g.RunFn(handlerTxOracleModify(replacer, opts))
g.RunFn(clientCliQueryOracleModify(replacer, opts))
g.RunFn(clientCliTxOracleModify(replacer, opts))
g.RunFn(codecOracleModify(replacer, opts))
Expand Down Expand Up @@ -227,29 +226,6 @@ message Msg%[2]vDataResponse {
}
}

func handlerTxOracleModify(replacer placeholder.Replacer, opts *OracleOptions) genny.RunFn {
return func(r *genny.Runner) error {
path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "handler.go")
f, err := r.Disk.Find(path)
if err != nil {
return err
}

// Set once the MsgServer definition if it is not defined yet
replacementMsgServer := `msgServer := keeper.NewMsgServerImpl(k)`
content := replacer.ReplaceOnce(f.String(), PlaceholderHandlerMsgServer, replacementMsgServer)

templateHandlers := `case *types.Msg%[2]vData:
res, err := msgServer.%[2]vData(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
%[1]v`
replacementHandlers := fmt.Sprintf(templateHandlers, Placeholder, opts.QueryName.UpperCamel)
content = replacer.Replace(content, Placeholder, replacementHandlers)
newFile := genny.NewFileS(path, content)
return r.File(newFile)
}
}

func clientCliQueryOracleModify(replacer placeholder.Replacer, opts *OracleOptions) genny.RunFn {
return func(r *genny.Runner) error {
path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "client/cli/query.go")
Expand Down
27 changes: 0 additions & 27 deletions ignite/templates/ibc/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ func NewPacket(replacer placeholder.Replacer, opts *PacketOptions) (*genny.Gener
// Add the send message
if !opts.NoMessage {
g.RunFn(protoTxModify(replacer, opts))
g.RunFn(handlerTxModify(replacer, opts))
g.RunFn(clientCliTxModify(replacer, opts))
g.RunFn(codecModify(replacer, opts))
if err := g.Box(messagesTemplate); err != nil {
Expand Down Expand Up @@ -336,32 +335,6 @@ message MsgSend%[2]vResponse {
}
}

func handlerTxModify(replacer placeholder.Replacer, opts *PacketOptions) genny.RunFn {
return func(r *genny.Runner) error {
path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "handler.go")
f, err := r.Disk.Find(path)
if err != nil {
return err
}

// Set once the MsgServer definition if it is not defined yet
replacementMsgServer := `msgServer := keeper.NewMsgServerImpl(k)`
content := replacer.ReplaceOnce(f.String(), PlaceholderHandlerMsgServer, replacementMsgServer)

templateHandlers := `case *types.MsgSend%[2]v:
res, err := msgServer.Send%[2]v(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
%[1]v`
replacementHandlers := fmt.Sprintf(templateHandlers,
Placeholder,
opts.PacketName.UpperCamel,
)
content = replacer.Replace(content, Placeholder, replacementHandlers)
newFile := genny.NewFileS(path, content)
return r.File(newFile)
}
}

func clientCliTxModify(replacer placeholder.Replacer, opts *PacketOptions) genny.RunFn {
return func(r *genny.Runner) error {
path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "client/cli/tx.go")
Expand Down
27 changes: 0 additions & 27 deletions ignite/templates/message/stargate.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
func NewStargate(replacer placeholder.Replacer, opts *Options) (*genny.Generator, error) {
g := genny.New()

g.RunFn(handlerModify(replacer, opts))
g.RunFn(protoTxRPCModify(replacer, opts))
g.RunFn(protoTxMessageModify(replacer, opts))
g.RunFn(typesCodecModify(replacer, opts))
Expand All @@ -42,32 +41,6 @@ func NewStargate(replacer placeholder.Replacer, opts *Options) (*genny.Generator
return g, Box(template, opts, g)
}

func handlerModify(replacer placeholder.Replacer, opts *Options) genny.RunFn {
return func(r *genny.Runner) error {
path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "handler.go")
f, err := r.Disk.Find(path)
if err != nil {
return err
}

// Set once the MsgServer definition if it is not defined yet
replacementMsgServer := `msgServer := keeper.NewMsgServerImpl(k)`
content := replacer.ReplaceOnce(f.String(), PlaceholderHandlerMsgServer, replacementMsgServer)

templateHandlers := `case *types.Msg%[2]v:
res, err := msgServer.%[2]v(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
%[1]v`
replacementHandlers := fmt.Sprintf(templateHandlers,
Placeholder,
opts.MsgName.UpperCamel,
)
content = replacer.Replace(content, Placeholder, replacementHandlers)
newFile := genny.NewFileS(path, content)
return r.File(newFile)
}
}

func protoTxRPCModify(replacer placeholder.Replacer, opts *Options) genny.RunFn {
return func(r *genny.Runner) error {
path := filepath.Join(opts.AppPath, "proto", opts.ModuleName, "tx.proto")
Expand Down
21 changes: 0 additions & 21 deletions ignite/templates/module/create/msgserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/ignite/cli/ignite/pkg/xgenny"
"github.com/ignite/cli/ignite/templates/field/plushhelpers"
"github.com/ignite/cli/ignite/templates/module"
"github.com/ignite/cli/ignite/templates/typed"
)

const msgServiceImport = `"github.com/cosmos/cosmos-sdk/types/msgservice"`
Expand All @@ -26,7 +25,6 @@ func AddMsgServerConventionToLegacyModule(replacer placeholder.Replacer, opts *M
template = xgenny.NewEmbedWalker(fsMsgServer, "msgserver/", opts.AppPath)
)

g.RunFn(handlerPatch(replacer, opts.AppPath, opts.ModuleName))
g.RunFn(codecPath(replacer, opts.AppPath, opts.ModuleName))

if err := g.Box(template); err != nil {
Expand All @@ -47,25 +45,6 @@ func AddMsgServerConventionToLegacyModule(replacer placeholder.Replacer, opts *M
return g, nil
}

func handlerPatch(replacer placeholder.Replacer, appPath, moduleName string) genny.RunFn {
return func(r *genny.Runner) error {
path := filepath.Join(appPath, "x", moduleName, "handler.go")
f, err := r.Disk.Find(path)
if err != nil {
return err
}

// Add the msg server definition placeholder
old := "func NewHandler(k keeper.Keeper) sdk.Handler {"
new := fmt.Sprintf(`%v
%v`, old, typed.PlaceholderHandlerMsgServer)
content := replacer.ReplaceOnce(f.String(), old, new)

newFile := genny.NewFileS(path, content)
return r.File(newFile)
}
}

func codecPath(replacer placeholder.Replacer, appPath, moduleName string) genny.RunFn {
return func(r *genny.Runner) error {
path := filepath.Join(appPath, "x", moduleName, "types/codec.go")
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,13 @@ func NewAppModule(
}

// Deprecated: use RegisterServices
func (am AppModule) Route() sdk.Route {
return sdk.NewRoute(types.RouterKey, NewHandler(am.keeper))
}
func (am AppModule) Route() sdk.Route { return sdk.Route{} }

// Deprecated: use RegisterServices
func (AppModule) QuerierRoute() string { return types.RouterKey }

// Deprecated: use RegisterServices
func (am AppModule) LegacyQuerierHandler(legacyQuerierCdc *codec.LegacyAmino) sdk.Querier {
func (am AppModule) LegacyQuerierHandler(_ *codec.LegacyAmino) sdk.Querier {
return nil
}

Expand Down
Loading

0 comments on commit ccfca78

Please sign in to comment.