cosmos-sdk/x/auth/tx/module/module.go
Julien Robert c859589f2f
feat: app wiring setup for tx's and ante/post handlers (#12193)
## 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)
2022-06-09 23:43:06 +00:00

110 lines
3.1 KiB
Go

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
}