From 17bb4c74fb0395a0d9ad873d03560bb99412389d Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Wed, 10 Apr 2024 20:47:12 +0200 Subject: [PATCH] refactor(x/auth): use transaction service (#19967) --- core/transaction/service.go | 2 +- simapp/ante.go | 4 +-- simapp/app_config.go | 6 ++-- simapp/app_di.go | 28 +++++++++++++++++++ types/context.go | 6 ++-- x/auth/CHANGELOG.md | 1 + x/auth/ante/ante.go | 2 +- x/auth/ante/basic.go | 26 ++++++++++++------ x/auth/ante/basic_test.go | 2 +- x/auth/ante/expected_keepers.go | 8 +++++- x/auth/ante/export_test.go | 12 ++++++++ x/auth/ante/ext.go | 2 +- x/auth/ante/fee.go | 9 ++++-- x/auth/ante/setup.go | 4 +-- x/auth/ante/sigverify.go | 11 ++++---- x/auth/ante/sigverify_internal_test.go | 38 ++++++++++++++++++++------ x/auth/ante/unordered.go | 12 +++++--- x/auth/ante/unordered_test.go | 24 ++++++++++++---- x/auth/ante/validator_tx_fee.go | 2 +- x/auth/keeper/keeper.go | 8 ++++++ 20 files changed, 157 insertions(+), 50 deletions(-) create mode 100644 x/auth/ante/export_test.go diff --git a/core/transaction/service.go b/core/transaction/service.go index bddc2ba4e634..627fb3f85dc3 100644 --- a/core/transaction/service.go +++ b/core/transaction/service.go @@ -9,7 +9,7 @@ type ExecMode uint8 // For backwards compatibility and easier casting, the exec mode values must be the same as in cosmos/cosmos-sdk/types package. const ( ExecModeCheck ExecMode = iota - _ + ExecModeReCheck ExecModeSimulate _ _ diff --git a/simapp/ante.go b/simapp/ante.go index 8fde65c79aee..713c342b8bcf 100644 --- a/simapp/ante.go +++ b/simapp/ante.go @@ -37,9 +37,9 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) { ante.NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first circuitante.NewCircuitBreakerDecorator(options.CircuitKeeper), ante.NewExtensionOptionsDecorator(options.ExtensionOptionChecker), - ante.NewValidateBasicDecorator(), + ante.NewValidateBasicDecorator(options.AccountKeeper.Environment()), ante.NewTxTimeoutHeightDecorator(), - ante.NewUnorderedTxDecorator(unorderedtx.DefaultMaxUnOrderedTTL, options.TxManager), + ante.NewUnorderedTxDecorator(unorderedtx.DefaultMaxUnOrderedTTL, options.TxManager, options.AccountKeeper.Environment()), ante.NewValidateMemoDecorator(options.AccountKeeper), ante.NewConsumeGasForTxSizeDecorator(options.AccountKeeper), ante.NewDeductFeeDecorator(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper, options.TxFeeChecker), diff --git a/simapp/app_config.go b/simapp/app_config.go index 46eadb103a7e..2fae905a537a 100644 --- a/simapp/app_config.go +++ b/simapp/app_config.go @@ -198,8 +198,10 @@ var ( Config: appconfig.WrapAny(&slashingmodulev1.Module{}), }, { - Name: "tx", - Config: appconfig.WrapAny(&txconfigv1.Config{}), + Name: "tx", + Config: appconfig.WrapAny(&txconfigv1.Config{ + SkipAnteHandler: true, // SimApp is using non default AnteHandler such as circuit and unorderedtx decorators + }), }, { Name: genutiltypes.ModuleName, diff --git a/simapp/app_di.go b/simapp/app_di.go index efaca0e3e21f..379e2272c942 100644 --- a/simapp/app_di.go +++ b/simapp/app_di.go @@ -16,6 +16,7 @@ import ( "cosmossdk.io/log" storetypes "cosmossdk.io/store/types" "cosmossdk.io/x/auth" + "cosmossdk.io/x/auth/ante" "cosmossdk.io/x/auth/ante/unorderedtx" authkeeper "cosmossdk.io/x/auth/keeper" authsims "cosmossdk.io/x/auth/simulation" @@ -292,6 +293,9 @@ func NewSimApp( } } + // set custom ante handlers + app.setCustomAnteHandler() + if err := app.Load(loadLatest); err != nil { panic(err) } @@ -299,6 +303,30 @@ func NewSimApp( return app } +// overwritte default ante handlers with custom ante handlers +// set SkipAnteHandler to true in app config and set custom ante handler on baseapp +func (app *SimApp) setCustomAnteHandler() { + anteHandler, err := NewAnteHandler( + HandlerOptions{ + ante.HandlerOptions{ + AccountKeeper: app.AuthKeeper, + BankKeeper: app.BankKeeper, + SignModeHandler: app.txConfig.SignModeHandler(), + FeegrantKeeper: app.FeeGrantKeeper, + SigGasConsumer: ante.DefaultSigVerificationGasConsumer, + }, + &app.CircuitBreakerKeeper, + app.UnorderedTxManager, + }, + ) + if err != nil { + panic(err) + } + + // Set the AnteHandler for the app + app.SetAnteHandler(anteHandler) +} + // Close implements the Application interface and closes all necessary application // resources. func (app *SimApp) Close() error { diff --git a/types/context.go b/types/context.go index 49b25a4f0729..0ebee5f132c5 100644 --- a/types/context.go +++ b/types/context.go @@ -77,10 +77,10 @@ func (c Context) Logger() log.Logger { return c.logge func (c Context) VoteInfos() []abci.VoteInfo { return c.voteInfo } func (c Context) GasMeter() storetypes.GasMeter { return c.gasMeter } func (c Context) BlockGasMeter() storetypes.GasMeter { return c.blockGasMeter } -func (c Context) IsCheckTx() bool { return c.checkTx } // Deprecated: use execMode instead -func (c Context) IsReCheckTx() bool { return c.recheckTx } // Deprecated: use execMode instead +func (c Context) IsCheckTx() bool { return c.checkTx } // Deprecated: use core/transaction service instead +func (c Context) IsReCheckTx() bool { return c.recheckTx } // Deprecated: use core/transaction service instead func (c Context) IsSigverifyTx() bool { return c.sigverifyTx } -func (c Context) ExecMode() ExecMode { return c.execMode } +func (c Context) ExecMode() ExecMode { return c.execMode } // Deprecated: use core/transaction service instead func (c Context) MinGasPrices() DecCoins { return c.minGasPrice } func (c Context) EventManager() EventManagerI { return c.eventManager } func (c Context) Priority() int64 { return c.priority } diff --git a/x/auth/CHANGELOG.md b/x/auth/CHANGELOG.md index 585075a9d39e..1e8fa2a5bdf4 100644 --- a/x/auth/CHANGELOG.md +++ b/x/auth/CHANGELOG.md @@ -33,6 +33,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Improvements +* [#19967](https://github.com/cosmos/cosmos-sdk/pull/19967) Refactor ante handlers to use `transaction.Service` for getting exec mode. * [#18780](https://github.com/cosmos/cosmos-sdk/pull/18780) Move sig verification out of the for loop, into the authenticate method. * [#19188](https://github.com/cosmos/cosmos-sdk/pull/19188) Remove creation of `BaseAccount` when sending a message to an account that does not exist. * When signing a transaction with an account that has not been created accountnumber 0 must be used diff --git a/x/auth/ante/ante.go b/x/auth/ante/ante.go index 657489a0c545..c3e68b92af59 100644 --- a/x/auth/ante/ante.go +++ b/x/auth/ante/ante.go @@ -42,7 +42,7 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) { anteDecorators := []sdk.AnteDecorator{ NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first NewExtensionOptionsDecorator(options.ExtensionOptionChecker), - NewValidateBasicDecorator(), + NewValidateBasicDecorator(options.AccountKeeper.Environment()), NewTxTimeoutHeightDecorator(), NewValidateMemoDecorator(options.AccountKeeper), NewConsumeGasForTxSizeDecorator(options.AccountKeeper), diff --git a/x/auth/ante/basic.go b/x/auth/ante/basic.go index 59e9eca61ee1..37994cad1ec7 100644 --- a/x/auth/ante/basic.go +++ b/x/auth/ante/basic.go @@ -1,6 +1,8 @@ package ante import ( + "cosmossdk.io/core/appmodule/v2" + "cosmossdk.io/core/transaction" errorsmod "cosmossdk.io/errors" storetypes "cosmossdk.io/store/types" "cosmossdk.io/x/auth/migrations/legacytx" @@ -18,15 +20,20 @@ import ( // If ValidateBasic passes, decorator calls next AnteHandler in chain. Note, // ValidateBasicDecorator decorator will not get executed on ReCheckTx since it // is not dependent on application state. -type ValidateBasicDecorator struct{} +type ValidateBasicDecorator struct { + env appmodule.Environment +} -func NewValidateBasicDecorator() ValidateBasicDecorator { - return ValidateBasicDecorator{} +func NewValidateBasicDecorator(env appmodule.Environment) ValidateBasicDecorator { + return ValidateBasicDecorator{ + env: env, + } } func (vbd ValidateBasicDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, _ bool, next sdk.AnteHandler) (sdk.Context, error) { // no need to validate basic on recheck tx, call next antehandler - if ctx.ExecMode() == sdk.ExecModeReCheck { + txService := vbd.env.TransactionService + if txService.ExecMode(ctx) == transaction.ExecModeReCheck { return next(ctx, tx, false) } @@ -36,7 +43,7 @@ func (vbd ValidateBasicDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, _ bool, } } - return next(ctx, tx, ctx.ExecMode() == sdk.ExecModeSimulate) + return next(ctx, tx, false) } // ValidateMemoDecorator will validate memo given the parameters passed in @@ -69,7 +76,7 @@ func (vmd ValidateMemoDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, _ bool, } } - return next(ctx, tx, ctx.ExecMode() == sdk.ExecModeSimulate) + return next(ctx, tx, false) } // ConsumeTxSizeGasDecorator will take in parameters and consume gas proportional @@ -101,7 +108,8 @@ func (cgts ConsumeTxSizeGasDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, _ b ctx.GasMeter().ConsumeGas(params.TxSizeCostPerByte*storetypes.Gas(len(ctx.TxBytes())), "txSize") // simulate gas cost for signatures in simulate mode - if ctx.ExecMode() == sdk.ExecModeSimulate { + txService := cgts.ak.Environment().TransactionService + if txService.ExecMode(ctx) == transaction.ExecModeSimulate { // in simulate mode, each element should be a nil signature sigs, err := sigTx.GetSignaturesV2() if err != nil { @@ -143,7 +151,7 @@ func (cgts ConsumeTxSizeGasDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, _ b } } - return next(ctx, tx, ctx.ExecMode() == sdk.ExecModeSimulate) + return next(ctx, tx, false) } // 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 ) } - return next(ctx, tx, ctx.ExecMode() == sdk.ExecModeSimulate) + return next(ctx, tx, false) } diff --git a/x/auth/ante/basic_test.go b/x/auth/ante/basic_test.go index 21d69d866b44..7546d59e7d62 100644 --- a/x/auth/ante/basic_test.go +++ b/x/auth/ante/basic_test.go @@ -36,7 +36,7 @@ func TestValidateBasic(t *testing.T) { invalidTx, err := suite.CreateTestTx(suite.ctx, privs, accNums, accSeqs, suite.ctx.ChainID(), signing.SignMode_SIGN_MODE_DIRECT) require.NoError(t, err) - vbd := ante.NewValidateBasicDecorator() + vbd := ante.NewValidateBasicDecorator(suite.accountKeeper.Environment()) antehandler := sdk.ChainAnteDecorators(vbd) _, err = antehandler(suite.ctx, invalidTx, false) diff --git a/x/auth/ante/expected_keepers.go b/x/auth/ante/expected_keepers.go index c33dcaa685d3..52bf0acc31f3 100644 --- a/x/auth/ante/expected_keepers.go +++ b/x/auth/ante/expected_keepers.go @@ -4,11 +4,16 @@ import ( "context" "cosmossdk.io/core/address" + "cosmossdk.io/core/appmodule" "cosmossdk.io/x/auth/types" sdk "github.com/cosmos/cosmos-sdk/types" ) +type HasEnvironment interface { + Environment() appmodule.Environment +} + // AccountKeeper defines the contract needed for AccountKeeper related APIs. // Interface provides support to use non-sdk AccountKeeper for AnteHandler's decorators. type AccountKeeper interface { @@ -16,8 +21,9 @@ type AccountKeeper interface { GetAccount(ctx context.Context, addr sdk.AccAddress) sdk.AccountI SetAccount(ctx context.Context, acc sdk.AccountI) GetModuleAddress(moduleName string) sdk.AccAddress - AddressCodec() address.Codec NewAccountWithAddress(ctx context.Context, addr sdk.AccAddress) sdk.AccountI + AddressCodec() address.Codec + Environment() appmodule.Environment } // FeegrantKeeper defines the expected feegrant keeper. diff --git a/x/auth/ante/export_test.go b/x/auth/ante/export_test.go new file mode 100644 index 000000000000..ad990a30d210 --- /dev/null +++ b/x/auth/ante/export_test.go @@ -0,0 +1,12 @@ +package ante + +import ( + cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" + sdk "github.com/cosmos/cosmos-sdk/types" +) + +var SimSecp256k1PubkeyInternal = simSecp256k1Pubkey + +func SetSVDPubKey(svd SigVerificationDecorator, ctx sdk.Context, acc sdk.AccountI, txPubKey cryptotypes.PubKey) error { + return svd.setPubKey(ctx, acc, txPubKey) +} diff --git a/x/auth/ante/ext.go b/x/auth/ante/ext.go index 92fefff3afff..b87a2b8b4f74 100644 --- a/x/auth/ante/ext.go +++ b/x/auth/ante/ext.go @@ -49,7 +49,7 @@ func (r RejectExtensionOptionsDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, return ctx, err } - return next(ctx, tx, ctx.ExecMode() == sdk.ExecModeSimulate) + return next(ctx, tx, false) } func checkExtOpts(tx sdk.Tx, checker ExtensionOptionChecker) error { diff --git a/x/auth/ante/fee.go b/x/auth/ante/fee.go index 018b447c97e3..cf3e54a5b272 100644 --- a/x/auth/ante/fee.go +++ b/x/auth/ante/fee.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" + "cosmossdk.io/core/transaction" errorsmod "cosmossdk.io/errors" "cosmossdk.io/x/auth/types" @@ -45,7 +46,9 @@ func (dfd DeductFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, _ bool, nex return ctx, errorsmod.Wrap(sdkerrors.ErrTxDecode, "Tx must be a FeeTx") } - if ctx.ExecMode() != sdk.ExecModeSimulate && ctx.BlockHeight() > 0 && feeTx.GetGas() == 0 { + txService := dfd.accountKeeper.Environment().TransactionService + execMode := txService.ExecMode(ctx) + if execMode != transaction.ExecModeSimulate && ctx.BlockHeight() > 0 && feeTx.GetGas() == 0 { return ctx, errorsmod.Wrap(sdkerrors.ErrInvalidGasLimit, "must provide positive gas") } @@ -55,7 +58,7 @@ func (dfd DeductFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, _ bool, nex ) fee := feeTx.GetFee() - if ctx.ExecMode() != sdk.ExecModeSimulate { + if execMode != transaction.ExecModeSimulate { fee, priority, err = dfd.txFeeChecker(ctx, tx) if err != nil { return ctx, err @@ -67,7 +70,7 @@ func (dfd DeductFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, _ bool, nex newCtx := ctx.WithPriority(priority) - return next(newCtx, tx, ctx.ExecMode() == sdk.ExecModeSimulate) + return next(newCtx, tx, false) } func (dfd DeductFeeDecorator) checkDeductFee(ctx sdk.Context, sdkTx sdk.Tx, fee sdk.Coins) error { diff --git a/x/auth/ante/setup.go b/x/auth/ante/setup.go index 6ab8fcd4c1d2..66e69fb7b8bf 100644 --- a/x/auth/ante/setup.go +++ b/x/auth/ante/setup.go @@ -67,14 +67,14 @@ func (sud SetUpContextDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, _ bool, } }() - return next(newCtx, tx, ctx.ExecMode() == sdk.ExecModeSimulate) + return next(newCtx, tx, false) } // SetGasMeter returns a new context with a gas meter set from a given context. func SetGasMeter(ctx sdk.Context, gasLimit uint64) sdk.Context { // In various cases such as simulation and during the genesis block, we do not // meter any gas utilization. - if ctx.ExecMode() == sdk.ExecModeSimulate || ctx.BlockHeight() == 0 { + if ctx.ExecMode() == sdk.ExecModeSimulate || ctx.BlockHeight() == 0 { // NOTE: using environment here breaks the API of SetGasMeter, an alternative must be found for server/v2. ref: https://github.com/cosmos/cosmos-sdk/issues/19640 return ctx.WithGasMeter(storetypes.NewInfiniteGasMeter()) } diff --git a/x/auth/ante/sigverify.go b/x/auth/ante/sigverify.go index facc160fcee3..9db1433c0d02 100644 --- a/x/auth/ante/sigverify.go +++ b/x/auth/ante/sigverify.go @@ -10,6 +10,7 @@ import ( secp256k1dcrd "github.com/decred/dcrd/dcrec/secp256k1/v4" "google.golang.org/protobuf/types/known/anypb" + "cosmossdk.io/core/transaction" errorsmod "cosmossdk.io/errors" storetypes "cosmossdk.io/store/types" aa_interface_v1 "cosmossdk.io/x/accounts/interfaces/account_abstraction/v1" @@ -215,7 +216,7 @@ func (svd SigVerificationDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, _ boo ctx.EventManager().EmitEvents(events) - return next(ctx, tx, ctx.ExecMode() == sdk.ExecModeSimulate) + return next(ctx, tx, false) } // authenticate the authentication of the TX for a specific tx signer. @@ -280,7 +281,7 @@ func (svd SigVerificationDecorator) consumeSignatureGas( pubKey cryptotypes.PubKey, signature signing.SignatureV2, ) error { - if ctx.ExecMode() == sdk.ExecModeSimulate && pubKey == nil { + if svd.ak.Environment().TransactionService.ExecMode(ctx) == transaction.ExecModeSimulate && pubKey == nil { pubKey = simSecp256k1Pubkey } @@ -310,7 +311,7 @@ func (svd SigVerificationDecorator) verifySig(ctx sdk.Context, tx sdk.Tx, acc sd // we're in simulation mode, or in ReCheckTx, or context is not // on sig verify tx, then we do not need to verify the signatures // in the tx. - if ctx.ExecMode() == sdk.ExecModeSimulate || ctx.IsReCheckTx() || !ctx.IsSigverifyTx() { + if svd.ak.Environment().TransactionService.ExecMode(ctx) == transaction.ExecModeSimulate || ctx.IsReCheckTx() || !ctx.IsSigverifyTx() { return nil } @@ -384,7 +385,7 @@ func (svd SigVerificationDecorator) setPubKey(ctx sdk.Context, acc sdk.AccountI, if txPubKey == nil { // if we're not in simulation mode, and we do not have a valid pubkey // for this signer, then we simply error. - if ctx.ExecMode() != sdk.ExecModeSimulate { + if svd.ak.Environment().TransactionService.ExecMode(ctx) != transaction.ExecModeSimulate { return fmt.Errorf("the account %s is without a pubkey and did not provide a pubkey in the tx to set it", acc.GetAddress().String()) } // if we're in simulation mode, then we can populate the pubkey with the @@ -479,7 +480,7 @@ func (vscd ValidateSigCountDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, _ b } } - return next(ctx, tx, ctx.ExecMode() == sdk.ExecModeSimulate) + return next(ctx, tx, false) } // DefaultSigVerificationGasConsumer is the default implementation of SignatureVerificationGasConsumer. It consumes gas diff --git a/x/auth/ante/sigverify_internal_test.go b/x/auth/ante/sigverify_internal_test.go index 3408b82f2634..47be009d798a 100644 --- a/x/auth/ante/sigverify_internal_test.go +++ b/x/auth/ante/sigverify_internal_test.go @@ -1,10 +1,14 @@ -package ante +package ante_test import ( + "context" "testing" "github.com/stretchr/testify/require" + "cosmossdk.io/core/appmodule" + "cosmossdk.io/core/transaction" + "cosmossdk.io/x/auth/ante" authcodec "cosmossdk.io/x/auth/codec" authtypes "cosmossdk.io/x/auth/types" @@ -12,8 +16,26 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ) +type mockAccount struct { + ante.AccountKeeper +} + +func (*mockAccount) Environment() appmodule.Environment { + return appmodule.Environment{ + TransactionService: &mockTransactionService{}, + } +} + +type mockTransactionService struct { + transaction.Service +} + +func (*mockTransactionService) ExecMode(ctx context.Context) transaction.ExecMode { + return transaction.ExecMode(sdk.UnwrapSDKContext(ctx).ExecMode()) +} + func TestSigVerify_setPubKey(t *testing.T) { - svd := SigVerificationDecorator{} + svd := ante.NewSigVerificationDecorator(&mockAccount{}, nil, nil, nil) alicePk := secp256k1.GenPrivKey().PubKey() bobPk := secp256k1.GenPrivKey().PubKey() @@ -28,22 +50,22 @@ func TestSigVerify_setPubKey(t *testing.T) { t.Run("on not sig verify tx - skip", func(t *testing.T) { acc := &authtypes.BaseAccount{} ctx = ctx.WithExecMode(sdk.ExecModeSimulate).WithIsSigverifyTx(false) - err := svd.setPubKey(ctx, acc, nil) + err := ante.SetSVDPubKey(svd, ctx, acc, nil) require.NoError(t, err) }) t.Run("on sim, populate with sim key, if pubkey is nil", func(t *testing.T) { acc := &authtypes.BaseAccount{Address: aliceAddr} ctx = ctx.WithExecMode(sdk.ExecModeSimulate).WithIsSigverifyTx(true) - err := svd.setPubKey(ctx, acc, nil) + err := ante.SetSVDPubKey(svd, ctx, acc, nil) require.NoError(t, err) - require.Equal(t, acc.PubKey.GetCachedValue(), simSecp256k1Pubkey) + require.Equal(t, acc.PubKey.GetCachedValue(), ante.SimSecp256k1PubkeyInternal) }) t.Run("on sim, populate with real pub key, if pubkey is not nil", func(t *testing.T) { acc := &authtypes.BaseAccount{Address: aliceAddr} ctx = ctx.WithExecMode(sdk.ExecModeSimulate).WithIsSigverifyTx(true) - err := svd.setPubKey(ctx, acc, alicePk) + err := ante.SetSVDPubKey(svd, ctx, acc, alicePk) require.NoError(t, err) require.Equal(t, acc.PubKey.GetCachedValue(), alicePk) }) @@ -51,7 +73,7 @@ func TestSigVerify_setPubKey(t *testing.T) { t.Run("not on sim, populate the address", func(t *testing.T) { acc := &authtypes.BaseAccount{Address: aliceAddr} ctx = ctx.WithExecMode(sdk.ExecModeFinalize).WithIsSigverifyTx(true) - err := svd.setPubKey(ctx, acc, alicePk) + err := ante.SetSVDPubKey(svd, ctx, acc, alicePk) require.NoError(t, err) require.Equal(t, acc.PubKey.GetCachedValue(), alicePk) }) @@ -59,7 +81,7 @@ func TestSigVerify_setPubKey(t *testing.T) { t.Run("not on sim, fail on invalid pubkey.address", func(t *testing.T) { acc := &authtypes.BaseAccount{Address: aliceAddr} ctx = ctx.WithExecMode(sdk.ExecModeFinalize).WithIsSigverifyTx(true) - err := svd.setPubKey(ctx, acc, bobPk) + err := ante.SetSVDPubKey(svd, ctx, acc, bobPk) require.ErrorContains(t, err, "cannot be claimed") }) } diff --git a/x/auth/ante/unordered.go b/x/auth/ante/unordered.go index 0394701ddf59..193f5545125a 100644 --- a/x/auth/ante/unordered.go +++ b/x/auth/ante/unordered.go @@ -3,6 +3,8 @@ package ante import ( "crypto/sha256" + "cosmossdk.io/core/appmodule/v2" + "cosmossdk.io/core/transaction" errorsmod "cosmossdk.io/errors" "cosmossdk.io/x/auth/ante/unorderedtx" @@ -28,12 +30,14 @@ type UnorderedTxDecorator struct { // maxUnOrderedTTL defines the maximum TTL a transaction can define. maxUnOrderedTTL uint64 txManager *unorderedtx.Manager + env appmodule.Environment } -func NewUnorderedTxDecorator(maxTTL uint64, m *unorderedtx.Manager) *UnorderedTxDecorator { +func NewUnorderedTxDecorator(maxTTL uint64, m *unorderedtx.Manager, env appmodule.Environment) *UnorderedTxDecorator { return &UnorderedTxDecorator{ maxUnOrderedTTL: maxTTL, txManager: m, + env: env, } } @@ -42,7 +46,7 @@ func (d *UnorderedTxDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, _ bool, ne if !ok || !unorderedTx.GetUnordered() { // If the transaction does not implement unordered capabilities or has the // unordered value as false, we bypass. - return next(ctx, tx, ctx.ExecMode() == sdk.ExecModeSimulate) + return next(ctx, tx, false) } // TTL is defined as a specific block height at which this tx is no longer valid @@ -65,10 +69,10 @@ func (d *UnorderedTxDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, _ bool, ne return ctx, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "tx %X is duplicated") } - if ctx.ExecMode() == sdk.ExecModeFinalize { + if d.env.TransactionService.ExecMode(ctx) == transaction.ExecModeFinalize { // a new tx included in the block, add the hash to the unordered tx manager d.txManager.Add(txHash, ttl) } - return next(ctx, tx, ctx.ExecMode() == sdk.ExecModeSimulate) + return next(ctx, tx, false) } diff --git a/x/auth/ante/unordered_test.go b/x/auth/ante/unordered_test.go index 61653ee75a46..5e4ec4a5ead3 100644 --- a/x/auth/ante/unordered_test.go +++ b/x/auth/ante/unordered_test.go @@ -23,7 +23,9 @@ func TestUnorderedTxDecorator_OrderedTx(t *testing.T) { txm.Start() - chain := sdk.ChainAnteDecorators(ante.NewUnorderedTxDecorator(unorderedtx.DefaultMaxUnOrderedTTL, txm)) + suite := SetupTestSuite(t, false) + + chain := sdk.ChainAnteDecorators(ante.NewUnorderedTxDecorator(unorderedtx.DefaultMaxUnOrderedTTL, txm, suite.accountKeeper.Environment())) tx, txBz := genUnorderedTx(t, false, 0) ctx := sdk.Context{}.WithTxBytes(txBz).WithBlockHeight(100) @@ -40,7 +42,9 @@ func TestUnorderedTxDecorator_UnorderedTx_NoTTL(t *testing.T) { txm.Start() - chain := sdk.ChainAnteDecorators(ante.NewUnorderedTxDecorator(unorderedtx.DefaultMaxUnOrderedTTL, txm)) + suite := SetupTestSuite(t, false) + + chain := sdk.ChainAnteDecorators(ante.NewUnorderedTxDecorator(unorderedtx.DefaultMaxUnOrderedTTL, txm, suite.accountKeeper.Environment())) tx, txBz := genUnorderedTx(t, true, 0) ctx := sdk.Context{}.WithTxBytes(txBz).WithBlockHeight(100) @@ -57,7 +61,9 @@ func TestUnorderedTxDecorator_UnorderedTx_InvalidTTL(t *testing.T) { txm.Start() - chain := sdk.ChainAnteDecorators(ante.NewUnorderedTxDecorator(unorderedtx.DefaultMaxUnOrderedTTL, txm)) + suite := SetupTestSuite(t, false) + + chain := sdk.ChainAnteDecorators(ante.NewUnorderedTxDecorator(unorderedtx.DefaultMaxUnOrderedTTL, txm, suite.accountKeeper.Environment())) tx, txBz := genUnorderedTx(t, true, 100+unorderedtx.DefaultMaxUnOrderedTTL+1) ctx := sdk.Context{}.WithTxBytes(txBz).WithBlockHeight(100) @@ -74,7 +80,9 @@ func TestUnorderedTxDecorator_UnorderedTx_AlreadyExists(t *testing.T) { txm.Start() - chain := sdk.ChainAnteDecorators(ante.NewUnorderedTxDecorator(unorderedtx.DefaultMaxUnOrderedTTL, txm)) + suite := SetupTestSuite(t, false) + + chain := sdk.ChainAnteDecorators(ante.NewUnorderedTxDecorator(unorderedtx.DefaultMaxUnOrderedTTL, txm, suite.accountKeeper.Environment())) tx, txBz := genUnorderedTx(t, true, 150) ctx := sdk.Context{}.WithTxBytes(txBz).WithBlockHeight(100) @@ -94,7 +102,9 @@ func TestUnorderedTxDecorator_UnorderedTx_ValidCheckTx(t *testing.T) { txm.Start() - chain := sdk.ChainAnteDecorators(ante.NewUnorderedTxDecorator(unorderedtx.DefaultMaxUnOrderedTTL, txm)) + suite := SetupTestSuite(t, false) + + chain := sdk.ChainAnteDecorators(ante.NewUnorderedTxDecorator(unorderedtx.DefaultMaxUnOrderedTTL, txm, suite.accountKeeper.Environment())) tx, txBz := genUnorderedTx(t, true, 150) ctx := sdk.Context{}.WithTxBytes(txBz).WithBlockHeight(100).WithExecMode(sdk.ExecModeCheck) @@ -111,7 +121,9 @@ func TestUnorderedTxDecorator_UnorderedTx_ValidDeliverTx(t *testing.T) { txm.Start() - chain := sdk.ChainAnteDecorators(ante.NewUnorderedTxDecorator(unorderedtx.DefaultMaxUnOrderedTTL, txm)) + suite := SetupTestSuite(t, false) + + chain := sdk.ChainAnteDecorators(ante.NewUnorderedTxDecorator(unorderedtx.DefaultMaxUnOrderedTTL, txm, suite.accountKeeper.Environment())) tx, txBz := genUnorderedTx(t, true, 150) ctx := sdk.Context{}.WithTxBytes(txBz).WithBlockHeight(100).WithExecMode(sdk.ExecModeFinalize) diff --git a/x/auth/ante/validator_tx_fee.go b/x/auth/ante/validator_tx_fee.go index eb04320c1abe..af4f02fed05f 100644 --- a/x/auth/ante/validator_tx_fee.go +++ b/x/auth/ante/validator_tx_fee.go @@ -24,7 +24,7 @@ func checkTxFeeWithValidatorMinGasPrices(ctx sdk.Context, tx sdk.Tx) (sdk.Coins, // Ensure that the provided fees meet a minimum threshold for the validator, // if this is a CheckTx. This is only for local mempool purposes, and thus // is only ran on check tx. - if ctx.ExecMode() == sdk.ExecModeCheck { + if ctx.ExecMode() == sdk.ExecModeCheck { // NOTE: using environment here breaks the API of fee logic, an alternative must be found for server/v2. ref: https://github.com/cosmos/cosmos-sdk/issues/19640 minGasPrices := ctx.MinGasPrices() if !minGasPrices.IsZero() { requiredFees := make(sdk.Coins, len(minGasPrices)) diff --git a/x/auth/keeper/keeper.go b/x/auth/keeper/keeper.go index 9f03b175fcb4..71492b36760f 100644 --- a/x/auth/keeper/keeper.go +++ b/x/auth/keeper/keeper.go @@ -53,6 +53,9 @@ type AccountKeeperI interface { // AddressCodec returns the account address codec. AddressCodec() address.Codec + + // Environment returns the module's environment. + Environment() appmodule.Environment } func NewAccountIndexes(sb *collections.SchemaBuilder) AccountsIndexes { @@ -275,3 +278,8 @@ func (ak AccountKeeper) GetParams(ctx context.Context) (params types.Params) { } return params } + +// Environment returns the module's environment. +func (ak AccountKeeper) Environment() appmodule.Environment { + return ak.environment +}