From 975ddc1d3146069c7e3a3df078cddfc2e136be17 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Thu, 29 Feb 2024 09:39:03 +0100 Subject: [PATCH] refactor(x/auth/ante): don't use simulate bool in ante handler (#19586) Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- types/handler.go | 26 +++++++++++++------------- types/handler_test.go | 4 ++-- x/auth/ante/basic.go | 20 ++++++++++---------- x/auth/ante/ext.go | 4 ++-- x/auth/ante/fee.go | 4 ++-- x/auth/ante/setup.go | 4 ++-- x/auth/ante/sigverify.go | 8 ++++---- x/auth/ante/unordered.go | 6 +++--- 8 files changed, 38 insertions(+), 38 deletions(-) diff --git a/types/handler.go b/types/handler.go index 42774086c2..28873703bd 100644 --- a/types/handler.go +++ b/types/handler.go @@ -6,23 +6,23 @@ package types // is required for the remainder of the AnteHandler execution, a new Context should // be created off of the provided Context and returned as . // -// The simulate argument is provided to indicate if the AnteHandler is being executed -// in simulation mode, which attempts to estimate a gas cost for the tx. Any state -// modifications made will be discarded if simulate is true. -type AnteHandler func(ctx Context, tx Tx, simulate bool) (newCtx Context, err error) +// When exec module is in simulation mode (ctx.ExecMode() == ExecModeSimulate), it indicates if the AnteHandler is +// being executed in simulation mode, which attempts to estimate a gas cost for the tx. +// Any state modifications made will be discarded in simulation mode. +type AnteHandler func(ctx Context, tx Tx, _ bool) (newCtx Context, err error) // PostHandler like AnteHandler but it executes after RunMsgs. Runs on success // or failure and enables use cases like gas refunding. -type PostHandler func(ctx Context, tx Tx, simulate, success bool) (newCtx Context, err error) +type PostHandler func(ctx Context, tx Tx, _, success bool) (newCtx Context, err error) // AnteDecorator wraps the next AnteHandler to perform custom pre-processing. type AnteDecorator interface { - AnteHandle(ctx Context, tx Tx, simulate bool, next AnteHandler) (newCtx Context, err error) + AnteHandle(ctx Context, tx Tx, _ bool, next AnteHandler) (newCtx Context, err error) } // PostDecorator wraps the next PostHandler to perform custom post-processing. type PostDecorator interface { - PostHandle(ctx Context, tx Tx, simulate, success bool, next PostHandler) (newCtx Context, err error) + PostHandle(ctx Context, tx Tx, _, success bool, next PostHandler) (newCtx Context, err error) } // ChainAnteDecorators ChainDecorator chains AnteDecorators together with each AnteDecorator @@ -46,13 +46,13 @@ func ChainAnteDecorators(chain ...AnteDecorator) AnteHandler { handlerChain := make([]AnteHandler, len(chain)+1) // set the terminal AnteHandler decorator - handlerChain[len(chain)] = func(ctx Context, tx Tx, simulate bool) (Context, error) { + handlerChain[len(chain)] = func(ctx Context, tx Tx, _ bool) (Context, error) { return ctx, nil } for i := 0; i < len(chain); i++ { ii := i - handlerChain[ii] = func(ctx Context, tx Tx, simulate bool) (Context, error) { - return chain[ii].AnteHandle(ctx, tx, simulate, handlerChain[ii+1]) + handlerChain[ii] = func(ctx Context, tx Tx, _ bool) (Context, error) { + return chain[ii].AnteHandle(ctx, tx, ctx.ExecMode() == ExecModeSimulate, handlerChain[ii+1]) } } @@ -74,13 +74,13 @@ func ChainPostDecorators(chain ...PostDecorator) PostHandler { handlerChain := make([]PostHandler, len(chain)+1) // set the terminal PostHandler decorator - handlerChain[len(chain)] = func(ctx Context, tx Tx, simulate, success bool) (Context, error) { + handlerChain[len(chain)] = func(ctx Context, tx Tx, _, success bool) (Context, error) { return ctx, nil } for i := 0; i < len(chain); i++ { ii := i - handlerChain[ii] = func(ctx Context, tx Tx, simulate, success bool) (Context, error) { - return chain[ii].PostHandle(ctx, tx, simulate, success, handlerChain[ii+1]) + handlerChain[ii] = func(ctx Context, tx Tx, _, success bool) (Context, error) { + return chain[ii].PostHandle(ctx, tx, ctx.ExecMode() == ExecModeSimulate, success, handlerChain[ii+1]) } } return handlerChain[0] diff --git a/types/handler_test.go b/types/handler_test.go index 3d20d3023d..21eba84a90 100644 --- a/types/handler_test.go +++ b/types/handler_test.go @@ -14,7 +14,7 @@ func TestChainAnteDecorators(t *testing.T) { // test panic require.Nil(t, sdk.ChainAnteDecorators([]sdk.AnteDecorator{}...)) - ctx, tx := sdk.Context{}, sdk.Tx(nil) + ctx, tx := sdk.Context{}.WithExecMode(sdk.ExecModeSimulate), sdk.Tx(nil) mockCtrl := gomock.NewController(t) mockAnteDecorator1 := mock.NewMockAnteDecorator(mockCtrl) mockAnteDecorator1.EXPECT().AnteHandle(gomock.Eq(ctx), gomock.Eq(tx), true, gomock.Any()).Times(1) @@ -39,7 +39,7 @@ func TestChainPostDecorators(t *testing.T) { require.Nil(t, sdk.ChainPostDecorators([]sdk.PostDecorator{}...)) // Create empty context as well as transaction - ctx := sdk.Context{} + ctx := sdk.Context{}.WithExecMode(sdk.ExecModeSimulate) tx := sdk.Tx(nil) // Create mocks diff --git a/x/auth/ante/basic.go b/x/auth/ante/basic.go index b5d14d32bb..59e9eca61e 100644 --- a/x/auth/ante/basic.go +++ b/x/auth/ante/basic.go @@ -24,10 +24,10 @@ func NewValidateBasicDecorator() ValidateBasicDecorator { return ValidateBasicDecorator{} } -func (vbd ValidateBasicDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) { +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 { - return next(ctx, tx, simulate) + return next(ctx, tx, false) } if validateBasic, ok := tx.(sdk.HasValidateBasic); ok { @@ -36,7 +36,7 @@ func (vbd ValidateBasicDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulat } } - return next(ctx, tx, simulate) + return next(ctx, tx, ctx.ExecMode() == sdk.ExecModeSimulate) } // ValidateMemoDecorator will validate memo given the parameters passed in @@ -52,7 +52,7 @@ func NewValidateMemoDecorator(ak AccountKeeper) ValidateMemoDecorator { } } -func (vmd ValidateMemoDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) { +func (vmd ValidateMemoDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, _ bool, next sdk.AnteHandler) (sdk.Context, error) { memoTx, ok := tx.(sdk.TxWithMemo) if !ok { return ctx, errorsmod.Wrap(sdkerrors.ErrTxDecode, "invalid transaction type") @@ -69,7 +69,7 @@ func (vmd ValidateMemoDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate } } - return next(ctx, tx, simulate) + return next(ctx, tx, ctx.ExecMode() == sdk.ExecModeSimulate) } // ConsumeTxSizeGasDecorator will take in parameters and consume gas proportional @@ -77,7 +77,7 @@ func (vmd ValidateMemoDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate // slightly over estimated due to the fact that any given signing account may need // to be retrieved from state. // -// CONTRACT: If simulate=true, then signatures must either be completely filled +// CONTRACT: If exec mode = simulate, then signatures must either be completely filled // in or empty. // CONTRACT: To use this decorator, signatures of transaction must be represented // as legacytx.StdSignature otherwise simulate mode will incorrectly estimate gas cost. @@ -91,7 +91,7 @@ func NewConsumeGasForTxSizeDecorator(ak AccountKeeper) ConsumeTxSizeGasDecorator } } -func (cgts ConsumeTxSizeGasDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) { +func (cgts ConsumeTxSizeGasDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, _ bool, next sdk.AnteHandler) (sdk.Context, error) { sigTx, ok := tx.(authsigning.SigVerifiableTx) if !ok { return ctx, errorsmod.Wrap(sdkerrors.ErrTxDecode, "invalid tx type") @@ -143,7 +143,7 @@ func (cgts ConsumeTxSizeGasDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, sim } } - return next(ctx, tx, simulate) + return next(ctx, tx, ctx.ExecMode() == sdk.ExecModeSimulate) } // isIncompleteSignature tests whether SignatureData is fully filled in for simulation purposes @@ -193,7 +193,7 @@ func NewTxTimeoutHeightDecorator() TxTimeoutHeightDecorator { // type where the current block height is checked against the tx's height timeout. // If a height timeout is provided (non-zero) and is less than the current block // height, then an error is returned. -func (txh TxTimeoutHeightDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) { +func (txh TxTimeoutHeightDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, _ bool, next sdk.AnteHandler) (sdk.Context, error) { timeoutTx, ok := tx.(TxWithTimeoutHeight) if !ok { return ctx, errorsmod.Wrap(sdkerrors.ErrTxDecode, "expected tx to implement TxWithTimeoutHeight") @@ -206,5 +206,5 @@ func (txh TxTimeoutHeightDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simul ) } - return next(ctx, tx, simulate) + return next(ctx, tx, ctx.ExecMode() == sdk.ExecModeSimulate) } diff --git a/x/auth/ante/ext.go b/x/auth/ante/ext.go index 6a072526aa..92fefff3af 100644 --- a/x/auth/ante/ext.go +++ b/x/auth/ante/ext.go @@ -43,13 +43,13 @@ func NewExtensionOptionsDecorator(checker ExtensionOptionChecker) sdk.AnteDecora var _ sdk.AnteDecorator = RejectExtensionOptionsDecorator{} // AnteHandle implements the AnteDecorator.AnteHandle method -func (r RejectExtensionOptionsDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) { +func (r RejectExtensionOptionsDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, _ bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) { err = checkExtOpts(tx, r.checker) if err != nil { return ctx, err } - return next(ctx, tx, simulate) + return next(ctx, tx, ctx.ExecMode() == sdk.ExecModeSimulate) } func checkExtOpts(tx sdk.Tx, checker ExtensionOptionChecker) error { diff --git a/x/auth/ante/fee.go b/x/auth/ante/fee.go index 6da4ecc3b9..018b447c97 100644 --- a/x/auth/ante/fee.go +++ b/x/auth/ante/fee.go @@ -39,7 +39,7 @@ func NewDeductFeeDecorator(ak AccountKeeper, bk types.BankKeeper, fk FeegrantKee } } -func (dfd DeductFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) { +func (dfd DeductFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, _ bool, next sdk.AnteHandler) (sdk.Context, error) { feeTx, ok := tx.(sdk.FeeTx) if !ok { return ctx, errorsmod.Wrap(sdkerrors.ErrTxDecode, "Tx must be a FeeTx") @@ -67,7 +67,7 @@ func (dfd DeductFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bo newCtx := ctx.WithPriority(priority) - return next(newCtx, tx, simulate) + return next(newCtx, tx, ctx.ExecMode() == sdk.ExecModeSimulate) } 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 4fbe8dac85..6ab8fcd4c1 100644 --- a/x/auth/ante/setup.go +++ b/x/auth/ante/setup.go @@ -27,7 +27,7 @@ func NewSetUpContextDecorator() SetUpContextDecorator { return SetUpContextDecorator{} } -func (sud SetUpContextDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) { +func (sud SetUpContextDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, _ bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) { // all transactions must implement GasTx gasTx, ok := tx.(GasTx) if !ok { @@ -67,7 +67,7 @@ func (sud SetUpContextDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate } }() - return next(newCtx, tx, simulate) + return next(newCtx, tx, ctx.ExecMode() == sdk.ExecModeSimulate) } // SetGasMeter returns a new context with a gas meter set from a given context. diff --git a/x/auth/ante/sigverify.go b/x/auth/ante/sigverify.go index e54a5964a6..facc160fce 100644 --- a/x/auth/ante/sigverify.go +++ b/x/auth/ante/sigverify.go @@ -149,7 +149,7 @@ func verifyIsOnCurve(pubKey cryptotypes.PubKey) (err error) { return nil } -func (svd SigVerificationDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) { +func (svd SigVerificationDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, _ bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) { sigTx, ok := tx.(authsigning.Tx) if !ok { return ctx, errorsmod.Wrap(sdkerrors.ErrTxDecode, "invalid transaction type") @@ -215,7 +215,7 @@ func (svd SigVerificationDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simul ctx.EventManager().EmitEvents(events) - return next(ctx, tx, simulate) + return next(ctx, tx, ctx.ExecMode() == sdk.ExecModeSimulate) } // authenticate the authentication of the TX for a specific tx signer. @@ -459,7 +459,7 @@ func NewValidateSigCountDecorator(ak AccountKeeper) ValidateSigCountDecorator { } } -func (vscd ValidateSigCountDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) { +func (vscd ValidateSigCountDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, _ bool, next sdk.AnteHandler) (sdk.Context, error) { sigTx, ok := tx.(authsigning.SigVerifiableTx) if !ok { return ctx, errorsmod.Wrap(sdkerrors.ErrTxDecode, "Tx must be a sigTx") @@ -479,7 +479,7 @@ func (vscd ValidateSigCountDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, sim } } - return next(ctx, tx, simulate) + return next(ctx, tx, ctx.ExecMode() == sdk.ExecModeSimulate) } // DefaultSigVerificationGasConsumer is the default implementation of SignatureVerificationGasConsumer. It consumes gas diff --git a/x/auth/ante/unordered.go b/x/auth/ante/unordered.go index c110e63650..0394701ddf 100644 --- a/x/auth/ante/unordered.go +++ b/x/auth/ante/unordered.go @@ -37,12 +37,12 @@ func NewUnorderedTxDecorator(maxTTL uint64, m *unorderedtx.Manager) *UnorderedTx } } -func (d *UnorderedTxDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) { +func (d *UnorderedTxDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, _ bool, next sdk.AnteHandler) (sdk.Context, error) { unorderedTx, ok := tx.(sdk.TxWithUnordered) 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, simulate) + return next(ctx, tx, ctx.ExecMode() == sdk.ExecModeSimulate) } // TTL is defined as a specific block height at which this tx is no longer valid @@ -70,5 +70,5 @@ func (d *UnorderedTxDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate b d.txManager.Add(txHash, ttl) } - return next(ctx, tx, simulate) + return next(ctx, tx, ctx.ExecMode() == sdk.ExecModeSimulate) }