Skip to content

Commit

Permalink
feat: app wiring setup for tx's and ante/post handlers (#12193)
Browse files Browse the repository at this point in the history
## Description

Closes: #12056



---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
  • Loading branch information
julienrbrt authored Jun 9, 2022
1 parent dd2e432 commit c859589
Show file tree
Hide file tree
Showing 9 changed files with 772 additions and 58 deletions.
633 changes: 633 additions & 0 deletions api/cosmos/tx/module/v1/module.pulsar.go

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions baseapp/block_gas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ func TestBaseApp_BlockGas(t *testing.T) {
return &sdk.Result{}, nil
}))
}

encCfg := simapp.MakeTestEncodingConfig()
encCfg.Amino.RegisterConcrete(&testdata.TestMsg{}, "testdata.TestMsg", nil)
encCfg.InterfaceRegistry.RegisterImplementations((*sdk.Msg)(nil),
app = simapp.NewSimApp(log.NewNopLogger(), dbm.NewMemDB(), nil, true, map[int64]bool{}, "", 0, encCfg, simapp.EmptyAppOptions{}, routerOpt)
app.InterfaceRegistry().RegisterImplementations((*sdk.Msg)(nil),
&testdata.TestMsg{},
)
app = simapp.NewSimApp(log.NewNopLogger(), dbm.NewMemDB(), nil, true, map[int64]bool{}, "", 0, encCfg, simapp.EmptyAppOptions{}, routerOpt)
genState := simapp.GenesisStateWithSingleValidator(t, app)
stateBytes, err := tmjson.MarshalIndent(genState, "", " ")
require.NoError(t, err)
Expand Down
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 cosmossdk.io/depinject
// 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 cosmossdk.io/depinject for
// documentation on the dependency injection system.
func Provide(providers ...interface{}) Option {
return funcOption(func(initializer *internal.ModuleInitializer) error {
Expand Down
18 changes: 18 additions & 0 deletions proto/cosmos/tx/module/v1/module.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
syntax = "proto3";

package cosmos.tx.module.v1;

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

// Module is the config object of the tx module.
message Module {
option (cosmos.app.v1alpha1.module) = {
go_import: "github.com/cosmos/cosmos-sdk/x/auth/tx"
};

// skip_ante_handler defines whether the ante handler registration should be skipped in case an app wants to override this functionality.
bool skip_ante_handler = 1;

// skip_post_handler defines whether the post handler registration should be skipped in case an app wants to override this functionality.
bool skip_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
52 changes: 1 addition & 51 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,10 +33,9 @@ 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"
_ "github.com/cosmos/cosmos-sdk/x/auth/tx/module" // import for side-effects
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/auth/vesting"
vestingtypes "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
Expand Down Expand Up @@ -377,23 +375,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 @@ -402,38 +384,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: 5 additions & 1 deletion simapp/app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,18 @@ modules:
config:
"@type": cosmos.params.module.v1.Module

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

- name: feegrant
config:
"@type": cosmos.feegrant.module.v1.Module

- name: bank
config:
"@type": cosmos.bank.module.v1.Module

- name: authz
config:
"@type": cosmos.authz.module.v1.Module
Expand Down
109 changes: 109 additions & 0 deletions x/auth/tx/module/module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package tx

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/codec"
"github.com/cosmos/cosmos-sdk/depinject"
"github.com/cosmos/cosmos-sdk/runtime"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
"github.com/cosmos/cosmos-sdk/x/auth/posthandler"
"github.com/cosmos/cosmos-sdk/x/auth/tx"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
feegrantkeeper "github.com/cosmos/cosmos-sdk/x/feegrant/keeper"
)

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

type txInputs struct {
depinject.In

Config *modulev1.Module
ProtoCodecMarshaler codec.ProtoCodecMarshaler

AccountKeeper ante.AccountKeeper `key:"cosmos.auth.v1.AccountKeeper" optional:"true"`
BankKeeper authtypes.BankKeeper `key:"cosmos.bank.v1.Keeper" optional:"true"`
FeeGrantKeeper feegrantkeeper.Keeper `key:"cosmos.feegrant.v1.Keeper" optional:"true"`
}

type txOutputs struct {
depinject.Out

TxConfig client.TxConfig
BaseAppOption runtime.BaseAppOption
}

func provideModule(in txInputs) txOutputs {
txConfig := tx.NewTxConfig(in.ProtoCodecMarshaler, tx.DefaultSignModes)

baseAppOption := func(app *baseapp.BaseApp) {

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

// PostHandlers
if !in.Config.SkipPostHandler {
// 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(txConfig.TxDecoder())
}

return txOutputs{TxConfig: txConfig, BaseAppOption: baseAppOption}
}

func newAnteHandler(txConfig client.TxConfig, in txInputs) (sdk.AnteHandler, error) {
if in.BankKeeper == nil {
return nil, fmt.Errorf("both AccountKeeper and BankKeeper are required")
}

anteHandler, err := ante.NewAnteHandler(
ante.HandlerOptions{
AccountKeeper: in.AccountKeeper,
BankKeeper: in.BankKeeper,
SignModeHandler: 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
}

0 comments on commit c859589

Please sign in to comment.