Skip to content

Commit

Permalink
refactor(x/auth/ante): don't use simulate bool in ante handler (#19586)
Browse files Browse the repository at this point in the history
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
  • Loading branch information
julienrbrt and coderabbitai[bot] authored Feb 29, 2024
1 parent 0a801e1 commit 975ddc1
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 38 deletions.
26 changes: 13 additions & 13 deletions types/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <newCtx>.
//
// 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
Expand All @@ -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])
}
}

Expand All @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions types/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
20 changes: 10 additions & 10 deletions x/auth/ante/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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")
Expand All @@ -69,15 +69,15 @@ 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
// to the size of tx before calling next AnteHandler. Note, the gas costs will be
// 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.
Expand All @@ -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")
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand All @@ -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)
}
4 changes: 2 additions & 2 deletions x/auth/ante/ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions x/auth/ante/fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions x/auth/ante/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions x/auth/ante/sigverify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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")
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions x/auth/ante/unordered.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

0 comments on commit 975ddc1

Please sign in to comment.