Skip to content

Commit 78a4c7a

Browse files
committed
refactor(x/auth): use transaction service
1 parent cd24915 commit 78a4c7a

17 files changed

+121
-46
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
_

types/context.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ func (c Context) Logger() log.Logger { return c.logge
7878
func (c Context) VoteInfos() []abci.VoteInfo { return c.voteInfo }
7979
func (c Context) GasMeter() storetypes.GasMeter { return c.gasMeter }
8080
func (c Context) BlockGasMeter() storetypes.GasMeter { return c.blockGasMeter }
81-
func (c Context) IsCheckTx() bool { return c.checkTx } // Deprecated: use execMode instead
82-
func (c Context) IsReCheckTx() bool { return c.recheckTx } // Deprecated: use execMode instead
81+
func (c Context) IsCheckTx() bool { return c.checkTx } // Deprecated: use core/transaction service instead
82+
func (c Context) IsReCheckTx() bool { return c.recheckTx } // Deprecated: use core/transaction service instead
8383
func (c Context) IsSigverifyTx() bool { return c.sigverifyTx }
84-
func (c Context) ExecMode() ExecMode { return c.execMode }
84+
func (c Context) ExecMode() ExecMode { return c.execMode } // Deprecated: use core/transaction service instead
8585
func (c Context) MinGasPrices() DecCoins { return c.minGasPrice }
8686
func (c Context) EventManager() EventManagerI { return c.eventManager }
8787
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+
* []() 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),
4646
NewTxTimeoutHeightDecorator(),
4747
NewValidateMemoDecorator(options.AccountKeeper),
4848
NewConsumeGasForTxSizeDecorator(options.AccountKeeper),

x/auth/ante/basic.go

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

33
import (
4+
"cosmossdk.io/core/transaction"
45
errorsmod "cosmossdk.io/errors"
56
storetypes "cosmossdk.io/store/types"
67
"cosmossdk.io/x/auth/migrations/legacytx"
@@ -18,15 +19,20 @@ import (
1819
// If ValidateBasic passes, decorator calls next AnteHandler in chain. Note,
1920
// ValidateBasicDecorator decorator will not get executed on ReCheckTx since it
2021
// is not dependent on application state.
21-
type ValidateBasicDecorator struct{}
22+
type ValidateBasicDecorator struct {
23+
ak AccountKeeper
24+
}
2225

23-
func NewValidateBasicDecorator() ValidateBasicDecorator {
24-
return ValidateBasicDecorator{}
26+
func NewValidateBasicDecorator(ak AccountKeeper) ValidateBasicDecorator {
27+
return ValidateBasicDecorator{
28+
ak: ak,
29+
}
2530
}
2631

2732
func (vbd ValidateBasicDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, _ bool, next sdk.AnteHandler) (sdk.Context, error) {
2833
// no need to validate basic on recheck tx, call next antehandler
29-
if ctx.ExecMode() == sdk.ExecModeReCheck {
34+
txService := vbd.ak.Environment().TransactionService
35+
if txService.ExecMode(ctx) == transaction.ExecModeReCheck {
3036
return next(ctx, tx, false)
3137
}
3238

@@ -36,7 +42,7 @@ func (vbd ValidateBasicDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, _ bool,
3642
}
3743
}
3844

39-
return next(ctx, tx, ctx.ExecMode() == sdk.ExecModeSimulate)
45+
return next(ctx, tx, false)
4046
}
4147

4248
// ValidateMemoDecorator will validate memo given the parameters passed in
@@ -69,7 +75,7 @@ func (vmd ValidateMemoDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, _ bool,
6975
}
7076
}
7177

72-
return next(ctx, tx, ctx.ExecMode() == sdk.ExecModeSimulate)
78+
return next(ctx, tx, false)
7379
}
7480

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

103109
// simulate gas cost for signatures in simulate mode
104-
if ctx.ExecMode() == sdk.ExecModeSimulate {
110+
txService := cgts.ak.Environment().TransactionService
111+
if txService.ExecMode(ctx) == transaction.ExecModeSimulate {
105112
// in simulate mode, each element should be a nil signature
106113
sigs, err := sigTx.GetSignaturesV2()
107114
if err != nil {
@@ -143,7 +150,7 @@ func (cgts ConsumeTxSizeGasDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, _ b
143150
}
144151
}
145152

146-
return next(ctx, tx, ctx.ExecMode() == sdk.ExecModeSimulate)
153+
return next(ctx, tx, false)
147154
}
148155

149156
// isIncompleteSignature tests whether SignatureData is fully filled in for simulation purposes
@@ -206,5 +213,5 @@ func (txh TxTimeoutHeightDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, _ boo
206213
)
207214
}
208215

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

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)
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.

x/auth/ante/export_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package ante
2+
3+
import (
4+
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
5+
sdk "github.com/cosmos/cosmos-sdk/types"
6+
)
7+
8+
var SimSecp256k1PubkeyInternal = simSecp256k1Pubkey
9+
10+
func SetSVDPubKey(svd SigVerificationDecorator, ctx sdk.Context, acc sdk.AccountI, txPubKey cryptotypes.PubKey) error {
11+
return svd.setPubKey(ctx, acc, txPubKey)
12+
}

x/auth/ante/ext.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func (r RejectExtensionOptionsDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx,
4949
return ctx, err
5050
}
5151

52-
return next(ctx, tx, ctx.ExecMode() == sdk.ExecModeSimulate)
52+
return next(ctx, tx, false)
5353
}
5454

5555
func checkExtOpts(tx sdk.Tx, checker ExtensionOptionChecker) error {

x/auth/ante/fee.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"fmt"
66

7+
"cosmossdk.io/core/transaction"
78
errorsmod "cosmossdk.io/errors"
89
"cosmossdk.io/x/auth/types"
910

@@ -45,7 +46,9 @@ func (dfd DeductFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, _ bool, nex
4546
return ctx, errorsmod.Wrap(sdkerrors.ErrTxDecode, "Tx must be a FeeTx")
4647
}
4748

48-
if ctx.ExecMode() != sdk.ExecModeSimulate && ctx.BlockHeight() > 0 && feeTx.GetGas() == 0 {
49+
txService := dfd.accountKeeper.Environment().TransactionService
50+
execMode := txService.ExecMode(ctx)
51+
if execMode != transaction.ExecModeSimulate && ctx.BlockHeight() > 0 && feeTx.GetGas() == 0 {
4952
return ctx, errorsmod.Wrap(sdkerrors.ErrInvalidGasLimit, "must provide positive gas")
5053
}
5154

@@ -55,7 +58,7 @@ func (dfd DeductFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, _ bool, nex
5558
)
5659

5760
fee := feeTx.GetFee()
58-
if ctx.ExecMode() != sdk.ExecModeSimulate {
61+
if execMode != transaction.ExecModeSimulate {
5962
fee, priority, err = dfd.txFeeChecker(ctx, tx)
6063
if err != nil {
6164
return ctx, err
@@ -67,7 +70,7 @@ func (dfd DeductFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, _ bool, nex
6770

6871
newCtx := ctx.WithPriority(priority)
6972

70-
return next(newCtx, tx, ctx.ExecMode() == sdk.ExecModeSimulate)
73+
return next(newCtx, tx, false)
7174
}
7275

7376
func (dfd DeductFeeDecorator) checkDeductFee(ctx sdk.Context, sdkTx sdk.Tx, fee sdk.Coins) error {

0 commit comments

Comments
 (0)