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(x/auth/tx): extend app module + docs #21409

Merged
merged 4 commits into from
Aug 28, 2024
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
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.
Comment on lines +65 to +66
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct grammatical error.

The sentence "is there to setup ante/post handlers on an runtime app" contains a grammatical error. Use "a" instead of "an".

Apply this diff to fix the grammatical error:

 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 depinject module is there to setup ante/post handlers on a 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.

Committable suggestion was skipped due to low confidence.

Tools
LanguageTool

[misspelling] ~66-~66: Use “a” instead of ‘an’ if the following word doesn’t start with a vowel sound, e.g. ‘a sentence’, ‘a university’.
Context: ...is there to setup ante/post handlers on an runtime app (via baseapp options) and t...

(EN_A_VS_AN)


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() {}
57 changes: 57 additions & 0 deletions x/auth/tx/config/module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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{}
)

// AppModule is a module that only implements tx validators.
// The goal of this module is to allow extensible registration of tx validators provided by chains without requiring a new modules.
// Additionally, it registers tx validators that do not really have a place in other modules.
// This module is only useful for chains using server/v2. Ante/Post handlers are setup via baseapp options in depinject.
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a check for the presence of txValidators.

The method correctly iterates over the txValidators slice and validates the transaction. However, adding a check for the presence of txValidators before iterating can improve robustness.

Apply this diff to add the check:

 func (a AppModule) TxValidator(ctx context.Context, tx transaction.Tx) error {
+  if len(a.txValidators) == 0 {
+    return nil
+  }
   for _, validator := range a.txValidators {
     if err := validator(ctx, tx); err != nil {
       return err
     }
   }
   return a.sigVerification.ValidateTx(ctx, tx)
 }
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// 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)
func (a AppModule) TxValidator(ctx context.Context, tx transaction.Tx) error {
if len(a.txValidators) == 0 {
return nil
}
for _, validator := range a.txValidators {
if err := validator(ctx, tx); err != nil {
return err
}
}
return a.sigVerification.ValidateTx(ctx, tx)
}

}
Loading