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

feat: app wiring setup for tx's and ante/post handlers #12193

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
631 changes: 631 additions & 0 deletions api/cosmos/tx/module/v1/module.pulsar.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion core/appmodule/doc.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Package appmodule defines the functionality for registering Cosmos SDK app
// modules that are assembled using the github.com/cosmos/cosmos-sdk/depinject v1.0.0-alpha.3
// modules that are assembled using the pkg.go.dev/github.com/cosmos/cosmos-sdk/depinject
julienrbrt marked this conversation as resolved.
Show resolved Hide resolved
// dependency injection system and the declarative app configuration format
// handled by the appconfig package.
package appmodule
2 changes: 1 addition & 1 deletion core/appmodule/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (f funcOption) apply(initializer *internal.ModuleInitializer) error {
}

// Provide registers providers with the dependency injection system that will be
// run within the module scope. See github.com/cosmos/cosmos-sdk/depinject v1.0.0-alpha.3 for
// run within the module scope. See pkg.go.dev/github.com/cosmos/cosmos-sdk/depinject for
julienrbrt marked this conversation as resolved.
Show resolved Hide resolved
// documentation on the dependency injection system.
func Provide(providers ...interface{}) Option {
return funcOption(func(initializer *internal.ModuleInitializer) error {
Expand Down
14 changes: 14 additions & 0 deletions proto/cosmos/tx/module/v1/module.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
syntax = "proto3";

package cosmos.tx.module.v1;

import "cosmos/app/v1alpha1/module.proto";

// Module is the config object of the tx module.
message Module {
// enable_ante_handler defines whether the ante handler are set in the app.
bool enable_ante_handler = 1;

// enable_post_handler defines whether the post handler are set in the app.
bool enable_post_handler = 2;
}
2 changes: 1 addition & 1 deletion runtime/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (a *AppBuilder) Build(
for _, option := range a.app.baseAppOptions {
baseAppOptions = append(baseAppOptions, option)
}
// TODO: when the auth module is configured, fill-in txDecoder

bApp := baseapp.NewBaseApp(a.app.config.AppName, logger, db, nil, baseAppOptions...)
bApp.SetMsgServiceRouter(msgServiceRouter)
bApp.SetCommitMultiStoreTracer(traceStore)
Expand Down
55 changes: 3 additions & 52 deletions simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/runtime"
"github.com/cosmos/cosmos-sdk/server"
"github.com/cosmos/cosmos-sdk/server/api"
"github.com/cosmos/cosmos-sdk/server/config"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
Expand All @@ -34,9 +33,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
"github.com/cosmos/cosmos-sdk/x/auth/posthandler"
authsims "github.com/cosmos/cosmos-sdk/x/auth/simulation"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/auth/vesting"
Expand Down Expand Up @@ -213,6 +210,7 @@ func NewSimApp(

var appBuilder *runtime.AppBuilder
var msgServiceRouter *baseapp.MsgServiceRouter
var txOptions func(*baseapp.BaseApp)
julienrbrt marked this conversation as resolved.
Show resolved Hide resolved

if err := depinject.Inject(AppConfig,
&appBuilder,
Expand All @@ -227,11 +225,12 @@ func NewSimApp(
&app.StakingKeeper,
&app.NFTKeeper,
&msgServiceRouter,
&txOptions,
); err != nil {
panic(err)
}

app.App = appBuilder.Build(logger, db, traceStore, msgServiceRouter, baseAppOptions...)
app.App = appBuilder.Build(logger, db, traceStore, msgServiceRouter, append(baseAppOptions, txOptions)...)
julienrbrt marked this conversation as resolved.
Show resolved Hide resolved

app.keys = sdk.NewKVStoreKeys(
minttypes.StoreKey, distrtypes.StoreKey,
Expand Down Expand Up @@ -381,23 +380,7 @@ func NewSimApp(
app.MountMemoryStores(app.memKeys)

// initialize BaseApp
app.SetTxDecoder(encodingConfig.TxConfig.TxDecoder())
app.SetInitChainer(app.InitChainer)
app.setAnteHandler(encodingConfig.TxConfig, cast.ToStringSlice(appOpts.Get(server.FlagIndexEvents)))
// In v0.46, the SDK introduces _postHandlers_. PostHandlers are like
// antehandlers, but are run _after_ the `runMsgs` execution. They are also
// defined as a chain, and have the same signature as antehandlers.
//
// In baseapp, postHandlers are run in the same store branch as `runMsgs`,
// meaning that both `runMsgs` and `postHandler` state will be committed if
// both are successful, and both will be reverted if any of the two fails.
//
// The SDK exposes a default empty postHandlers chain.
//
// Please note that changing any of the anteHandler or postHandler chain is
// likely to be a state-machine breaking change, which needs a coordinated
// upgrade.
app.setPostHandler()

if err := app.Load(loadLatest); err != nil {
panic(err)
Expand All @@ -406,38 +389,6 @@ func NewSimApp(
return app
}

func (app *SimApp) setAnteHandler(txConfig client.TxConfig, indexEventsStr []string) {
indexEvents := map[string]struct{}{}
for _, e := range indexEventsStr {
indexEvents[e] = struct{}{}
}
anteHandler, err := ante.NewAnteHandler(
ante.HandlerOptions{
AccountKeeper: app.AccountKeeper,
BankKeeper: app.BankKeeper,
SignModeHandler: txConfig.SignModeHandler(),
FeegrantKeeper: app.FeeGrantKeeper,
SigGasConsumer: ante.DefaultSigVerificationGasConsumer,
},
)
if err != nil {
panic(err)
}

app.SetAnteHandler(anteHandler)
}

func (app *SimApp) setPostHandler() {
postHandler, err := posthandler.NewPostHandler(
posthandler.HandlerOptions{},
)
if err != nil {
panic(err)
}

app.SetPostHandler(postHandler)
}

// Name returns the name of the App
func (app *SimApp) Name() string { return app.BaseApp.Name() }

Expand Down
6 changes: 6 additions & 0 deletions simapp/app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ modules:
config:
"@type": cosmos.params.module.v1.Module

- name: tx
config:
"@type": cosmos.tx.module.v1.Module
enable_ante_handler: true
enable_post_handler: true

- name: feegrant
config:
"@type": cosmos.feegrant.module.v1.Module
Expand Down
106 changes: 106 additions & 0 deletions types/tx/module/module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package tx
julienrbrt marked this conversation as resolved.
Show resolved Hide resolved

import (
"fmt"

modulev1 "cosmossdk.io/api/cosmos/tx/module/v1"
"cosmossdk.io/core/appmodule"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/depinject"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
"github.com/cosmos/cosmos-sdk/x/auth/posthandler"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
feegrantkeeper "github.com/cosmos/cosmos-sdk/x/feegrant/keeper"
)

//
// New App Wiring Setup
//

func init() {
appmodule.Register(&modulev1.Module{},
appmodule.Provide(provideModule),
)
}

type txInputs struct {
depinject.In

Config *modulev1.Module

TxConfig client.TxConfig

AccountKeeper authkeeper.AccountKeeper `key:"cosmos.auth.v1.AccountKeeper"`
BankKeeper bankkeeper.Keeper `key:"cosmos.bank.v1.Keeper"`
FeeGrantKeeper feegrantkeeper.Keeper `key:"cosmos.feegrant.v1.Keeper"`
}

type txOutputs struct {
depinject.Out

TxConfig client.TxConfig
BaseAppOption func(app *baseapp.BaseApp)
}
julienrbrt marked this conversation as resolved.
Show resolved Hide resolved

func provideModule(in txInputs) txOutputs {
baseAppOption := func(app *baseapp.BaseApp) {

if in.Config.EnableAnteHandler {
// AnteHandlers
anteHandler, err := newAnteHandler(in)
if err != nil {
panic(err)
}
app.SetAnteHandler(anteHandler)
}

if in.Config.EnablePostHandler {
// PostHandlers
// In v0.46, the SDK introduces _postHandlers_. PostHandlers are like
// antehandlers, but are run _after_ the `runMsgs` execution. They are also
// defined as a chain, and have the same signature as antehandlers.
//
// In baseapp, postHandlers are run in the same store branch as `runMsgs`,
// meaning that both `runMsgs` and `postHandler` state will be committed if
// both are successful, and both will be reverted if any of the two fails.
//
// The SDK exposes a default empty postHandlers chain.
//
// Please note that changing any of the anteHandler or postHandler chain is
// likely to be a state-machine breaking change, which needs a coordinated
// upgrade.
postHandler, err := posthandler.NewPostHandler(
posthandler.HandlerOptions{},
)
if err != nil {
panic(err)
}
app.SetPostHandler(postHandler)
}

// TxDecoder
app.SetTxDecoder(in.TxConfig.TxDecoder())
}

return txOutputs{TxConfig: in.TxConfig, BaseAppOption: baseAppOption}
}

func newAnteHandler(in txInputs) (sdk.AnteHandler, error) {
anteHandler, err := ante.NewAnteHandler(
ante.HandlerOptions{
AccountKeeper: in.AccountKeeper,
BankKeeper: in.BankKeeper,
SignModeHandler: in.TxConfig.SignModeHandler(),
FeegrantKeeper: in.FeeGrantKeeper,
SigGasConsumer: ante.DefaultSigVerificationGasConsumer,
},
)
if err != nil {
return nil, fmt.Errorf("failed to create ante handler: %w", err)
}

return anteHandler, nil
}