Skip to content

Commit 355f4d7

Browse files
authored
feat(x/auth/tx): extend app module + docs (#21409)
1 parent 39b61a3 commit 355f4d7

File tree

4 files changed

+83
-35
lines changed

4 files changed

+83
-35
lines changed

x/auth/module.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ type AppModule struct {
5151
// IsAppModule implements the appmodule.AppModule interface.
5252
func (am AppModule) IsAppModule() {}
5353

54-
// NewAppModule creates a new AppModule object
54+
// NewAppModule creates a new AppModule object.
5555
func NewAppModule(
5656
cdc codec.Codec,
5757
accountKeeper keeper.AccountKeeper,

x/auth/tx/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@ This document specifies the `x/auth/tx` package of the Cosmos SDK.
1717

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

20+
It contains as well a depinject module and app module the registration of ante/post handler via `runtime` and tx validator via `runtime/v2`.
21+
2022
## Contents
2123

2224
* [Transactions](#transactions)
2325
* [`TxConfig`](#txconfig)
2426
* [`TxBuilder`](#txbuilder)
2527
* [`TxEncoder`/ `TxDecoder`](#txencoder-txdecoder)
28+
* [Depinject \& App Module](#depinject--app-module)
2629
* [Client](#client)
2730
* [CLI](#cli)
2831
* [gRPC](#grpc)
@@ -57,6 +60,13 @@ A `client.TxBuilder` can be accessed with `TxConfig.NewTxBuilder()`.
5760

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

63+
## Depinject & App Module
64+
65+
The `x/auth/tx/config` contains a depinject module and app module.
66+
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.
67+
68+
The app module is purely there for registering tx validators, due to the design of tx validators (tx validator belong to modules).
69+
6070
## Client
6171

6272
### CLI

x/auth/tx/config/depinject.go

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ import (
1414
bankv1beta1 "cosmossdk.io/api/cosmos/bank/v1beta1"
1515
txconfigv1 "cosmossdk.io/api/cosmos/tx/config/v1"
1616
"cosmossdk.io/core/address"
17-
"cosmossdk.io/core/appmodule"
18-
appmodulev2 "cosmossdk.io/core/appmodule/v2"
17+
"cosmossdk.io/core/appmodule/v2"
1918
"cosmossdk.io/core/transaction"
2019
"cosmossdk.io/depinject"
2120
"cosmossdk.io/depinject/appconfig"
@@ -52,23 +51,24 @@ type ModuleInputs struct {
5251
ProtoFileResolver txsigning.ProtoFileResolver
5352
Environment appmodule.Environment
5453
// BankKeeper is the expected bank keeper to be passed to AnteHandlers
55-
BankKeeper authtypes.BankKeeper `optional:"true"`
56-
MetadataBankKeeper BankKeeper `optional:"true"`
57-
AccountKeeper ante.AccountKeeper `optional:"true"`
58-
FeeGrantKeeper ante.FeegrantKeeper `optional:"true"`
59-
AccountAbstractionKeeper ante.AccountAbstractionKeeper `optional:"true"`
60-
CustomSignModeHandlers func() []txsigning.SignModeHandler `optional:"true"`
61-
CustomGetSigners []txsigning.CustomGetSigner `optional:"true"`
62-
UnorderedTxManager *unorderedtx.Manager `optional:"true"`
54+
BankKeeper authtypes.BankKeeper `optional:"true"`
55+
MetadataBankKeeper BankKeeper `optional:"true"`
56+
AccountKeeper ante.AccountKeeper `optional:"true"`
57+
FeeGrantKeeper ante.FeegrantKeeper `optional:"true"`
58+
AccountAbstractionKeeper ante.AccountAbstractionKeeper `optional:"true"`
59+
CustomSignModeHandlers func() []txsigning.SignModeHandler `optional:"true"`
60+
CustomGetSigners []txsigning.CustomGetSigner `optional:"true"`
61+
UnorderedTxManager *unorderedtx.Manager `optional:"true"`
62+
ExtraTxValidators []appmodule.TxValidator[transaction.Tx] `optional:"true"`
6363
}
6464

6565
type ModuleOutputs struct {
6666
depinject.Out
6767

68+
Module appmodule.AppModule // This is only useful for chains using server/v2. It setup tx validators that don't belong to other modules.
69+
BaseAppOption runtime.BaseAppOption // This is only useful for chains using baseapp. Server/v2 chains use TxValidator.
6870
TxConfig client.TxConfig
6971
TxConfigOptions tx.ConfigOptions
70-
BaseAppOption runtime.BaseAppOption // This is only useful for chains using baseapp. Server/v2 chains use TxValidator.
71-
Module appmodule.AppModule
7272
}
7373

7474
func ProvideProtoRegistry() txsigning.ProtoFileResolver {
@@ -152,9 +152,13 @@ func ProvideModule(in ModuleInputs) ModuleOutputs {
152152
ante.DefaultSigVerificationGasConsumer,
153153
in.AccountAbstractionKeeper,
154154
)
155-
appModule := AppModule{svd}
156155

157-
return ModuleOutputs{TxConfig: txConfig, TxConfigOptions: txConfigOptions, BaseAppOption: baseAppOption, Module: appModule}
156+
return ModuleOutputs{
157+
Module: NewAppModule(svd, in.ExtraTxValidators...),
158+
TxConfig: txConfig,
159+
TxConfigOptions: txConfigOptions,
160+
BaseAppOption: baseAppOption,
161+
}
158162
}
159163

160164
func newAnteHandler(txConfig client.TxConfig, in ModuleInputs) (sdk.AnteHandler, error) {
@@ -235,23 +239,3 @@ func metadataExists(err error) error {
235239

236240
return err
237241
}
238-
239-
var (
240-
_ appmodulev2.AppModule = AppModule{}
241-
_ appmodulev2.HasTxValidator[transaction.Tx] = AppModule{}
242-
)
243-
244-
type AppModule struct {
245-
sigVerification ante.SigVerificationDecorator
246-
}
247-
248-
// TxValidator implements appmodule.HasTxValidator.
249-
func (a AppModule) TxValidator(ctx context.Context, tx transaction.Tx) error {
250-
return a.sigVerification.ValidateTx(ctx, tx)
251-
}
252-
253-
// IsAppModule implements appmodule.AppModule.
254-
func (a AppModule) IsAppModule() {}
255-
256-
// IsOnePerModuleType implements appmodule.AppModule.
257-
func (a AppModule) IsOnePerModuleType() {}

x/auth/tx/config/module.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package tx
2+
3+
import (
4+
"context"
5+
6+
appmodulev2 "cosmossdk.io/core/appmodule/v2"
7+
"cosmossdk.io/core/transaction"
8+
"cosmossdk.io/x/auth/ante"
9+
)
10+
11+
var (
12+
_ appmodulev2.AppModule = AppModule{}
13+
_ appmodulev2.HasTxValidator[transaction.Tx] = AppModule{}
14+
)
15+
16+
// AppModule is a module that only implements tx validators.
17+
// The goal of this module is to allow extensible registration of tx validators provided by chains without requiring a new modules.
18+
// Additionally, it registers tx validators that do not really have a place in other modules.
19+
// This module is only useful for chains using server/v2. Ante/Post handlers are setup via baseapp options in depinject.
20+
type AppModule struct {
21+
sigVerification ante.SigVerificationDecorator
22+
// txValidators contains tx validator that can be injected into the module via depinject.
23+
// tx validators should be module based, but it can happen that you do not want to create a new module
24+
// and simply depinject-it.
25+
txValidators []appmodulev2.TxValidator[transaction.Tx]
26+
}
27+
28+
// NewAppModule creates a new AppModule object.
29+
func NewAppModule(
30+
sigVerification ante.SigVerificationDecorator,
31+
txValidators ...appmodulev2.TxValidator[transaction.Tx],
32+
) AppModule {
33+
return AppModule{
34+
sigVerification: sigVerification,
35+
txValidators: txValidators,
36+
}
37+
}
38+
39+
// IsAppModule implements appmodule.AppModule.
40+
func (a AppModule) IsAppModule() {}
41+
42+
// IsOnePerModuleType implements appmodule.AppModule.
43+
func (a AppModule) IsOnePerModuleType() {}
44+
45+
// TxValidator implements appmodule.HasTxValidator.
46+
func (a AppModule) TxValidator(ctx context.Context, tx transaction.Tx) error {
47+
for _, txValidator := range a.txValidators {
48+
if err := txValidator.ValidateTx(ctx, tx); err != nil {
49+
return err
50+
}
51+
}
52+
53+
return a.sigVerification.ValidateTx(ctx, tx)
54+
}

0 commit comments

Comments
 (0)