Skip to content

Commit 17bb4c7

Browse files
authored
refactor(x/auth): use transaction service (#19967)
1 parent 76bb0cd commit 17bb4c7

20 files changed

+157
-50
lines changed

core/transaction/service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ type ExecMode uint8
99
// For backwards compatibility and easier casting, the exec mode values must be the same as in cosmos/cosmos-sdk/types package.
1010
const (
1111
ExecModeCheck ExecMode = iota
12-
_
12+
ExecModeReCheck
1313
ExecModeSimulate
1414
_
1515
_

simapp/ante.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
3737
ante.NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first
3838
circuitante.NewCircuitBreakerDecorator(options.CircuitKeeper),
3939
ante.NewExtensionOptionsDecorator(options.ExtensionOptionChecker),
40-
ante.NewValidateBasicDecorator(),
40+
ante.NewValidateBasicDecorator(options.AccountKeeper.Environment()),
4141
ante.NewTxTimeoutHeightDecorator(),
42-
ante.NewUnorderedTxDecorator(unorderedtx.DefaultMaxUnOrderedTTL, options.TxManager),
42+
ante.NewUnorderedTxDecorator(unorderedtx.DefaultMaxUnOrderedTTL, options.TxManager, options.AccountKeeper.Environment()),
4343
ante.NewValidateMemoDecorator(options.AccountKeeper),
4444
ante.NewConsumeGasForTxSizeDecorator(options.AccountKeeper),
4545
ante.NewDeductFeeDecorator(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper, options.TxFeeChecker),

simapp/app_config.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,10 @@ var (
198198
Config: appconfig.WrapAny(&slashingmodulev1.Module{}),
199199
},
200200
{
201-
Name: "tx",
202-
Config: appconfig.WrapAny(&txconfigv1.Config{}),
201+
Name: "tx",
202+
Config: appconfig.WrapAny(&txconfigv1.Config{
203+
SkipAnteHandler: true, // SimApp is using non default AnteHandler such as circuit and unorderedtx decorators
204+
}),
203205
},
204206
{
205207
Name: genutiltypes.ModuleName,

simapp/app_di.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"cosmossdk.io/log"
1717
storetypes "cosmossdk.io/store/types"
1818
"cosmossdk.io/x/auth"
19+
"cosmossdk.io/x/auth/ante"
1920
"cosmossdk.io/x/auth/ante/unorderedtx"
2021
authkeeper "cosmossdk.io/x/auth/keeper"
2122
authsims "cosmossdk.io/x/auth/simulation"
@@ -292,13 +293,40 @@ func NewSimApp(
292293
}
293294
}
294295

296+
// set custom ante handlers
297+
app.setCustomAnteHandler()
298+
295299
if err := app.Load(loadLatest); err != nil {
296300
panic(err)
297301
}
298302

299303
return app
300304
}
301305

306+
// overwritte default ante handlers with custom ante handlers
307+
// set SkipAnteHandler to true in app config and set custom ante handler on baseapp
308+
func (app *SimApp) setCustomAnteHandler() {
309+
anteHandler, err := NewAnteHandler(
310+
HandlerOptions{
311+
ante.HandlerOptions{
312+
AccountKeeper: app.AuthKeeper,
313+
BankKeeper: app.BankKeeper,
314+
SignModeHandler: app.txConfig.SignModeHandler(),
315+
FeegrantKeeper: app.FeeGrantKeeper,
316+
SigGasConsumer: ante.DefaultSigVerificationGasConsumer,
317+
},
318+
&app.CircuitBreakerKeeper,
319+
app.UnorderedTxManager,
320+
},
321+
)
322+
if err != nil {
323+
panic(err)
324+
}
325+
326+
// Set the AnteHandler for the app
327+
app.SetAnteHandler(anteHandler)
328+
}
329+
302330
// Close implements the Application interface and closes all necessary application
303331
// resources.
304332
func (app *SimApp) Close() error {

types/context.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ func (c Context) Logger() log.Logger { return c.logge
7777
func (c Context) VoteInfos() []abci.VoteInfo { return c.voteInfo }
7878
func (c Context) GasMeter() storetypes.GasMeter { return c.gasMeter }
7979
func (c Context) BlockGasMeter() storetypes.GasMeter { return c.blockGasMeter }
80-
func (c Context) IsCheckTx() bool { return c.checkTx } // Deprecated: use execMode instead
81-
func (c Context) IsReCheckTx() bool { return c.recheckTx } // Deprecated: use execMode instead
80+
func (c Context) IsCheckTx() bool { return c.checkTx } // Deprecated: use core/transaction service instead
81+
func (c Context) IsReCheckTx() bool { return c.recheckTx } // Deprecated: use core/transaction service instead
8282
func (c Context) IsSigverifyTx() bool { return c.sigverifyTx }
83-
func (c Context) ExecMode() ExecMode { return c.execMode }
83+
func (c Context) ExecMode() ExecMode { return c.execMode } // Deprecated: use core/transaction service instead
8484
func (c Context) MinGasPrices() DecCoins { return c.minGasPrice }
8585
func (c Context) EventManager() EventManagerI { return c.eventManager }
8686
func (c Context) Priority() int64 { return c.priority }

x/auth/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
3333

3434
### Improvements
3535

36+
* [#19967](https://github.com/cosmos/cosmos-sdk/pull/19967) Refactor ante handlers to use `transaction.Service` for getting exec mode.
3637
* [#18780](https://github.com/cosmos/cosmos-sdk/pull/18780) Move sig verification out of the for loop, into the authenticate method.
3738
* [#19188](https://github.com/cosmos/cosmos-sdk/pull/19188) Remove creation of `BaseAccount` when sending a message to an account that does not exist.
3839
* When signing a transaction with an account that has not been created accountnumber 0 must be used

x/auth/ante/ante.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
4242
anteDecorators := []sdk.AnteDecorator{
4343
NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first
4444
NewExtensionOptionsDecorator(options.ExtensionOptionChecker),
45-
NewValidateBasicDecorator(),
45+
NewValidateBasicDecorator(options.AccountKeeper.Environment()),
4646
NewTxTimeoutHeightDecorator(),
4747
NewValidateMemoDecorator(options.AccountKeeper),
4848
NewConsumeGasForTxSizeDecorator(options.AccountKeeper),

x/auth/ante/basic.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package ante
22

33
import (
4+
"cosmossdk.io/core/appmodule/v2"
5+
"cosmossdk.io/core/transaction"
46
errorsmod "cosmossdk.io/errors"
57
storetypes "cosmossdk.io/store/types"
68
"cosmossdk.io/x/auth/migrations/legacytx"
@@ -18,15 +20,20 @@ import (
1820
// If ValidateBasic passes, decorator calls next AnteHandler in chain. Note,
1921
// ValidateBasicDecorator decorator will not get executed on ReCheckTx since it
2022
// is not dependent on application state.
21-
type ValidateBasicDecorator struct{}
23+
type ValidateBasicDecorator struct {
24+
env appmodule.Environment
25+
}
2226

23-
func NewValidateBasicDecorator() ValidateBasicDecorator {
24-
return ValidateBasicDecorator{}
27+
func NewValidateBasicDecorator(env appmodule.Environment) ValidateBasicDecorator {
28+
return ValidateBasicDecorator{
29+
env: env,
30+
}
2531
}
2632

2733
func (vbd ValidateBasicDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, _ bool, next sdk.AnteHandler) (sdk.Context, error) {
2834
// no need to validate basic on recheck tx, call next antehandler
29-
if ctx.ExecMode() == sdk.ExecModeReCheck {
35+
txService := vbd.env.TransactionService
36+
if txService.ExecMode(ctx) == transaction.ExecModeReCheck {
3037
return next(ctx, tx, false)
3138
}
3239

@@ -36,7 +43,7 @@ func (vbd ValidateBasicDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, _ bool,
3643
}
3744
}
3845

39-
return next(ctx, tx, ctx.ExecMode() == sdk.ExecModeSimulate)
46+
return next(ctx, tx, false)
4047
}
4148

4249
// ValidateMemoDecorator will validate memo given the parameters passed in
@@ -69,7 +76,7 @@ func (vmd ValidateMemoDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, _ bool,
6976
}
7077
}
7178

72-
return next(ctx, tx, ctx.ExecMode() == sdk.ExecModeSimulate)
79+
return next(ctx, tx, false)
7380
}
7481

7582
// ConsumeTxSizeGasDecorator will take in parameters and consume gas proportional
@@ -101,7 +108,8 @@ func (cgts ConsumeTxSizeGasDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, _ b
101108
ctx.GasMeter().ConsumeGas(params.TxSizeCostPerByte*storetypes.Gas(len(ctx.TxBytes())), "txSize")
102109

103110
// simulate gas cost for signatures in simulate mode
104-
if ctx.ExecMode() == sdk.ExecModeSimulate {
111+
txService := cgts.ak.Environment().TransactionService
112+
if txService.ExecMode(ctx) == transaction.ExecModeSimulate {
105113
// in simulate mode, each element should be a nil signature
106114
sigs, err := sigTx.GetSignaturesV2()
107115
if err != nil {
@@ -143,7 +151,7 @@ func (cgts ConsumeTxSizeGasDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, _ b
143151
}
144152
}
145153

146-
return next(ctx, tx, ctx.ExecMode() == sdk.ExecModeSimulate)
154+
return next(ctx, tx, false)
147155
}
148156

149157
// isIncompleteSignature tests whether SignatureData is fully filled in for simulation purposes
@@ -206,5 +214,5 @@ func (txh TxTimeoutHeightDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, _ boo
206214
)
207215
}
208216

209-
return next(ctx, tx, ctx.ExecMode() == sdk.ExecModeSimulate)
217+
return next(ctx, tx, false)
210218
}

x/auth/ante/basic_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func TestValidateBasic(t *testing.T) {
3636
invalidTx, err := suite.CreateTestTx(suite.ctx, privs, accNums, accSeqs, suite.ctx.ChainID(), signing.SignMode_SIGN_MODE_DIRECT)
3737
require.NoError(t, err)
3838

39-
vbd := ante.NewValidateBasicDecorator()
39+
vbd := ante.NewValidateBasicDecorator(suite.accountKeeper.Environment())
4040
antehandler := sdk.ChainAnteDecorators(vbd)
4141
_, err = antehandler(suite.ctx, invalidTx, false)
4242

x/auth/ante/expected_keepers.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,26 @@ import (
44
"context"
55

66
"cosmossdk.io/core/address"
7+
"cosmossdk.io/core/appmodule"
78
"cosmossdk.io/x/auth/types"
89

910
sdk "github.com/cosmos/cosmos-sdk/types"
1011
)
1112

13+
type HasEnvironment interface {
14+
Environment() appmodule.Environment
15+
}
16+
1217
// AccountKeeper defines the contract needed for AccountKeeper related APIs.
1318
// Interface provides support to use non-sdk AccountKeeper for AnteHandler's decorators.
1419
type AccountKeeper interface {
1520
GetParams(ctx context.Context) (params types.Params)
1621
GetAccount(ctx context.Context, addr sdk.AccAddress) sdk.AccountI
1722
SetAccount(ctx context.Context, acc sdk.AccountI)
1823
GetModuleAddress(moduleName string) sdk.AccAddress
19-
AddressCodec() address.Codec
2024
NewAccountWithAddress(ctx context.Context, addr sdk.AccAddress) sdk.AccountI
25+
AddressCodec() address.Codec
26+
Environment() appmodule.Environment
2127
}
2228

2329
// FeegrantKeeper defines the expected feegrant keeper.

0 commit comments

Comments
 (0)