Skip to content

Commit

Permalink
feat(x/auth/tx): extend app module + docs
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt committed Aug 26, 2024
1 parent 19e0de5 commit 3a0df27
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 27 deletions.
2 changes: 1 addition & 1 deletion x/auth/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type AppModule struct {
// IsAppModule implements the appmodule.AppModule interface.
func (am AppModule) IsAppModule() {}

// NewAppModule creates a new AppModule object
// NewAppModule creates a new AppModule object.
func NewAppModule(
cdc codec.Codec,
accountKeeper keeper.AccountKeeper,
Expand Down
10 changes: 10 additions & 0 deletions x/auth/tx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@ This document specifies the `x/auth/tx` package of the Cosmos SDK.

This package represents the Cosmos SDK implementation of the `client.TxConfig`, `client.TxBuilder`, `client.TxEncoder` and `client.TxDecoder` interfaces.

It contains as well a depinject module and app module the registration of ante/post handler via `runtime` and tx validator via `runtime/v2`.

## Contents

* [Transactions](#transactions)
* [`TxConfig`](#txconfig)
* [`TxBuilder`](#txbuilder)
* [`TxEncoder`/ `TxDecoder`](#txencoder-txdecoder)
* [Depinject \& App Module](#depinject--app-module)
* [Client](#client)
* [CLI](#cli)
* [gRPC](#grpc)
Expand Down Expand Up @@ -57,6 +60,13 @@ A `client.TxBuilder` can be accessed with `TxConfig.NewTxBuilder()`.

More information about `TxEncoder` and `TxDecoder` can be found [here](https://docs.cosmos.network/main/core/encoding#transaction-encoding).

## Depinject & App Module

The `x/auth/tx/config` contains a depinject module and app module.
The depinject module is there to setup ante/post handlers on an runtime app (via baseapp options) and the tx validator on the runtime/v2 app (via app module). It as well outputs the `TxConfig` and `TxConfigOptions` for the app.

The app module is purely there for registering tx validators, due to the design of tx validators (tx validator belong to modules).

## Client

### CLI
Expand Down
35 changes: 9 additions & 26 deletions x/auth/tx/config/depinject.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import (
txconfigv1 "cosmossdk.io/api/cosmos/tx/config/v1"
"cosmossdk.io/core/address"
"cosmossdk.io/core/appmodule"
appmodulev2 "cosmossdk.io/core/appmodule/v2"
"cosmossdk.io/core/transaction"
"cosmossdk.io/depinject"
"cosmossdk.io/depinject/appconfig"
"cosmossdk.io/x/auth/ante"
Expand Down Expand Up @@ -58,15 +56,16 @@ type ModuleInputs struct {
AccountAbstractionKeeper ante.AccountAbstractionKeeper `optional:"true"`
CustomSignModeHandlers func() []txsigning.SignModeHandler `optional:"true"`
CustomGetSigners []txsigning.CustomGetSigner `optional:"true"`
ExtraTxValidators []TxValidator `optional:"true"`
}

type ModuleOutputs struct {
depinject.Out

Module appmodule.AppModule // This is only useful for chains using server/v2. It setup tx validators that don't belong to other modules.
BaseAppOption runtime.BaseAppOption // This is only useful for chains using baseapp. Server/v2 chains use TxValidator.
TxConfig client.TxConfig
TxConfigOptions tx.ConfigOptions
BaseAppOption runtime.BaseAppOption // This is only useful for chains using baseapp. Server/v2 chains use TxValidator.
Module appmodule.AppModule
}

func ProvideProtoRegistry() txsigning.ProtoFileResolver {
Expand Down Expand Up @@ -150,9 +149,13 @@ func ProvideModule(in ModuleInputs) ModuleOutputs {
ante.DefaultSigVerificationGasConsumer,
in.AccountAbstractionKeeper,
)
appModule := AppModule{svd}

return ModuleOutputs{TxConfig: txConfig, TxConfigOptions: txConfigOptions, BaseAppOption: baseAppOption, Module: appModule}
return ModuleOutputs{
Module: NewAppModule(svd, in.ExtraTxValidators...),
TxConfig: txConfig,
TxConfigOptions: txConfigOptions,
BaseAppOption: baseAppOption,
}
}

func newAnteHandler(txConfig client.TxConfig, in ModuleInputs) (sdk.AnteHandler, error) {
Expand Down Expand Up @@ -232,23 +235,3 @@ func metadataExists(err error) error {

return err
}

var (
_ appmodulev2.AppModule = AppModule{}
_ appmodulev2.HasTxValidator[transaction.Tx] = AppModule{}
)

type AppModule struct {
sigVerification ante.SigVerificationDecorator
}

// TxValidator implements appmodule.HasTxValidator.
func (a AppModule) TxValidator(ctx context.Context, tx transaction.Tx) error {
return a.sigVerification.ValidateTx(ctx, tx)
}

// IsAppModule implements appmodule.AppModule.
func (a AppModule) IsAppModule() {}

// IsOnePerModuleType implements appmodule.AppModule.
func (a AppModule) IsOnePerModuleType() {}
53 changes: 53 additions & 0 deletions x/auth/tx/config/module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package tx

import (
"context"

appmodulev2 "cosmossdk.io/core/appmodule/v2"
"cosmossdk.io/core/transaction"
"cosmossdk.io/x/auth/ante"
)

// TxValidator is appmodulev2.HasTxValidator without the AppModule requirement.
type TxValidator = func(context.Context, transaction.Tx) error

var (
_ appmodulev2.AppModule = AppModule{}
_ appmodulev2.HasTxValidator[transaction.Tx] = AppModule{}
)

type AppModule struct {
sigVerification ante.SigVerificationDecorator
// txValidators contains tx validator that can be injected into the module via depinject.
// tx validators should be module based, but it can happen that you do not want to create a new module
// and simply depinject-it.
txValidators []TxValidator
}

// NewAppModule creates a new AppModule object.
func NewAppModule(
sigVerification ante.SigVerificationDecorator,
txValidators ...TxValidator,
) AppModule {
return AppModule{
sigVerification: sigVerification,
txValidators: txValidators,
}
}

// IsAppModule implements appmodule.AppModule.
func (a AppModule) IsAppModule() {}

// IsOnePerModuleType implements appmodule.AppModule.
func (a AppModule) IsOnePerModuleType() {}

// TxValidator implements appmodule.HasTxValidator.
func (a AppModule) TxValidator(ctx context.Context, tx transaction.Tx) error {
for _, validator := range a.txValidators {
if err := validator(ctx, tx); err != nil {
return err
}
}

return a.sigVerification.ValidateTx(ctx, tx)
}

0 comments on commit 3a0df27

Please sign in to comment.